KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > markers > internal > ProblemFilter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.ui.views.markers.internal;
13
14 import org.eclipse.core.resources.IMarker;
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.eclipse.jface.dialogs.IDialogSettings;
17 import org.eclipse.ui.IMemento;
18 import org.eclipse.ui.IPluginContribution;
19 import org.eclipse.ui.IWorkbenchPage;
20 import org.eclipse.ui.IWorkbenchPreferenceConstants;
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.activities.IIdentifier;
24 import org.eclipse.ui.activities.WorkbenchActivityHelper;
25
26 /**
27  * ProblemFilters are the filters used in the problems view.
28  *
29  */

30 public class ProblemFilter extends MarkerFilter {
31
32     private static final String JavaDoc TAG_CONTAINS = "contains"; //$NON-NLS-1$
33

34     private static final String JavaDoc TAG_DESCRIPTION = "description"; //$NON-NLS-1$
35

36     private static final String JavaDoc TAG_SELECT_BY_SEVERITY = "selectBySeverity"; //$NON-NLS-1$
37

38     private static final String JavaDoc TAG_SEVERITY = "severity"; //$NON-NLS-1$
39

40     final static boolean DEFAULT_CONTAINS = true;
41
42     final static String JavaDoc DEFAULT_DESCRIPTION = ""; //$NON-NLS-1$
43

44     final static boolean DEFAULT_SELECT_BY_SEVERITY = false;
45
46     final static int DEFAULT_SEVERITY = 0;
47
48     /**
49      * Severity for errors
50      */

51     public final static int SEVERITY_ERROR = 1 << 2;
52
53     /**
54      * Severity for warnings
55      */

56     public final static int SEVERITY_WARNING = 1 << 1;
57
58     /**
59      * Severity for infos
60      */

61     public final static int SEVERITY_INFO = 1 << 0;
62
63     private boolean contains;
64
65     private String JavaDoc description;
66
67     private boolean selectBySeverity;
68
69     private int severity;
70
71     private IPluginContribution contributionDescriptor = null;
72
73     private IIdentifier identifier;
74
75     /**
76      * Create a new instance of the receiver with name filterName.
77      *
78      * @param filterName
79      * A human readable name for the filter.
80      */

81     public ProblemFilter(String JavaDoc filterName) {
82         super(filterName, new String JavaDoc[] { IMarker.PROBLEM });
83         if ((PlatformUI.getPreferenceStore()
84                 .getBoolean(IWorkbenchPreferenceConstants.USE_WINDOW_WORKING_SET_BY_DEFAULT))) {
85             IWorkbenchWindow window = PlatformUI.getWorkbench()
86                     .getActiveWorkbenchWindow();
87             if (window != null) {
88                 IWorkbenchPage page = window.getActivePage();
89                 if (page != null) {
90                     setOnResource(MarkerFilter.ON_WORKING_SET);
91                     setWorkingSet(page.getAggregateWorkingSet());
92                 }
93             }
94         }
95
96     }
97
98     public boolean selectMarker(ConcreteMarker marker) {
99         if (!(marker instanceof ProblemMarker)) {
100             return false;
101         }
102
103         ProblemMarker problemMarker = (ProblemMarker) marker;
104
105         return !isEnabled()
106                 || (super.selectMarker(problemMarker)
107                         && selectByDescription(problemMarker) && selectBySeverity(problemMarker));
108     }
109
110     private boolean selectByDescription(ConcreteMarker item) {
111         if (description == null || description.equals("")) { //$NON-NLS-1$
112
return true;
113         }
114
115         String JavaDoc markerDescription = item.getDescription();
116         int index = markerDescription.indexOf(description);
117         return contains ? (index >= 0) : (index < 0);
118     }
119
120     private boolean selectBySeverity(ProblemMarker item) {
121         if (selectBySeverity) {
122             int markerSeverity = item.getSeverity();
123
124             if (markerSeverity == IMarker.SEVERITY_ERROR) {
125                 return (severity & SEVERITY_ERROR) > 0;
126             } else if (markerSeverity == IMarker.SEVERITY_WARNING) {
127                 return (severity & SEVERITY_WARNING) > 0;
128             } else if (markerSeverity == IMarker.SEVERITY_INFO) {
129                 return (severity & SEVERITY_INFO) > 0;
130             }
131         }
132
133         return true;
134     }
135
136     /**
137      * Get the value for if there is a check for containing a phrase.
138      *
139      * @return boolean
140      */

141     public boolean getContains() {
142         return contains;
143     }
144
145     /**
146      * Get the value for the description.
147      *
148      * @return boolean
149      */

150     public String JavaDoc getDescription() {
151         return description;
152     }
153
154     /**
155      * Get the value for if there is a check for severity.
156      *
157      * @return boolean
158      */

159     public boolean getSelectBySeverity() {
160         return selectBySeverity;
161     }
162
163     /**
164      * Get the value for if there is a severity.
165      *
166      * @return boolean
167      */

168     public int getSeverity() {
169         return severity;
170     }
171
172     /**
173      * Set the value for if there is a check for containing a phrase.
174      *
175      * @param contains
176      */

177     public void setContains(boolean contains) {
178         this.contains = contains;
179     }
180
181     /**
182      * Set the value for the description.
183      *
184      * @param description
185      */

186     public void setDescription(String JavaDoc description) {
187         this.description = description;
188     }
189
190     /**
191      * Set the value for if there is a check for severity
192      *
193      * @param selectBySeverity
194      */

195     public void setSelectBySeverity(boolean selectBySeverity) {
196         this.selectBySeverity = selectBySeverity;
197     }
198
199     /**
200      * Set the value for the severity to match against.
201      *
202      * @param severity
203      */

204     public void setSeverity(int severity) {
205         this.severity = severity;
206     }
207
208     /*
209      * (non-Javadoc)
210      *
211      * @see org.eclipse.ui.views.markers.internal.MarkerFilter#resetState()
212      */

213     public void resetState() {
214         super.resetState();
215         contains = DEFAULT_CONTAINS;
216         description = DEFAULT_DESCRIPTION;
217         selectBySeverity = DEFAULT_SELECT_BY_SEVERITY;
218         severity = DEFAULT_SEVERITY;
219     }
220
221     /*
222      * (non-Javadoc)
223      *
224      * @see org.eclipse.ui.views.markers.internal.MarkerFilter#restoreFilterSettings(org.eclipse.jface.dialogs.IDialogSettings)
225      */

226     public void restoreFilterSettings(IDialogSettings settings) {
227
228         super.restoreFilterSettings(settings);
229
230         String JavaDoc setting = settings.get(TAG_CONTAINS);
231
232         if (setting != null) {
233             contains = Boolean.valueOf(setting).booleanValue();
234         }
235
236         setting = settings.get(TAG_DESCRIPTION);
237
238         if (setting != null) {
239             description = new String JavaDoc(setting);
240         }
241
242         setting = settings.get(TAG_SELECT_BY_SEVERITY);
243
244         if (setting != null) {
245             selectBySeverity = Boolean.valueOf(setting).booleanValue();
246         }
247
248         setting = settings.get(TAG_SEVERITY);
249
250         if (setting != null) {
251             try {
252                 severity = Integer.parseInt(setting);
253             } catch (NumberFormatException JavaDoc eNumberFormat) {
254             }
255         }
256
257     }
258
259     /*
260      * (non-Javadoc)
261      *
262      * @see org.eclipse.ui.views.markers.internal.MarkerFilter#restoreFilterSettings(org.eclipse.ui.IMemento)
263      */

264     protected void restoreFilterSettings(IMemento memento) {
265
266         super.restoreFilterSettings(memento);
267
268         String JavaDoc setting = memento.getString(TAG_CONTAINS);
269
270         if (setting != null) {
271             contains = Boolean.valueOf(setting).booleanValue();
272         }
273
274         setting = memento.getString(TAG_DESCRIPTION);
275
276         if (setting != null) {
277             description = new String JavaDoc(setting);
278         }
279
280         setting = memento.getString(TAG_SELECT_BY_SEVERITY);
281
282         if (setting != null) {
283             selectBySeverity = Boolean.valueOf(setting).booleanValue();
284         }
285
286         Integer JavaDoc severitySetting = memento.getInteger(TAG_SEVERITY);
287
288         if (setting != null) {
289             severity = severitySetting.intValue();
290         }
291     }
292
293     /*
294      * (non-Javadoc)
295      *
296      * @see org.eclipse.ui.views.markers.internal.MarkerFilter#saveFilterSettings(org.eclipse.ui.IMemento)
297      */

298     public void saveFilterSettings(IMemento settings) {
299         super.saveFilterSettings(settings);
300         settings.putString(TAG_CONTAINS, String.valueOf(contains));
301         settings.putString(TAG_DESCRIPTION, description);
302         settings.putString(TAG_SELECT_BY_SEVERITY, String
303                 .valueOf(selectBySeverity));
304         settings.putInteger(TAG_SEVERITY, severity);
305
306     }
307
308     /**
309      * Get the id of the filter. <code>null</code> if the filter is user
310      * defined.
311      *
312      * @return String
313      */

314     public String JavaDoc getId() {
315         if (contributionDescriptor == null) {
316             return null;
317         }
318         return contributionDescriptor.getLocalId();
319     }
320
321     void createContributionFrom(IConfigurationElement element) {
322         final String JavaDoc id = element.getAttribute(MarkerSupportRegistry.ID);
323         final String JavaDoc namespace = element.getNamespace();
324         contributionDescriptor = new IPluginContribution() {
325             /*
326              * (non-Javadoc)
327              *
328              * @see org.eclipse.ui.IPluginContribution#getLocalId()
329              */

330             public String JavaDoc getLocalId() {
331                 return id;
332             }
333
334             /*
335              * (non-Javadoc)
336              *
337              * @see org.eclipse.ui.IPluginContribution#getPluginId()
338              */

339             public String JavaDoc getPluginId() {
340                 return namespace;
341             }
342         };
343     }
344
345     /**
346      * Return whether or not the receiver will be filtered out due to an
347      * activity match.
348      *
349      * @return boolean <code>true</code> if it is filtered out.
350      */

351     public boolean isFilteredOutByActivity() {
352         if (contributionDescriptor == null) {
353             return false;
354         }
355         if (identifier == null) {
356             identifier = WorkbenchActivityHelper
357                     .getIdentifier(contributionDescriptor);
358         }
359         return !identifier.isEnabled();
360     }
361
362     public boolean isEnabled() {
363         return super.isEnabled() && !isFilteredOutByActivity();
364     }
365
366 }
367
Popular Tags