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.jface.util.Assert;
27 import org.eclipse.jface.util.SafeRunnable;
28 import org.eclipse.jface.viewers.ViewerFilter;
29 import org.eclipse.ui.IPluginContribution;
30 import org.eclipse.ui.activities.WorkbenchActivityHelper;
33 * Represents a custom filter which is provided by the
34 * "net.sourceforge.phpdt.ui.javaElementFilters" extension point.
38 public class FilterDescriptor implements Comparable, IPluginContribution {
40 private static String PATTERN_FILTER_ID_PREFIX = "_patternFilterId_"; //$NON-NLS-1$
42 private static final String EXTENSION_POINT_NAME = "phpElementFilters"; //$NON-NLS-1$
44 private static final String FILTER_TAG = "filter"; //$NON-NLS-1$
46 private static final String PATTERN_ATTRIBUTE = "pattern"; //$NON-NLS-1$
48 private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
51 * @deprecated as of 3.0 use {@link FilterDescriptor#TARGET_ID_ATTRIBUTE}
53 private static final String VIEW_ID_ATTRIBUTE = "viewId"; //$NON-NLS-1$
55 private static final String TARGET_ID_ATTRIBUTE = "targetId"; //$NON-NLS-1$
57 private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
59 private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
61 private static final String ENABLED_ATTRIBUTE = "enabled"; //$NON-NLS-1$
63 private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
66 * @deprecated use "enabled" instead
68 private static final String SELECTED_ATTRIBUTE = "selected"; //$NON-NLS-1$
70 private static FilterDescriptor[] fgFilterDescriptors;
72 private IConfigurationElement fElement;
75 * Returns all contributed Java element filters.
77 public static FilterDescriptor[] getFilterDescriptors() {
78 if (fgFilterDescriptors == null) {
79 IExtensionRegistry registry = Platform.getExtensionRegistry();
80 IConfigurationElement[] elements = registry
81 .getConfigurationElementsFor(PHPeclipsePlugin.PLUGIN_ID,
82 EXTENSION_POINT_NAME);
83 fgFilterDescriptors = createFilterDescriptors(elements);
85 return fgFilterDescriptors;
89 * Returns all Java element filters which are contributed to the given view.
91 public static FilterDescriptor[] getFilterDescriptors(String targetId) {
92 FilterDescriptor[] filterDescs = FilterDescriptor
93 .getFilterDescriptors();
94 List result = new ArrayList(filterDescs.length);
95 for (int i = 0; i < filterDescs.length; i++) {
96 String tid = filterDescs[i].getTargetId();
97 if (WorkbenchActivityHelper.filterItem(filterDescs[i]))
99 if (tid == null || tid.equals(targetId))
100 result.add(filterDescs[i]);
102 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
107 * Creates a new filter descriptor for the given configuration element.
109 private FilterDescriptor(IConfigurationElement element) {
111 // it is either a pattern filter or a custom filter
114 isPatternFilter() ^ isCustomFilter(),
115 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not specify a correct filter"); //$NON-NLS-1$
119 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid ID"); //$NON-NLS-1$
123 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid name"); //$NON-NLS-1$
127 * Creates a new <code>ViewerFilter</code>. This method is only valid for
130 public ViewerFilter createViewerFilter() {
131 if (!isCustomFilter())
134 final ViewerFilter[] result = new ViewerFilter[1];
135 String message = FilterMessages.getFormattedString(
136 "FilterDescriptor.filterCreationError.message", getId()); //$NON-NLS-1$
137 ISafeRunnable code = new SafeRunnable(message) {
139 * @see org.eclipse.core.runtime.ISafeRunnable#run()
141 public void run() throws Exception {
142 result[0] = (ViewerFilter) fElement
143 .createExecutableExtension(CLASS_ATTRIBUTE);
151 // ---- XML Attribute accessors
152 // ---------------------------------------------
155 * Returns the filter's id.
157 * This attribute is mandatory for custom filters. The ID for pattern
158 * filters is PATTERN_FILTER_ID_PREFIX plus the pattern itself.
161 public String getId() {
162 if (isPatternFilter()) {
163 String targetId = getTargetId();
164 if (targetId == null)
165 return PATTERN_FILTER_ID_PREFIX + getPattern();
167 return targetId + PATTERN_FILTER_ID_PREFIX + getPattern();
169 return fElement.getAttribute(ID_ATTRIBUTE);
173 * Returns the filter's name.
175 * If the name of a pattern filter is missing then the pattern is used as
179 public String getName() {
180 String name = fElement.getAttribute(NAME_ATTRIBUTE);
181 if (name == null && isPatternFilter())
187 * Returns the filter's pattern.
189 * @return the pattern string or <code>null</code> if it's not a pattern
192 public String getPattern() {
193 return fElement.getAttribute(PATTERN_ATTRIBUTE);
197 * Returns the filter's viewId.
199 * @return the view ID or <code>null</code> if the filter is for all views
202 public String getTargetId() {
203 String tid = fElement.getAttribute(TARGET_ID_ATTRIBUTE);
208 // Backwards compatibility code
209 return fElement.getAttribute(VIEW_ID_ATTRIBUTE);
214 * Returns the filter's description.
216 * @return the description or <code>null</code> if no description is
219 public String getDescription() {
220 String description = fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
221 if (description == null)
222 description = ""; //$NON-NLS-1$
227 * @return <code>true</code> if this filter is a custom filter.
229 public boolean isPatternFilter() {
230 return getPattern() != null;
234 * @return <code>true</code> if this filter is a pattern filter.
236 public boolean isCustomFilter() {
237 return fElement.getAttribute(CLASS_ATTRIBUTE) != null;
241 * Returns <code>true</code> if the filter is initially enabled.
243 * This attribute is optional and defaults to <code>true</code>.
245 public boolean isEnabled() {
246 String strVal = fElement.getAttribute(ENABLED_ATTRIBUTE);
248 // backward compatibility
249 strVal = fElement.getAttribute(SELECTED_ATTRIBUTE);
250 return strVal == null || Boolean.valueOf(strVal).booleanValue();
254 * Implements a method from IComparable
256 public int compareTo(Object o) {
257 if (o instanceof FilterDescriptor)
258 return Collator.getInstance().compare(getName(),
259 ((FilterDescriptor) o).getName());
261 return Integer.MIN_VALUE;
264 // ---- initialization ---------------------------------------------------
267 * Creates the filter descriptors.
269 private static FilterDescriptor[] createFilterDescriptors(
270 IConfigurationElement[] elements) {
271 List result = new ArrayList(5);
272 Set descIds = new HashSet(5);
273 for (int i = 0; i < elements.length; i++) {
274 final IConfigurationElement element = elements[i];
275 if (FILTER_TAG.equals(element.getName())) {
277 final FilterDescriptor[] desc = new FilterDescriptor[1];
279 .run(new SafeRunnable(
281 .getString("FilterDescriptor.filterDescriptionCreationError.message")) { //$NON-NLS-1$
282 public void run() throws Exception {
283 desc[0] = new FilterDescriptor(element);
287 if (desc[0] != null && !descIds.contains(desc[0].getId())) {
289 descIds.add(desc[0].getId());
293 Collections.sort(result);
294 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
298 public String getLocalId() {
299 return fElement.getAttribute(ID_ATTRIBUTE);
302 public String getPluginId() {
303 return fElement.getDeclaringExtension().getNamespace();