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