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