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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.filters;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
18 import com.ibm.icu.text.Collator;
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;
27 import org.eclipse.jface.util.SafeRunnable;
28 import org.eclipse.jface.viewers.ViewerFilter;
30 import org.eclipse.ui.IPluginContribution;
31 import org.eclipse.ui.activities.WorkbenchActivityHelper;
33 import org.eclipse.jdt.internal.corext.util.Messages;
35 import org.eclipse.jdt.ui.JavaUI;
38 * Represents a custom filter which is provided by the
39 * "org.eclipse.jdt.ui.javaElementFilters" extension point.
43 public class FilterDescriptor implements Comparable<FilterDescriptor>, IPluginContribution {
45 private static String PATTERN_FILTER_ID_PREFIX= "_patternFilterId_"; //$NON-NLS-1$
48 private static final String EXTENSION_POINT_NAME= "javaElementFilters"; //$NON-NLS-1$
50 private static final String FILTER_TAG= "filter"; //$NON-NLS-1$
52 private static final String PATTERN_ATTRIBUTE= "pattern"; //$NON-NLS-1$
53 private static final String ID_ATTRIBUTE= "id"; //$NON-NLS-1$
55 * @deprecated as of 3.0 use {@link FilterDescriptor#TARGET_ID_ATTRIBUTE}
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$
64 * @deprecated use "enabled" instead
66 private static final String SELECTED_ATTRIBUTE= "selected"; //$NON-NLS-1$
68 private static FilterDescriptor[] fgFilterDescriptors;
71 private IConfigurationElement fElement;
74 * Returns all contributed Java element filters.
75 * @return all contributed Java element filters
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);
83 return fgFilterDescriptors;
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
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]))
98 if (tid == null || tid.equals(targetId))
99 result.add(filterDescs[i]);
101 return result.toArray(new FilterDescriptor[result.size()]);
105 * Creates a new filter descriptor for the given configuration element.
106 * @param element configuration element
108 private FilterDescriptor(IConfigurationElement 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$
117 * Creates a new <code>ViewerFilter</code>.
118 * This method is only valid for viewer filters.
119 * @return a new <code>ViewerFilter</code>
121 public ViewerFilter createViewerFilter() {
122 if (!isCustomFilter())
125 final ViewerFilter[] result= new ViewerFilter[1];
126 String message= Messages.format(FilterMessages.FilterDescriptor_filterCreationError_message, getId());
127 ISafeRunnable code= new SafeRunnable(message) {
129 * @see org.eclipse.core.runtime.ISafeRunnable#run()
131 public void run() throws Exception {
132 result[0]= (ViewerFilter)fElement.createExecutableExtension(CLASS_ATTRIBUTE);
136 SafeRunner.run(code);
140 //---- XML Attribute accessors ---------------------------------------------
143 * Returns the filter's id.
145 * This attribute is mandatory for custom filters.
146 * The ID for pattern filters is
147 * PATTERN_FILTER_ID_PREFIX plus the pattern itself.
149 * @return the filter id
151 public String getId() {
152 if (isPatternFilter()) {
153 String targetId= getTargetId();
154 if (targetId == null)
155 return PATTERN_FILTER_ID_PREFIX + getPattern();
157 return targetId + PATTERN_FILTER_ID_PREFIX + getPattern();
159 return fElement.getAttribute(ID_ATTRIBUTE);
163 * Returns the filter's name.
165 * If the name of a pattern filter is missing
166 * then the pattern is used as its name.
168 * @return the filter's name
170 public String getName() {
171 String name= fElement.getAttribute(NAME_ATTRIBUTE);
172 if (name == null && isPatternFilter())
178 * Returns the filter's pattern.
180 * @return the pattern string or <code>null</code> if it's not a pattern filter
182 public String getPattern() {
183 return fElement.getAttribute(PATTERN_ATTRIBUTE);
187 * Returns the filter's viewId.
189 * @return the view ID or <code>null</code> if the filter is for all views
192 public String getTargetId() {
193 String tid= fElement.getAttribute(TARGET_ID_ATTRIBUTE);
198 // Backwards compatibility code
199 return fElement.getAttribute(VIEW_ID_ATTRIBUTE);
204 * Returns the filter's description.
206 * @return the description or <code>null</code> if no description is provided
208 public String getDescription() {
209 String description= fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
210 if (description == null)
211 description= ""; //$NON-NLS-1$
216 * @return <code>true</code> if this filter is a custom filter.
218 public boolean isPatternFilter() {
219 return getPattern() != null;
223 * @return <code>true</code> if this filter is a pattern filter.
225 public boolean isCustomFilter() {
226 return fElement.getAttribute(CLASS_ATTRIBUTE) != null;
230 * Returns <code>true</code> if the filter
231 * is initially enabled.
233 * This attribute is optional and defaults to <code>true</code>.
234 * @return returns <code>true</code> if the filter is initially enabled
236 public boolean isEnabled() {
237 String strVal= fElement.getAttribute(ENABLED_ATTRIBUTE);
239 // backward compatibility
240 strVal= fElement.getAttribute(SELECTED_ATTRIBUTE);
241 return strVal == null || Boolean.valueOf(strVal).booleanValue();
245 * @see java.lang.Comparable#compareTo(java.lang.Object)
247 public int compareTo(FilterDescriptor o) {
248 return Collator.getInstance().compare(getName(), o.getName());
251 //---- initialization ---------------------------------------------------
254 * Creates the filter descriptors.
255 * @param elements the configuration elements
256 * @return new filter descriptors
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())) {
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);
272 if (desc[0] != null && !descIds.contains(desc[0].getId())) {
274 descIds.add(desc[0].getId());
278 return result.toArray(new FilterDescriptor[result.size()]);
281 public String getLocalId() {
282 return fElement.getAttribute(ID_ATTRIBUTE);
285 public String getPluginId() {
286 return fElement.getContributor().getName();