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.jface.util.Assert;
29 import org.eclipse.jface.util.SafeRunnable;
30 import org.eclipse.jface.viewers.ViewerFilter;
31 import org.eclipse.ui.IPluginContribution;
32 import org.eclipse.ui.activities.WorkbenchActivityHelper;
35 * Represents a custom filter which is provided by the
36 * "net.sourceforge.phpdt.ui.javaElementFilters" extension point.
40 public class FilterDescriptor implements Comparable, IPluginContribution {
42 private static String PATTERN_FILTER_ID_PREFIX = "_patternFilterId_"; //$NON-NLS-1$
44 private static final String EXTENSION_POINT_NAME = "phpElementFilters"; //$NON-NLS-1$
46 private static final String FILTER_TAG = "filter"; //$NON-NLS-1$
48 private static final String PATTERN_ATTRIBUTE = "pattern"; //$NON-NLS-1$
50 private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
53 * @deprecated as of 3.0 use {@link FilterDescriptor#TARGET_ID_ATTRIBUTE}
55 private static final String VIEW_ID_ATTRIBUTE = "viewId"; //$NON-NLS-1$
57 private static final String TARGET_ID_ATTRIBUTE = "targetId"; //$NON-NLS-1$
59 private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
61 private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
63 private static final String ENABLED_ATTRIBUTE = "enabled"; //$NON-NLS-1$
65 private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
68 * @deprecated use "enabled" instead
70 private static final String SELECTED_ATTRIBUTE = "selected"; //$NON-NLS-1$
72 private static FilterDescriptor[] fgFilterDescriptors;
74 private IConfigurationElement fElement;
77 * Returns all contributed Java element filters.
79 public static FilterDescriptor[] getFilterDescriptors() {
80 if (fgFilterDescriptors == null) {
81 IExtensionRegistry registry = Platform.getExtensionRegistry();
82 IConfigurationElement[] elements = registry
83 .getConfigurationElementsFor(WebUI.PLUGIN_ID,
84 EXTENSION_POINT_NAME);
85 fgFilterDescriptors = createFilterDescriptors(elements);
87 return fgFilterDescriptors;
91 * Returns all Java element filters which are contributed to the given view.
93 public static FilterDescriptor[] getFilterDescriptors(String targetId) {
94 FilterDescriptor[] filterDescs = FilterDescriptor
95 .getFilterDescriptors();
96 List result = new ArrayList(filterDescs.length);
97 for (int i = 0; i < filterDescs.length; i++) {
98 String tid = filterDescs[i].getTargetId();
99 if (WorkbenchActivityHelper.filterItem(filterDescs[i]))
101 if (tid == null || tid.equals(targetId))
102 result.add(filterDescs[i]);
104 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
109 * Creates a new filter descriptor for the given configuration element.
111 private FilterDescriptor(IConfigurationElement element) {
113 // it is either a pattern filter or a custom filter
116 isPatternFilter() ^ isCustomFilter(),
117 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not specify a correct filter"); //$NON-NLS-1$
121 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid ID"); //$NON-NLS-1$
125 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid name"); //$NON-NLS-1$
129 * Creates a new <code>ViewerFilter</code>. This method is only valid for
132 public ViewerFilter createViewerFilter() {
133 if (!isCustomFilter())
136 final ViewerFilter[] result = new ViewerFilter[1];
137 String message = FilterMessages.getFormattedString(
138 "FilterDescriptor.filterCreationError.message", getId()); //$NON-NLS-1$
139 ISafeRunnable code = new SafeRunnable(message) {
141 * @see org.eclipse.core.runtime.ISafeRunnable#run()
143 public void run() throws Exception {
144 result[0] = (ViewerFilter) fElement
145 .createExecutableExtension(CLASS_ATTRIBUTE);
153 // ---- XML Attribute accessors
154 // ---------------------------------------------
157 * Returns the filter's id.
159 * This attribute is mandatory for custom filters. The ID for pattern
160 * filters is PATTERN_FILTER_ID_PREFIX plus the pattern itself.
163 public String getId() {
164 if (isPatternFilter()) {
165 String targetId = getTargetId();
166 if (targetId == null)
167 return PATTERN_FILTER_ID_PREFIX + getPattern();
169 return targetId + PATTERN_FILTER_ID_PREFIX + getPattern();
171 return fElement.getAttribute(ID_ATTRIBUTE);
175 * Returns the filter's name.
177 * If the name of a pattern filter is missing then the pattern is used as
181 public String getName() {
182 String name = fElement.getAttribute(NAME_ATTRIBUTE);
183 if (name == null && isPatternFilter())
189 * Returns the filter's pattern.
191 * @return the pattern string or <code>null</code> if it's not a pattern
194 public String getPattern() {
195 return fElement.getAttribute(PATTERN_ATTRIBUTE);
199 * Returns the filter's viewId.
201 * @return the view ID or <code>null</code> if the filter is for all views
204 public String getTargetId() {
205 String tid = fElement.getAttribute(TARGET_ID_ATTRIBUTE);
210 // Backwards compatibility code
211 return fElement.getAttribute(VIEW_ID_ATTRIBUTE);
216 * Returns the filter's description.
218 * @return the description or <code>null</code> if no description is
221 public String getDescription() {
222 String description = fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
223 if (description == null)
224 description = ""; //$NON-NLS-1$
229 * @return <code>true</code> if this filter is a custom filter.
231 public boolean isPatternFilter() {
232 return getPattern() != null;
236 * @return <code>true</code> if this filter is a pattern filter.
238 public boolean isCustomFilter() {
239 return fElement.getAttribute(CLASS_ATTRIBUTE) != null;
243 * Returns <code>true</code> if the filter is initially enabled.
245 * This attribute is optional and defaults to <code>true</code>.
247 public boolean isEnabled() {
248 String strVal = fElement.getAttribute(ENABLED_ATTRIBUTE);
250 // backward compatibility
251 strVal = fElement.getAttribute(SELECTED_ATTRIBUTE);
252 return strVal == null || Boolean.valueOf(strVal).booleanValue();
256 * Implements a method from IComparable
258 public int compareTo(Object o) {
259 if (o instanceof FilterDescriptor)
260 return Collator.getInstance().compare(getName(),
261 ((FilterDescriptor) o).getName());
263 return Integer.MIN_VALUE;
266 // ---- initialization ---------------------------------------------------
269 * Creates the filter descriptors.
271 private static FilterDescriptor[] createFilterDescriptors(
272 IConfigurationElement[] elements) {
273 List result = new ArrayList(5);
274 Set descIds = new HashSet(5);
275 for (int i = 0; i < elements.length; i++) {
276 final IConfigurationElement element = elements[i];
277 if (FILTER_TAG.equals(element.getName())) {
279 final FilterDescriptor[] desc = new FilterDescriptor[1];
281 .run(new SafeRunnable(
283 .getString("FilterDescriptor.filterDescriptionCreationError.message")) { //$NON-NLS-1$
284 public void run() throws Exception {
285 desc[0] = new FilterDescriptor(element);
289 if (desc[0] != null && !descIds.contains(desc[0].getId())) {
291 descIds.add(desc[0].getId());
295 Collections.sort(result);
296 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
300 public String getLocalId() {
301 return fElement.getAttribute(ID_ATTRIBUTE);
304 public String getPluginId() {
305 return fElement.getDeclaringExtension().getNamespace();