tags:

views:

33

answers:

1

I'm trying to develop a plugin that will do some work when any kind of eclipse job is scheduled.

Basically I want to be able to know when a job is in the progress dialog and monitor it.

Is there a listener that can be set up to catch the begging and end of such jobs?

+2  A: 

When you think about it, the ProgressView does just that: whenever a Job starts, it displays it in its view.
Note, as mentioned in this thread, the ProgressView is tightly coupled with the eclipse Jobs API (i.e., it may not monitor any plain vanilla thread)

alt text
(From the article On the Job: The Eclipse Jobs API)

So may be you can start by looking at the code in org.eclipse.ui.internal.progress.ProgressView, which needs a ProgressViewerContentProvider, based on a ProgressContentProvider (implementing a IProgressUpdateCollector)

The all thing seems to be based upon the ProgressViewUpdater singleton, which create one UI thread in charge of monitoring those Jobs:

    /**
     * Create the update job that handles the updatesInfo.
     */
    private void createUpdateJob() {
        updateJob = new WorkbenchJob(ProgressMessages.ProgressContentProvider_UpdateProgressJob) {
            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
             */
            public IStatus runInUIThread(IProgressMonitor monitor) {

                //Abort the job if there isn't anything
                if (collectors.length == 0) {
        return Status.CANCEL_STATUS;
       }

                if (currentInfo.updateAll) {
                    synchronized (updateLock) {
                        currentInfo.reset();
                    }
                    for (int i = 0; i < collectors.length; i++) {
                        collectors[i].refresh();
                    }

                } else {
                    //Lock while getting local copies of the caches.
                    Object[] updateItems;
                    Object[] additionItems;
                    Object[] deletionItems;
                    synchronized (updateLock) {
                        currentInfo.processForUpdate();

                        updateItems = currentInfo.refreshes.toArray();
                        additionItems = currentInfo.additions.toArray();
                        deletionItems = currentInfo.deletions.toArray();

                        currentInfo.reset();
                    }

                    for (int v = 0; v < collectors.length; v++) {
                        IProgressUpdateCollector collector = collectors[v];

                        if (updateItems.length > 0) {
          collector.refresh(updateItems);
         }
                        if (additionItems.length > 0) {
          collector.add(additionItems);
         }
                        if (deletionItems.length > 0) {
          collector.remove(deletionItems);
         }
                    }
                }

                return Status.OK_STATUS;
            }
        };
        updateJob.setSystem(true);
        updateJob.setPriority(Job.DECORATE);
        updateJob.setProperty(ProgressManagerUtil.INFRASTRUCTURE_PROPERTY, new Object());

    }
VonC
great answer, VonC. :thumbs up:
pimpf0r
Thanks, I'll try it out tomorrow.
Valadas