document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import javax.swing.SwingUtilities;
  2.  
  3. /**
  4.  * This is the 3rd version of SwingWorker (also known as
  5.  * SwingWorker 3), an abstract class that you subclass to
  6.  * perform GUI-related work in a dedicated thread.  For
  7.  * instructions on using this class, see:
  8.  *
  9.  * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
  10.  *
  11.  * Note that the API changed slightly in the 3rd version:
  12.  * You must now invoke start() on the SwingWorker after
  13.  * creating it.
  14.  */
  15. public abstract class SwingWorker {
  16.     private Object value;  // see getValue(), setValue()
  17.     private Thread thread;
  18.  
  19.     /**
  20.      * Class to maintain reference to current worker thread
  21.      * under separate synchronization control.
  22.      */
  23.     private static class ThreadVar {
  24.         private Thread thread;
  25.         ThreadVar(Thread t) { thread = t; }
  26.         synchronized Thread get() { return thread; }
  27.         synchronized void clear() { thread = null; }
  28.     }
  29.  
  30.     private ThreadVar threadVar;
  31.  
  32.     /**
  33.      * Get the value produced by the worker thread, or null if it
  34.      * hasn\'t been constructed yet.
  35.      */
  36.     protected synchronized Object getValue() {
  37.         return value;
  38.     }
  39.  
  40.     /**
  41.      * Set the value produced by worker thread
  42.      */
  43.     private synchronized void setValue(Object x) {
  44.         value = x;
  45.     }
  46.  
  47.     /**
  48.      * Compute the value to be returned by the <code>get</code> method.
  49.      */
  50.     public abstract Object construct();
  51.  
  52.     /**
  53.      * Called on the event dispatching thread (not on the worker thread)
  54.      * after the <code>construct</code> method has returned.
  55.      */
  56.     public void finished() {
  57.     }
  58.  
  59.     /**
  60.      * A new method that interrupts the worker thread.  Call this method
  61.      * to force the worker to stop what it\'s doing.
  62.      */
  63.     public void interrupt() {
  64.         Thread t = threadVar.get();
  65.         if (t != null) {
  66.             t.interrupt();
  67.         }
  68.         threadVar.clear();
  69.     }
  70.  
  71.     /**
  72.      * Return the value created by the <code>construct</code> method.
  73.      * Returns null if either the constructing thread or the current
  74.      * thread was interrupted before a value was produced.
  75.      *
  76.      * @return the value created by the <code>construct</code> method
  77.      */
  78.     public Object get() {
  79.         while (true) {
  80.             Thread t = threadVar.get();
  81.             if (t == null) {
  82.                 return getValue();
  83.             }
  84.             try {
  85.                 t.join();
  86.             }
  87.             catch (InterruptedException e) {
  88.                 Thread.currentThread().interrupt(); // propagate
  89.                 return null;
  90.             }
  91.         }
  92.     }
  93.  
  94.  
  95.     /**
  96.      * Start a thread that will call the <code>construct</code> method
  97.      * and then exit.
  98.      */
  99.     public SwingWorker() {
  100.         final Runnable doFinished = new Runnable() {
  101.            public void run() { finished(); }
  102.         };
  103.  
  104.         Runnable doConstruct = new Runnable() {
  105.             public void run() {
  106.                 try {
  107.                     setValue(construct());
  108.                 }
  109.                 finally {
  110.                     threadVar.clear();
  111.                 }
  112.  
  113.                 SwingUtilities.invokeLater(doFinished);
  114.             }
  115.         };
  116.  
  117.         Thread t = new Thread(doConstruct);
  118.         threadVar = new ThreadVar(t);
  119.     }
  120.  
  121.     /**
  122.      * Start the worker thread.
  123.      */
  124.     public void start() {
  125.         Thread t = threadVar.get();
  126.         if (t != null) {
  127.             t.start();
  128.         }
  129.     }
  130. }
  131. //I will put this complete source codes in my blog
  132. //My blog url is http://java2everyone.blogspot.com
');