]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/filters/FilterDescriptor.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / filters / FilterDescriptor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.filters;
12
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17
18 import com.ibm.icu.text.Collator;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IExtensionRegistry;
23 import org.eclipse.core.runtime.ISafeRunnable;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.core.runtime.SafeRunner;
26
27 import org.eclipse.jface.util.SafeRunnable;
28 import org.eclipse.jface.viewers.ViewerFilter;
29
30 import org.eclipse.ui.IPluginContribution;
31 import org.eclipse.ui.activities.WorkbenchActivityHelper;
32
33 import org.eclipse.jdt.internal.corext.util.Messages;
34
35 import org.eclipse.jdt.ui.JavaUI;
36
37 /**
38  * Represents a custom filter which is provided by the
39  * "org.eclipse.jdt.ui.javaElementFilters" extension point.
40  *
41  * since 2.0
42  */
43 public class FilterDescriptor implements Comparable<FilterDescriptor>, IPluginContribution {
44
45         private static String PATTERN_FILTER_ID_PREFIX= "_patternFilterId_"; //$NON-NLS-1$
46
47
48         private static final String EXTENSION_POINT_NAME= "javaElementFilters"; //$NON-NLS-1$
49
50         private static final String FILTER_TAG= "filter"; //$NON-NLS-1$
51
52         private static final String PATTERN_ATTRIBUTE= "pattern"; //$NON-NLS-1$
53         private static final String ID_ATTRIBUTE= "id"; //$NON-NLS-1$
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         private static final String TARGET_ID_ATTRIBUTE= "targetId"; //$NON-NLS-1$
59         private static final String CLASS_ATTRIBUTE= "class"; //$NON-NLS-1$
60         private static final String NAME_ATTRIBUTE= "name"; //$NON-NLS-1$
61         private static final String ENABLED_ATTRIBUTE= "enabled"; //$NON-NLS-1$
62         private static final String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
63         /**
64          * @deprecated  use "enabled" instead
65          */
66         private static final String SELECTED_ATTRIBUTE= "selected"; //$NON-NLS-1$
67
68         private static FilterDescriptor[] fgFilterDescriptors;
69
70
71         private IConfigurationElement fElement;
72
73         /**
74          * Returns all contributed Java element filters.
75          * @return all contributed Java element filters
76          */
77         public static FilterDescriptor[] getFilterDescriptors() {
78                 if (fgFilterDescriptors == null) {
79                         IExtensionRegistry registry= Platform.getExtensionRegistry();
80                         IConfigurationElement[] elements= registry.getConfigurationElementsFor(JavaUI.ID_PLUGIN, EXTENSION_POINT_NAME);
81                         fgFilterDescriptors= createFilterDescriptors(elements);
82                 }
83                 return fgFilterDescriptors;
84         }
85         /**
86          * Returns all Java element filters which
87          * are contributed to the given view.
88          * @param targetId the target id
89          * @return all contributed Java element filters for the given view
90          */
91         public static FilterDescriptor[] getFilterDescriptors(String targetId) {
92                 FilterDescriptor[] filterDescs= FilterDescriptor.getFilterDescriptors();
93                 List<FilterDescriptor> result= new ArrayList<FilterDescriptor>(filterDescs.length);
94                 for (int i= 0; i < filterDescs.length; i++) {
95                         String tid= filterDescs[i].getTargetId();
96                         if (WorkbenchActivityHelper.filterItem(filterDescs[i]))
97                                 continue;
98                         if (tid == null || tid.equals(targetId))
99                                 result.add(filterDescs[i]);
100                 }
101                 return result.toArray(new FilterDescriptor[result.size()]);
102         }
103
104         /**
105          * Creates a new filter descriptor for the given configuration element.
106          * @param element configuration element
107          */
108         private FilterDescriptor(IConfigurationElement element) {
109                 fElement= element;
110                 // it is either a pattern filter or a custom filter
111                 Assert.isTrue(isPatternFilter() ^ isCustomFilter(), "An extension for extension-point org.eclipse.jdt.ui.javaElementFilters does not specify a correct filter"); //$NON-NLS-1$
112                 Assert.isNotNull(getId(), "An extension for extension-point org.eclipse.jdt.ui.javaElementFilters does not provide a valid ID"); //$NON-NLS-1$
113                 Assert.isNotNull(getName(), "An extension for extension-point org.eclipse.jdt.ui.javaElementFilters does not provide a valid name"); //$NON-NLS-1$
114         }
115
116         /**
117          * Creates a new <code>ViewerFilter</code>.
118          * This method is only valid for viewer filters.
119          * @return a new <code>ViewerFilter</code>
120          */
121         public ViewerFilter createViewerFilter() {
122                 if (!isCustomFilter())
123                         return null;
124
125                 final ViewerFilter[] result= new ViewerFilter[1];
126                 String message= Messages.format(FilterMessages.FilterDescriptor_filterCreationError_message, getId());
127                 ISafeRunnable code= new SafeRunnable(message) {
128                         /*
129                          * @see org.eclipse.core.runtime.ISafeRunnable#run()
130                          */
131                         public void run() throws Exception {
132                                 result[0]= (ViewerFilter)fElement.createExecutableExtension(CLASS_ATTRIBUTE);
133                         }
134
135                 };
136                 SafeRunner.run(code);
137                 return result[0];
138         }
139
140         //---- XML Attribute accessors ---------------------------------------------
141
142         /**
143          * Returns the filter's id.
144          * <p>
145          * This attribute is mandatory for custom filters.
146          * The ID for pattern filters is
147          * PATTERN_FILTER_ID_PREFIX plus the pattern itself.
148          * </p>
149          * @return the filter id
150          */
151         public String getId() {
152                 if (isPatternFilter()) {
153                         String targetId= getTargetId();
154                         if (targetId == null)
155                                 return PATTERN_FILTER_ID_PREFIX + getPattern();
156                         else
157                                 return targetId + PATTERN_FILTER_ID_PREFIX + getPattern();
158                 } else
159                         return fElement.getAttribute(ID_ATTRIBUTE);
160         }
161
162         /**
163          * Returns the filter's name.
164          * <p>
165          * If the name of a pattern filter is missing
166          * then the pattern is used as its name.
167          * </p>
168          * @return the filter's name
169          */
170         public String getName() {
171                 String name= fElement.getAttribute(NAME_ATTRIBUTE);
172                 if (name == null && isPatternFilter())
173                         name= getPattern();
174                 return name;
175         }
176
177         /**
178          * Returns the filter's pattern.
179          *
180          * @return the pattern string or <code>null</code> if it's not a pattern filter
181          */
182         public String getPattern() {
183                 return fElement.getAttribute(PATTERN_ATTRIBUTE);
184         }
185
186         /**
187          * Returns the filter's viewId.
188          *
189          * @return the view ID or <code>null</code> if the filter is for all views
190          * @since 3.0
191          */
192         public String getTargetId() {
193                 String tid= fElement.getAttribute(TARGET_ID_ATTRIBUTE);
194
195                 if (tid != null)
196                         return tid;
197
198                 // Backwards compatibility code
199                 return fElement.getAttribute(VIEW_ID_ATTRIBUTE);
200
201         }
202
203         /**
204          * Returns the filter's description.
205          *
206          * @return the description or <code>null</code> if no description is provided
207          */
208         public String getDescription() {
209                 String description= fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
210                 if (description == null)
211                         description= ""; //$NON-NLS-1$
212                 return description;
213         }
214
215         /**
216          * @return <code>true</code> if this filter is a custom filter.
217          */
218         public boolean isPatternFilter() {
219                 return getPattern() != null;
220         }
221
222         /**
223          * @return <code>true</code> if this filter is a pattern filter.
224          */
225         public boolean isCustomFilter() {
226                 return fElement.getAttribute(CLASS_ATTRIBUTE) != null;
227         }
228
229         /**
230          * Returns <code>true</code> if the filter
231          * is initially enabled.
232          *
233          * This attribute is optional and defaults to <code>true</code>.
234          * @return returns <code>true</code> if the filter is initially enabled
235          */
236         public boolean isEnabled() {
237                 String strVal= fElement.getAttribute(ENABLED_ATTRIBUTE);
238                 if (strVal == null)
239                         // backward compatibility
240                         strVal= fElement.getAttribute(SELECTED_ATTRIBUTE);
241                 return strVal == null || Boolean.valueOf(strVal).booleanValue();
242         }
243
244         /* (non-Javadoc)
245          * @see java.lang.Comparable#compareTo(java.lang.Object)
246          */
247         public int compareTo(FilterDescriptor o) {
248                 return Collator.getInstance().compare(getName(), o.getName());
249         }
250
251         //---- initialization ---------------------------------------------------
252
253         /**
254          * Creates the filter descriptors.
255          * @param elements the configuration elements
256          * @return new filter descriptors
257          */
258         private static FilterDescriptor[] createFilterDescriptors(IConfigurationElement[] elements) {
259                 List<FilterDescriptor> result= new ArrayList<FilterDescriptor>(5);
260                 Set<String> descIds= new HashSet<String>(5);
261                 for (int i= 0; i < elements.length; i++) {
262                         final IConfigurationElement element= elements[i];
263                         if (FILTER_TAG.equals(element.getName())) {
264
265                                 final FilterDescriptor[] desc= new FilterDescriptor[1];
266                                 SafeRunner.run(new SafeRunnable(FilterMessages.FilterDescriptor_filterDescriptionCreationError_message) {
267                                         public void run() throws Exception {
268                                                 desc[0]= new FilterDescriptor(element);
269                                         }
270                                 });
271
272                                 if (desc[0] != null && !descIds.contains(desc[0].getId())) {
273                                         result.add(desc[0]);
274                                         descIds.add(desc[0].getId());
275                                 }
276                         }
277                 }
278                 return result.toArray(new FilterDescriptor[result.size()]);
279         }
280
281         public String getLocalId() {
282                 return fElement.getAttribute(ID_ATTRIBUTE);
283         }
284
285     public String getPluginId() {
286         return fElement.getContributor().getName();
287     }
288 }