KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > tasklist > TasksFilter


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  * Cagatay Kavukcuoglu <cagatayk@acm.org> - Filter for markers in same project
11  *******************************************************************************/

12
13 package org.eclipse.ui.views.tasklist;
14
15 import java.util.Arrays JavaDoc;
16 import java.util.HashSet JavaDoc;
17
18 import org.eclipse.core.resources.IMarker;
19 import org.eclipse.core.resources.IMarkerDelta;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.IAdaptable;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.jface.viewers.Viewer;
24 import org.eclipse.jface.viewers.ViewerFilter;
25 import org.eclipse.ui.IContainmentAdapter;
26 import org.eclipse.ui.IMemento;
27 import org.eclipse.ui.IPersistable;
28 import org.eclipse.ui.IWorkingSet;
29 import org.eclipse.ui.IWorkingSetManager;
30 import org.eclipse.ui.PlatformUI;
31
32 class TasksFilter extends ViewerFilter implements Cloneable JavaDoc {
33
34     public static final String JavaDoc[] ROOT_TYPES = new String JavaDoc[] { IMarker.PROBLEM,
35             IMarker.TASK };
36
37     // Filter on resource constants
38
static final int ON_ANY_RESOURCE = 0;
39
40     static final int ON_SELECTED_RESOURCE_ONLY = 1;
41
42     static final int ON_SELECTED_RESOURCE_AND_CHILDREN = 2;
43
44     static final int ON_ANY_RESOURCE_OF_SAME_PROJECT = 3; // added by cagatayk@acm.org
45

46     static final int ON_WORKING_SET = 4;
47
48     // Description filter kind constants
49
static final int FILTER_CONTAINS = 0;
50
51     static final int FILTER_DOES_NOT_CONTAIN = 1;
52
53     //final static int MINIMUM_MARKER_LIMIT = 10;
54
final static int DEFAULT_MARKER_LIMIT = 2000;
55
56     //final static int MAXIMUM_MARKER_LIMIT = 20000;
57

58     String JavaDoc[] types;
59
60     int onResource;
61
62     IWorkingSet workingSet;
63
64     boolean filterOnDescription;
65
66     int descriptionFilterKind;
67
68     String JavaDoc descriptionFilter;
69
70     boolean filterOnSeverity;
71
72     int severityFilter;
73
74     boolean filterOnPriority;
75
76     int priorityFilter;
77
78     boolean filterOnCompletion;
79
80     int completionFilter;
81
82     private boolean filterOnMarkerLimit = true;
83
84     private int markerLimit = DEFAULT_MARKER_LIMIT;
85
86     private static final String JavaDoc TAG_ID = "id"; //$NON-NLS-1$
87

88     private static final String JavaDoc TAG_TYPE = "type"; //$NON-NLS-1$
89

90     private static final String JavaDoc TAG_ON_RESOURCE = "onResource"; //$NON-NLS-1$
91

92     private static final String JavaDoc TAG_WORKING_SET = "workingSet"; //$NON-NLS-1$
93

94     private static final String JavaDoc TAG_FILTER_ON_DESCRIPTION = "filterOnDescription"; //$NON-NLS-1$
95

96     private static final String JavaDoc TAG_DESCRIPTION_FILTER_KIND = "descriptionFilterKind"; //$NON-NLS-1$
97

98     private static final String JavaDoc TAG_DESCRIPTION_FILTER = "descriptionFilter"; //$NON-NLS-1$
99

100     private static final String JavaDoc TAG_FILTER_ON_SEVERITY = "filterOnSeverity"; //$NON-NLS-1$
101

102     private static final String JavaDoc TAG_SEVERITY_FILTER = "severityFilter"; //$NON-NLS-1$
103

104     private static final String JavaDoc TAG_FILTER_ON_PRIORITY = "filterOnPriority"; //$NON-NLS-1$
105

106     private static final String JavaDoc TAG_PRIORITY_FILTER = "priorityFilter"; //$NON-NLS-1$
107

108     private static final String JavaDoc TAG_FILTER_ON_COMPLETION = "filterOnCompletion"; //$NON-NLS-1$
109

110     private static final String JavaDoc TAG_COMPLETION_FILTER = "completionFilter"; //$NON-NLS-1$
111

112     private static final String JavaDoc TAG_FILTER_ON_MARKER_LIMIT = "filterOnMarkerLimit"; //$NON-NLS-1$
113

114     private static final String JavaDoc TAG_MARKER_LIMIT = "markerLimit"; //$NON-NLS-1$
115

116     public TasksFilter() {
117         reset();
118     }
119
120     boolean getFilterOnMarkerLimit() {
121         return filterOnMarkerLimit;
122     }
123
124     void setFilterOnMarkerLimit(boolean filterOnMarkerLimit) {
125         this.filterOnMarkerLimit = filterOnMarkerLimit;
126     }
127
128     int getMarkerLimit() {
129         return markerLimit;
130     }
131
132     void setMarkerLimit(int markerLimit) {
133         if (markerLimit < 1) {
134             markerLimit = TasksFilter.DEFAULT_MARKER_LIMIT;
135         }
136
137         //if (markerLimit < TasksFilter.MINIMUM_MARKER_LIMIT) {
138
// markerLimit = TasksFilter.MINIMUM_MARKER_LIMIT;
139
//} else if (markerLimit > TasksFilter.MAXIMUM_MARKER_LIMIT) {
140
// markerLimit = TasksFilter.MAXIMUM_MARKER_LIMIT;
141
//}
142

143         this.markerLimit = markerLimit;
144     }
145
146     boolean checkDescription(String JavaDoc desc) {
147         if (desc == null) { // be paranoid
148
desc = ""; //$NON-NLS-1$
149
}
150         boolean contains = containsSubstring(desc, descriptionFilter);
151         return descriptionFilterKind == FILTER_CONTAINS ? contains : !contains;
152     }
153
154     public Object JavaDoc clone() {
155         try {
156             return super.clone();
157         } catch (CloneNotSupportedException JavaDoc e) {
158             throw new Error JavaDoc(); // shouldn't happen
159
}
160     }
161
162     boolean containsSubstring(String JavaDoc string, String JavaDoc substring) {
163         int strLen = string.length();
164         int subLen = substring.length();
165         int len = strLen - subLen;
166         for (int i = 0; i <= len; ++i) {
167             if (string.regionMatches(true, i, substring, 0, subLen)) {
168                 return true;
169             }
170         }
171         return false;
172     }
173
174     /**
175      * Returns if the given resource is enclosed by a working set element.
176      * The IContainmentAdapter of each working set element is used for the
177      * containment test. If there is no IContainmentAdapter for a working
178      * set element, a simple resource based test is used.
179      *
180      * @param element resource to test for enclosure by a working set
181      * element
182      * @return true if element is enclosed by a working set element and
183      * false otherwise.
184      */

185     private boolean isEnclosed(IResource element) {
186         IPath elementPath = element.getFullPath();
187         IAdaptable[] workingSetElements = workingSet.getElements();
188
189         if (elementPath.isEmpty() || elementPath.isRoot()) {
190             return false;
191         }
192         for (int i = 0; i < workingSetElements.length; i++) {
193             IAdaptable workingSetElement = workingSetElements[i];
194             IContainmentAdapter containmentAdapter = (IContainmentAdapter) workingSetElement
195                     .getAdapter(IContainmentAdapter.class);
196
197             // if there is no IContainmentAdapter defined for the working
198
// set element type fall back to using resource based
199
// containment check
200
if (containmentAdapter != null) {
201                 if (containmentAdapter.contains(workingSetElement, element,
202                         IContainmentAdapter.CHECK_CONTEXT
203                                 | IContainmentAdapter.CHECK_IF_CHILD
204                                 | IContainmentAdapter.CHECK_IF_DESCENDANT)) {
205                     return true;
206                 }
207             } else if (isEnclosedResource(element, elementPath,
208                     workingSetElement)) {
209                 return true;
210             }
211         }
212         return false;
213     }
214
215     /**
216      * Returns if the given resource is enclosed by a working set element.
217      * A resource is enclosed if it is either a parent of a working set
218      * element, a child of a working set element or a working set element
219      * itself.
220      * Simple path comparison is used. This is only guaranteed to return
221      * correct results for resource working set elements.
222      *
223      * @param element resource to test for enclosure by a working set
224      * element
225      * @param elementPath full, absolute path of the element to test
226      * @return true if element is enclosed by a working set element and
227      * false otherwise.
228      */

229     private boolean isEnclosedResource(IResource element, IPath elementPath,
230             IAdaptable workingSetElement) {
231         IResource workingSetResource = null;
232
233         if (workingSetElement.equals(element)) {
234             return true;
235         }
236         if (workingSetElement instanceof IResource) {
237             workingSetResource = (IResource) workingSetElement;
238         } else {
239             workingSetResource = (IResource) workingSetElement
240                     .getAdapter(IResource.class);
241         }
242         if (workingSetResource != null) {
243             IPath resourcePath = workingSetResource.getFullPath();
244             if (resourcePath.isPrefixOf(elementPath)) {
245                 return true;
246             }
247         }
248         return false;
249     }
250
251     public void reset() {
252         types = ROOT_TYPES;
253         onResource = ON_ANY_RESOURCE;
254         filterOnDescription = false;
255         descriptionFilter = ""; //$NON-NLS-1$
256
filterOnSeverity = false;
257         severityFilter = 0;
258         filterOnPriority = false;
259         priorityFilter = 0;
260         filterOnCompletion = false;
261         completionFilter = 0;
262         filterOnMarkerLimit = true;
263         markerLimit = DEFAULT_MARKER_LIMIT;
264     }
265
266     /**
267      * @see IPersistable
268      */

269     public void restoreState(IMemento memento) {
270         IMemento children[] = memento.getChildren(TAG_TYPE);
271         types = new String JavaDoc[children.length];
272         for (int i = 0; i < children.length; i++) {
273             types[i] = children[i].getString(TAG_ID);
274         }
275         Integer JavaDoc ival = memento.getInteger(TAG_ON_RESOURCE);
276         onResource = ival == null ? ON_ANY_RESOURCE : ival.intValue();
277         restoreWorkingSet(memento.getString(TAG_WORKING_SET));
278         ival = memento.getInteger(TAG_FILTER_ON_DESCRIPTION);
279         filterOnDescription = ival != null && ival.intValue() == 1;
280         ival = memento.getInteger(TAG_DESCRIPTION_FILTER_KIND);
281         descriptionFilterKind = ival == null ? FILTER_CONTAINS : ival
282                 .intValue();
283         descriptionFilter = memento.getString(TAG_DESCRIPTION_FILTER);
284         if (descriptionFilter == null) {
285             descriptionFilter = ""; //$NON-NLS-1$
286
}
287         ival = memento.getInteger(TAG_FILTER_ON_SEVERITY);
288         filterOnSeverity = ival != null && ival.intValue() == 1;
289         ival = memento.getInteger(TAG_SEVERITY_FILTER);
290         severityFilter = ival == null ? 0 : ival.intValue();
291         ival = memento.getInteger(TAG_FILTER_ON_PRIORITY);
292         filterOnPriority = ival != null && ival.intValue() == 1;
293         ival = memento.getInteger(TAG_PRIORITY_FILTER);
294         priorityFilter = ival == null ? 0 : ival.intValue();
295         ival = memento.getInteger(TAG_FILTER_ON_COMPLETION);
296         filterOnCompletion = ival != null && ival.intValue() == 1;
297         ival = memento.getInteger(TAG_COMPLETION_FILTER);
298         completionFilter = ival == null ? 0 : ival.intValue();
299         ival = memento.getInteger(TAG_FILTER_ON_MARKER_LIMIT);
300         filterOnMarkerLimit = ival == null || ival.intValue() == 1;
301         ival = memento.getInteger(TAG_MARKER_LIMIT);
302         markerLimit = ival == null ? DEFAULT_MARKER_LIMIT : ival.intValue();
303     }
304
305     /**
306      * Restores the saved working set, if any.
307      *
308      * @param the saved working set name or null
309      */

310     private void restoreWorkingSet(String JavaDoc workingSetName) {
311         if (workingSetName != null) {
312             IWorkingSetManager workingSetManager = PlatformUI.getWorkbench()
313                     .getWorkingSetManager();
314             IWorkingSet workingSet = workingSetManager
315                     .getWorkingSet(workingSetName);
316
317             if (workingSet != null) {
318                 this.workingSet = workingSet;
319             }
320         }
321     }
322
323     /**
324      * Saves the object state within a memento.
325      *
326      * @param memento a memento to receive the object state
327      */

328     public void saveState(IMemento memento) {
329         for (int i = 0; i < types.length; i++) {
330             memento.createChild(TAG_TYPE).putString(TAG_ID, types[i]);
331         }
332         memento.putInteger(TAG_ON_RESOURCE, onResource);
333         if (workingSet != null) {
334             memento.putString(TAG_WORKING_SET, workingSet.getName());
335         }
336         memento.putInteger(TAG_FILTER_ON_DESCRIPTION, filterOnDescription ? 1
337                 : 0);
338         memento.putInteger(TAG_DESCRIPTION_FILTER_KIND, descriptionFilterKind);
339         memento.putString(TAG_DESCRIPTION_FILTER, descriptionFilter);
340         memento.putInteger(TAG_FILTER_ON_SEVERITY, filterOnSeverity ? 1 : 0);
341         memento.putInteger(TAG_SEVERITY_FILTER, severityFilter);
342         memento.putInteger(TAG_FILTER_ON_PRIORITY, filterOnPriority ? 1 : 0);
343         memento.putInteger(TAG_PRIORITY_FILTER, priorityFilter);
344         memento
345                 .putInteger(TAG_FILTER_ON_COMPLETION, filterOnCompletion ? 1
346                         : 0);
347         memento.putInteger(TAG_COMPLETION_FILTER, completionFilter);
348         memento.putInteger(TAG_FILTER_ON_MARKER_LIMIT, filterOnMarkerLimit ? 1
349                 : 0);
350         memento.putInteger(TAG_MARKER_LIMIT, markerLimit);
351     }
352
353     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
354         return select((IMarker) element);
355     }
356
357     public boolean select(IMarker marker) {
358         // resource settings are handled by the content provider
359
return selectByType(marker) && selectByAttributes(marker)
360                 && selectByWorkingSet(marker);
361     }
362
363     public boolean select(IMarkerDelta markerDelta) {
364         // resource settings are handled by the content provider
365
return selectByType(markerDelta) && selectByAttributes(markerDelta)
366                 && selectByWorkingSet(markerDelta);
367     }
368
369     private boolean selectByType(IMarker marker) {
370         for (int i = 0; i < types.length; ++i) {
371             if (MarkerUtil.isMarkerType(marker, types[i])) {
372                 return true;
373             }
374         }
375         return false;
376     }
377
378     private boolean selectByType(IMarkerDelta markerDelta) {
379         for (int i = 0; i < types.length; ++i) {
380             if (markerDelta.isSubtypeOf(types[i])) {
381                 return true;
382             }
383         }
384         return false;
385     }
386
387     /**
388      * Returns whether the specified marker should be filter out or not.
389      *
390      * @param marker the marker to test
391      * @return
392      * true=the marker should not be filtered out
393      * false=the marker should be filtered out
394      */

