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