001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 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 Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.jobs;
011:
012: import org.eclipse.core.resources.ResourcesPlugin;
013: import org.eclipse.core.runtime.*;
014: import org.eclipse.core.runtime.jobs.Job;
015:
016: /**
017: * Base class for a simple test job with configurable parameters
018: */
019: public class TestJob extends Job {
020: /**
021: * A family identifier for all test jobs
022: */
023: public static final Object FAMILY_TEST_JOB = new Object();
024: /**
025: * Total duration that the test job should sleep, in milliseconds.
026: */
027: private long duration;
028: /**
029: * Whether the test job should fail.
030: */
031: private boolean failure;
032: /**
033: * Whether the job should report unknown progress.
034: */
035: private boolean unknown;
036: private boolean reschedule;
037: private long rescheduleWait;
038:
039: /**
040: * Creates a new test job
041: * @param duration Total time that the test job should sleep, in milliseconds.
042: * @param lock Whether the job should use a workspace scheduling rule
043: * @param failure Whether the job should fail
044: * @param indeterminate Whether the job should report indeterminate progress
045: * @param rescheduleWait
046: * @param reschedule
047: */
048: public TestJob(long duration, boolean lock, boolean failure,
049: boolean indeterminate, boolean reschedule,
050: long rescheduleWait) {
051: super ("Test job"); //$NON-NLS-1$
052: this .duration = duration;
053: this .failure = failure;
054: this .unknown = indeterminate;
055: this .reschedule = reschedule;
056: this .rescheduleWait = rescheduleWait;
057: if (lock)
058: setRule(ResourcesPlugin.getWorkspace().getRoot());
059: }
060:
061: /* (non-Javadoc)
062: * @see org.eclipse.core.internal.jobs.InternalJob#belongsTo(java.lang.Object)
063: */
064: public boolean belongsTo(Object family) {
065: if (family instanceof TestJob) {
066: return true;
067: }
068: return family == FAMILY_TEST_JOB;
069: }
070:
071: /* (non-Javadoc)
072: * @see org.eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor)
073: */
074: public IStatus run(IProgressMonitor monitor) {
075: if (failure) {
076: MultiStatus result = new MultiStatus(
077: "org.eclipse.ui.examples.jobs", 1, "This is the MultiStatus message", new RuntimeException("This is the MultiStatus exception")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
078: result
079: .add(new Status(
080: IStatus.ERROR,
081: "org.eclipse.ui.examples.jobs", 1, "This is the child status message", new RuntimeException("This is the child exception"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
082: return result;
083: }
084: final long sleep = 10;
085: int ticks = (int) (duration / sleep);
086: if (this .unknown)
087: monitor.beginTask(toString(), IProgressMonitor.UNKNOWN);
088: else
089: monitor.beginTask(toString(), ticks);
090: try {
091: for (int i = 0; i < ticks; i++) {
092: if (monitor.isCanceled())
093: return Status.CANCEL_STATUS;
094: monitor.subTask("Processing tick #" + i); //$NON-NLS-1$
095: try {
096: Thread.sleep(sleep);
097: } catch (InterruptedException e) {
098: return Status.CANCEL_STATUS;
099: }
100: monitor.worked(1);
101: }
102: } finally {
103: if (reschedule)
104: schedule(rescheduleWait);
105: monitor.done();
106: }
107: return Status.OK_STATUS;
108: }
109: }
|