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;
29 //import org.eclipse.jface.text.Assert;
30 import org.eclipse.core.runtime.Assert;
31 import org.eclipse.jface.util.SafeRunnable;
32 import org.eclipse.jface.viewers.ViewerFilter;
33 import org.eclipse.ui.IPluginContribution;
34 import org.eclipse.ui.activities.WorkbenchActivityHelper;
37 * Represents a custom filter which is provided by the
38 * "net.sourceforge.phpdt.ui.javaElementFilters" extension point.
42 public class FilterDescriptor implements Comparable, IPluginContribution {
44 private static String PATTERN_FILTER_ID_PREFIX = "_patternFilterId_"; //$NON-NLS-1$
46 private static final String EXTENSION_POINT_NAME = "phpElementFilters"; //$NON-NLS-1$
48 private static final String FILTER_TAG = "filter"; //$NON-NLS-1$
50 private static final String PATTERN_ATTRIBUTE = "pattern"; //$NON-NLS-1$
52 private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
55 * @deprecated as of 3.0 use {@link FilterDescriptor#TARGET_ID_ATTRIBUTE}
57 private static final String VIEW_ID_ATTRIBUTE = "viewId"; //$NON-NLS-1$
59 private static final String TARGET_ID_ATTRIBUTE = "targetId"; //$NON-NLS-1$
61 private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
63 private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
65 private static final String ENABLED_ATTRIBUTE = "enabled"; //$NON-NLS-1$
67 private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
70 * @deprecated use "enabled" instead
72 private static final String SELECTED_ATTRIBUTE = "selected"; //$NON-NLS-1$
74 private static FilterDescriptor[] fgFilterDescriptors;
76 private IConfigurationElement fElement;
79 * Returns all contributed Java element filters.
81 public static FilterDescriptor[] getFilterDescriptors() {
82 if (fgFilterDescriptors == null) {
83 IExtensionRegistry registry = Platform.getExtensionRegistry();
84 IConfigurationElement[] elements = registry
85 .getConfigurationElementsFor(WebUI.PLUGIN_ID,
86 EXTENSION_POINT_NAME);
87 fgFilterDescriptors = createFilterDescriptors(elements);
89 return fgFilterDescriptors;
93 * Returns all Java element filters which are contributed to the given view.
95 public static FilterDescriptor[] getFilterDescriptors(String targetId) {
96 FilterDescriptor[] filterDescs = FilterDescriptor
97 .getFilterDescriptors();
98 List result = new ArrayList(filterDescs.length);
99 for (int i = 0; i < filterDescs.length; i++) {
100 String tid = filterDescs[i].getTargetId();
101 if (WorkbenchActivityHelper.filterItem(filterDescs[i]))
103 if (tid == null || tid.equals(targetId))
104 result.add(filterDescs[i]);
106 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
111 * Creates a new filter descriptor for the given configuration element.
113 private FilterDescriptor(IConfigurationElement element) {
115 // it is either a pattern filter or a custom filter
118 isPatternFilter() ^ isCustomFilter(),
119 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not specify a correct filter"); //$NON-NLS-1$
123 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid ID"); //$NON-NLS-1$
127 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid name"); //$NON-NLS-1$
131 * Creates a new <code>ViewerFilter</code>. This method is only valid for
134 public ViewerFilter createViewerFilter() {
135 if (!isCustomFilter())
138 final ViewerFilter[] result = new ViewerFilter[1];
139 String message = FilterMessages.getFormattedString(
140 "FilterDescriptor.filterCreationError.message", getId()); //$NON-NLS-1$
141 ISafeRunnable code = new SafeRunnable(message) {
143 * @see org.eclipse.core.runtime.ISafeRunnable#run()
145 public void run() throws Exception {
146 result[0] = (ViewerFilter) fElement
147 .createExecutableExtension(CLASS_ATTRIBUTE);
155 // ---- XML Attribute accessors
156 // ---------------------------------------------
159 * Returns the filter's id.
161 * This attribute is mandatory for custom filters. The ID for pattern
162 * filters is PATTERN_FILTER_ID_PREFIX plus the pattern itself.
165 public String getId() {
166 if (isPatternFilter()) {
167 String targetId = getTargetId();
168 if (targetId == null)
169 return PATTERN_FILTER_ID_PREFIX + getPattern();
171 return targetId + PATTERN_FILTER_ID_PREFIX + getPattern();
173 return fElement.getAttribute(ID_ATTRIBUTE);
177 * Returns the filter's name.
179 * If the name of a pattern filter is missing then the pattern is used as
183 public String getName() {
184 String name = fElement.getAttribute(NAME_ATTRIBUTE);
185 if (name == null && isPatternFilter())
191 * Returns the filter's pattern.
193 * @return the pattern string or <code>null</code> if it's not a pattern
196 public String getPattern() {
197 return fElement.getAttribute(PATTERN_ATTRIBUTE);
201 * Returns the filter's viewId.
203 * @return the view ID or <code>null</code> if the filter is for all views
206 public String getTargetId() {
207 String tid = fElement.getAttribute(TARGET_ID_ATTRIBUTE);
212 // Backwards compatibility code
213 return fElement.getAttribute(VIEW_ID_ATTRIBUTE);
218 * Returns the filter's description.
220 * @return the description or <code>null</code> if no description is
223 public String getDescription() {
224 String description = fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
225 if (description == null)
226 description = ""; //$NON-NLS-1$
231 * @return <code>true</code> if this filter is a custom filter.
233 public boolean isPatternFilter() {
234 return getPattern() != null;
238 * @return <code>true</code> if this filter is a pattern filter.
240 public boolean isCustomFilter() {
241 return fElement.getAttribute(CLASS_ATTRIBUTE) != null;
245 * Returns <code>true</code> if the filter is initially enabled.
247 * This attribute is optional and defaults to <code>true</code>.
249 public boolean isEnabled() {
250 String strVal = fElement.getAttribute(ENABLED_ATTRIBUTE);
252 // backward compatibility
253 strVal = fElement.getAttribute(SELECTED_ATTRIBUTE);
254 return strVal == null || Boolean.valueOf(strVal).booleanValue();
258 * Implements a method from IComparable
260 public int compareTo(Object o) {
261 if (o instanceof FilterDescriptor)
262 return Collator.getInstance().compare(getName(),
263 ((FilterDescriptor) o).getName());
265 return Integer.MIN_VALUE;
268 // ---- initialization ---------------------------------------------------
271 * Creates the filter descriptors.
273 private static FilterDescriptor[] createFilterDescriptors(
274 IConfigurationElement[] elements) {
275 List result = new ArrayList(5);
276 Set descIds = new HashSet(5);
277 for (int i = 0; i < elements.length; i++) {
278 final IConfigurationElement element = elements[i];
279 if (FILTER_TAG.equals(element.getName())) {
281 final FilterDescriptor[] desc = new FilterDescriptor[1];
283 .run(new SafeRunnable(
285 .getString("FilterDescriptor.filterDescriptionCreationError.message")) { //$NON-NLS-1$
286 public void run() throws Exception {
287 desc[0] = new FilterDescriptor(element);
291 if (desc[0] != null && !descIds.contains(desc[0].getId())) {
293 descIds.add(desc[0].getId());
297 Collections.sort(result);
298 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
302 public String getLocalId() {
303 return fElement.getAttribute(ID_ATTRIBUTE);
306 public String getPluginId() {
307 return fElement.getDeclaringExtension().getNamespace();