* Get the update WorkbenchJob.
*
* @return WorkbenchJob
*/
private WorkbenchJob getUpdateJob() {
WorkbenchJob job = new WorkbenchJob(
WorkbenchMessages.DecorationScheduler_UpdateJobName) {
int currentIndex = NEEDS_INIT;
LabelProviderChangedEvent labelProviderChangedEvent;
ILabelProviderListener[] listeners;
public IStatus runInUIThread(IProgressMonitor monitor) {
synchronized (DecorationScheduler.this) {
if (shutdown) {
return Status.CANCEL_STATUS;
}
}
// If this is the first one check again in case
// someone has already cleared it out.
if (currentIndex == NEEDS_INIT) {
if (hasPendingUpdates()) {
// If the removal came in while we were waiting clear it
// anyways
removedListeners.clear();
return Status.OK_STATUS;
}
setUpUpdates();
}
if (listeners.length == 0) {
return Status.OK_STATUS;
}
monitor.beginTask(
WorkbenchMessages.DecorationScheduler_UpdatingTask,
IProgressMonitor.UNKNOWN);
long startTime = System.currentTimeMillis();
while (currentIndex < listeners.length) {
ILabelProviderListener listener = listeners[currentIndex];
currentIndex++;
// If it was removed in the meantime then skip it.
if (!removedListeners.contains(listener)) {
decoratorManager.fireListener(
labelProviderChangedEvent, listener);
}
// If it is taking long enough for the user to notice then
// cancel the
// updates.
if ((System.currentTimeMillis() - startTime) >= UPDATE_DELAY / 2) {
break;
}
}
monitor.done();
if (currentIndex >= listeners.length) {
// Other decoration requests may have occurred due to
// updates or we may have timed out updating listeners.
// Only clear the results if there are none pending.
if (awaitingDecoration.isEmpty()) {
resultCache.clear();
}
if (!hasPendingUpdates()) {
decorated();
}
currentIndex = NEEDS_INIT;// Reset
labelProviderChangedEvent = null;
removedListeners.clear();
listeners = EMPTY_LISTENER_LIST;
} else {
schedule(UPDATE_DELAY);// Reschedule if we are not done
}
return Status.OK_STATUS;
}
private void setUpUpdates() {
// Get the elements awaiting update and then
// clear the list
removedListeners.clear();
currentIndex = 0;
synchronized (pendingKey) {
Object[] elements = pendingUpdate
.toArray(new Object[pendingUpdate.size()]);
pendingUpdate.clear();
labelProviderChangedEvent = new LabelProviderChangedEvent(
decoratorManager, elements);
}
listeners = decoratorManager.getListeners();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object)
*/
public boolean belongsTo(Object family) {
return DecoratorManager.FAMILY_DECORATE == family;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.runtime.jobs.Job#shouldRun()
*/
public boolean shouldRun() {
return PlatformUI.isWorkbenchRunning();
}
};
job.setSystem(true);
return job;
}