KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > ResourceWorkingSetUpdater


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.internal.ide;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.IResourceChangeEvent;
21 import org.eclipse.core.resources.IResourceChangeListener;
22 import org.eclipse.core.resources.IResourceDelta;
23 import org.eclipse.core.resources.ResourcesPlugin;
24 import org.eclipse.core.runtime.IAdaptable;
25 import org.eclipse.ui.IWorkingSet;
26 import org.eclipse.ui.IWorkingSetUpdater;
27
28 /**
29  * A working set updater that updates resource working sets on resource deltas
30  *
31  * @since 3.2
32  */

33 public class ResourceWorkingSetUpdater implements IWorkingSetUpdater,
34         IResourceChangeListener {
35
36     /**
37      * Utility class used to help process incoming resource deltas.
38      */

39     private static class WorkingSetDelta {
40         private IWorkingSet fWorkingSet;
41
42         private List JavaDoc fElements;
43
44         private boolean fChanged;
45
46         /**
47          * Create a new instance of this class.
48          *
49          * @param workingSet
50          * the working set to track.
51          */

52         public WorkingSetDelta(IWorkingSet workingSet) {
53             fWorkingSet = workingSet;
54             fElements = new ArrayList JavaDoc(Arrays.asList(workingSet.getElements()));
55         }
56
57         /**
58          * Returns the index of this element in the list of known elements.
59          *
60          * @param element
61          * the element to search for
62          * @return the index, or -1 if unknown.
63          */

64         public int indexOf(Object JavaDoc element) {
65             return fElements.indexOf(element);
66         }
67
68         /**
69          * Add a new element to the list of known elements.
70          *
71          * @param index
72          * the index at which to place the element
73          * @param element
74          * the element to set
75          */

76         public void set(int index, Object JavaDoc element) {
77             fElements.set(index, element);
78             fChanged = true;
79         }
80
81         /**
82          * Remove an element from the list of known elements.
83          *
84          * @param index
85          * the index of the element to remove
86          */

87         public void remove(int index) {
88             if (fElements.remove(index) != null) {
89                 fChanged = true;
90             }
91         }
92
93         /**
94          * Process the changes to this delta and update the working set if
95          * necessary.
96          */

97         public void process() {
98             if (fChanged) {
99                 fWorkingSet.setElements((IAdaptable[]) fElements
100                         .toArray(new IAdaptable[fElements.size()]));
101             }
102         }
103     }
104
105     private List JavaDoc fWorkingSets;
106
107     /**
108      * Create a new instance of this updater.
109      */

110     public ResourceWorkingSetUpdater() {
111         fWorkingSets = new ArrayList JavaDoc();
112         ResourcesPlugin.getWorkspace().addResourceChangeListener(this,
113                 IResourceChangeEvent.POST_CHANGE);
114     }
115
116     public void add(IWorkingSet workingSet) {
117         checkElementExistence(workingSet);
118         synchronized (fWorkingSets) {
119             fWorkingSets.add(workingSet);
120         }
121     }
122
123     public boolean remove(IWorkingSet workingSet) {
124         boolean result;
125         synchronized (fWorkingSets) {
126             result = fWorkingSets.remove(workingSet);
127         }
128
129         return result;
130     }
131
132     public boolean contains(IWorkingSet workingSet) {
133         synchronized (fWorkingSets) {
134             return fWorkingSets.contains(workingSet);
135         }
136     }
137
138     public void dispose() {
139         synchronized (fWorkingSets) {
140             fWorkingSets.clear();
141         }
142         ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
143     }
144
145     public void resourceChanged(IResourceChangeEvent event) {
146         IResourceDelta delta = event.getDelta();
147         if (delta == null) {
148             return;
149         }
150         IWorkingSet[] workingSets;
151         synchronized (fWorkingSets) {
152             workingSets = (IWorkingSet[]) fWorkingSets
153                     .toArray(new IWorkingSet[fWorkingSets.size()]);
154         }
155         for (int w = 0; w < workingSets.length; w++) {
156             WorkingSetDelta workingSetDelta = new WorkingSetDelta(
157                     workingSets[w]);
158             processResourceDelta(workingSetDelta, delta);
159             workingSetDelta.process();
160         }
161     }
162
163     private void processResourceDelta(WorkingSetDelta result,
164             IResourceDelta delta) {
165         IResource resource = delta.getResource();
166         int type = resource.getType();
167         int index = result.indexOf(resource);
168         int kind = delta.getKind();
169         int flags = delta.getFlags();
170         if (kind == IResourceDelta.CHANGED && type == IResource.PROJECT
171                 && index != -1) {
172             if ((flags & IResourceDelta.OPEN) != 0) {
173                 result.set(index, resource);
174             }
175         }
176         if (index != -1 && kind == IResourceDelta.REMOVED) {
177             if ((flags & IResourceDelta.MOVED_TO) != 0) {
178                 result.set(index, ResourcesPlugin.getWorkspace().getRoot()
179                         .findMember(delta.getMovedToPath()));
180             } else {
181                 result.remove(index);
182             }
183         }
184
185         // Don't dive into closed or opened projects
186
if (projectGotClosedOrOpened(resource, kind, flags)) {
187             return;
188         }
189
190         IResourceDelta[] children = delta.getAffectedChildren();
191         for (int i = 0; i < children.length; i++) {
192             processResourceDelta(result, children[i]);
193         }
194     }
195
196     private boolean projectGotClosedOrOpened(IResource resource, int kind,
197             int flags) {
198         return resource.getType() == IResource.PROJECT
199                 && kind == IResourceDelta.CHANGED
200                 && (flags & IResourceDelta.OPEN) != 0;
201     }
202
203     private void checkElementExistence(IWorkingSet workingSet) {
204         List JavaDoc elements = new ArrayList JavaDoc(Arrays.asList(workingSet.getElements()));
205         boolean changed = false;
206         for (Iterator JavaDoc iter = elements.iterator(); iter.hasNext();) {
207             IAdaptable element = (IAdaptable) iter.next();
208             boolean remove = false;
209             if (element instanceof IResource) {
210                 IResource resource = (IResource) element;
211                 IProject project = resource.getProject();
212                 remove = (project != null ? project.isOpen() : true)
213                         && !resource.exists();
214             }
215             if (remove) {
216                 iter.remove();
217                 changed = true;
218             }
219         }
220         if (changed) {
221             workingSet.setElements((IAdaptable[]) elements
222                     .toArray(new IAdaptable[elements.size()]));
223         }
224     }
225 }
226
Popular Tags