1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.filters;
13 import java.text.Collator;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.List;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExtensionRegistry;
24 import org.eclipse.core.runtime.ISafeRunnable;
25 import org.eclipse.core.runtime.Platform;
26 import org.eclipse.core.runtime.SafeRunner;
28 //import org.eclipse.jface.text.Assert;
29 import org.eclipse.core.runtime.Assert;
30 import org.eclipse.jface.util.SafeRunnable;
31 import org.eclipse.jface.viewers.ViewerFilter;
32 import org.eclipse.ui.IPluginContribution;
33 import org.eclipse.ui.activities.WorkbenchActivityHelper;
36 * Represents a custom filter which is provided by the
37 * "net.sourceforge.phpdt.ui.javaElementFilters" extension point.
41 public class FilterDescriptor implements Comparable, IPluginContribution {
43 private static String PATTERN_FILTER_ID_PREFIX = "_patternFilterId_"; //$NON-NLS-1$
45 private static final String EXTENSION_POINT_NAME = "phpElementFilters"; //$NON-NLS-1$
47 private static final String FILTER_TAG = "filter"; //$NON-NLS-1$
49 private static final String PATTERN_ATTRIBUTE = "pattern"; //$NON-NLS-1$
51 private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
54 * @deprecated as of 3.0 use {@link FilterDescriptor#TARGET_ID_ATTRIBUTE}
56 private static final String VIEW_ID_ATTRIBUTE = "viewId"; //$NON-NLS-1$
58 private static final String TARGET_ID_ATTRIBUTE = "targetId"; //$NON-NLS-1$
60 private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
62 private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
64 private static final String ENABLED_ATTRIBUTE = "enabled"; //$NON-NLS-1$
66 private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
69 * @deprecated use "enabled" instead
71 private static final String SELECTED_ATTRIBUTE = "selected"; //$NON-NLS-1$
73 private static FilterDescriptor[] fgFilterDescriptors;
75 private IConfigurationElement fElement;
78 * Returns all contributed Java element filters.
80 public static FilterDescriptor[] getFilterDescriptors() {
81 if (fgFilterDescriptors == null) {
82 IExtensionRegistry registry = Platform.getExtensionRegistry();
83 IConfigurationElement[] elements = registry
84 .getConfigurationElementsFor(PHPeclipsePlugin.PLUGIN_ID,
85 EXTENSION_POINT_NAME);
86 fgFilterDescriptors = createFilterDescriptors(elements);
88 return fgFilterDescriptors;
92 * Returns all Java element filters which are contributed to the given view.
94 public static FilterDescriptor[] getFilterDescriptors(String targetId) {
95 FilterDescriptor[] filterDescs = FilterDescriptor
96 .getFilterDescriptors();
97 List result = new ArrayList(filterDescs.length);
98 for (int i = 0; i < filterDescs.length; i++) {
99 String tid = filterDescs[i].getTargetId();
100 if (WorkbenchActivityHelper.filterItem(filterDescs[i]))
102 if (tid == null || tid.equals(targetId))
103 result.add(filterDescs[i]);
105 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
110 * Creates a new filter descriptor for the given configuration element.
112 private FilterDescriptor(IConfigurationElement element) {
114 // it is either a pattern filter or a custom filter
117 isPatternFilter() ^ isCustomFilter(),
118 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not specify a correct filter"); //$NON-NLS-1$
122 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid ID"); //$NON-NLS-1$
126 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid name"); //$NON-NLS-1$
130 * Creates a new <code>ViewerFilter</code>. This method is only valid for
133 public ViewerFilter createViewerFilter() {
134 if (!isCustomFilter())
137 final ViewerFilter[] result = new ViewerFilter[1];
138 String message = FilterMessages.getFormattedString(
139 "FilterDescriptor.filterCreationError.message", getId()); //$NON-NLS-1$
140 ISafeRunnable code = new SafeRunnable(message) {
142 * @see org.eclipse.core.runtime.ISafeRunnable#run()
144 public void run() throws Exception {
145 result[0] = (ViewerFilter) fElement
146 .createExecutableExtension(CLASS_ATTRIBUTE);
150 SafeRunner.run(code);
154 // ---- XML Attribute accessors
155 // ---------------------------------------------
158 * Returns the filter's id.
160 * This attribute is mandatory for custom filters. The ID for pattern
161 * filters is PATTERN_FILTER_ID_PREFIX plus the pattern itself.
164 public String getId() {
165 if (isPatternFilter()) {
166 String targetId = getTargetId();
167 if (targetId == null)
168 return PATTERN_FILTER_ID_PREFIX + getPattern();
170 return targetId + PATTERN_FILTER_ID_PREFIX + getPattern();
172 return fElement.getAttribute(ID_ATTRIBUTE);
176 * Returns the filter's name.
178 * If the name of a pattern filter is missing then the pattern is used as
182 public String getName() {
183 String name = fElement.getAttribute(NAME_ATTRIBUTE);
184 if (name == null && isPatternFilter())
190 * Returns the filter's pattern.
192 * @return the pattern string or <code>null</code> if it's not a pattern
195 public String getPattern() {
196 return fElement.getAttribute(PATTERN_ATTRIBUTE);
200 * Returns the filter's viewId.
202 * @return the view ID or <code>null</code> if the filter is for all views
205 public String getTargetId() {
206 String tid = fElement.getAttribute(TARGET_ID_ATTRIBUTE);
211 // Backwards compatibility code
212 return fElement.getAttribute(VIEW_ID_ATTRIBUTE);
217 * Returns the filter's description.
219 * @return the description or <code>null</code> if no description is
222 public String getDescription() {
223 String description = fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
224 if (description == null)
225 description = ""; //$NON-NLS-1$
230 * @return <code>true</code> if this filter is a custom filter.
232 public boolean isPatternFilter() {
233 return getPattern() != null;
237 * @return <code>true</code> if this filter is a pattern filter.
239 public boolean isCustomFilter() {
240 return fElement.getAttribute(CLASS_ATTRIBUTE) != null;
244 * Returns <code>true</code> if the filter is initially enabled.
246 * This attribute is optional and defaults to <code>true</code>.
248 public boolean isEnabled() {
249 String strVal = fElement.getAttribute(ENABLED_ATTRIBUTE);
251 // backward compatibility
252 strVal = fElement.getAttribute(SELECTED_ATTRIBUTE);
253 return strVal == null || Boolean.valueOf(strVal).booleanValue();
257 * Implements a method from IComparable
259 public int compareTo(Object o) {
260 if (o instanceof FilterDescriptor)
261 return Collator.getInstance().compare(getName(),
262 ((FilterDescriptor) o).getName());
264 return Integer.MIN_VALUE;
267 // ---- initialization ---------------------------------------------------
270 * Creates the filter descriptors.
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())) {
280 final FilterDescriptor[] desc = new FilterDescriptor[1];
282 .run(new SafeRunnable(
284 .getString("FilterDescriptor.filterDescriptionCreationError.message")) { //$NON-NLS-1$
285 public void run() throws Exception {
286 desc[0] = new FilterDescriptor(element);
290 if (desc[0] != null && !descIds.contains(desc[0].getId())) {
292 descIds.add(desc[0].getId());
296 Collections.sort(result);
297 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
301 public String getLocalId() {
302 return fElement.getAttribute(ID_ATTRIBUTE);
305 public String getPluginId() {
306 return fElement.getDeclaringExtension().getNamespace();