01: /*******************************************************************************
02: * Copyright (c) 2004, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.examples.jobs;
11:
12: import org.eclipse.core.resources.ResourcesPlugin;
13: import org.eclipse.core.runtime.*;
14: import org.eclipse.ui.progress.UIJob;
15:
16: /**
17: * Base class for a simple test UI job with configurable parameters
18: */
19: public class UITestJob extends UIJob {
20: private long duration;
21: private boolean failure;
22: private boolean unknown;
23:
24: public UITestJob(long duration, boolean lock, boolean failure,
25: boolean indeterminate) {
26: super ("Test job"); //$NON-NLS-1$
27: this .duration = duration;
28: this .failure = failure;
29: this .unknown = indeterminate;
30:
31: if (lock)
32: setRule(ResourcesPlugin.getWorkspace().getRoot());
33: }
34:
35: public IStatus runInUIThread(IProgressMonitor monitor) {
36: if (failure)
37: throw new RuntimeException();
38: final long sleep = 10;
39: int ticks = (int) (duration / sleep);
40: if (unknown)
41: monitor.beginTask(toString(), IProgressMonitor.UNKNOWN);
42: else
43: monitor.beginTask(toString(), ticks);
44: try {
45: for (int i = 0; i < ticks; i++) {
46: if (monitor.isCanceled())
47: return Status.CANCEL_STATUS;
48: monitor.subTask("Processing tick #" + i); //$NON-NLS-1$
49: try {
50: Thread.sleep(sleep);
51: } catch (InterruptedException e) {
52: return Status.CANCEL_STATUS;
53: }
54: monitor.worked(1);
55: }
56: } finally {
57: monitor.done();
58: }
59: return Status.OK_STATUS;
60: }
61: }
|