View Javadoc

1   package org.glassbox;
2   
3   import java.lang.reflect.InvocationTargetException;
4   import javax.swing.SwingUtilities;
5   
6   /***
7      Little abstraction layer for the Swing event dispatch thread.
8      Just to avoid unnecessary <code>SwingUtilities.isEventDispatchThread()</code> checks.
9      @author Tilman Linden
10  */
11  public class SwingThread {
12      /***
13         This class is never instantiated.
14       */
15      private SwingThread() {
16  	//
17      }
18  
19      /***
20         Executes the given task.
21         If the current thread is the event dispatch thread, the task will be executed immidiately.
22         Otherwise it is executed by passing it to </code>SwingUtilities.invokeLater()</code>.
23       */
24      public static void invokeLater(Runnable task) {
25  	if (SwingUtilities.isEventDispatchThread()) 
26  	    task.run();
27  	else
28  	    SwingUtilities.invokeLater(task);
29      }
30  
31      /***
32         Executes the given task.
33         If the current thread is the event dispatch thread, the task will be executed immidiately.
34         Otherwise it is executed by passing it to </code>SwingUtilities.invokeAndWait()</code>.
35       */
36      public static void invokeAndWait(Runnable task) throws InterruptedException, InvocationTargetException {
37  	if (SwingUtilities.isEventDispatchThread()) 
38  	    task.run();
39  	else
40  	    SwingUtilities.invokeAndWait(task);
41      }
42  
43      /***
44         Executes the given task and silently discards InterruptedException and InvocationTargetException.
45         If the current thread is the event dispatch thread, the task will be executed immidiately.
46         Otherwise it is executed by passing it to </code>SwingUtilities.invokeAndWait()</code>.
47       */
48      public static void invokeAndWaitSilently(Runnable task) {
49  	try {
50  	    if (SwingUtilities.isEventDispatchThread()) 
51  		task.run();
52  	    else
53  		SwingUtilities.invokeAndWait(task);
54  	} catch (InvocationTargetException x) {
55  	    //
56  	} catch (InterruptedException x) {
57  	    Thread.currentThread().interrupt();
58  	}
59      }
60  }