001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.navigator.filters;
011:
012: import org.eclipse.core.expressions.Expression;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.core.runtime.IConfigurationElement;
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.jface.viewers.ViewerFilter;
017: import org.eclipse.ui.internal.navigator.CustomAndExpression;
018: import org.eclipse.ui.internal.navigator.NavigatorPlugin;
019: import org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtPtConstants;
020: import org.eclipse.ui.navigator.ICommonFilterDescriptor;
021:
022: /**
023: *
024: * Describes a <b>commonFilter</b> element under a
025: * <b>org.eclipse.ui.navigator.navigatorContent</b> extension.
026: *
027: * @since 3.2
028: *
029: */
030: public class CommonFilterDescriptor implements ICommonFilterDescriptor,
031: INavigatorContentExtPtConstants {
032:
033: private IConfigurationElement element;
034:
035: private Expression filterExpression;
036:
037: private String id;
038:
039: protected CommonFilterDescriptor(IConfigurationElement anElement) {
040:
041: element = anElement;
042: init();
043: }
044:
045: private void init() {
046: id = element.getAttribute(ATT_ID);
047: if (id == null) {
048: id = ""; //$NON-NLS-1$
049: }
050: IConfigurationElement[] children = element
051: .getChildren(TAG_FILTER_EXPRESSION);
052: if (children.length == 1) {
053: filterExpression = new CustomAndExpression(children[0]);
054: }
055: }
056:
057: /**
058: *
059: * @return An identifier used to determine whether the filter is visible.
060: * May not be unique.
061: */
062: public String getId() {
063: return id;
064: }
065:
066: /**
067: *
068: * @return A translated name to identify the filter
069: */
070: public String getName() {
071: return element.getAttribute(ATT_NAME);
072: }
073:
074: /**
075: *
076: * @return A translated description to explain to the user what the defined
077: * filter will hide from the view.
078: */
079: public String getDescription() {
080: return element.getAttribute(ATT_DESCRIPTION);
081: }
082:
083: /**
084: *
085: * @return Indicates the filter should be in an "Active" state by default.
086: */
087: public boolean isActiveByDefault() {
088: return Boolean.valueOf(
089: element.getAttribute(ATT_ACTIVE_BY_DEFAULT))
090: .booleanValue();
091: }
092:
093: /**
094: *
095: * @return An instance of the ViewerFilter defined by the extension. Callers
096: * of this method are responsible for managing the instantiated
097: * filter.
098: */
099: public ViewerFilter createFilter() {
100: try {
101:
102: if (filterExpression != null) {
103:
104: if (element.getAttribute(ATT_CLASS) != null) {
105: NavigatorPlugin
106: .log(
107: IStatus.WARNING,
108: 0,
109: "A \"commonFilter\" was specified in " + //$NON-NLS-1$
110: element
111: .getDeclaringExtension()
112: .getNamespaceIdentifier()
113: + " which specifies a \"class\" attribute and an Core Expression.\n" + //$NON-NLS-1$
114: "Only the Core Expression will be respected.", //$NON-NLS-1$
115: null);
116: }
117:
118: return new CoreExpressionFilter(filterExpression);
119: }
120:
121: return (ViewerFilter) element
122: .createExecutableExtension(ATT_CLASS);
123: } catch (RuntimeException re) {
124: NavigatorPlugin.logError(0, re.getMessage(), re);
125: } catch (CoreException e) {
126: NavigatorPlugin.logError(0, e.getMessage(), e);
127: }
128:
129: return SkeletonViewerFilter.INSTANCE;
130: }
131:
132: /*
133: * (non-Javadoc)
134: *
135: * @see java.lang.Object#toString()
136: */
137: public String toString() {
138: return "CommonFilterDescriptor[" + getName() + " (" + getId() + ")]"; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
139: }
140: }
|