extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/AbortableProgressReporter.java

Code
Comments
Other
Rev Date Author Line
5919 24 Apr 20 nicklas 1 package net.sf.basedb.reggie;
5919 24 Apr 20 nicklas 2
5919 24 Apr 20 nicklas 3 import net.sf.basedb.core.ProgressReporter;
5919 24 Apr 20 nicklas 4 import net.sf.basedb.core.SimpleProgressReporter;
5919 24 Apr 20 nicklas 5 import net.sf.basedb.core.signal.SignalException;
5919 24 Apr 20 nicklas 6
5919 24 Apr 20 nicklas 7 /**
5919 24 Apr 20 nicklas 8   Progress reporter implementation that add support for aborting 
5919 24 Apr 20 nicklas 9   a process. The implementation requires a parent progress
5919 24 Apr 20 nicklas 10   reporter that is responsible for reporting the progress. 
5919 24 Apr 20 nicklas 11   
5919 24 Apr 20 nicklas 12   By calling {@link #setAbort()} a flag is set on this implementation
5919 24 Apr 20 nicklas 13   that causes the next call to {@link #display(int, String)} or
5919 24 Apr 20 nicklas 14   {@link #append(String)} to result in an exception.
5919 24 Apr 20 nicklas 15
5919 24 Apr 20 nicklas 16   @author nicklas
5919 24 Apr 20 nicklas 17   @since 4.27
5919 24 Apr 20 nicklas 18 */
5919 24 Apr 20 nicklas 19 public class AbortableProgressReporter 
5919 24 Apr 20 nicklas 20   extends SimpleProgressReporter
5919 24 Apr 20 nicklas 21 {
5919 24 Apr 20 nicklas 22
5919 24 Apr 20 nicklas 23   private transient boolean shouldAbort;
5919 24 Apr 20 nicklas 24   
5919 24 Apr 20 nicklas 25   public AbortableProgressReporter(ProgressReporter parent)
5919 24 Apr 20 nicklas 26   {
5919 24 Apr 20 nicklas 27     super(parent);
5919 24 Apr 20 nicklas 28   }
5919 24 Apr 20 nicklas 29   
5919 24 Apr 20 nicklas 30   @Override
5919 24 Apr 20 nicklas 31   public void display(int percent, String message) 
5919 24 Apr 20 nicklas 32   {
5919 24 Apr 20 nicklas 33     checkAbort();
5919 24 Apr 20 nicklas 34     super.display(percent, message);
5919 24 Apr 20 nicklas 35   }
5919 24 Apr 20 nicklas 36
5919 24 Apr 20 nicklas 37   @Override
5919 24 Apr 20 nicklas 38   public void append(String message) 
5919 24 Apr 20 nicklas 39   {
5919 24 Apr 20 nicklas 40     checkAbort();
5919 24 Apr 20 nicklas 41     super.append(message);
5919 24 Apr 20 nicklas 42   }
5919 24 Apr 20 nicklas 43
5919 24 Apr 20 nicklas 44   /**
5919 24 Apr 20 nicklas 45     Set a flag indicating that the process should be aborted.
5919 24 Apr 20 nicklas 46   */
5919 24 Apr 20 nicklas 47   public void setAbort()
5919 24 Apr 20 nicklas 48   {
5919 24 Apr 20 nicklas 49     shouldAbort = true;
5919 24 Apr 20 nicklas 50   }
5919 24 Apr 20 nicklas 51   
5919 24 Apr 20 nicklas 52   /**
5919 24 Apr 20 nicklas 53     Check the flag indicating if the process should be aborted.
5919 24 Apr 20 nicklas 54   */
5919 24 Apr 20 nicklas 55   public boolean shouldAbort()
5919 24 Apr 20 nicklas 56   {
5919 24 Apr 20 nicklas 57     return shouldAbort;
5919 24 Apr 20 nicklas 58   }
5919 24 Apr 20 nicklas 59   
5919 24 Apr 20 nicklas 60   /**
5919 24 Apr 20 nicklas 61     Check the flag indicating if the process should be aborted
5919 24 Apr 20 nicklas 62     and throw an expection if the flag has been set.
5919 24 Apr 20 nicklas 63   */
5919 24 Apr 20 nicklas 64   public void checkAbort()
5919 24 Apr 20 nicklas 65   {
5919 24 Apr 20 nicklas 66     if (shouldAbort()) throw new SignalException("Aborted by user");
5919 24 Apr 20 nicklas 67   }
5919 24 Apr 20 nicklas 68   
5919 24 Apr 20 nicklas 69 }