9c722b69142211477f9cdca3fb6a78421f3e38f3
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / filters / FilterDescriptor.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.filters;
12
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;
18 import java.util.Set;
19
20 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21
22 import net.sourceforge.phpeclipse.ui.WebUI;
23
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;
33
34 /**
35  * Represents a custom filter which is provided by the
36  * "net.sourceforge.phpdt.ui.javaElementFilters" extension point.
37  * 
38  * since 2.0
39  */
40 public class FilterDescriptor implements Comparable, IPluginContribution {
41
42         private static String PATTERN_FILTER_ID_PREFIX = "_patternFilterId_"; //$NON-NLS-1$
43
44         private static final String EXTENSION_POINT_NAME = "phpElementFilters"; //$NON-NLS-1$
45
46         private static final String FILTER_TAG = "filter"; //$NON-NLS-1$
47
48         private static final String PATTERN_ATTRIBUTE = "pattern"; //$NON-NLS-1$        
49
50         private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
51
52         /**
53          * @deprecated as of 3.0 use {@link FilterDescriptor#TARGET_ID_ATTRIBUTE}
54          */
55         private static final String VIEW_ID_ATTRIBUTE = "viewId"; //$NON-NLS-1$
56
57         private static final String TARGET_ID_ATTRIBUTE = "targetId"; //$NON-NLS-1$
58
59         private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
60
61         private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
62
63         private static final String ENABLED_ATTRIBUTE = "enabled"; //$NON-NLS-1$
64
65         private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$        
66
67         /**
68          * @deprecated use "enabled" instead
69          */
70         private static final String SELECTED_ATTRIBUTE = "selected"; //$NON-NLS-1$
71
72         private static FilterDescriptor[] fgFilterDescriptors;
73
74         private IConfigurationElement fElement;
75
76         /**
77          * Returns all contributed Java element filters.
78          */
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);
86                 }
87                 return fgFilterDescriptors;
88         }
89
90         /**
91          * Returns all Java element filters which are contributed to the given view.
92          */
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]))
100                                 continue;
101                         if (tid == null || tid.equals(targetId))
102                                 result.add(filterDescs[i]);
103                 }
104                 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
105                                 .size()]);
106         }
107
108         /**
109          * Creates a new filter descriptor for the given configuration element.
110          */
111         private FilterDescriptor(IConfigurationElement element) {
112                 fElement = element;
113                 // it is either a pattern filter or a custom filter
114                 Assert
115                                 .isTrue(
116                                                 isPatternFilter() ^ isCustomFilter(),
117                                                 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not specify a correct filter"); //$NON-NLS-1$
118                 Assert
119                                 .isNotNull(
120                                                 getId(),
121                                                 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid ID"); //$NON-NLS-1$
122                 Assert
123                                 .isNotNull(
124                                                 getName(),
125                                                 "An extension for extension-point net.sourceforge.phpdt.ui.javaElementFilters does not provide a valid name"); //$NON-NLS-1$
126         }
127
128         /**
129          * Creates a new <code>ViewerFilter</code>. This method is only valid for
130          * viewer filters.
131          */
132         public ViewerFilter createViewerFilter() {
133                 if (!isCustomFilter())
134                         return null;
135
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) {
140                         /*
141                          * @see org.eclipse.core.runtime.ISafeRunnable#run()
142                          */
143                         public void run() throws Exception {
144                                 result[0] = (ViewerFilter) fElement
145                                                 .createExecutableExtension(CLASS_ATTRIBUTE);
146                         }
147
148                 };
149                 Platform.run(code);
150                 return result[0];
151         }
152
153         // ---- XML Attribute accessors
154         // ---------------------------------------------
155
156         /**
157          * Returns the filter's id.
158          * <p>
159          * This attribute is mandatory for custom filters. The ID for pattern
160          * filters is PATTERN_FILTER_ID_PREFIX plus the pattern itself.
161          * </p>
162          */
163         public String getId() {
164                 if (isPatternFilter()) {
165                         String targetId = getTargetId();
166                         if (targetId == null)
167                                 return PATTERN_FILTER_ID_PREFIX + getPattern();
168                         else
169                                 return targetId + PATTERN_FILTER_ID_PREFIX + getPattern();
170                 } else
171                         return fElement.getAttribute(ID_ATTRIBUTE);
172         }
173
174         /**
175          * Returns the filter's name.
176          * <p>
177          * If the name of a pattern filter is missing then the pattern is used as
178          * its name.
179          * </p>
180          */
181         public String getName() {
182                 String name = fElement.getAttribute(NAME_ATTRIBUTE);
183                 if (name == null && isPatternFilter())
184                         name = getPattern();
185                 return name;
186         }
187
188         /**
189          * Returns the filter's pattern.
190          * 
191          * @return the pattern string or <code>null</code> if it's not a pattern
192          *         filter
193          */
194         public String getPattern() {
195                 return fElement.getAttribute(PATTERN_ATTRIBUTE);
196         }
197
198         /**
199          * Returns the filter's viewId.
200          * 
201          * @return the view ID or <code>null</code> if the filter is for all views
202          * @since 3.0
203          */
204         public String getTargetId() {
205                 String tid = fElement.getAttribute(TARGET_ID_ATTRIBUTE);
206
207                 if (tid != null)
208                         return tid;
209
210                 // Backwards compatibility code
211                 return fElement.getAttribute(VIEW_ID_ATTRIBUTE);
212
213         }
214
215         /**
216          * Returns the filter's description.
217          * 
218          * @return the description or <code>null</code> if no description is
219          *         provided
220          */
221         public String getDescription() {
222                 String description = fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
223                 if (description == null)
224                         description = ""; //$NON-NLS-1$
225                 return description;
226         }
227
228         /**
229          * @return <code>true</code> if this filter is a custom filter.
230          */
231         public boolean isPatternFilter() {
232                 return getPattern() != null;
233         }
234
235         /**
236          * @return <code>true</code> if this filter is a pattern filter.
237          */
238         public boolean isCustomFilter() {
239                 return fElement.getAttribute(CLASS_ATTRIBUTE) != null;
240         }
241
242         /**
243          * Returns <code>true</code> if the filter is initially enabled.
244          * 
245          * This attribute is optional and defaults to <code>true</code>.
246          */
247         public boolean isEnabled() {
248                 String strVal = fElement.getAttribute(ENABLED_ATTRIBUTE);
249                 if (strVal == null)
250                         // backward compatibility
251                         strVal = fElement.getAttribute(SELECTED_ATTRIBUTE);
252                 return strVal == null || Boolean.valueOf(strVal).booleanValue();
253         }
254
255         /*
256          * Implements a method from IComparable
257          */
258         public int compareTo(Object o) {
259                 if (o instanceof FilterDescriptor)
260                         return Collator.getInstance().compare(getName(),
261                                         ((FilterDescriptor) o).getName());
262                 else
263                         return Integer.MIN_VALUE;
264         }
265
266         // ---- initialization ---------------------------------------------------
267
268         /**
269          * Creates the filter descriptors.
270          */
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())) {
278
279                                 final FilterDescriptor[] desc = new FilterDescriptor[1];
280                                 Platform
281                                                 .run(new SafeRunnable(
282                                                                 FilterMessages
283                                                                                 .getString("FilterDescriptor.filterDescriptionCreationError.message")) { //$NON-NLS-1$
284                                                         public void run() throws Exception {
285                                                                 desc[0] = new FilterDescriptor(element);
286                                                         }
287                                                 });
288
289                                 if (desc[0] != null && !descIds.contains(desc[0].getId())) {
290                                         result.add(desc[0]);
291                                         descIds.add(desc[0].getId());
292                                 }
293                         }
294                 }
295                 Collections.sort(result);
296                 return (FilterDescriptor[]) result.toArray(new FilterDescriptor[result
297                                 .size()]);
298         }
299
300         public String getLocalId() {
301                 return fElement.getAttribute(ID_ATTRIBUTE);
302         }
303
304         public String getPluginId() {
305                 return fElement.getDeclaringExtension().getNamespace();
306         }
307
308 }