1 11 package org.eclipse.jdt.internal.ui.workingsets; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IAdaptable; 22 23 import org.eclipse.core.resources.IFile; 24 import org.eclipse.core.resources.IProject; 25 import org.eclipse.core.resources.IResource; 26 import org.eclipse.core.resources.IResourceDelta; 27 import org.eclipse.core.resources.IResourceDeltaVisitor; 28 import org.eclipse.core.resources.ResourcesPlugin; 29 30 import org.eclipse.jface.util.Assert; 31 32 import org.eclipse.ui.IEditorInput; 33 import org.eclipse.ui.IEditorPart; 34 import org.eclipse.ui.IFileEditorInput; 35 import org.eclipse.ui.IWorkbench; 36 import org.eclipse.ui.IWorkbenchPage; 37 import org.eclipse.ui.IWorkbenchWindow; 38 import org.eclipse.ui.IWorkingSet; 39 import org.eclipse.ui.IWorkingSetUpdater; 40 import org.eclipse.ui.PlatformUI; 41 42 import org.eclipse.jdt.core.ElementChangedEvent; 43 import org.eclipse.jdt.core.IElementChangedListener; 44 import org.eclipse.jdt.core.IJavaElement; 45 import org.eclipse.jdt.core.IJavaElementDelta; 46 import org.eclipse.jdt.core.IJavaProject; 47 import org.eclipse.jdt.core.JavaCore; 48 49 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; 50 51 public class HistoryWorkingSetUpdater implements IWorkingSetUpdater { 52 53 public static final String ID= "org.eclipse.jdt.internal.ui.HistoryWorkingSet"; 55 private class Tracker extends EditorTracker { 56 public void editorOpened(IEditorPart part) { 57 IAdaptable file= getInput(part); 58 if (file == null) 59 return; 60 fOpenFiles.add(file); 61 } 62 public void editorClosed(IEditorPart part) { 63 IAdaptable file= getInput(part); 64 if (file == null) 65 return; 66 fOpenFiles.remove(file); 67 68 } 69 private IAdaptable getInput(IEditorPart part) { 70 IEditorInput input= part.getEditorInput(); 71 if (!(input instanceof IFileEditorInput)) 72 return null; 73 74 if (part instanceof CompilationUnitEditor) { 75 return JavaCore.create(((IFileEditorInput)input).getFile()); 76 } 77 return ((IFileEditorInput)input).getFile(); 78 } 79 } 80 81 private IWorkingSet fWorkingSet; 82 private Set fOpenFiles; 83 private Tracker fTracker; 84 private IElementChangedListener fJavaListener; 85 private int fMaxElements= 15; 86 87 public HistoryWorkingSetUpdater() { 88 fOpenFiles= new HashSet (); 89 initListeners(); 90 } 91 92 95 public void add(IWorkingSet workingSet) { 96 Assert.isTrue(fWorkingSet == null); 97 fWorkingSet= workingSet; 98 } 99 100 103 public boolean remove(IWorkingSet workingSet) { 104 Assert.isTrue(fWorkingSet == workingSet); 105 fWorkingSet= null; 106 return true; 107 } 108 109 112 public boolean contains(IWorkingSet workingSet) { 113 return fWorkingSet == workingSet; 114 } 115 116 public void dispose() { 117 if (fTracker != null) { 118 IWorkbench workbench= PlatformUI.getWorkbench(); 119 workbench.removeWindowListener(fTracker); 120 IWorkbenchWindow[] windows= workbench.getWorkbenchWindows(); 121 for (int i= 0; i < windows.length; i++) { 122 windows[i].removePageListener(fTracker); 123 IWorkbenchPage[] pages= windows[i].getPages(); 124 for (int j= 0; j < pages.length; j++) { 125 pages[j].removePartListener(fTracker); 126 } 127 } 128 fTracker= null; 129 } 130 if (fJavaListener != null) { 131 JavaCore.removeElementChangedListener(fJavaListener); 132 fJavaListener= null; 133 } 134 } 135 136 private void initListeners() { 137 fTracker= new Tracker(); 138 IWorkbench workbench= PlatformUI.getWorkbench(); 139 workbench.addWindowListener(fTracker); 140 IWorkbenchWindow[] windows= workbench.getWorkbenchWindows(); 141 for (int i= 0; i < windows.length; i++) { 142 windows[i].addPageListener(fTracker); 143 IWorkbenchPage[] pages= windows[i].getPages(); 144 for (int j= 0; j < pages.length; j++) { 145 pages[j].addPartListener(fTracker); 146 } 147 } 148 fJavaListener= new IElementChangedListener() { 149 public void elementChanged(ElementChangedEvent event) { 150 if (fWorkingSet == null) 151 return; 152 processDelta(event.getDelta()); 153 } 154 public void processDelta(IJavaElementDelta delta) { 155 IJavaElement element= delta.getElement(); 156 int type= element.getElementType(); 157 int kind= delta.getKind(); 158 int flags= delta.getFlags(); 159 if (type == IJavaElement.COMPILATION_UNIT) { 160 if (kind == IJavaElementDelta.CHANGED && (flags & IJavaElementDelta.F_PRIMARY_RESOURCE) != 0) { 161 elementSaved(element); 162 } else if (kind == IJavaElementDelta.REMOVED) { 163 if ((delta.getFlags() & IJavaElementDelta.F_MOVED_TO) != 0) { 164 elementMoved(element, delta.getMovedToElement()); 165 } else { 166 elementRemoved(element); 167 } 168 } 169 } else if (type == IJavaElement.JAVA_PROJECT && kind == IJavaElementDelta.CHANGED && (flags & IJavaElementDelta.F_CLOSED) != 0) { 170 projectClosed((IJavaProject)element); 171 } else { 172 IJavaElementDelta[] children= delta.getAffectedChildren(); 176 for (int i= 0; i < children.length; i++) { 177 processDelta(children[i]); 178 } 179 } 180 IResourceDelta[] resourceDeltas= delta.getResourceDeltas(); 181 if (resourceDeltas != null) { 182 for (int i= 0; i < resourceDeltas.length; i++) { 183 processDelta(resourceDeltas[i]); 184 } 185 } 186 } 187 private void processDelta(IResourceDelta outerDelta) { 188 try { 189 outerDelta.accept(new IResourceDeltaVisitor() { 190 public boolean visit(IResourceDelta delta) throws CoreException { 191 IResource resource= delta.getResource(); 192 if (resource.getType() != IResource.FILE) 193 return true; 194 IFile file= (IFile)resource; 195 if ((delta.getKind() & IResourceDelta.CHANGED) != 0) { 196 if ((delta.getFlags() & IResourceDelta.CONTENT) != 0) { 197 elementSaved(file); 198 } 199 } else if ((delta.getKind() & IResourceDelta.REMOVED) != 0) { 200 if ((delta.getFlags() & IResourceDelta.MOVED_TO) != 0) { 201 IFile newFile= ResourcesPlugin.getWorkspace().getRoot().getFile(delta.getMovedToPath()); 202 if (!newFile.exists()) { 203 elementRemoved(file); 204 } else { 205 elementMoved(file, newFile); 206 } 207 } else { 208 elementRemoved(file); 209 } 210 } 211 return false; 212 } 213 }); 214 } catch (CoreException e) { 215 } 216 } 217 }; 218 JavaCore.addElementChangedListener(fJavaListener, ElementChangedEvent.POST_CHANGE); 219 } 220 221 private void elementSaved(IAdaptable element) { 222 if (!fOpenFiles.contains(element)) 223 return; 224 updateHistory(element); 225 } 226 227 private void elementRemoved(IAdaptable element) { 228 fOpenFiles.remove(element); 229 List elements= getElements(); 230 if (elements.remove(element)) { 231 setElements(elements); 232 } 233 } 234 235 private void elementMoved(IAdaptable oldElement, IAdaptable newElement) { 236 List elements= getElements(); 237 int index= elements.indexOf(oldElement); 238 if (index == -1) 239 return; 240 elements.set(index, newElement); 241 fOpenFiles.remove(oldElement); 242 fOpenFiles.add(newElement); 243 setElements(elements); 244 } 245 246 private void projectClosed(IJavaProject jProject) { 247 IProject project= jProject.getProject(); 248 List elements= getElements(); 249 int removed= 0; 250 for (Iterator iter= elements.iterator(); iter.hasNext();) { 251 IAdaptable element= (IAdaptable)iter.next(); 252 IProject container= getProject(element); 253 if (project.equals(container)) { 254 iter.remove(); 255 removed++; 256 } 257 } 258 if (removed > 0) { 259 setElements(elements); 260 } 261 } 262 263 private IProject getProject(IAdaptable element) { 264 if (element instanceof IResource) { 265 return ((IResource)element).getProject(); 266 } else if (element instanceof IJavaElement) { 267 return ((IJavaElement)element).getJavaProject().getProject(); 268 } 269 return null; 270 } 271 272 private void updateHistory(IAdaptable element) { 273 List elements= getElements(); 274 int index= elements.indexOf(element); 275 if (index != -1) { 276 elements.remove(index); 277 elements.add(0, element); 278 } else { 279 if (elements.size() == fMaxElements) { 280 elements.remove(elements.size() - 1); 281 } 282 elements.add(0, element); 283 } 284 setElements(elements); 285 } 286 287 private List getElements() { 288 return new ArrayList (Arrays.asList(fWorkingSet.getElements())); 289 } 290 291 private void setElements(List elements) { 292 fWorkingSet.setElements((IAdaptable[])elements.toArray(new IAdaptable[elements.size()])); 293 } 294 } 295 | Popular Tags |