KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > WorkerJob


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.jface.operation.IRunnableWithProgress;
19
20 public class WorkerJob extends Job {
21
22     private final Worker worker;
23     
24     public WorkerJob(String JavaDoc name) {
25         super(name);
26         worker = new Worker(name);
27     }
28
29     protected IStatus run(IProgressMonitor monitor) {
30         worker.run(monitor);
31         // reschedule to ensure we don't miss a task
32
IStatus result = getResult(worker);
33         schedule();
34         return result;
35     }
36     
37     private IStatus getResult(Worker w) {
38         Throwable JavaDoc[] errors = w.getErrors();
39         if (errors.length == 0)
40             return Status.OK_STATUS;
41         if (errors.length == 1)
42             return new Status(IStatus.ERROR, CompareUIPlugin.PLUGIN_ID, 0, errors[0].getMessage(), errors[0]);
43         List JavaDoc statii = new ArrayList JavaDoc();
44         for (int i = 0; i < errors.length; i++) {
45             Throwable JavaDoc throwable = errors[i];
46             statii.add(new Status(IStatus.ERROR, CompareUIPlugin.PLUGIN_ID, 0, errors[0].getMessage(), throwable));
47         }
48         return new MultiStatus(CompareUIPlugin.PLUGIN_ID, 0, (IStatus[]) statii.toArray(new IStatus[statii.size()]), CompareMessages.WorkerJob_0, null);
49     }
50     
51     public boolean shouldRun() {
52         return worker.hasWork();
53     }
54     
55     public void add(IRunnableWithProgress runnable) {
56         worker.add(runnable);
57         schedule();
58     }
59
60 }
61
Popular Tags