001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM - Initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.progress;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.core.runtime.jobs.IJobChangeEvent;
014: import org.eclipse.core.runtime.jobs.JobChangeAdapter;
015: import org.eclipse.swt.widgets.Display;
016: import org.eclipse.ui.PlatformUI;
017:
018: /**
019: * WorkbenchJob is a type of job that implements a done listener
020: * and does the shutdown checks before scheduling. This is used if
021: * a job is not meant to run when the Workbench is shutdown.
022: * @since 3.0
023: */
024: public abstract class WorkbenchJob extends UIJob {
025:
026: /**
027: * Create a new instance of the receiver with the
028: * supplied display and name.
029: * Normally this constructor would not be used as
030: * it is best to let the job find the display from
031: * the workbench
032: * @param jobDisplay Display. The display to run the
033: * job with.
034: * @param name String
035: */
036: public WorkbenchJob(Display jobDisplay, String name) {
037: super (jobDisplay, name);
038: addDefaultJobChangeListener();
039: }
040:
041: /**
042: * Add a new instance of the reciever with the
043: * supplied name.
044: * @param name String
045: */
046: public WorkbenchJob(String name) {
047: super (name);
048: addDefaultJobChangeListener();
049: }
050:
051: /**
052: * Add a job change listeners that handles a done
053: * event if the result was IStatus.OK.
054: *
055: */
056: private void addDefaultJobChangeListener() {
057: addJobChangeListener(new JobChangeAdapter() {
058: /* (non-Javadoc)
059: * @see org.eclipse.core.runtime.jobs.JobChangeAdapter#done(org.eclipse.core.runtime.jobs.IJobChangeEvent)
060: */
061: public void done(IJobChangeEvent event) {
062:
063: //Abort if it is not running
064: if (!PlatformUI.isWorkbenchRunning()) {
065: return;
066: }
067:
068: if (event.getResult().getCode() == IStatus.OK) {
069: performDone(event);
070: }
071: }
072: });
073: }
074:
075: /**
076: * Perform done with the supplied event. This will
077: * only occur if the returned status was OK.
078: * This is called only if the job is finished with an IStatus.OK
079: * result and the workbench is still running.
080: * @param event IJobChangeEvent
081: */
082: public void performDone(IJobChangeEvent event) {
083: //Do nothing by default.
084: }
085:
086: /* (non-Javadoc)
087: * @see org.eclipse.core.internal.jobs.InternalJob#shouldSchedule()
088: */
089: public boolean shouldSchedule() {
090: return super .shouldSchedule()
091: && PlatformUI.isWorkbenchRunning();
092: }
093:
094: /* (non-Javadoc)
095: * @see org.eclipse.core.runtime.jobs.Job#shouldRun()
096: */
097: public boolean shouldRun() {
098: return super.shouldRun() && PlatformUI.isWorkbenchRunning();
099: }
100:
101: }
|