1 11 12 package org.eclipse.ui.views.markers.internal; 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.Collection ; 16 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.core.runtime.SubProgressMonitor; 22 import org.eclipse.core.runtime.jobs.ILock; 23 import org.eclipse.core.runtime.jobs.Job; 24 import org.eclipse.jface.operation.IRunnableWithProgress; 25 import org.eclipse.jface.viewers.IStructuredContentProvider; 26 import org.eclipse.jface.viewers.TableViewer; 27 import org.eclipse.jface.viewers.Viewer; 28 import org.eclipse.swt.widgets.Control; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.progress.IWorkbenchSiteProgressService; 31 import org.eclipse.ui.progress.WorkbenchJob; 32 33 48 class TableContentProvider implements IStructuredContentProvider { 49 50 private static final String TABLE_SYNCHRONIZATION = Messages 52 .getString("TableContentProvider.TableSynchronization"); 54 private static final String UPDATING_TABLE_WIDGET = Messages 55 .getString("TableContentProvider.Updating"); 57 60 private String description = ""; 62 65 private TableSorter sortOrder = null; 66 67 private DeferredQueue queues; 69 70 73 private Job disableUpdatesJob = new WorkbenchJob(TABLE_SYNCHRONIZATION) { 74 public IStatus runInUIThread(IProgressMonitor monitor) { 75 if (controlExists()) { 76 getViewer().getTable().setRedraw(false); 77 } 78 79 return Status.OK_STATUS; 80 } 81 }; 82 83 86 private Job enableUpdatesJob = new WorkbenchJob(TABLE_SYNCHRONIZATION) { 87 public IStatus runInUIThread(IProgressMonitor monitor) { 88 if (controlExists()) { 89 getViewer().getTable().setRedraw(true); 90 } 91 92 return Status.OK_STATUS; 93 } 94 }; 95 96 102 private class WidgetRefreshJob extends WorkbenchJob { 103 104 107 int lastWorked = 0; 108 109 112 boolean controlExists = true; 113 114 WidgetRefreshJob(String title) { 115 super(title); 116 } 117 118 122 public IStatus runInUIThread(IProgressMonitor monitor) { 123 124 if (lock.getDepth() > 0) { 126 lastWorked = 0; 127 return Status.OK_STATUS; 128 } 129 130 lock.acquire(); 131 try { 132 if (!PlatformUI.isWorkbenchRunning()) { 133 controlExists = false; 134 } else { 135 controlExists = controlExists(); 136 if (controlExists) { 137 lastWorked = updateViewer(); 138 } 139 } 140 } finally { 141 lock.release(); 142 } 143 144 return Status.OK_STATUS; 145 } 146 } 147 148 151 WidgetRefreshJob uiJob; 152 153 159 RestartableJob updateJob; 160 161 private ILock lock; 162 163 173 public TableContentProvider(TableViewer viewer, String description, 174 IWorkbenchSiteProgressService service) { 175 this.queues = new DeferredQueue(viewer); 176 this.description = description; 177 178 uiJob = new WidgetRefreshJob(UPDATING_TABLE_WIDGET); 179 uiJob.setPriority(Job.LONG); 180 uiJob.setSystem(true); 181 182 updateJob = new RestartableJob(TABLE_SYNCHRONIZATION, 183 new IRunnableWithProgress() { 184 public void run(IProgressMonitor monitor) 185 throws InvocationTargetException , 186 InterruptedException { 187 doUpdate(monitor); 188 } 189 }, service); 190 191 lock = Platform.getJobManager().newLock(); 192 } 193 194 199 public void setSorter(TableSorter c) { 200 if (sortOrder != c) { 201 sortOrder = c; 202 scheduleUpdate(); 203 } 204 } 205 206 209 public Object [] getElements(Object inputElement) { 210 return queues.getVisibleItems(); 211 } 212 213 216 public void dispose() { 217 } 219 220 223 public void inputChanged(Viewer inputViewer, Object oldInput, 224 Object newInput) { 225 scheduleUpdate(); 226 } 227 228 234 public void set(Collection newVisibleItems, IProgressMonitor mon) { 235 lock.acquire(); 236 237 try { 238 queues.set(newVisibleItems, mon); 239 240 scheduleUpdate(); 241 } finally { 242 lock.release(); 243 } 244 } 245 246 250 private void resync() { 251 if (controlExists()) { 252 int count = queues.getViewer().getTable().getItemCount(); 253 if (count != queues.countVisibleItems()) { 254 queues.getViewer().refresh(); 255 } 256 } 257 } 258 259 264 void change(Collection changes) { 265 266 lock.acquire(); 267 try { 268 271 queues.change(changes); 272 scheduleUpdate(); 273 } finally { 274 lock.release(); 275 } 276 } 277 278 283 private TableViewer getViewer() { 284 return queues.getViewer(); 285 } 286 287 292 private boolean controlExists() { 293 Control control = getViewer().getControl(); 294 295 if (control == null || control.isDisposed()) { 296 return false; 297 } 298 299 return true; 300 } 301 302 308 public boolean hasPendingChanges() { 309 return queues.hasPendingChanges() || sortOrder != queues.getSorter(); 310 } 311 312 317 private int totalWork() { 318 return queues.workRemaining() + 1; 319 } 320 321 326 private void scheduleUpdate() { 327 if (hasPendingChanges()) { 328 updateJob.schedule(); 329 } 330 } 331 332 339 public void cancelPendingChanges() { 340 updateJob.cancel(); 341 342 lock.acquire(); 343 try { 344 queues.cancelPending(); 345 } finally { 346 lock.release(); 347 } 348 } 349 350 private void doUpdate(IProgressMonitor monitor) throws InterruptedException { 351 352 if (!PlatformUI.isWorkbenchRunning()) 354 return; 355 356 int remainingWorkUnits = 100000; 358 359 monitor.beginTask(description, remainingWorkUnits); 360 361 disableUpdatesJob.schedule(); 362 disableUpdatesJob.join(); 363 try { 364 365 while (hasPendingChanges() && !monitor.isCanceled()) { 368 369 372 try { 373 374 int totalWork; 375 lock.acquire(); 376 try { 377 totalWork = totalWork(); 378 if (sortOrder != queues.getSorter()) { 379 queues.setComparator(sortOrder); 380 } 381 382 SubProgressMonitor sub = new SubProgressMonitor( 383 monitor, 0); 384 queues.refreshQueues(sub); 385 } finally { 386 lock.release(); 387 } 388 389 try { 390 uiJob.schedule(); 391 uiJob.join(); 393 } catch (IllegalStateException e) { 394 } 400 401 int consumedUnits = uiJob.lastWorked * remainingWorkUnits 404 / totalWork; 405 monitor.worked(consumedUnits); 406 remainingWorkUnits -= consumedUnits; 407 408 } catch (InterruptedException e) { 409 monitor.setCanceled(true); 410 } 411 412 if (!uiJob.controlExists) { 413 break; 414 } 415 } 416 } finally { 417 if (PlatformUI.isWorkbenchRunning()) { 419 enableUpdatesJob.schedule(); 420 enableUpdatesJob.join(); 421 } 422 423 monitor.done(); 424 } 425 } 426 427 434 private int updateViewer() { 435 436 int result; 437 438 lock.acquire(); 442 try { 443 if (getViewer().getSorter() != null) { 444 getViewer().setSorter(null); 445 } 446 447 resync(); 448 449 result = queues.nextUpdate(); 450 } finally { 451 lock.release(); 452 } 453 454 return result; 455 } 456 } 457 | Popular Tags |