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