KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.eclipse.core.resources.IContainer;
25 import org.eclipse.core.resources.IMarker;
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.resources.ResourcesPlugin;
29 import org.eclipse.core.resources.mapping.ResourceMapping;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IAdaptable;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.jface.dialogs.IDialogSettings;
34 import org.eclipse.ui.IMemento;
35 import org.eclipse.ui.IWorkingSet;
36 import org.eclipse.ui.PlatformUI;
37
38 /**
39  * MarkerFilter is the class that defines a filter on markers in a
40  * MarkerView.
41  *
42  */

43 public class MarkerFilter implements Cloneable JavaDoc {
44
45     static final String JavaDoc TAG_ENABLED = "enabled"; //$NON-NLS-1$
46

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

49     private static final String JavaDoc TAG_SELECTED_TYPES = "selectedType"; //$NON-NLS-1$
50

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

53     private static final String JavaDoc TAG_TYPES_DELIMITER = ":"; //$NON-NLS-1$
54

55     /**
56      * New attribute to handle the selection status of marker types.
57      */

58     private static final String JavaDoc TAG_SELECTION_STATUS = "selectionStatus"; //$NON-NLS-1$
59

60     /**
61      * Attribute status true.
62      */

63     private static final String JavaDoc SELECTED_FALSE = "false"; //$NON-NLS-1$
64

65     /**
66      * Attribute status false.
67      */

68     private static final String JavaDoc SELECTED_TRUE = "true"; //$NON-NLS-1$
69

70     /**
71      * Constant for any element.
72      */

73     public static final int ON_ANY = 0;
74
75     /**
76      * Constant for any selected element only.
77      */

78     public static final int ON_SELECTED_ONLY = 1;
79
80     /**
81      * Constant for selected element and children.
82      */

83     public static final int ON_SELECTED_AND_CHILDREN = 2;
84
85     /**
86      * Constant for any element in same container.
87      */

88     public static final int ON_ANY_IN_SAME_CONTAINER = 3;
89
90     /**
91      * Constant for on working set.
92      */

93     public static final int ON_WORKING_SET = 4;
94
95     static final int DEFAULT_ON_RESOURCE = ON_ANY;
96
97     static final boolean DEFAULT_ACTIVATION_STATUS = true;
98
99     protected List JavaDoc rootTypes = new ArrayList JavaDoc();
100
101     protected List JavaDoc selectedTypes = new ArrayList JavaDoc();
102
103     protected IWorkingSet workingSet;
104
105     protected int onResource;
106
107     protected boolean enabled;
108
109     private IResource[] focusResource;
110
111     private Set JavaDoc cachedWorkingSet;
112
113     // The human readable name for the filter
114
private String JavaDoc name;
115
116     /**
117      * Create a new instance of the receiver.
118      *
119      * @param filterName
120      * The human readable name for the filter
121      * @param rootTypes
122      * The types this filter will be applied to
123      */

124     MarkerFilter(String JavaDoc filterName, String JavaDoc[] rootTypes) {
125
126         name = filterName;
127
128         for (int i = 0; i < rootTypes.length; i++) {
129             MarkerType type = MarkerTypesModel.getInstance().getType(
130                     rootTypes[i]);
131
132             if (!this.rootTypes.contains(type)) {
133                 this.rootTypes.add(type);
134             }
135         }
136         resetState();
137     }
138
139     /**
140      * List all types known to this MarkerFilter.
141      *
142      * @param types
143      * list to be filled in with types
144      */

145     public void addAllSubTypes(List JavaDoc types) {
146         for (int i = 0; i < rootTypes.size(); i++) {
147             MarkerType rootType = (MarkerType) rootTypes.get(i);
148             addAllSubTypes(types, rootType);
149         }
150     }
151
152     private void addAllSubTypes(List JavaDoc types, MarkerType type) {
153         if (type == null) {
154             return;
155         }
156
157         if (!types.contains(type)) {
158             types.add(type);
159         }
160
161         MarkerType[] subTypes = type.getSubtypes();
162
163         for (int i = 0; i < subTypes.length; i++) {
164             addAllSubTypes(types, subTypes[i]);
165         }
166     }
167
168     /**
169      * Adds all markers in the given set of resources to the given list
170      *
171      * @param resultList
172      * @param resources
173      * @param markerTypeId
174      * @param depth
175      * @throws CoreException
176      */

177     private List JavaDoc findMarkers(IResource[] resources, int depth, int limit,
178             IProgressMonitor mon, boolean ignoreExceptions)
179             throws CoreException {
180         if (resources == null) {
181             return Collections.EMPTY_LIST;
182         }
183
184         List JavaDoc resultList = new ArrayList JavaDoc(resources.length * 2);
185
186         // Optimization: if a type appears in the selectedTypes list along with
187
// all of its
188
// subtypes, then combine these in a single search.
189

190         // List of types that haven't been replaced by one of their supertypes
191
HashSet JavaDoc typesToSearch = new HashSet JavaDoc(selectedTypes.size());
192
193         // List of types that appeared in selectedTypes along with all of their
194
// subtypes
195
HashSet JavaDoc includeAllSubtypes = new HashSet JavaDoc(selectedTypes.size());
196
197         typesToSearch.addAll(selectedTypes);
198
199         Iterator JavaDoc iter = selectedTypes.iterator();
200
201         while (iter.hasNext()) {
202             MarkerType type = (MarkerType) iter.next();
203
204             Collection JavaDoc subtypes = Arrays.asList(type.getAllSubTypes());
205
206             if (selectedTypes.containsAll(subtypes)) {
207                 typesToSearch.removeAll(subtypes);
208
209                 includeAllSubtypes.add(type);
210             }
211         }
212
213         mon.beginTask(MarkerMessages.MarkerFilter_searching, typesToSearch
214                 .size()
215                 * resources.length);
216
217         // Use this hash set to determine if there are any resources in the
218
// list that appear along with their parent.
219
HashSet JavaDoc resourcesToSearch = new HashSet JavaDoc();
220
221         // Insert all the resources into the hashset
222
for (int idx = 0; idx < resources.length; idx++) {
223             IResource next = resources[idx];
224
225             if (!next.exists()) {
226                 continue;
227             }
228
229             if (resourcesToSearch.contains(next)) {
230                 mon.worked(typesToSearch.size());
231             } else {
232                 resourcesToSearch.add(next);
233             }
234         }
235
236         // Iterate through all the selected resources
237
for (int resourceIdx = 0; resourceIdx < resources.length; resourceIdx++) {
238             iter = typesToSearch.iterator();
239
240             IResource resource = resources[resourceIdx];
241
242             // Skip resources that don't exist
243
if (!resource.isAccessible()) {
244                 continue;
245             }
246
247             if (depth == IResource.DEPTH_INFINITE) {
248                 // Determine if any parent of this resource is also in our
249
// filter
250
IResource parent = resource.getParent();
251                 boolean found = false;
252                 while (parent != null) {
253                     if (resourcesToSearch.contains(parent)) {
254                         found = true;
255                     }
256
257                     parent = parent.getParent();
258                 }
259
260                 // If a parent of this resource is also in the filter, we can
261
// skip it
262
// because we'll pick up its markers when we search the parent.
263
if (found) {
264                     continue;
265                 }
266             }
267
268             // Iterate through all the marker types
269
while (iter.hasNext()) {
270                 MarkerType markerType = (MarkerType) iter.next();
271
272                 // Only search for subtypes of the marker if we found all of its
273
// subtypes in the filter criteria.
274
IMarker[] markers = resource.findMarkers(markerType.getId(),
275                         includeAllSubtypes.contains(markerType), depth);
276
277                 mon.worked(1);
278
279                 for (int idx = 0; idx < markers.length; idx++) {
280                     ConcreteMarker marker;
281                     try {
282                         marker = MarkerList.createMarker(markers[idx]);
283                     } catch (CoreException e) {
284                         if (ignoreExceptions) {
285                             continue;
286                         }
287                         throw e;
288
289                     }
290
291                     if (limit != -1 && resultList.size() >= limit) {
292                         return resultList;
293                     }
294
295                     if (selectMarker(marker)) {
296                         resultList.add(marker);
297                     }
298                 }
299             }
300         }
301
302         mon.done();
303
304         return resultList;
305     }
306
307     /**
308      * Subclasses should override to determine if the given marker passes the
309      * filter.
310      *
311      * @param marker
312      * @return <code>true</code> if the marker passes the filter and
313      * <code>false</code> otherwise
314      */

315     protected boolean selectMarker(ConcreteMarker marker) {
316         return true;
317     }
318
319     /**
320      * Searches the workspace for markers that pass this filter.
321      *
322      * @return Collection of markers.
323      */

324     Collection JavaDoc findMarkers(IProgressMonitor mon, boolean ignoreExceptions)
325             throws CoreException {
326
327         List JavaDoc unfiltered = Collections.EMPTY_LIST;
328
329         if (!isEnabled()) {
330             unfiltered = findMarkers(new IResource[] { ResourcesPlugin
331                     .getWorkspace().getRoot() }, IResource.DEPTH_INFINITE, -1,
332                     mon, ignoreExceptions);
333         } else {
334             // int limit = getFilterOnMarkerLimit() ? getMarkerLimit() + 1 : -1;
335
int limit = -1;
336
337             switch (getOnResource()) {
338             case ON_ANY: {
339                 unfiltered = findMarkers(new IResource[] { ResourcesPlugin
340                         .getWorkspace().getRoot() }, IResource.DEPTH_INFINITE,
341                         limit, mon, ignoreExceptions);
342                 break;
343             }
344             case ON_SELECTED_ONLY: {
345                 unfiltered = findMarkers(focusResource, IResource.DEPTH_ZERO,
346                         limit, mon, ignoreExceptions);
347                 break;
348             }
349             case ON_SELECTED_AND_CHILDREN: {
350                 unfiltered = findMarkers(focusResource,
351                         IResource.DEPTH_INFINITE, limit, mon, ignoreExceptions);
352                 break;
353             }
354             case ON_ANY_IN_SAME_CONTAINER: {
355                 unfiltered = findMarkers(getProjects(focusResource),
356                         IResource.DEPTH_INFINITE, limit, mon, ignoreExceptions);
357                 break;
358             }
359             case ON_WORKING_SET: {
360                 unfiltered = findMarkers(getResourcesInWorkingSet(),
361                         IResource.DEPTH_INFINITE, limit, mon, ignoreExceptions);
362             }
363             }
364         }
365
366         if (unfiltered == null) {
367             unfiltered = Collections.EMPTY_LIST;
368         }
369
370         return unfiltered;
371     }
372
373     /**
374      * Return the resources in the working set. If it is empty then return the
375      * workspace root.
376      *
377      * @return IResource[]
378      */

379     IResource[] getResourcesInWorkingSet() {
380         if (workingSet == null) {
381             return new IResource[0];
382         }
383
384         if (workingSet.isEmpty()) {
385             return new IResource[] { ResourcesPlugin.getWorkspace().getRoot() };
386         }
387
388         IAdaptable[] elements = workingSet.getElements();
389         List JavaDoc result = new ArrayList JavaDoc(elements.length);
390
391         for (int idx = 0; idx < elements.length; idx++) {
392             IResource next = (IResource) elements[idx]
393                     .getAdapter(IResource.class);
394
395             if (next != null) {
396                 result.add(next);
397             }
398         }
399
400         return (IResource[]) result.toArray(new IResource[result.size()]);
401     }
402
403     /**
404      * Returns a set of strings representing the full pathnames to every
405      * resource directly or indirectly contained in the working set. A resource
406      * is in the working set iff its path name can be found in this set.
407      *
408      * @return Set
409      */

410     private Set JavaDoc getWorkingSetAsSetOfPaths() {
411         if (cachedWorkingSet == null) {
412             HashSet JavaDoc result = new HashSet JavaDoc();
413
414             addResourcesAndChildren(result, getResourcesInWorkingSet());
415
416             cachedWorkingSet = result;
417         }
418
419         return cachedWorkingSet;
420     }
421
422     /***************************************************************************
423      * Adds the paths of all resources in the given array to the given set.
424      */

425     private void addResourcesAndChildren(HashSet JavaDoc result, IResource[] resources) {
426         for (int idx = 0; idx < resources.length; idx++) {
427
428             IResource currentResource = resources[idx];
429
430             result.add(currentResource.getFullPath().toString());
431
432             if (currentResource instanceof IContainer) {
433                 IContainer cont = (IContainer) currentResource;
434
435                 try {
436                     addResourcesAndChildren(result, cont.members());
437                 } catch (CoreException e) {
438                     // Ignore errors
439
}
440             }
441
442         }
443     }
444
445     /**
446      * Returns the set of projects that contain the given set of resources.
447      *
448      * @param resources
449      * @return IProject[]
450      */

451     static IProject[] getProjects(IResource[] resources) {
452         if (resources == null) {
453             return new IProject[0];
454         }
455
456         Collection JavaDoc projects = getProjectsAsCollection(resources);
457
458         return (IProject[]) projects.toArray(new IProject[projects.size()]);
459     }
460
461     /**
462      * Return the projects for the elements.
463      *
464      * @param elements
465      * collection of IResource or IResourceMapping
466      * @return Collection of IProject
467      */

468     static Collection JavaDoc getProjectsAsCollection(Object JavaDoc[] elements) {
469         HashSet JavaDoc projects = new HashSet JavaDoc();
470
471         for (int idx = 0; idx < elements.length; idx++) {
472             if (elements[idx] instanceof IResource) {
473                 projects.add(((IResource) elements[idx]).getProject());
474             } else {
475                 IProject[] mappingProjects = (((ResourceMapping) elements[idx])
476                         .getProjects());
477                 for (int i = 0; i < mappingProjects.length; i++) {
478                     projects.add(mappingProjects[i]);
479                 }
480             }
481
482         }
483
484         return projects;
485     }
486
487     /**
488      * Return whether or not the receiver would select the marker.
489      *
490      * @param marker
491      * @return boolean
492      */

493     public boolean select(ConcreteMarker marker) {
494         if (!isEnabled()) {
495             return true;
496         }
497
498         return selectByType(marker) && selectBySelection(marker)
499                 && selectMarker(marker);
500     }
501
502     private boolean selectByType(ConcreteMarker marker) {
503         return selectedTypes.contains(MarkerTypesModel.getInstance().getType(
504                 marker.getType()));
505     }
506
507     /**
508      * Returns whether the specified marker should be filter out or not.
509      *
510      * @param marker
511      * the marker to test
512      * @return true=the marker should not be filtered out false=the marker
513      * should be filtered out
514      */

515     private boolean selectBySelection(ConcreteMarker marker) {
516         if (onResource == ON_ANY || marker == null) {
517             return true;
518         }
519
520         if (focusResource == null) {
521             return true;
522         }
523
524         IResource resource = marker.getResource();
525
526         if (onResource == ON_WORKING_SET) {
527
528             if (resource != null) {
529                 return isEnclosed(resource);
530             }
531
532         } else if (onResource == ON_ANY_IN_SAME_CONTAINER) {
533             IProject project = resource.getProject();
534
535             if (project == null) {
536                 return false;
537             }
538
539             for (int i = 0; i < focusResource.length; i++) {
540                 IProject selectedProject = focusResource[i].getProject();
541
542                 if (selectedProject == null) {
543                     continue;
544                 }
545
546                 if (project.equals(selectedProject)) {
547                     return true;
548                 }
549             }
550         } else if (onResource == ON_SELECTED_ONLY) {
551             for (int i = 0; i < focusResource.length; i++) {
552                 if (resource.equals(focusResource[i])) {
553                     return true;
554                 }
555             }
556         } else if (onResource == ON_SELECTED_AND_CHILDREN) {
557             for (int i = 0; i < focusResource.length; i++) {
558                 IResource parentResource = resource;
559
560                 while (parentResource != null) {
561                     if (parentResource.equals(focusResource[i])) {
562                         return true;
563                     }
564
565                     parentResource = parentResource.getParent();
566                 }
567             }
568         }
569
570         return false;
571     }
572
573     /**
574      * Returns if the given resource is enclosed by a working set element.
575      * Previous versions of this method used IContainmentAdapter for containment
576      * tests. For performance reasons, this is no longer possible. Code that
577      * relies on this behavior should be updated appropriately.
578      *
579      * @param element
580      * resource to test for enclosure by a working set element
581      * @return true if element is enclosed by a working set element and false
582      * otherwise.
583      */

584     private boolean isEnclosed(IResource element) {
585         if (workingSet == null) {
586             return false;
587         }
588
589         if (workingSet.isEmpty()) {
590             return true; // Everything is in an empty working set
591
}
592         Set JavaDoc workingSetPaths = getWorkingSetAsSetOfPaths();
593
594         return workingSetPaths.contains(element.getFullPath().toString());
595     }
596
597     /**
598      * <ul>
599      * <li><code>MarkerFilter.ON_ANY</code> if showing items associated with
600      * any resource.</li>
601      * <li><code>MarkerFilter.ON_SELECTED_ONLY</code> if showing items
602      * associated with the selected resource within the workbench.</li>
603      * <li><code>MarkerFilter.ON_SELECTED_AND_CHILDREN</code> if showing
604      * items associated with the selected resource within the workbench and its
605      * children.</li>
606      * <li><code>MarkerFilter.ON_ANY_OF_SAME_PROJECT</code> if showing items
607      * in the same project as the selected resource within the workbench.</li>
608      * <li><code>MarkerFilter.ON_WORKING_SET</code> if showing items in some
609      * working set.</li>
610      * </ul>
611      *
612      * @return int
613      */

614     public int getOnResource() {
615         return onResource;
616     }
617
618     /**
619      * Sets the type of filtering by selection.
620      *
621      * @param onResource
622      * must be one of:
623      * <ul>
624      * <li><code>MarkerFilter.ON_ANY_RESOURCE</code></li>
625      * <li><code>MarkerFilter.ON_SELECTED_RESOURCE_ONLY</code></li>
626      * <li><code>MarkerFilter.ON_SELECTED_RESOURCE_AND_CHILDREN</code></li>
627      * <li><code>MarkerFilter.ON_ANY_RESOURCE_OF_SAME_PROJECT</code></li>
628      * <li><code>MarkerFilter.ON_WORKING_SET</code></li>
629      * </ul>
630      */

631     void setOnResource(int onResource) {
632         if (onResource >= ON_ANY && onResource <= ON_WORKING_SET) {
633             this.onResource = onResource;
634         }
635     }
636
637     /**
638      * @return the selected resource(s) withing the workbench.
639      */

640     IResource[] getFocusResource() {
641         return focusResource;
642     }
643
644     /**
645      * Sets the focused resources.
646      *
647      * @param resources
648      */

649     public void setFocusResource(IResource[] resources) {
650         focusResource = resources;
651     }
652
653     /**
654      * @return
655      * <ul>
656      * <li><code>true</code> if the filter is enabled.</li>
657      * <li><code>false</code> if the filter is not enabled.</li>
658      * </ul>
659      */

660     public boolean isEnabled() {
661         return enabled;
662     }
663
664     /**
665      * <b>Warning:</b> for internal package use only. Return the root marker
666      * types.
667      *
668      * @return the root marker types.
669      */

670     public List JavaDoc getRootTypes() {
671         return rootTypes;
672     }
673
674     /**
675      * <b>Warning:</b> for internal package use only. Return the selected
676      * types.
677      *
678      * @return the selected marker types to be displayed.
679      */

680     public List JavaDoc getSelectedTypes() {
681         return selectedTypes;
682     }
683
684     /**
685      * Find the typeModel entry that matches id.
686      *
687      * @param id
688      * the ID for a marker type
689      * @return MarkerType or <code>null</code> if it is not found.
690      */

691     public MarkerType getMarkerType(String JavaDoc id) {
692         return MarkerTypesModel.getInstance().getType(id);
693     }
694
695     /**
696      * @return the current working set or <code>null</code> if no working set
697      * is defined.
698      */

699     IWorkingSet getWorkingSet() {
700         return workingSet;
701     }
702
703     /**
704      * Sets the enablement state of the filter.
705      */

706     void setEnabled(boolean enabled) {
707         this.enabled = enabled;
708     }
709
710     /**
711      * Sets the current working set.
712      */

713     void setWorkingSet(IWorkingSet workingSet) {
714         this.workingSet = workingSet;
715         cachedWorkingSet = null;
716     }
717
718     /**
719      * Reset to the default state.
720      */

721     void resetState() {
722         enabled = DEFAULT_ACTIVATION_STATUS;
723         onResource = DEFAULT_ON_RESOURCE;
724         selectedTypes.clear();
725         addAllSubTypes(selectedTypes);
726         setWorkingSet(null);
727     }
728
729     /**
730      * Restore the state in the memento.
731      *
732      * @param memento
733      */

734     public final void restoreState(IMemento memento) {
735         resetState();
736         restoreFilterSettings(memento);
737
738     }
739
740     /**
741      * Restore the state of the receiver in the supplied settings. This is kept
742      * for backwards compatibility with 3.1 dialog settings.
743      *
744      * @param settings
745      */

746     public void restoreFilterSettings(IDialogSettings settings) {
747
748         resetState();
749
750         String JavaDoc setting = settings.get(TAG_ENABLED);
751
752         if (setting != null) {
753             enabled = Boolean.valueOf(setting).booleanValue();
754         }
755
756         setting = settings.get(TAG_ON_RESOURCE);
757
758         if (setting != null) {
759             try {
760                 onResource = Integer.parseInt(setting);
761             } catch (NumberFormatException JavaDoc eNumberFormat) {
762             }
763         }
764
765         // new selection list attribute
766
// format is "id:(true|false):"
767
setting = settings.get(TAG_SELECTION_STATUS);
768
769         if (setting != null) {
770             selectedTypes.clear();
771
772             // get the complete list of types
773
List JavaDoc newTypes = new ArrayList JavaDoc();
774             addAllSubTypes(newTypes);
775
776             StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(setting);
777
778             while (stringTokenizer.hasMoreTokens()) {
779                 String JavaDoc id = stringTokenizer.nextToken(TAG_TYPES_DELIMITER);
780                 String JavaDoc status = null;
781                 if (stringTokenizer.hasMoreTokens()) {
782                     status = stringTokenizer.nextToken(TAG_TYPES_DELIMITER);
783                 }
784
785                 MarkerType markerType = MarkerTypesModel.getInstance().getType(
786                         id);
787                 if (markerType != null) {
788                     newTypes.remove(markerType);
789
790                     // add the type to the selected list
791
if (!SELECTED_FALSE.equals(status)
792                             && !selectedTypes.contains(markerType)) {
793                         selectedTypes.add(markerType);
794                     }
795                 }
796             }
797
798             // any types we know about that weren't either true or
799
// false in the selection attribute are new. By default,
800
// new marker types will be selected=true
801
for (int i = 0; i < newTypes.size(); ++i) {
802                 selectedTypes.add(newTypes.get(i));
803             }
804         } else {
805             // the settings didn't contain the new selection attribute
806
// so check for the old selection attribute.
807
// format is just "id:"
808
setting = settings.get(TAG_SELECTED_TYPES);
809
810             if (setting != null) {
811                 generateSelectedTypes(setting);
812             }
813
814         }
815
816         setting = settings.get(TAG_WORKING_SET);
817
818         if (setting != null) {
819             setWorkingSet(PlatformUI.getWorkbench().getWorkingSetManager()
820                     .getWorkingSet(setting));
821         }
822     }
823
824     /**
825      * Set the selected types based on the value.
826      *
827      * @param selectedTypesValue
828      */

829     void generateSelectedTypes(String JavaDoc selectedTypesValue) {
830         selectedTypes.clear();
831         StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(
832                 selectedTypesValue);
833
834         while (stringTokenizer.hasMoreTokens()) {
835             MarkerType markerType = getMarkerType(stringTokenizer
836                     .nextToken(TAG_TYPES_DELIMITER));
837
838             if (markerType != null && !selectedTypes.contains(markerType)) {
839                 selectedTypes.add(markerType);
840             }
841         }
842     }
843
844     /**
845      * Find the markerType matching typeName
846      *
847      * @param typeName
848      * @return MarkerType
849      */

850     MarkerType findMarkerType(String JavaDoc typeName) {
851         return MarkerTypesModel.getInstance().getType(typeName);
852     }
853
854     /**
855      * Restore the state of the receiver in the supplied settings.
856      *
857      * @param memento
858      */

859     protected void restoreFilterSettings(IMemento memento) {
860         String JavaDoc setting = memento.getString(TAG_ENABLED);
861
862         if (setting != null) {
863             enabled = Boolean.valueOf(setting).booleanValue();
864         }
865
866         Integer JavaDoc resourceSetting = memento.getInteger(TAG_ON_RESOURCE);
867
868         if (resourceSetting != null) {
869             onResource = resourceSetting.intValue();
870         }
871
872         // new selection list attribute
873
// format is "id:(true|false):"
874
setting = memento.getString(TAG_SELECTION_STATUS);
875
876         if (setting != null) {
877             selectedTypes.clear();
878
879             // get the complete list of types
880
List JavaDoc newTypes = new ArrayList JavaDoc();
881             addAllSubTypes(newTypes);
882
883             StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(setting);
884
885             while (stringTokenizer.hasMoreTokens()) {
886                 String JavaDoc id = stringTokenizer.nextToken(TAG_TYPES_DELIMITER);
887                 String JavaDoc status = null;
888                 if (stringTokenizer.hasMoreTokens()) {
889                     status = stringTokenizer.nextToken(TAG_TYPES_DELIMITER);
890                 }
891
892                 MarkerType markerType = MarkerTypesModel.getInstance().getType(
893                         id);
894                 if (markerType != null) {
895                     newTypes.remove(markerType);
896
897                     // add the type to the selected list
898
if (!SELECTED_FALSE.equals(status)
899                             && !selectedTypes.contains(markerType)) {
900                         selectedTypes.add(markerType);
901                     }
902                 }
903             }
904
905             // any types we know about that weren't either true or
906
// false in the selection attribute are new. By default,
907
// new marker types will be selected=true
908
for (int i = 0; i < newTypes.size(); ++i) {
909                 selectedTypes.add(newTypes.get(i));
910             }
911         } else {
912             // the settings didn't contain the new selection attribute
913
// so check for the old selection attribute.
914
// format is just "id:"
915
setting = memento.getString(TAG_SELECTED_TYPES);
916
917             if (setting != null) {
918                 generateSelectedTypes(setting);
919             }
920
921         }
922
923         setting = memento.getString(TAG_WORKING_SET);
924
925         if (setting != null) {
926             setWorkingSet(PlatformUI.getWorkbench().getWorkingSetManager()
927                     .getWorkingSet(setting));
928         }
929     }
930
931     /**
932      * Save the filter settings for the receiver.
933      *
934      * @param settings
935      */

936     public void saveFilterSettings(IMemento settings) {
937
938         settings.putString(TAG_ENABLED, String.valueOf(enabled));
939         settings.putInteger(TAG_ON_RESOURCE, onResource);
940
941         String JavaDoc markerTypeIds = ""; //$NON-NLS-1$
942

943         List JavaDoc includedTypes = new ArrayList JavaDoc();
944         addAllSubTypes(includedTypes);
945         for (int i = 0; i < includedTypes.size(); i++) {
946             MarkerType markerType = (MarkerType) includedTypes.get(i);
947             markerTypeIds += markerType.getId() + TAG_TYPES_DELIMITER;
948             if (selectedTypes.contains(markerType)) {
949                 markerTypeIds += SELECTED_TRUE + TAG_TYPES_DELIMITER;
950             } else {
951                 markerTypeIds += SELECTED_FALSE + TAG_TYPES_DELIMITER;
952             }
953         }
954
955         settings.putString(TAG_SELECTION_STATUS, markerTypeIds);
956
957         if (workingSet != null) {
958             settings.putString(TAG_WORKING_SET, workingSet.getName());
959         }
960     }
961
962     /**
963      * Get the name of the receiver
964      *
965      * @return String
966      */

967     public String JavaDoc getName() {
968         return name;
969     }
970
971     /**
972      * Make a clone of the receiver.
973      *
974      * @return MarkerFilter
975      * @throws CloneNotSupportedException
976      */

977     public MarkerFilter makeClone() throws CloneNotSupportedException JavaDoc {
978         return (MarkerFilter) this.clone();
979     }
980
981     /**
982      * Set the selected types.
983      *
984      * @param selectedTypes
985      * List of MarkerType.
986      */

987     public void setSelectedTypes(List JavaDoc selectedTypes) {
988         this.selectedTypes = selectedTypes;
989     }
990
991 }
992
Popular Tags