Debian Codegraph @

src/dr/app/gui/util/LongTask.java

raw symbols SCIP
1 /*
2 * LongTask.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.gui.util;
27
28 public abstract class LongTask {
29
30 private SwingWorker worker = null;
31 Object answer;
32 boolean finished = false;
33
34 /**
35 * Does the actual work and returns some kind of result.
36 */
37 public abstract Object doWork() throws java.lang.Exception;
38
39 /**
40 * Called to start the task.
41 */
42 public final void go() {
43 worker = new SwingWorker() {
44 public Object construct() {
45
46 try {
47 answer = doWork();
48 } catch (java.lang.Exception e) {
49 throw new RuntimeException(e.toString());
50 }
51 finished = true;
52 return answer;
53 }
54 };
55 worker.start();
56 }
57
58 public final Object getAnswer() {
59 return answer;
60 }
61
62 /**
63 * Called to find out how much work needs
64 * to be done.
65 */
66 public abstract int getLengthOfTask();
67
68 /**
69 * Called to find out how much has been done.
70 */
71 public abstract int getCurrent();
72
73 /**
74 * Called to stop task.
75 */
76 public void stop() {
77 finished = true;
78 }
79
80 /**
81 * Called to find out if the task has completed.
82 */
83 public boolean done() {
84 return finished;
85 }
86
87 /**
88 * Called to get the current message of the task.
89 */
90 public abstract String getMessage();
91
92 /**
93 * Called to get the description of this task.
94 */
95 public String getDescription() {
96 return "Running a long task...";
97 }
98 }