Advertisement
Guest User

Swing Worker

a guest
Jul 17th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import org.apache.log4j.Logger;
  2.  
  3. import javax.swing.*;
  4. import java.util.concurrent.CancellationException;
  5. import java.util.concurrent.ExecutionException;
  6.  
  7.  
  8. public abstract class Worker {
  9.     SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
  10.         @Override
  11.         protected Void doInBackground() throws Exception {
  12.             Worker.this.doInBackground();
  13.             return null;
  14.         }
  15.         @Override
  16.         public final void done() {
  17.             try {
  18.                 get();
  19.             } catch (InterruptedException e) {
  20.                 throw new RuntimeException(e);
  21.             } catch (ExecutionException e) {
  22.                 throw new RuntimeException(e);
  23.             } catch (CancellationException e) {
  24.                 cancelled();
  25.             }
  26.             afterDone();
  27.         }
  28.     };
  29.     protected void cancelled() {
  30.         Logger.getLogger(this.getClass()).error(toString() + " cancelled");
  31.     }
  32.     protected abstract void doInBackground() throws Exception;
  33.     protected void afterDone() {
  34.     }
  35.     public final void execute() {
  36.         worker.execute();
  37.     }
  38.     public final boolean isDone() {
  39.         return worker.isDone();
  40.     }
  41.     public final void cancel(boolean interrupt) {
  42.         worker.cancel(interrupt);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement