1 11 package org.eclipse.ui.model; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 17 import org.eclipse.core.resources.IContainer; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.IResourceChangeEvent; 20 import org.eclipse.core.resources.IResourceChangeListener; 21 import org.eclipse.core.resources.IResourceDelta; 22 import org.eclipse.core.resources.IWorkspace; 23 import org.eclipse.jface.viewers.AbstractTreeViewer; 24 import org.eclipse.jface.viewers.StructuredViewer; 25 import org.eclipse.jface.viewers.Viewer; 26 import org.eclipse.swt.widgets.Control; 27 28 37 public class WorkbenchContentProvider extends BaseWorkbenchContentProvider 38 implements IResourceChangeListener { 39 private Viewer viewer; 40 41 44 public WorkbenchContentProvider() { 45 super(); 46 } 47 48 51 public void dispose() { 52 if (viewer != null) { 53 IWorkspace workspace = null; 54 Object obj = viewer.getInput(); 55 if (obj instanceof IWorkspace) { 56 workspace = (IWorkspace) obj; 57 } else if (obj instanceof IContainer) { 58 workspace = ((IContainer) obj).getWorkspace(); 59 } 60 if (workspace != null) { 61 workspace.removeResourceChangeListener(this); 62 } 63 } 64 65 super.dispose(); 66 } 67 68 71 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 72 super.inputChanged(viewer, oldInput, newInput); 73 74 this.viewer = viewer; 75 IWorkspace oldWorkspace = null; 76 IWorkspace newWorkspace = null; 77 78 if (oldInput instanceof IWorkspace) { 79 oldWorkspace = (IWorkspace) oldInput; 80 } else if (oldInput instanceof IContainer) { 81 oldWorkspace = ((IContainer) oldInput).getWorkspace(); 82 } 83 84 if (newInput instanceof IWorkspace) { 85 newWorkspace = (IWorkspace) newInput; 86 } else if (newInput instanceof IContainer) { 87 newWorkspace = ((IContainer) newInput).getWorkspace(); 88 } 89 90 if (oldWorkspace != newWorkspace) { 91 if (oldWorkspace != null) { 92 oldWorkspace.removeResourceChangeListener(this); 93 } 94 if (newWorkspace != null) { 95 newWorkspace.addResourceChangeListener(this, 96 IResourceChangeEvent.POST_CHANGE); 97 } 98 } 99 } 100 101 104 public final void resourceChanged(final IResourceChangeEvent event) { 105 106 processDelta(event.getDelta()); 107 108 } 109 110 115 protected void processDelta(IResourceDelta delta) { 116 117 Control ctrl = viewer.getControl(); 118 if (ctrl == null || ctrl.isDisposed()) { 119 return; 120 } 121 122 123 final Collection runnables = new ArrayList (); 124 processDelta(delta, runnables); 125 126 if (runnables.isEmpty()) { 127 return; 128 } 129 130 if (ctrl.getDisplay().getThread() == Thread.currentThread()) { 132 runUpdates(runnables); 133 } else { 134 ctrl.getDisplay().asyncExec(new Runnable (){ 135 138 public void run() { 139 Control ctrl = viewer.getControl(); 141 if (ctrl == null || ctrl.isDisposed()) { 142 return; 143 } 144 145 runUpdates(runnables); 146 } 147 }); 148 } 149 150 } 151 152 156 private void runUpdates(Collection runnables) { 157 Iterator runnableIterator = runnables.iterator(); 158 while(runnableIterator.hasNext()){ 159 ((Runnable )runnableIterator.next()).run(); 160 } 161 162 } 163 164 167 private void processDelta(IResourceDelta delta, Collection runnables) { 168 Control ctrl = viewer.getControl(); 171 if (ctrl == null || ctrl.isDisposed()) { 172 return; 173 } 174 175 final IResource resource = delta.getResource(); 177 178 IResourceDelta[] affectedChildren = delta 185 .getAffectedChildren(IResourceDelta.CHANGED); 186 for (int i = 0; i < affectedChildren.length; i++) { 187 if ((affectedChildren[i].getFlags() & IResourceDelta.TYPE) != 0) { 188 runnables.add(getRefreshRunnable(resource)); 189 return; 190 } 191 } 192 193 int changeFlags = delta.getFlags(); 197 if ((changeFlags & IResourceDelta.OPEN) != 0) { 198 if (resource.isAccessible()) { 199 runnables.add(getUpdateRunnable(resource)); 200 } else { 201 runnables.add(getRefreshRunnable(resource)); 202 return; 203 } 204 } 205 if ((changeFlags & (IResourceDelta.SYNC 209 | IResourceDelta.TYPE | IResourceDelta.DESCRIPTION)) != 0) { 210 runnables.add(getUpdateRunnable(resource)); 211 } 212 if ((changeFlags & IResourceDelta.REPLACED) != 0) { 214 runnables.add(getRefreshRunnable(resource)); 215 return; 216 } 217 218 for (int i = 0; i < affectedChildren.length; i++) { 220 processDelta(affectedChildren[i], runnables); 221 } 222 223 237 IResourceDelta[] addedChildren = delta 238 .getAffectedChildren(IResourceDelta.ADDED); 239 IResourceDelta[] removedChildren = delta 240 .getAffectedChildren(IResourceDelta.REMOVED); 241 242 if (addedChildren.length == 0 && removedChildren.length == 0) { 243 return; 244 } 245 246 final Object [] addedObjects; 247 final Object [] removedObjects; 248 249 int numMovedFrom = 0; 253 int numMovedTo = 0; 254 if (addedChildren.length > 0) { 255 addedObjects = new Object [addedChildren.length]; 256 for (int i = 0; i < addedChildren.length; i++) { 257 addedObjects[i] = addedChildren[i].getResource(); 258 if ((addedChildren[i].getFlags() & IResourceDelta.MOVED_FROM) != 0) { 259 ++numMovedFrom; 260 } 261 } 262 } else { 263 addedObjects = new Object [0]; 264 } 265 266 if (removedChildren.length > 0) { 268 removedObjects = new Object [removedChildren.length]; 269 for (int i = 0; i < removedChildren.length; i++) { 270 removedObjects[i] = removedChildren[i].getResource(); 271 if ((removedChildren[i].getFlags() & IResourceDelta.MOVED_TO) != 0) { 272 ++numMovedTo; 273 } 274 } 275 } else { 276 removedObjects = new Object [0]; 277 } 278 final boolean hasRename = numMovedFrom > 0 && numMovedTo > 0; 280 281 Runnable addAndRemove = new Runnable (){ 282 public void run() { 283 if (viewer instanceof AbstractTreeViewer) { 284 AbstractTreeViewer treeViewer = (AbstractTreeViewer) viewer; 285 if (hasRename) { 291 treeViewer.getControl().setRedraw(false); 292 } 293 try { 294 if (addedObjects.length > 0) { 295 treeViewer.add(resource, addedObjects); 296 } 297 if (removedObjects.length > 0) { 298 treeViewer.remove(removedObjects); 299 } 300 } 301 finally { 302 if (hasRename) { 303 treeViewer.getControl().setRedraw(true); 304 } 305 } 306 } else { 307 ((StructuredViewer) viewer).refresh(resource); 308 } 309 } 310 }; 311 runnables.add(addAndRemove); 312 } 313 318 private Runnable getRefreshRunnable(final IResource resource) { 319 return new Runnable (){ 320 public void run() { 321 ((StructuredViewer) viewer).refresh(resource); 322 } 323 }; 324 } 325 326 331 private Runnable getUpdateRunnable(final IResource resource) { 332 return new Runnable (){ 333 public void run() { 334 ((StructuredViewer) viewer).update(resource, null); 335 } 336 }; 337 } 338 } 339 | Popular Tags |