1 package com.melloware.jukes.gui.view.tasks;
2
3 import java.awt.Toolkit;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6
7 import javax.swing.ProgressMonitor;
8 import javax.swing.Timer;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import com.jgoodies.uif.application.Application;
14 import com.jgoodies.uif.util.ResourceUtils;
15 import com.melloware.jukes.util.MessageUtil;
16
17
18
19
20
21
22
23
24 public final class TimerListener
25 implements ActionListener {
26
27 private static final Log LOG = LogFactory.getLog(TimerListener.class);
28 private LongTask task;
29 private final ProgressMonitor progressMonitor;
30 private final Timer timer;
31
32
33
34
35 public TimerListener(ProgressMonitor aProgressMonitor, LongTask aTask, Timer aTimer) {
36 super();
37 this.progressMonitor = aProgressMonitor;
38 this.task = aTask;
39 this.timer = aTimer;
40 }
41
42
43
44
45 public void actionPerformed(ActionEvent evt) {
46 LOG.debug("Current " + task.getCurrent());
47 progressMonitor.setProgress(task.getCurrent());
48 String message = task.getMessage();
49 if (message != null) {
50 progressMonitor.setNote(message);
51 LOG.debug(message);
52 }
53 if (progressMonitor.isCanceled() || task.isDone()) {
54 progressMonitor.close();
55 task.stop();
56 Toolkit.getDefaultToolkit().beep();
57 timer.stop();
58 if (task.isDone()) {
59 if (task.hasWarning()) {
60 LOG.debug("Task completed with errors.");
61 MessageUtil.showwarn(Application.getDefaultParentFrame(),
62 ResourceUtils.getString("messages.TaskWithWarning"));
63
64 } else {
65 LOG.debug("Task completed.");
66 MessageUtil.showTaskCompleted(Application.getDefaultParentFrame());
67 }
68 } else {
69 LOG.debug("Task canceled.");
70 }
71 }
72
73
74 if (task.isCancelled()) {
75 progressMonitor.close();
76 timer.stop();
77 }
78 }
79
80
81
82
83
84
85 public LongTask getTask() {
86 return this.task;
87 }
88
89
90
91
92
93
94 public void setTask(LongTask aTask) {
95 this.task = aTask;
96 }
97
98 }