KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > ResourceWorkingSetFilter


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 package org.eclipse.ui;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.jface.viewers.ViewerFilter;
18
19 /**
20  * A resource working set filter filters resources from a view that
21  * are neither a parent nor children of a working set element.
22  *
23  * @since 2.0
24  */

25 public class ResourceWorkingSetFilter extends ViewerFilter {
26     private IWorkingSet workingSet = null;
27
28     private IAdaptable[] cachedWorkingSet = null;
29
30     /**
31      * Returns the active working set the filter is working with.
32      *
33      * @return the active working set
34      */

35     public IWorkingSet getWorkingSet() {
36         return workingSet;
37     }
38
39     /**
40      * Sets the active working set.
41      *
42      * @param workingSet the working set the filter should work with
43      */

44     public void setWorkingSet(IWorkingSet workingSet) {
45         this.workingSet = workingSet;
46     }
47
48     /**
49      * Determines if an element should be filtered out.
50      *
51      * @see ViewerFilter#select(Viewer, Object, Object)
52      */

53     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
54         IResource resource = null;
55
56         if (workingSet == null) {
57             return true;
58         }
59         if (element instanceof IResource) {
60             resource = (IResource) element;
61         } else if (element instanceof IAdaptable) {
62             IAdaptable adaptable = (IAdaptable) element;
63             resource = (IResource) adaptable.getAdapter(IResource.class);
64         }
65         if (resource != null) {
66             return isEnclosed(resource);
67         }
68         return true;
69     }
70
71     /**
72      * Returns if the given resource is enclosed by a working set element.
73      * The IContainmentAdapter of each working set element is used for the
74      * containment test. If there is no IContainmentAdapter for a working
75      * set element, a simple resource based test is used.
76      *
77      * @param element resource to test for enclosure by a working set
78      * element
79      * @return true if element is enclosed by a working set element and
80      * false otherwise.
81      */

82     private boolean isEnclosed(IResource element) {
83         IPath elementPath = element.getFullPath();
84         IAdaptable[] workingSetElements = cachedWorkingSet;
85
86         // working set elements won't be cached if select is called
87
// directly, outside filter. fixes bug 14500.
88
if (workingSetElements == null) {
89             workingSetElements = workingSet.getElements();
90         }
91
92         for (int i = 0; i < workingSetElements.length; i++) {
93             IAdaptable workingSetElement = workingSetElements[i];
94             IContainmentAdapter containmentAdapter = (IContainmentAdapter) workingSetElement
95                     .getAdapter(IContainmentAdapter.class);
96
97             // if there is no IContainmentAdapter defined for the working
98
// set element type fall back to using resource based
99
// containment check
100
if (containmentAdapter != null) {
101                 if (containmentAdapter.contains(workingSetElement, element,
102                         IContainmentAdapter.CHECK_CONTEXT
103                                 | IContainmentAdapter.CHECK_IF_CHILD
104                                 | IContainmentAdapter.CHECK_IF_ANCESTOR
105                                 | IContainmentAdapter.CHECK_IF_DESCENDANT)) {
106                     return true;
107                 }
108             } else if (isEnclosedResource(element, elementPath,
109                     workingSetElement)) {
110                 return true;
111             }
112         }
113         return false;
114     }
115
116     /**
117      * Returns if the given resource is enclosed by a working set element.
118      * A resource is enclosed if it is either a parent of a working set
119      * element, a child of a working set element or a working set element
120      * itself.
121      * Simple path comparison is used. This is only guaranteed to return
122      * correct results for resource working set elements.
123      *
124      * @param element resource to test for enclosure by a working set
125      * element
126      * @param elementPath full, absolute path of the element to test
127      * @return true if element is enclosed by a working set element and
128      * false otherwise.
129      */

130     private boolean isEnclosedResource(IResource element, IPath elementPath,
131             IAdaptable workingSetElement) {
132         IResource workingSetResource = null;
133
134         if (workingSetElement.equals(element)) {
135             return true;
136         }
137         if (workingSetElement instanceof IResource) {
138             workingSetResource = (IResource) workingSetElement;
139         } else {
140             workingSetResource = (IResource) workingSetElement
141                     .getAdapter(IResource.class);
142         }
143         if (workingSetResource != null) {
144             IPath resourcePath = workingSetResource.getFullPath();
145             if (resourcePath.isPrefixOf(elementPath)) {
146                 return true;
147             }
148             if (elementPath.isPrefixOf(resourcePath)) {
149                 return true;
150             }
151         }
152         return false;
153     }
154
155     /**
156      * Filters out elements that are neither a parent nor a child of
157      * a working set element.
158      *
159      * @see ViewerFilter#filter(Viewer, Object, Object[])
160      */

161     public Object JavaDoc[] filter(Viewer viewer, Object JavaDoc parent, Object JavaDoc[] elements) {
162         Object JavaDoc[] result = null;
163         if (workingSet != null) {
164             cachedWorkingSet = workingSet.getElements();
165         }
166         try {
167             result = super.filter(viewer, parent, elements);
168         } finally {
169             cachedWorkingSet = null;
170         }
171         return result;
172     }
173 }
174
Popular Tags