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.jgoodies.uif.util.ResourceUtils;
15  import com.melloware.jukes.util.MessageUtil;
16  
17  /**
18   * Timer listener for Long tasks.
19   * <p>
20   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
21   * @author Emil A. Lefkof III <info@melloware.com>
22   * @version 4.0
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       * Default contructor
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      /* (non-Javadoc)
43       * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
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()) {//AZ
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          // was cancelled due to error
74          if (task.isCancelled()) {
75              progressMonitor.close();
76              timer.stop();
77          }
78      }
79  
80  	/**
81  	 * Gets the task.
82  	 * <p>
83  	 * @return Returns the task.
84  	 */
85  	public LongTask getTask() {
86  		return this.task;
87  	}
88  
89  	/**
90  	 * Sets the task.
91  	 * <p>
92  	 * @param aTask The task to set.
93  	 */
94  	public void setTask(LongTask aTask) {
95  		this.task = aTask;
96  	}
97  
98  }