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 net.sourceforge.phpeclipse.ui.WebUI;
24 import org.eclipse.core.runtime.IConfigurationElement;
25 import org.eclipse.core.runtime.IExtensionRegistry;
26 import org.eclipse.core.runtime.ISafeRunnable;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.core.runtime.SafeRunner;
30 //import org.eclipse.jface.text.Assert;
31 import org.eclipse.core.runtime.Assert;
32 import org.eclipse.jface.util.SafeRunnable;
33 import org.eclipse.jface.viewers.ViewerFilter;
34 import org.eclipse.ui.IPluginContribution;
35 import org.eclipse.ui.activities.WorkbenchActivityHelper;
38 * Represents a custom filter which is provided by the
39 * "net.sourceforge.phpdt.ui.javaElementFilters" extension point.
43 public class FilterDescriptor implements Comparable, IPluginContribution {
45 private static String PATTERN_FILTER_ID_PREFIX = "_patternFilterId_"; //$NON-NLS-1$
47 private static final String EXTENSION_POINT_NAME = "phpElementFilters"; //$NON-NLS-1$
49 private static final String FILTER_TAG = "filter"; //$NON-NLS-1$
51 private static final String PATTERN_ATTRIBUTE = "pattern"; //$NON-NLS-1$
53 private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
56 * @deprecated as of 3.0 use {@link FilterDescriptor#TARGET_ID_ATTRIBUTE}
58 private static final String VIEW_ID_ATTRIBUTE = "viewId"; //$NON-NLS-1$
60 private static final String TARGET_ID_ATTRIBUTE = "targetId"; //$NON-NLS-1$
62 private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
64 private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
66 private static final String ENABLED_ATTRIBUTE = "enabled"; //$NON-NLS-1$
68 private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
71 * @deprecated use "enabled" instead
73 private static final String SELECTED_ATTRIBUTE = "selected"; //$NON-NLS-1$
75 private static FilterDescriptor[] fgFilterDescriptors;
77 private IConfigurationElement fElement;
80 * Returns all contributed Java element filters.
82 public static FilterDescriptor[] getFilterDescriptors() {
83 if (fgFilterDescriptors == null) {
84 IExtensionRegistry registry = Platform.getExtensionRegistry();
85 IConfigurationElement[] elements = registry
86 .getConfigurationElementsFor(WebUI.PLUGIN_ID,
87 EXTENSION_POINT_NAME);
88 fgFilterDescriptors = createFilterDescriptors(elements);
90 return fgFilterDescriptors;
94 * Returns all Java element filters which are contributed to the given view.
96 public static FilterDescriptor[] getFilterDescriptors(String targetId) {
97 FilterDescriptor[] filterDescs = FilterDescriptor
98 .getFilterDescriptors();
99 List result = new ArrayList(filterDescs.length);
100 for (int i = 0; i < filterDescs.length; i++) {
101 String tid = filterDescs[i].getTargetId();
102 if (WorkbenchActivityHelper.filterItem(filterDescs[i]))
104 if (tid == null || tid.equals(targetId))
105 result.add(filterDescs[i]);
107 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
112 * Creates a new filter descriptor for the given configuration element.
114 private FilterDescriptor(IConfigurationElement element) {
116 // it is either a pattern filter or a custom filter
119 isPatternFilter() ^ isCustomFilter(),
120 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not specify a correct filter"); //$NON-NLS-1$
124 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid ID"); //$NON-NLS-1$
128 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid name"); //$NON-NLS-1$
132 * Creates a new <code>ViewerFilter</code>. This method is only valid for
135 public ViewerFilter createViewerFilter() {
136 if (!isCustomFilter())
139 final ViewerFilter[] result = new ViewerFilter[1];
140 String message = FilterMessages.getFormattedString(
141 "FilterDescriptor.filterCreationError.message", getId()); //$NON-NLS-1$
142 ISafeRunnable code = new SafeRunnable(message) {
144 * @see org.eclipse.core.runtime.ISafeRunnable#run()
146 public void run() throws Exception {
147 result[0] = (ViewerFilter) fElement
148 .createExecutableExtension(CLASS_ATTRIBUTE);
152 SafeRunner.run(code);
156 // ---- XML Attribute accessors
157 // ---------------------------------------------
160 * Returns the filter's id.
162 * This attribute is mandatory for custom filters. The ID for pattern
163 * filters is PATTERN_FILTER_ID_PREFIX plus the pattern itself.
166 public String getId() {
167 if (isPatternFilter()) {
168 String targetId = getTargetId();
169 if (targetId == null)
170 return PATTERN_FILTER_ID_PREFIX + getPattern();
172 return targetId + PATTERN_FILTER_ID_PREFIX + getPattern();
174 return fElement.getAttribute(ID_ATTRIBUTE);
178 * Returns the filter's name.
180 * If the name of a pattern filter is missing then the pattern is used as
184 public String getName() {
185 String name = fElement.getAttribute(NAME_ATTRIBUTE);
186 if (name == null && isPatternFilter())
192 * Returns the filter's pattern.
194 * @return the pattern string or <code>null</code> if it's not a pattern
197 public String getPattern() {
198 return fElement.getAttribute(PATTERN_ATTRIBUTE);
202 * Returns the filter's viewId.
204 * @return the view ID or <code>null</code> if the filter is for all views
207 public String getTargetId() {
208 String tid = fElement.getAttribute(TARGET_ID_ATTRIBUTE);
213 // Backwards compatibility code
214 return fElement.getAttribute(VIEW_ID_ATTRIBUTE);
219 * Returns the filter's description.
221 * @return the description or <code>null</code> if no description is
224 public String getDescription() {
225 String description = fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
226 if (description == null)
227 description = ""; //$NON-NLS-1$
232 * @return <code>true</code> if this filter is a custom filter.
234 public boolean isPatternFilter() {
235 return getPattern() != null;
239 * @return <code>true</code> if this filter is a pattern filter.
241 public boolean isCustomFilter() {
242 return fElement.getAttribute(CLASS_ATTRIBUTE) != null;
246 * Returns <code>true</code> if the filter is initially enabled.
248 * This attribute is optional and defaults to <code>true</code>.
250 public boolean isEnabled() {
251 String strVal = fElement.getAttribute(ENABLED_ATTRIBUTE);
253 // backward compatibility
254 strVal = fElement.getAttribute(SELECTED_ATTRIBUTE);
255 return strVal == null || Boolean.valueOf(strVal).booleanValue();
259 * Implements a method from IComparable
261 public int compareTo(Object o) {
262 if (o instanceof FilterDescriptor)
263 return Collator.getInstance().compare(getName(),
264 ((FilterDescriptor) o).getName());
266 return Integer.MIN_VALUE;
269 // ---- initialization ---------------------------------------------------
272 * Creates the filter descriptors.
274 private static FilterDescriptor[] createFilterDescriptors(
275 IConfigurationElement[] elements) {
276 List result = new ArrayList(5);
277 Set descIds = new HashSet(5);
278 for (int i = 0; i < elements.length; i++) {
279 final IConfigurationElement element = elements[i];
280 if (FILTER_TAG.equals(element.getName())) {
282 final FilterDescriptor[] desc = new FilterDescriptor[1];
284 .run(new SafeRunnable(
286 .getString("FilterDescriptor.filterDescriptionCreationError.message")) { //$NON-NLS-1$
287 public void run() throws Exception {
288 desc[0] = new FilterDescriptor(element);
292 if (desc[0] != null && !descIds.contains(desc[0].getId())) {
294 descIds.add(desc[0].getId());
298 Collections.sort(result);
299 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
303 public String getLocalId() {
304 return fElement.getAttribute(ID_ATTRIBUTE);
307 public String getPluginId() {
308 return fElement.getDeclaringExtension().getNamespace();