KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > foxtrot > AbstractSyncWorker


1 /**
2  * Copyright (c) 2002-2005, Simone Bordet
3  * All rights reserved.
4  *
5  * This software is distributable under the BSD license.
6  * See the terms of the BSD license in the documentation provided with this software.
7  */

8
9 package foxtrot;
10
11 import javax.swing.SwingUtilities JavaDoc;
12
13 import foxtrot.pumps.ConditionalEventPump;
14 import foxtrot.pumps.QueueEventPump;
15 import foxtrot.pumps.SunJDK14ConditionalEventPump;
16
17 /**
18  * Base class for Foxtrot workers that have synchronous behavior.
19  * @version $Revision: 1.3 $
20  */

21 abstract class AbstractSyncWorker extends AbstractWorker
22 {
23    private EventPump eventPump;
24
25    AbstractSyncWorker()
26    {
27    }
28
29    /**
30     * Returns the EventPump for this worker, creating it if not already set. <br />
31     * Uses a C-style getter method to avoid clash with the static getter method
32     * present in subclasses for API compatibility.
33     * @see #createDefaultEventPump
34     * @see #eventPump(EventPump)
35     */

36    EventPump eventPump()
37    {
38       if (eventPump == null) eventPump(createDefaultEventPump());
39       return eventPump;
40    }
41
42    /**
43     * Sets the EventPump for this worker. <br />
44     * Uses a C-style setter method to avoid clash with the static setter method
45     * present in subclasses for API compatibility.
46     * @throws IllegalArgumentException if eventPump is null
47     * @see #eventPump()
48     */

49    void eventPump(EventPump eventPump)
50    {
51       if (eventPump == null) throw new IllegalArgumentException JavaDoc("EventPump cannot be null");
52       this.eventPump = eventPump;
53       if (debug) System.out.println("[AbstractSyncWorker] Initialized EventPump: " + eventPump);
54    }
55
56    /**
57     * Creates and returns the default EventPump for this worker
58     */

59    EventPump createDefaultEventPump()
60    {
61       if (JREVersion.isJRE14())
62       {
63          // Handles also JDK 5.0
64
return new SunJDK14ConditionalEventPump();
65       }
66       else if (JREVersion.isJRE13())
67       {
68          return new ConditionalEventPump();
69       }
70       else if (JREVersion.isJRE12())
71       {
72          return new QueueEventPump();
73       }
74       else
75       {
76          throw new Error JavaDoc("The current JRE is not supported");
77       }
78    }
79
80    /**
81     * Executes the given Task using the given workerThread and eventPump.
82     * This method blocks (while dequeuing AWT events) until the Task is finished,
83     * either by returning a result or by throwing.
84     */

85    Object JavaDoc post(Task task, WorkerThread workerThread, EventPump eventPump) throws Exception JavaDoc
86    {
87       boolean isEventThread = SwingUtilities.isEventDispatchThread();
88       if (!isEventThread && !workerThread.isWorkerThread())
89       {
90          throw new IllegalStateException JavaDoc("Method post() can be called only from the AWT Event Dispatch Thread or from a worker thread");
91       }
92
93       if (isEventThread)
94       {
95          workerThread.postTask(task);
96
97          // The following line blocks until the task has been executed
98
eventPump.pumpEvents(task);
99       }
100       else
101       {
102          // Executes the Task in this thread
103
workerThread.runTask(task);
104       }
105
106       try
107       {
108          return task.getResultOrThrow();
109       }
110       finally
111       {
112          task.reset();
113       }
114    }
115
116    /**
117     * Executes the given Job using the given workerThread and eventPump.
118     * This method has the same behavior of {@link #post(Task, WorkerThread, EventPump)}
119     */

120    Object JavaDoc post(Job job, WorkerThread workerThread, EventPump eventPump)
121    {
122       try
123       {
124          return post((Task)job, workerThread, eventPump);
125       }
126       catch (RuntimeException JavaDoc x)
127       {
128          throw x;
129       }
130       catch (Exception JavaDoc x)
131       {
132          // If it happens, it's a bug in the compiler
133
if (debug)
134          {
135             System.err.println("[Worker] PANIC: checked exception thrown by a Job !");
136             x.printStackTrace();
137          }
138
139          // I should throw an UndeclaredThrowableException, but that is
140
// available only in JDK 1.3+, so here I use RuntimeException
141
throw new RuntimeException JavaDoc(x.toString());
142       }
143       catch (Error JavaDoc x)
144       {
145          throw x;
146       }
147    }
148 }
149
Popular Tags