1 11 package org.eclipse.ui.progress; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.Platform; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.core.runtime.jobs.IJobChangeEvent; 19 import org.eclipse.core.runtime.jobs.Job; 20 import org.eclipse.core.runtime.jobs.JobChangeAdapter; 21 import org.eclipse.jface.viewers.AbstractTreeViewer; 22 import org.eclipse.jface.viewers.ITreeContentProvider; 23 import org.eclipse.osgi.util.NLS; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.ui.IWorkbenchPartSite; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.internal.progress.ProgressMessages; 28 import org.eclipse.ui.internal.util.Util; 29 import org.eclipse.ui.model.IWorkbenchAdapter; 30 31 48 public class DeferredTreeContentManager { 49 ITreeContentProvider contentProvider; 50 51 AbstractTreeViewer treeViewer; 52 53 IWorkbenchSiteProgressService progressService; 54 55 62 class DeferredContentFamily { 63 protected DeferredTreeContentManager manager; 64 protected Object element; 65 66 72 DeferredContentFamily(DeferredTreeContentManager schedulingManager, Object object) { 73 this.manager = schedulingManager; 74 this.element = object; 75 } 76 } 77 78 86 public DeferredTreeContentManager(ITreeContentProvider provider, 87 AbstractTreeViewer viewer, IWorkbenchPartSite site) { 88 this(provider, viewer); 89 Object siteService = Util.getAdapter(site, 90 IWorkbenchSiteProgressService.class); 91 if (siteService != null) { 92 progressService = (IWorkbenchSiteProgressService) siteService; 93 } 94 } 95 96 103 public DeferredTreeContentManager(ITreeContentProvider provider, 104 AbstractTreeViewer viewer) { 105 contentProvider = provider; 106 treeViewer = viewer; 107 } 108 109 120 public boolean mayHaveChildren(Object element) { 121 Assert.isNotNull(element, ProgressMessages.DeferredTreeContentManager_NotDeferred); 122 IDeferredWorkbenchAdapter adapter = getAdapter(element); 123 return adapter != null && adapter.isContainer(); 124 } 125 126 136 public Object [] getChildren(final Object parent) { 137 IDeferredWorkbenchAdapter element = getAdapter(parent); 138 if (element == null) { 139 return null; 140 } 141 PendingUpdateAdapter placeholder = createPendingUpdateAdapter(); 142 startFetchingDeferredChildren(parent, element, placeholder); 143 return new Object [] { placeholder }; 144 } 145 146 153 protected PendingUpdateAdapter createPendingUpdateAdapter() { 154 return new PendingUpdateAdapter(); 155 } 156 157 165 protected IDeferredWorkbenchAdapter getAdapter(Object element) { 166 return (IDeferredWorkbenchAdapter)Util.getAdapter(element, IDeferredWorkbenchAdapter.class); 167 } 168 169 182 protected void startFetchingDeferredChildren(final Object parent, 183 final IDeferredWorkbenchAdapter adapter, 184 final PendingUpdateAdapter placeholder) { 185 final IElementCollector collector = createElementCollector(parent, 186 placeholder); 187 cancel(parent); 190 String jobName = getFetchJobName(parent, adapter); 191 Job job = new Job(jobName) { 192 195 public IStatus run(IProgressMonitor monitor) { 196 adapter.fetchDeferredChildren(parent, collector, monitor); 197 if(monitor.isCanceled()) { 198 return Status.CANCEL_STATUS; 199 } 200 return Status.OK_STATUS; 201 } 202 203 204 207 public boolean belongsTo(Object family) { 208 if (family instanceof DeferredContentFamily) { 209 DeferredContentFamily contentFamily = (DeferredContentFamily) family; 210 if (contentFamily.manager == DeferredTreeContentManager.this) { 211 return isParent(contentFamily, parent); 212 } 213 } 214 return false; 215 216 } 217 218 230 private boolean isParent(DeferredContentFamily family, Object child) { 231 if (family.element.equals(child)) { 232 return true; 233 } 234 IWorkbenchAdapter workbenchAdapter = getWorkbenchAdapter(child); 235 if (workbenchAdapter == null) { 236 return false; 237 } 238 Object elementParent = workbenchAdapter.getParent(child); 239 if (elementParent == null) { 240 return false; 241 } 242 return isParent(family, elementParent); 243 } 244 245 251 private IWorkbenchAdapter getWorkbenchAdapter(Object element) { 252 return (IWorkbenchAdapter) Util.getAdapter(element, IWorkbenchAdapter.class); 253 } 254 }; 255 job.addJobChangeListener(new JobChangeAdapter() { 256 261 public void done(IJobChangeEvent event) { 262 runClearPlaceholderJob(placeholder); 263 } 264 }); 265 job.setRule(adapter.getRule(parent)); 266 if (progressService == null) { 267 job.schedule(); 268 } else { 269 progressService.schedule(job); 270 } 271 } 272 273 281 protected String getFetchJobName(Object parent, IDeferredWorkbenchAdapter adapter) { 282 return NLS.bind( 283 ProgressMessages.DeferredTreeContentManager_FetchingName, 284 adapter.getLabel(parent)); 285 } 286 287 294 protected void addChildren(final Object parent, final Object [] children, 295 IProgressMonitor monitor) { 296 WorkbenchJob updateJob = new WorkbenchJob( 297 ProgressMessages.DeferredTreeContentManager_AddingChildren) { 298 303 public IStatus runInUIThread(IProgressMonitor updateMonitor) { 304 if (treeViewer.getControl().isDisposed() || updateMonitor.isCanceled()) { 306 return Status.CANCEL_STATUS; 307 } 308 treeViewer.add(parent, children); 309 return Status.OK_STATUS; 310 } 311 }; 312 updateJob.setSystem(true); 313 updateJob.schedule(); 314 315 } 316 317 325 public boolean isDeferredAdapter(Object element) { 326 return getAdapter(element) != null; 327 } 328 329 336 protected void runClearPlaceholderJob(final PendingUpdateAdapter placeholder) { 337 if (placeholder.isRemoved() || !PlatformUI.isWorkbenchRunning()) { 338 return; 339 } 340 WorkbenchJob clearJob = new WorkbenchJob(ProgressMessages.DeferredTreeContentManager_ClearJob) { 342 347 public IStatus runInUIThread(IProgressMonitor monitor) { 348 if (!placeholder.isRemoved()) { 349 Control control = treeViewer.getControl(); 350 if (control.isDisposed()) { 351 return Status.CANCEL_STATUS; 352 } 353 treeViewer.remove(placeholder); 354 placeholder.setRemoved(true); 355 } 356 return Status.OK_STATUS; 357 } 358 }; 359 clearJob.setSystem(true); 360 clearJob.schedule(); 361 } 362 363 369 public void cancel(Object parent) { 370 if(parent == null) { 371 return; 372 } 373 374 Platform.getJobManager().cancel(new DeferredContentFamily(this, parent)); 375 } 376 377 386 protected IElementCollector createElementCollector(final Object parent, 387 final PendingUpdateAdapter placeholder) { 388 return new IElementCollector() { 389 393 public void add(Object element, IProgressMonitor monitor) { 394 add(new Object [] { element }, monitor); 395 } 396 397 401 public void add(Object [] elements, IProgressMonitor monitor) { 402 addChildren(parent, elements, monitor); 403 } 404 405 410 public void done() { 411 runClearPlaceholderJob(placeholder); 412 } 413 }; 414 } 415 } 416 | Popular Tags |