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 * "org.eclipse.jdt.ui.javaElementFilters" extension point.
38 public class FilterDescriptor implements Comparable, IPluginContribution {
40 private static String PATTERN_FILTER_ID_PREFIX= "_patternFilterId_"; //$NON-NLS-1$
43 private static final String EXTENSION_POINT_NAME= "phpElementFilters"; //$NON-NLS-1$
45 private static final String FILTER_TAG= "filter"; //$NON-NLS-1$
47 private static final String PATTERN_ATTRIBUTE= "pattern"; //$NON-NLS-1$
48 private static final String ID_ATTRIBUTE= "id"; //$NON-NLS-1$
50 * @deprecated as of 3.0 use {@link FilterDescriptor#TARGET_ID_ATTRIBUTE}
52 private static final String VIEW_ID_ATTRIBUTE= "viewId"; //$NON-NLS-1$
53 private static final String TARGET_ID_ATTRIBUTE= "targetId"; //$NON-NLS-1$
54 private static final String CLASS_ATTRIBUTE= "class"; //$NON-NLS-1$
55 private static final String NAME_ATTRIBUTE= "name"; //$NON-NLS-1$
56 private static final String ENABLED_ATTRIBUTE= "enabled"; //$NON-NLS-1$
57 private static final String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
59 * @deprecated use "enabled" instead
61 private static final String SELECTED_ATTRIBUTE= "selected"; //$NON-NLS-1$
63 private static FilterDescriptor[] fgFilterDescriptors;
66 private IConfigurationElement fElement;
69 * Returns all contributed Java element filters.
71 public static FilterDescriptor[] getFilterDescriptors() {
72 if (fgFilterDescriptors == null) {
73 IExtensionRegistry registry= Platform.getExtensionRegistry();
74 IConfigurationElement[] elements= registry.getConfigurationElementsFor(PHPeclipsePlugin.PLUGIN_ID, EXTENSION_POINT_NAME);
75 fgFilterDescriptors= createFilterDescriptors(elements);
77 return fgFilterDescriptors;
80 * Returns all Java element filters which
81 * are contributed to the given view.
83 public static FilterDescriptor[] getFilterDescriptors(String targetId) {
84 FilterDescriptor[] filterDescs= FilterDescriptor.getFilterDescriptors();
85 List result= new ArrayList(filterDescs.length);
86 for (int i= 0; i < filterDescs.length; i++) {
87 String tid= filterDescs[i].getTargetId();
88 if (WorkbenchActivityHelper.filterItem(filterDescs[i]))
90 if (tid == null || tid.equals(targetId))
91 result.add(filterDescs[i]);
93 return (FilterDescriptor[])result.toArray(new FilterDescriptor[result.size()]);
97 * Creates a new filter descriptor for the given configuration element.
99 private FilterDescriptor(IConfigurationElement element) {
101 // it is either a pattern filter or a custom filter
102 Assert.isTrue(isPatternFilter() ^ isCustomFilter(), "An extension for extension-point org.eclipse.jdt.ui.javaElementFilters does not specify a correct filter"); //$NON-NLS-1$
103 Assert.isNotNull(getId(), "An extension for extension-point org.eclipse.jdt.ui.javaElementFilters does not provide a valid ID"); //$NON-NLS-1$
104 Assert.isNotNull(getName(), "An extension for extension-point org.eclipse.jdt.ui.javaElementFilters does not provide a valid name"); //$NON-NLS-1$
108 * Creates a new <code>ViewerFilter</code>.
109 * This method is only valid for viewer filters.
111 public ViewerFilter createViewerFilter() {
112 if (!isCustomFilter())
115 final ViewerFilter[] result= new ViewerFilter[1];
116 String message= FilterMessages.getFormattedString("FilterDescriptor.filterCreationError.message", getId()); //$NON-NLS-1$
117 ISafeRunnable code= new SafeRunnable(message) {
119 * @see org.eclipse.core.runtime.ISafeRunnable#run()
121 public void run() throws Exception {
122 result[0]= (ViewerFilter)fElement.createExecutableExtension(CLASS_ATTRIBUTE);
130 //---- XML Attribute accessors ---------------------------------------------
133 * Returns the filter's id.
135 * This attribute is mandatory for custom filters.
136 * The ID for pattern filters is
137 * PATTERN_FILTER_ID_PREFIX plus the pattern itself.
140 public String getId() {
141 if (isPatternFilter()) {
142 String targetId= getTargetId();
143 if (targetId == null)
144 return PATTERN_FILTER_ID_PREFIX + getPattern();
146 return targetId + PATTERN_FILTER_ID_PREFIX + getPattern();
148 return fElement.getAttribute(ID_ATTRIBUTE);
152 * Returns the filter's name.
154 * If the name of a pattern filter is missing
155 * then the pattern is used as its name.
158 public String getName() {
159 String name= fElement.getAttribute(NAME_ATTRIBUTE);
160 if (name == null && isPatternFilter())
166 * Returns the filter's pattern.
168 * @return the pattern string or <code>null</code> if it's not a pattern filter
170 public String getPattern() {
171 return fElement.getAttribute(PATTERN_ATTRIBUTE);
175 * Returns the filter's viewId.
177 * @return the view ID or <code>null</code> if the filter is for all views
180 public String getTargetId() {
181 String tid= fElement.getAttribute(TARGET_ID_ATTRIBUTE);
186 // Backwards compatibility code
187 return fElement.getAttribute(VIEW_ID_ATTRIBUTE);
192 * Returns the filter's description.
194 * @return the description or <code>null</code> if no description is provided
196 public String getDescription() {
197 String description= fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
198 if (description == null)
199 description= ""; //$NON-NLS-1$
204 * @return <code>true</code> if this filter is a custom filter.
206 public boolean isPatternFilter() {
207 return getPattern() != null;
211 * @return <code>true</code> if this filter is a pattern filter.
213 public boolean isCustomFilter() {
214 return fElement.getAttribute(CLASS_ATTRIBUTE) != null;
218 * Returns <code>true</code> if the filter
219 * is initially enabled.
221 * This attribute is optional and defaults to <code>true</code>.
223 public boolean isEnabled() {
224 String strVal= fElement.getAttribute(ENABLED_ATTRIBUTE);
226 // backward compatibility
227 strVal= fElement.getAttribute(SELECTED_ATTRIBUTE);
228 return strVal == null || Boolean.valueOf(strVal).booleanValue();
232 * Implements a method from IComparable
234 public int compareTo(Object o) {
235 if (o instanceof FilterDescriptor)
236 return Collator.getInstance().compare(getName(), ((FilterDescriptor)o).getName());
238 return Integer.MIN_VALUE;
241 //---- initialization ---------------------------------------------------
244 * Creates the filter descriptors.
246 private static FilterDescriptor[] createFilterDescriptors(IConfigurationElement[] elements) {
247 List result= new ArrayList(5);
248 Set descIds= new HashSet(5);
249 for (int i= 0; i < elements.length; i++) {
250 final IConfigurationElement element= elements[i];
251 if (FILTER_TAG.equals(element.getName())) {
253 final FilterDescriptor[] desc= new FilterDescriptor[1];
254 Platform.run(new SafeRunnable(FilterMessages.getString("FilterDescriptor.filterDescriptionCreationError.message")) { //$NON-NLS-1$
255 public void run() throws Exception {
256 desc[0]= new FilterDescriptor(element);
260 if (desc[0] != null && !descIds.contains(desc[0].getId())) {
262 descIds.add(desc[0].getId());
266 Collections.sort(result);
267 return (FilterDescriptor[])result.toArray(new FilterDescriptor[result.size()]);
270 public String getLocalId() {
271 return fElement.getAttribute(ID_ATTRIBUTE);
274 public String getPluginId() {
275 return fElement.getDeclaringExtension().getNamespace();