1 11 package org.eclipse.debug.internal.ui.views; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.IAdaptable; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.core.runtime.jobs.Job; 21 import org.eclipse.debug.internal.ui.DebugUIPlugin; 22 import org.eclipse.jface.viewers.ITreeContentProvider; 23 import org.eclipse.ui.IWorkbenchPartSite; 24 import org.eclipse.ui.internal.progress.PendingUpdateAdapter; 25 import org.eclipse.ui.model.IWorkbenchAdapter; 26 import org.eclipse.ui.progress.DeferredTreeContentManager; 27 import org.eclipse.ui.progress.IDeferredWorkbenchAdapter; 28 import org.eclipse.ui.progress.IElementCollector; 29 import org.eclipse.ui.progress.IWorkbenchSiteProgressService; 30 import org.eclipse.ui.progress.WorkbenchJob; 31 import org.osgi.framework.Bundle; 32 40 public class RemoteTreeContentManager extends DeferredTreeContentManager { 41 42 private RemoteTreeViewer fViewer; 43 private IWorkbenchSiteProgressService progressService; 44 45 48 private Job fFetchJob = new FetchJob(); 49 50 54 private List fElementQueue = new ArrayList (); 55 private List fCollectors = new ArrayList (); 56 private List fAdapaters = new ArrayList (); 57 58 62 class FetchJob extends Job { 63 64 public FetchJob() { 65 super(DebugUIViewsMessages.RemoteTreeContentManager_0); 66 setSystem(true); 67 } 68 69 72 protected IStatus run(IProgressMonitor monitor) { 73 while (!fElementQueue.isEmpty() && !monitor.isCanceled()) { 74 Object element = null; 75 IElementCollector collector = null; 76 IDeferredWorkbenchAdapter adapter = null; 77 synchronized (fElementQueue) { 78 if (fElementQueue.isEmpty()) { 80 return Status.CANCEL_STATUS; 81 } 82 element = fElementQueue.remove(0); 83 collector = (IElementCollector) fCollectors.remove(0); 84 adapter = (IDeferredWorkbenchAdapter) fAdapaters.remove(0); 85 } 86 adapter.fetchDeferredChildren(element, collector, monitor); 87 } 88 if (monitor.isCanceled()) { 89 return Status.CANCEL_STATUS; 90 } 91 return Status.OK_STATUS; 92 } 93 94 } 95 96 99 public class Collector implements IElementCollector { 100 int offset = 0; 102 Object fParent; 103 104 public Collector(Object parent) { 105 fParent = parent; 106 } 107 111 public void add(Object element, IProgressMonitor monitor) { 112 add(new Object [] { element }, monitor); 113 } 114 115 119 public void add(Object [] elements, IProgressMonitor monitor) { 120 Object [] filtered = fViewer.filter(elements); 121 if (filtered.length > 0) { 122 replaceChildren(fParent, filtered, offset, monitor); 123 offset = offset + filtered.length; 124 } 125 } 126 127 132 public void done() { 133 prune(fParent, offset); 134 } 135 } 136 137 144 public RemoteTreeContentManager(ITreeContentProvider provider, RemoteTreeViewer viewer, IWorkbenchPartSite site) { 145 super(provider, viewer, site); 146 fViewer = viewer; 147 Object siteService = site.getAdapter(IWorkbenchSiteProgressService.class); 148 if (siteService != null) { 149 progressService = (IWorkbenchSiteProgressService) siteService; 150 } 151 } 152 153 162 protected IElementCollector createElementCollector(Object parent, PendingUpdateAdapter placeholder) { 163 return new Collector(parent); 164 } 165 166 176 public Object [] getChildren(final Object parent) { 177 IDeferredWorkbenchAdapter element = getAdapter(parent); 178 if (element == null) 179 return null; 180 Object [] currentChildren = fViewer.getCurrentChildren(parent); 181 PendingUpdateAdapter placeholder = null; 182 if (currentChildren == null || currentChildren.length == 0) { 183 placeholder = new PendingUpdateAdapter(); 184 } 185 startFetchingDeferredChildren(parent, element, placeholder); 186 if (placeholder == null) { 187 return currentChildren; 188 } 189 return new Object [] { placeholder }; 190 } 191 192 200 protected void replaceChildren(final Object parent, final Object [] children, final int offset, IProgressMonitor monitor) { 201 if (monitor.isCanceled()) { 202 return; 203 } 204 WorkbenchJob updateJob = new WorkbenchJob(DebugUIViewsMessages.IncrementalDeferredTreeContentManager_0) { 210 public IStatus runInUIThread(IProgressMonitor updateMonitor) { 211 if (fViewer.getControl().isDisposed()) 213 return Status.CANCEL_STATUS; 214 fViewer.replace(parent, children, offset); 215 return Status.OK_STATUS; 216 } 217 }; 218 updateJob.setSystem(true); 219 updateJob.setPriority(Job.INTERACTIVE); 220 updateJob.schedule(); 221 } 222 223 232 protected void prune(final Object parent, final int offset) { 233 WorkbenchJob updateJob = new WorkbenchJob(DebugUIViewsMessages.IncrementalDeferredTreeContentManager_1) { 239 public IStatus runInUIThread(IProgressMonitor updateMonitor) { 240 if (fViewer.getControl().isDisposed()) 242 return Status.CANCEL_STATUS; 243 fViewer.prune(parent, offset); 244 return Status.OK_STATUS; 245 } 246 }; 247 updateJob.setSystem(true); 248 updateJob.setPriority(Job.INTERACTIVE); 249 updateJob.schedule(); 250 } 251 252 255 protected void runClearPlaceholderJob(PendingUpdateAdapter placeholder) { 256 if (placeholder != null) { 258 super.runClearPlaceholderJob(placeholder); 259 } 260 } 261 262 265 protected String getFetchJobName(Object parent, IDeferredWorkbenchAdapter adapter) { 266 return DebugUIViewsMessages.RemoteTreeContentManager_0; } 268 269 270 279 protected IDeferredWorkbenchAdapter getAdapter(Object element) { 280 if (element instanceof IDeferredWorkbenchAdapter) 281 return (IDeferredWorkbenchAdapter) element; 282 if (!(element instanceof IAdaptable)) 283 return null; 284 IAdaptable adaptable = (IAdaptable) element; 285 IDeferredWorkbenchAdapter deferred = (IDeferredWorkbenchAdapter) adaptable.getAdapter(IDeferredWorkbenchAdapter.class); 286 if (deferred == null) 287 return null; 288 289 DebugUIPlugin plugin = DebugUIPlugin.getDefault(); 290 Bundle bundle = plugin.getBundle(deferred.getClass()); 291 Bundle debugBundle = plugin.getBundle(); 292 if (!debugBundle.equals(bundle)) { 293 return deferred; 295 } 296 IWorkbenchAdapter nonDeferred = (IWorkbenchAdapter) adaptable.getAdapter(IWorkbenchAdapter.class); 298 if (nonDeferred != null) { 299 bundle = plugin.getBundle(nonDeferred.getClass()); 300 if (!debugBundle.equals(bundle)) { 301 return null; 304 } 305 } 306 return deferred; 307 } 308 309 protected void startFetchingDeferredChildren(final Object parent, final IDeferredWorkbenchAdapter adapter, PendingUpdateAdapter placeholder) { 310 final IElementCollector collector = createElementCollector(parent, placeholder); 311 synchronized (fElementQueue) { 312 if (!fElementQueue.contains(parent)) { 313 fElementQueue.add(parent); 314 fCollectors.add(collector); 315 fAdapaters.add(adapter); 316 } 317 } 318 if (progressService == null) 319 fFetchJob.schedule(); 320 else 321 progressService.schedule(fFetchJob); 322 } 323 324 327 public void cancel() { 328 synchronized (fElementQueue) { 329 fFetchJob.cancel(); 330 fElementQueue.clear(); 331 fAdapaters.clear(); 332 fCollectors.clear(); 333 } 334 } 335 } 336 | Popular Tags |