1 11 package org.eclipse.ui.internal.navigator.resources.workbench; 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.IResourceDelta; 20 import org.eclipse.jface.viewers.AbstractTreeViewer; 21 import org.eclipse.jface.viewers.StructuredViewer; 22 import org.eclipse.jface.viewers.Viewer; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.ui.model.WorkbenchContentProvider; 25 26 35 public class ResourceExtensionContentProvider extends WorkbenchContentProvider { 36 37 private static final Object [] NO_CHILDREN = new Object [0]; 38 private Viewer viewer; 39 40 43 public ResourceExtensionContentProvider() { 44 super(); 45 } 46 47 52 public Object [] getElements(Object element) { 53 return super.getChildren(element); 54 } 55 56 61 public Object [] getChildren(Object element) { 62 if(element instanceof IResource) 63 return super.getChildren(element); 64 return NO_CHILDREN; 65 } 66 67 70 public boolean hasChildren(Object element) { 71 return element instanceof IContainer && super.hasChildren(element); 72 } 73 74 77 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 78 super.inputChanged(viewer, oldInput, newInput); 79 this.viewer = viewer; 80 } 81 82 83 88 protected void processDelta(IResourceDelta delta) { 89 90 Control ctrl = viewer.getControl(); 91 if (ctrl == null || ctrl.isDisposed()) { 92 return; 93 } 94 95 96 final Collection runnables = new ArrayList (); 97 processDelta(delta, runnables); 98 99 if (runnables.isEmpty()) { 100 return; 101 } 102 103 if (ctrl.getDisplay().getThread() == Thread.currentThread()) { 105 runUpdates(runnables); 106 } else { 107 ctrl.getDisplay().asyncExec(new Runnable (){ 108 111 public void run() { 112 Control ctrl = viewer.getControl(); 114 if (ctrl == null || ctrl.isDisposed()) { 115 return; 116 } 117 118 runUpdates(runnables); 119 } 120 }); 121 } 122 123 } 124 125 128 private void processDelta(IResourceDelta delta, Collection runnables) { 129 Control ctrl = viewer.getControl(); 132 if (ctrl == null || ctrl.isDisposed()) { 133 return; 134 } 135 136 final IResource resource = delta.getResource(); 138 139 IResourceDelta[] affectedChildren = delta 146 .getAffectedChildren(IResourceDelta.CHANGED); 147 for (int i = 0; i < affectedChildren.length; i++) { 148 if ((affectedChildren[i].getFlags() & IResourceDelta.TYPE) != 0) { 149 runnables.add(getRefreshRunnable(resource)); 150 return; 151 } 152 } 153 154 int changeFlags = delta.getFlags(); 158 if ((changeFlags & (IResourceDelta.OPEN | IResourceDelta.SYNC 159 | IResourceDelta.TYPE | IResourceDelta.DESCRIPTION)) != 0) { 160 167 170 runnables.add(getRefreshRunnable(resource.getParent())); 171 } 172 if ((changeFlags & IResourceDelta.REPLACED) != 0) { 174 runnables.add(getRefreshRunnable(resource)); 175 return; 176 } 177 178 179 for (int i = 0; i < affectedChildren.length; i++) { 181 processDelta(affectedChildren[i], runnables); 182 } 183 184 198 IResourceDelta[] addedChildren = delta 199 .getAffectedChildren(IResourceDelta.ADDED); 200 IResourceDelta[] removedChildren = delta 201 .getAffectedChildren(IResourceDelta.REMOVED); 202 203 if (addedChildren.length == 0 && removedChildren.length == 0) { 204 return; 205 } 206 207 final Object [] addedObjects; 208 final Object [] removedObjects; 209 210 int numMovedFrom = 0; 214 int numMovedTo = 0; 215 if (addedChildren.length > 0) { 216 addedObjects = new Object [addedChildren.length]; 217 for (int i = 0; i < addedChildren.length; i++) { 218 addedObjects[i] = addedChildren[i].getResource(); 219 if ((addedChildren[i].getFlags() & IResourceDelta.MOVED_FROM) != 0) { 220 ++numMovedFrom; 221 } 222 } 223 } else { 224 addedObjects = new Object [0]; 225 } 226 227 if (removedChildren.length > 0) { 229 removedObjects = new Object [removedChildren.length]; 230 for (int i = 0; i < removedChildren.length; i++) { 231 removedObjects[i] = removedChildren[i].getResource(); 232 if ((removedChildren[i].getFlags() & IResourceDelta.MOVED_TO) != 0) { 233 ++numMovedTo; 234 } 235 } 236 } else { 237 removedObjects = new Object [0]; 238 } 239 final boolean hasRename = numMovedFrom > 0 && numMovedTo > 0; 241 242 Runnable addAndRemove = new Runnable (){ 243 public void run() { 244 if (viewer instanceof AbstractTreeViewer) { 245 AbstractTreeViewer treeViewer = (AbstractTreeViewer) viewer; 246 if (hasRename) { 252 treeViewer.getControl().setRedraw(false); 253 } 254 try { 255 if (addedObjects.length > 0) { 256 treeViewer.add(resource, addedObjects); 257 } 258 if (removedObjects.length > 0) { 259 treeViewer.remove(removedObjects); 260 } 261 } 262 finally { 263 if (hasRename) { 264 treeViewer.getControl().setRedraw(true); 265 } 266 } 267 } else { 268 ((StructuredViewer) viewer).refresh(resource); 269 } 270 } 271 }; 272 runnables.add(addAndRemove); 273 } 274 275 280 private Runnable getRefreshRunnable(final IResource resource) { 281 return new Runnable (){ 282 public void run() { 283 ((StructuredViewer) viewer).refresh(resource); 284 } 285 }; 286 } 287 288 292 private void runUpdates(Collection runnables) { 293 Iterator runnableIterator = runnables.iterator(); 294 while(runnableIterator.hasNext()){ 295 ((Runnable )runnableIterator.next()).run(); 296 } 297 298 } 299 300 } 301 | Popular Tags |