1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * ident        "%Z%%M% %I%     %E% SMI"
  24  *
  25  * Copyright (c) 1998-1999 by Sun Microsystems, Inc.
  26  * All rights reserved.
  27  */
  28 package com.sun.dhcpmgr.ui;
  29 
  30 import javax.swing.SwingUtilities;
  31 
  32 /**
  33  * An abstract class that you subclass to perform
  34  * GUI-related work in a dedicated thread.
  35  * For instructions on using this class, see 
  36  * http://java.sun.com/products/jfc/swingdoc-current/threads2.html
  37  */
  38 public abstract class SwingWorker {
  39     private Object value;
  40     private Thread thread;
  41 
  42     /** 
  43      * Compute the value to be returned by the <code>get</code> method. 
  44      */
  45     public abstract Object construct();
  46 
  47     /**
  48      * Called on the event dispatching thread (not on the worker thread)
  49      * after the <code>construct</code> method has returned.
  50      */
  51     public void finished() {
  52     }
  53 
  54     /**
  55      * A new method that interrupts the worker thread.  Call this method
  56      * to force the worker to abort what it's doing.
  57      */
  58     public void interrupt() {
  59         Thread t = thread;
  60         if (t != null) {
  61             t.interrupt();
  62         }
  63         thread = null;
  64     }
  65 
  66     /**
  67      * Return the value created by the <code>construct</code> method.  
  68      */
  69     public Object get() {
  70         while (true) {  // keep trying if we're interrupted
  71             Thread t;
  72             synchronized (SwingWorker.this) {
  73                 t = thread;
  74                 if (t == null) {
  75                     return value;
  76                 }
  77             }
  78             try {
  79                 t.join();
  80             }
  81             catch (InterruptedException e) {
  82             }
  83         }
  84     }
  85 
  86     /**
  87      * Start a thread that will call the <code>construct</code> method
  88      * and then exit.
  89      */
  90     public SwingWorker() {
  91         final Runnable doFinished = new Runnable() {
  92            public void run() { finished(); }
  93         };
  94 
  95         Runnable doConstruct = new Runnable() { 
  96             public void run() {
  97                 synchronized (SwingWorker.this) {
  98                     value = construct();
  99                     thread = null;
 100                 }
 101                 SwingUtilities.invokeLater(doFinished);
 102             }
 103         };
 104 
 105         thread = new Thread(doConstruct);
 106         thread.start();
 107     }
 108 }