1 11 package org.eclipse.ui.internal.ide; 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.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 33 public class ResourceWorkingSetUpdater implements IWorkingSetUpdater, 34 IResourceChangeListener { 35 36 39 private static class WorkingSetDelta { 40 private IWorkingSet fWorkingSet; 41 42 private List fElements; 43 44 private boolean fChanged; 45 46 52 public WorkingSetDelta(IWorkingSet workingSet) { 53 fWorkingSet = workingSet; 54 fElements = new ArrayList (Arrays.asList(workingSet.getElements())); 55 } 56 57 64 public int indexOf(Object element) { 65 return fElements.indexOf(element); 66 } 67 68 76 public void set(int index, Object element) { 77 fElements.set(index, element); 78 fChanged = true; 79 } 80 81 87 public void remove(int index) { 88 if (fElements.remove(index) != null) { 89 fChanged = true; 90 } 91 } 92 93 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 fWorkingSets; 106 107 110 public ResourceWorkingSetUpdater() { 111 fWorkingSets = new ArrayList (); 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 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 elements = new ArrayList (Arrays.asList(workingSet.getElements())); 205 boolean changed = false; 206 for (Iterator 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 |