View Javadoc

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.melloware.jukes.util.MessageUtil;
15  
16  /**
17   * Timer listener for Long tasks.
18   * <p>
19   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
20   * @author Emil A. Lefkof III <info@melloware.com>
21   * @version 4.0
22   */
23  public final class TimerListener
24      implements ActionListener {
25  
26      private static final Log LOG = LogFactory.getLog(TimerListener.class);
27      private LongTask task;
28      private final ProgressMonitor progressMonitor;
29      private final Timer timer;
30  
31      /**
32       * Default contructor
33       */
34      public TimerListener(ProgressMonitor aProgressMonitor, LongTask aTask, Timer aTimer) {
35          super();
36          this.progressMonitor = aProgressMonitor;
37          this.task = aTask;
38          this.timer = aTimer;
39      }
40  
41      /* (non-Javadoc)
42       * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
43       */
44      public void actionPerformed(ActionEvent evt) {
45      	LOG.debug("Current " + task.getCurrent());
46          progressMonitor.setProgress(task.getCurrent());
47          String message = task.getMessage();
48          if (message != null) {
49              progressMonitor.setNote(message);
50              LOG.debug(message);
51          }
52          if (progressMonitor.isCanceled() || task.isDone()) {
53              progressMonitor.close();
54              task.stop();
55              Toolkit.getDefaultToolkit().beep();
56              timer.stop();
57              if (task.isDone()) {
58                  LOG.debug("Task completed.");
59                  MessageUtil.showTaskCompleted(Application.getDefaultParentFrame());
60              } else {
61                  LOG.debug("Task canceled.");
62              }
63          }
64  
65          // was cancelled due to error
66          if (task.isCancelled()) {
67              progressMonitor.close();
68              timer.stop();
69          }
70      }
71  
72  	/**
73  	 * Gets the task.
74  	 * <p>
75  	 * @return Returns the task.
76  	 */
77  	public LongTask getTask() {
78  		return this.task;
79  	}
80  
81  	/**
82  	 * Sets the task.
83  	 * <p>
84  	 * @param aTask The task to set.
85  	 */
86  	public void setTask(LongTask aTask) {
87  		this.task = aTask;
88  	}
89  
90  }