395     private boolean selectByWorkingSet(IMarker marker) {
396         if (workingSet == null || onResource != ON_WORKING_SET) {
397             return true;
398         }
399         IResource resource = marker.getResource();
400         if (resource != null) {
401             return isEnclosed(resource);
402         }
403         return false;
404     }
405
406     /**
407      * Returns whether the specified marker delta should be filter out
408      * or not.
409      *
410      * @param markerDelta the marker delta to test
411      * @return
412      * true=the marker delta should not be filtered out
413      * false=the marker delta should be filtered out
414      */

415     private boolean selectByWorkingSet(IMarkerDelta markerDelta) {
416         if (workingSet == null || onResource != ON_WORKING_SET) {
417             return true;
418         }
419         IResource resource = markerDelta.getResource();
420         if (resource != null) {
421             return isEnclosed(resource);
422         }
423         return false;
424     }
425
426     /*
427      * WARNING: selectByAttributes(IMarker) and selectByAttributes(IMarkerDelta) must correspond.
428      */

429
430     private boolean selectByAttributes(IMarker marker) {
431
432         // severity filter applies only to problems
433
if (filterOnSeverity
434                 && MarkerUtil.isMarkerType(marker, IMarker.PROBLEM)) {
435             int sev = MarkerUtil.getSeverity(marker);
436             if ((severityFilter & (1 << sev)) == 0) {
437                 return false;
438             }
439         }
440
441         // priority and completion filters apply only to tasks
442
// avoid doing type check more than once
443
if ((filterOnPriority || filterOnCompletion)
444                 && MarkerUtil.isMarkerType(marker, IMarker.TASK)) {
445             if (filterOnPriority) {
446                 int pri = MarkerUtil.getPriority(marker);
447                 if ((priorityFilter & (1 << pri)) == 0) {
448                     return false;
449                 }
450             }
451             if (filterOnCompletion) {
452                 boolean complete = MarkerUtil.isComplete(marker);
453                 if ((completionFilter & (complete ? 2 : 1)) == 0) {
454                     return false;
455                 }
456             }
457         }
458
459         // description applies to all markers
460
if (filterOnDescription) {
461             String JavaDoc desc = MarkerUtil.getMessage(marker);
462             if (!checkDescription(desc)) {
463                 return false;
464             }
465         }
466         return true;
467     }
468
469     private boolean selectByAttributes(IMarkerDelta markerDelta) {
470
471         // severity filter applies only to problems
472
if (filterOnSeverity && markerDelta.isSubtypeOf(IMarker.PROBLEM)) {
473             int sev = markerDelta.getAttribute(IMarker.SEVERITY,
474                     IMarker.SEVERITY_WARNING);
475             if ((severityFilter & (1 << sev)) == 0) {
476                 return false;
477             }
478         }
479
480         // priority and completion filters apply only to tasks
481
// avoid doing type check more than once
482
if ((filterOnPriority || filterOnCompletion)
483                 && markerDelta.isSubtypeOf(IMarker.TASK)) {
484             if (filterOnPriority) {
485                 int pri = markerDelta.getAttribute(IMarker.PRIORITY,
486                         IMarker.PRIORITY_NORMAL);
487                 if ((priorityFilter & (1 << pri)) == 0) {
488                     return false;
489                 }
490             }
491             if (filterOnCompletion) {
492                 boolean complete = markerDelta
493                         .getAttribute(IMarker.DONE, false);
494                 if ((completionFilter & (complete ? 2 : 1)) == 0) {
495                     return false;
496                 }
497             }
498         }
499
500         // description applies to all markers
501
if (filterOnDescription) {
502             String JavaDoc desc = markerDelta.getAttribute(IMarker.MESSAGE, ""); //$NON-NLS-1$
503
if (!checkDescription(desc)) {
504                 return false;
505             }
506         }
507         return true;
508     }
509
510     /**
511      * Returns whether the filter is including all markers.
512      *
513      * @return <code>true</code> if the filter includes all markers, <code>false</code> if not
514      */

515     public boolean isShowingAll() {
516         if (filterOnDescription || filterOnSeverity || filterOnPriority
517                 || filterOnCompletion) {
518             return false;
519         }
520         if (onResource != ON_ANY_RESOURCE) {
521             return false;
522         }
523
524         HashSet JavaDoc set = new HashSet JavaDoc(Arrays.asList(types));
525         if (set.size() != ROOT_TYPES.length) {
526             return false;
527         }
528         for (int i = 0; i < ROOT_TYPES.length; ++i) {
529             if (!set.contains(ROOT_TYPES[i])) {
530                 return false;
531             }
532         }
533         return true;
534     }
535 }
536
Popular Tags