1 11 package org.eclipse.jdt.internal.ui.workingsets; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.IAdaptable; 19 20 import org.eclipse.core.resources.IProject; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.resources.IResourceDelta; 23 import org.eclipse.core.resources.ResourcesPlugin; 24 25 import org.eclipse.ui.IWorkingSet; 26 import org.eclipse.ui.IWorkingSetUpdater; 27 28 import org.eclipse.jdt.core.ElementChangedEvent; 29 import org.eclipse.jdt.core.IElementChangedListener; 30 import org.eclipse.jdt.core.IJavaElement; 31 import org.eclipse.jdt.core.IJavaElementDelta; 32 import org.eclipse.jdt.core.IJavaProject; 33 import org.eclipse.jdt.core.JavaCore; 34 35 36 public class JavaWorkingSetUpdater implements IWorkingSetUpdater, IElementChangedListener { 37 38 public static final String ID= "org.eclipse.jdt.ui.JavaWorkingSetPage"; 40 private List fWorkingSets; 41 42 private static class WorkingSetDelta { 43 private IWorkingSet fWorkingSet; 44 private List fElements; 45 private boolean fChanged; 46 public WorkingSetDelta(IWorkingSet workingSet) { 47 fWorkingSet= workingSet; 48 fElements= new ArrayList (Arrays.asList(workingSet.getElements())); 49 } 50 public int indexOf(Object element) { 51 return fElements.indexOf(element); 52 } 53 public void set(int index, Object element) { 54 fElements.set(index, element); 55 fChanged= true; 56 } 57 public void remove(int index) { 58 if (fElements.remove(index) != null) { 59 fChanged= true; 60 } 61 } 62 public void process() { 63 if (fChanged) { 64 fWorkingSet.setElements((IAdaptable[])fElements.toArray(new IAdaptable[fElements.size()])); 65 } 66 } 67 } 68 69 public JavaWorkingSetUpdater() { 70 fWorkingSets= new ArrayList (); 71 JavaCore.addElementChangedListener(this); 72 } 73 74 77 public void add(IWorkingSet workingSet) { 78 checkElementExistence(workingSet); 79 synchronized (fWorkingSets) { 80 fWorkingSets.add(workingSet); 81 } 82 } 83 84 87 public boolean remove(IWorkingSet workingSet) { 88 boolean result; 89 synchronized(fWorkingSets) { 90 result= fWorkingSets.remove(workingSet); 91 } 92 return result; 93 } 94 95 98 public boolean contains(IWorkingSet workingSet) { 99 synchronized(fWorkingSets) { 100 return fWorkingSets.contains(workingSet); 101 } 102 } 103 104 107 public void dispose() { 108 synchronized(fWorkingSets) { 109 fWorkingSets.clear(); 110 } 111 JavaCore.removeElementChangedListener(this); 112 } 113 114 117 public void elementChanged(ElementChangedEvent event) { 118 IWorkingSet[] workingSets; 119 synchronized(fWorkingSets) { 120 workingSets= (IWorkingSet[])fWorkingSets.toArray(new IWorkingSet[fWorkingSets.size()]); 121 } 122 for (int w= 0; w < workingSets.length; w++) { 123 WorkingSetDelta workingSetDelta= new WorkingSetDelta(workingSets[w]); 124 processJavaDelta(workingSetDelta, event.getDelta()); 125 IResourceDelta[] resourceDeltas= event.getDelta().getResourceDeltas(); 126 if (resourceDeltas != null) { 127 for (int r= 0; r < resourceDeltas.length; r++) { 128 processResourceDelta(workingSetDelta, resourceDeltas[r]); 129 } 130 } 131 workingSetDelta.process(); 132 } 133 } 134 135 private void processJavaDelta(WorkingSetDelta result, IJavaElementDelta delta) { 136 IJavaElement jElement= delta.getElement(); 137 int index= result.indexOf(jElement); 138 int type= jElement.getElementType(); 139 int kind= delta.getKind(); 140 int flags= delta.getFlags(); 141 if (type == IJavaElement.JAVA_PROJECT && kind == IJavaElementDelta.CHANGED) { 142 if (index != -1 && (flags & IJavaElementDelta.F_CLOSED) != 0) { 143 result.set(index, ((IJavaProject)jElement).getProject()); 144 } else if ((flags & IJavaElementDelta.F_OPENED) != 0) { 145 index= result.indexOf(((IJavaProject)jElement).getProject()); 146 if (index != -1) 147 result.set(index, jElement); 148 } 149 } 150 if (index != -1) { 151 if (kind == IJavaElementDelta.REMOVED) { 152 if ((flags & IJavaElementDelta.F_MOVED_TO) != 0) { 153 result.set(index, delta.getMovedToElement()); 154 } else { 155 result.remove(index); 156 } 157 } 158 } 159 IResourceDelta[] resourceDeltas= delta.getResourceDeltas(); 160 if (resourceDeltas != null) { 161 for (int i= 0; i < resourceDeltas.length; i++) { 162 processResourceDelta(result, resourceDeltas[i]); 163 } 164 } 165 IJavaElementDelta[] children= delta.getAffectedChildren(); 166 for (int i= 0; i < children.length; i++) { 167 processJavaDelta(result, children[i]); 168 } 169 } 170 171 private void processResourceDelta(WorkingSetDelta result, IResourceDelta delta) { 172 IResource resource= delta.getResource(); 173 int type= resource.getType(); 174 int index= result.indexOf(resource); 175 int kind= delta.getKind(); 176 int flags= delta.getFlags(); 177 if (kind == IResourceDelta.CHANGED && type == IResource.PROJECT && index != -1) { 178 if ((flags & IResourceDelta.OPEN) != 0) { 179 result.set(index, resource); 180 } 181 } 182 if (index != -1 && kind == IResourceDelta.REMOVED) { 183 if ((flags & IResourceDelta.MOVED_TO) != 0) { 184 result.set(index, 185 ResourcesPlugin.getWorkspace().getRoot().findMember(delta.getMovedToPath())); 186 } else { 187 result.remove(index); 188 } 189 } 190 191 if (projectGotClosedOrOpened(resource, kind, flags)) 193 return; 194 195 IResourceDelta[] children= delta.getAffectedChildren(); 196 for (int i= 0; i < children.length; i++) { 197 processResourceDelta(result, children[i]); 198 } 199 } 200 201 private boolean projectGotClosedOrOpened(IResource resource, int kind, int flags) { 202 return resource.getType() == IResource.PROJECT 203 && kind == IResourceDelta.CHANGED 204 && (flags & IResourceDelta.OPEN) != 0; 205 } 206 207 private void checkElementExistence(IWorkingSet workingSet) { 208 List elements= new ArrayList (Arrays.asList(workingSet.getElements())); 209 boolean changed= false; 210 for (Iterator iter= elements.iterator(); iter.hasNext();) { 211 IAdaptable element= (IAdaptable)iter.next(); 212 boolean remove= false; 213 if (element instanceof IJavaElement) { 214 IJavaElement jElement= (IJavaElement)element; 215 if (jElement instanceof IJavaProject) { 221 remove= !jElement.exists(); 222 } else { 223 IProject project= jElement.getJavaProject().getProject(); 224 remove= project.isOpen() && !jElement.exists(); 225 } 226 } else if (element instanceof IResource) { 227 IResource resource= (IResource)element; 228 if (resource instanceof IProject) { 230 remove= !resource.exists(); 231 } else { 232 IProject project= resource.getProject(); 233 remove= (project != null ? project.isOpen() : true) && !resource.exists(); 234 } 235 } 236 if (remove) { 237 iter.remove(); 238 changed= true; 239 } 240 } 241 if (changed) { 242 workingSet.setElements((IAdaptable[])elements.toArray(new IAdaptable[elements.size()])); 243 } 244 } 245 } 246 | Popular Tags |