Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * LongTaskMonitor.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 import javax.swing.*;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31
32 public class LongTaskMonitor {
33
34 public final static int ONE_SECOND = 1000;
35
36 private ProgressMonitor progressMonitor;
37 private Timer timer;
38 private LongTask task;
39 private JComponent parent;
40 private TaskListener listener;
41
42 public LongTaskMonitor(LongTask task, JComponent parent, TaskListener listener) {
43
44 this.task = task;
45 this.parent = parent;
46 this.listener = listener;
47
48 //Create a timer.
49 timer = new Timer(ONE_SECOND, new TimerListener());
50
51 progressMonitor = new ProgressMonitor(parent,
52 task.getDescription(),
53 "", 0, task.getLengthOfTask());
54 progressMonitor.setProgress(0);
55 progressMonitor.setMillisToPopup(ONE_SECOND);
56
57 task.go();
58 timer.start();
59 }
60
61 /**
62 * The actionPerformed method in this class
63 * is called each time the Timer "goes off".
64 */
65 class TimerListener implements ActionListener {
66 public void actionPerformed(ActionEvent evt) {
67 if (progressMonitor.isCanceled() || task.done()) {
68
69 boolean canceled = progressMonitor.isCanceled();
70 progressMonitor.close();
71 task.stop();
72 timer.stop();
73 if (canceled) {
74 listener.taskCanceled();
75 } else {
76 listener.taskFinished();
77 //Toolkit.getDefaultToolkit().beep();
78 }
79 } else {
80 progressMonitor.setNote(task.getMessage());
81 progressMonitor.setProgress(task.getCurrent());
82 }
83 }
84 }
85 }