001: /*******************************************************************************
002: * Copyright (c) 2000, 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.jdt.internal.ui.filters;
011:
012: import com.ibm.icu.text.Collator;
013:
014: import java.util.ArrayList;
015: import java.util.HashSet;
016: import java.util.List;
017: import java.util.Set;
018:
019: import org.eclipse.core.runtime.Assert;
020: import org.eclipse.core.runtime.IConfigurationElement;
021: import org.eclipse.core.runtime.IExtensionRegistry;
022: import org.eclipse.core.runtime.ISafeRunnable;
023: import org.eclipse.core.runtime.Platform;
024: import org.eclipse.core.runtime.SafeRunner;
025:
026: import org.eclipse.jface.util.SafeRunnable;
027: import org.eclipse.jface.viewers.ViewerFilter;
028:
029: import org.eclipse.ui.IPluginContribution;
030: import org.eclipse.ui.activities.WorkbenchActivityHelper;
031:
032: import org.eclipse.jdt.internal.corext.util.Messages;
033:
034: import org.eclipse.jdt.ui.JavaUI;
035:
036: /**
037: * Represents a custom filter which is provided by the
038: * "org.eclipse.jdt.ui.javaElementFilters" extension point.
039: *
040: * since 2.0
041: */
042: public class FilterDescriptor implements Comparable,
043: IPluginContribution {
044:
045: private static String PATTERN_FILTER_ID_PREFIX = "_patternFilterId_"; //$NON-NLS-1$
046:
047: private static final String EXTENSION_POINT_NAME = "javaElementFilters"; //$NON-NLS-1$
048:
049: private static final String FILTER_TAG = "filter"; //$NON-NLS-1$
050:
051: private static final String PATTERN_ATTRIBUTE = "pattern"; //$NON-NLS-1$
052: private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
053: /**
054: * @deprecated as of 3.0 use {@link FilterDescriptor#TARGET_ID_ATTRIBUTE}
055: */
056: private static final String VIEW_ID_ATTRIBUTE = "viewId"; //$NON-NLS-1$
057: private static final String TARGET_ID_ATTRIBUTE = "targetId"; //$NON-NLS-1$
058: private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
059: private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
060: private static final String ENABLED_ATTRIBUTE = "enabled"; //$NON-NLS-1$
061: private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
062: /**
063: * @deprecated use "enabled" instead
064: */
065: private static final String SELECTED_ATTRIBUTE = "selected"; //$NON-NLS-1$
066:
067: private static FilterDescriptor[] fgFilterDescriptors;
068:
069: private IConfigurationElement fElement;
070:
071: /**
072: * Returns all contributed Java element filters.
073: */
074: public static FilterDescriptor[] getFilterDescriptors() {
075: if (fgFilterDescriptors == null) {
076: IExtensionRegistry registry = Platform
077: .getExtensionRegistry();
078: IConfigurationElement[] elements = registry
079: .getConfigurationElementsFor(JavaUI.ID_PLUGIN,
080: EXTENSION_POINT_NAME);
081: fgFilterDescriptors = createFilterDescriptors(elements);
082: }
083: return fgFilterDescriptors;
084: }
085:
086: /**
087: * Returns all Java element filters which
088: * are contributed to the given view.
089: */
090: public static FilterDescriptor[] getFilterDescriptors(
091: String targetId) {
092: FilterDescriptor[] filterDescs = FilterDescriptor
093: .getFilterDescriptors();
094: List result = new ArrayList(filterDescs.length);
095: for (int i = 0; i < filterDescs.length; i++) {
096: String tid = filterDescs[i].getTargetId();
097: if (WorkbenchActivityHelper.filterItem(filterDescs[i]))
098: continue;
099: if (tid == null || tid.equals(targetId))
100: result.add(filterDescs[i]);
101: }
102: return (FilterDescriptor[]) result
103: .toArray(new FilterDescriptor[result.size()]);
104: }
105:
106: /**
107: * Creates a new filter descriptor for the given configuration element.
108: */
109: private FilterDescriptor(IConfigurationElement element) {
110: fElement = element;
111: // it is either a pattern filter or a custom filter
112: Assert
113: .isTrue(
114: isPatternFilter() ^ isCustomFilter(),
115: "An extension for extension-point org.eclipse.jdt.ui.javaElementFilters does not specify a correct filter"); //$NON-NLS-1$
116: Assert
117: .isNotNull(
118: getId(),
119: "An extension for extension-point org.eclipse.jdt.ui.javaElementFilters does not provide a valid ID"); //$NON-NLS-1$
120: Assert
121: .isNotNull(
122: getName(),
123: "An extension for extension-point org.eclipse.jdt.ui.javaElementFilters does not provide a valid name"); //$NON-NLS-1$
124: }
125:
126: /**
127: * Creates a new <code>ViewerFilter</code>.
128: * This method is only valid for viewer filters.
129: */
130: public ViewerFilter createViewerFilter() {
131: if (!isCustomFilter())
132: return null;
133:
134: final ViewerFilter[] result = new ViewerFilter[1];
135: String message = Messages
136: .format(
137: FilterMessages.FilterDescriptor_filterCreationError_message,
138: getId());
139: ISafeRunnable code = new SafeRunnable(message) {
140: /*
141: * @see org.eclipse.core.runtime.ISafeRunnable#run()
142: */
143: public void run() throws Exception {
144: result[0] = (ViewerFilter) fElement
145: .createExecutableExtension(CLASS_ATTRIBUTE);
146: }
147:
148: };
149: SafeRunner.run(code);
150: return result[0];
151: }
152:
153: //---- XML Attribute accessors ---------------------------------------------
154:
155: /**
156: * Returns the filter's id.
157: * <p>
158: * This attribute is mandatory for custom filters.
159: * The ID for pattern filters is
160: * PATTERN_FILTER_ID_PREFIX plus the pattern itself.
161: * </p>
162: */
163: public String getId() {
164: if (isPatternFilter()) {
165: String targetId = getTargetId();
166: if (targetId == null)
167: return PATTERN_FILTER_ID_PREFIX + getPattern();
168: else
169: return targetId + PATTERN_FILTER_ID_PREFIX
170: + getPattern();
171: } else
172: return fElement.getAttribute(ID_ATTRIBUTE);
173: }
174:
175: /**
176: * Returns the filter's name.
177: * <p>
178: * If the name of a pattern filter is missing
179: * then the pattern is used as its name.
180: * </p>
181: */
182: public String getName() {
183: String name = fElement.getAttribute(NAME_ATTRIBUTE);
184: if (name == null && isPatternFilter())
185: name = getPattern();
186: return name;
187: }
188:
189: /**
190: * Returns the filter's pattern.
191: *
192: * @return the pattern string or <code>null</code> if it's not a pattern filter
193: */
194: public String getPattern() {
195: return fElement.getAttribute(PATTERN_ATTRIBUTE);
196: }
197:
198: /**
199: * Returns the filter's viewId.
200: *
201: * @return the view ID or <code>null</code> if the filter is for all views
202: * @since 3.0
203: */
204: public String getTargetId() {
205: String tid = fElement.getAttribute(TARGET_ID_ATTRIBUTE);
206:
207: if (tid != null)
208: return tid;
209:
210: // Backwards compatibility code
211: return fElement.getAttribute(VIEW_ID_ATTRIBUTE);
212:
213: }
214:
215: /**
216: * Returns the filter's description.
217: *
218: * @return the description or <code>null</code> if no description is provided
219: */
220: public String getDescription() {
221: String description = fElement
222: .getAttribute(DESCRIPTION_ATTRIBUTE);
223: if (description == null)
224: description = ""; //$NON-NLS-1$
225: return description;
226: }
227:
228: /**
229: * @return <code>true</code> if this filter is a custom filter.
230: */
231: public boolean isPatternFilter() {
232: return getPattern() != null;
233: }
234:
235: /**
236: * @return <code>true</code> if this filter is a pattern filter.
237: */
238: public boolean isCustomFilter() {
239: return fElement.getAttribute(CLASS_ATTRIBUTE) != null;
240: }
241:
242: /**
243: * Returns <code>true</code> if the filter
244: * is initially enabled.
245: *
246: * This attribute is optional and defaults to <code>true</code>.
247: */
248: public boolean isEnabled() {
249: String strVal = fElement.getAttribute(ENABLED_ATTRIBUTE);
250: if (strVal == null)
251: // backward compatibility
252: strVal = fElement.getAttribute(SELECTED_ATTRIBUTE);
253: return strVal == null || Boolean.valueOf(strVal).booleanValue();
254: }
255:
256: /*
257: * Implements a method from IComparable
258: */
259: public int compareTo(Object o) {
260: if (o instanceof FilterDescriptor)
261: return Collator.getInstance().compare(getName(),
262: ((FilterDescriptor) o).getName());
263: else
264: return Integer.MIN_VALUE;
265: }
266:
267: //---- initialization ---------------------------------------------------
268:
269: /**
270: * Creates the filter descriptors.
271: */
272: private static FilterDescriptor[] createFilterDescriptors(
273: IConfigurationElement[] elements) {
274: List result = new ArrayList(5);
275: Set descIds = new HashSet(5);
276: for (int i = 0; i < elements.length; i++) {
277: final IConfigurationElement element = elements[i];
278: if (FILTER_TAG.equals(element.getName())) {
279:
280: final FilterDescriptor[] desc = new FilterDescriptor[1];
281: SafeRunner
282: .run(new SafeRunnable(
283: FilterMessages.FilterDescriptor_filterDescriptionCreationError_message) {
284: public void run() throws Exception {
285: desc[0] = new FilterDescriptor(element);
286: }
287: });
288:
289: if (desc[0] != null
290: && !descIds.contains(desc[0].getId())) {
291: result.add(desc[0]);
292: descIds.add(desc[0].getId());
293: }
294: }
295: }
296: return (FilterDescriptor[]) result
297: .toArray(new FilterDescriptor[result.size()]);
298: }
299:
300: public String getLocalId() {
301: return fElement.getAttribute(ID_ATTRIBUTE);
302: }
303:
304: public String getPluginId() {
305: return fElement.getContributor().getName();
306: }
307: }
|