| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 
 |  
public class Task {
  /** Gets the length (number of notifications, not time length) of the task depending on the task parameter.
  * @return A positive number, -1 is the task length is unknown/undefined.
  */
  public int getTaskLength() {
    ...
  }
 
  /** Do the task.
   * @param obs An optional observer that is interrested in receiving this task's notifications, may be <code>null</code>.
   */
  public void doTask(TaskObserver obs) { 
    ... 
    if ((obs != null) && (obs.hasBeenCanceled())) {
       return;
    }
    if (obs != null) {
      obs.stepDone();
      obs.setNote("Switching to next sub task.");
    }
    ...
  }
}
 
/** An implementation that only holds a <code>JProgressBar</code>.
 */
public class ProgressBarTaskObserver implements TaskObserver {
 private JProgressBar bar = ...
 
  ...
 
  /** @inheritDoc
   * <br>This implementation always returns <code>false</code> as we do not have a cancel button.
   */
  public boolean hasBeenCanceled() {
    return false;
  }
 
  /** @inheritDoc
   * <br>Increments the bar.
   */
  public void stepDone() {
    try {
      // Update at EDT and wait until update is done.
      SwingUtilities.invokeAndWait(new Runnable() {
         public void run() {
           bar.setValue(bar.getValue()+1);
         }
      });
    }
    // Silently consume exception.
    catch (Exception e) {
    }
  }
} | 
Partager