KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > thread > ActionThread


1 package net.suberic.util.thread;
2 import java.util.HashMap JavaDoc;
3 import javax.swing.Action JavaDoc;
4 import java.awt.event.*;
5 import java.util.Vector JavaDoc;
6
7 /**
8  * This is a Thread which handles ActionEvents. Instead of having the
9  * Action handled by the main thread, it is put into a queue on
10  * this thread, which will then handle the Action.
11  */

12 public class ActionThread extends Thread JavaDoc {
13
14   // indicates high priority
15
public static int PRIORITY_HIGH = 10;
16
17   // indicates normal priority
18
public static int PRIORITY_NORMAL = 5;
19
20   // indicates low priority
21
public static int PRIORITY_LOW = 0;
22
23   boolean stopMe = false;
24
25   Object JavaDoc runLock = new Object JavaDoc();
26
27   String JavaDoc mCurrentActionName = "";
28
29   /**
30    * Creates an ActionThread with the given threadName.
31    */

32   public ActionThread(String JavaDoc threadName) {
33     super(threadName);
34     setPriority(Thread.NORM_PRIORITY);
35   }
36
37   /**
38    * Represents an action/event pair. This also stores the priority of
39    * the event.
40    */

41   public class ActionEventPair {
42     public Action JavaDoc action;
43     public ActionEvent event;
44     public int priority = PRIORITY_NORMAL;
45
46     /**
47      * Creates an NORMAL priority ActionEventPair.
48      */

49     public ActionEventPair(Action JavaDoc newAction, ActionEvent newEvent) {
50       this(newAction, newEvent, PRIORITY_NORMAL);
51     }
52
53     /**
54      * Creates an ActionEventPair with the given priority.
55      */

56     public ActionEventPair(Action JavaDoc newAction, ActionEvent newEvent, int newPriority) {
57       action=newAction;
58       event=newEvent;
59       priority = newPriority;
60     }
61   }
62
63   // the action queue.
64
private Vector JavaDoc actionQueue = new Vector JavaDoc();
65
66   private boolean sleeping;
67
68   public void run() {
69     while(! stopMe ) {
70       sleeping = false;
71       ActionEventPair pair = popQueue();
72       while (pair != null) {
73         try {
74           synchronized(runLock) {
75             mCurrentActionName = (String JavaDoc)pair.action.getValue(Action.SHORT_DESCRIPTION);
76             if (mCurrentActionName == null) {
77               mCurrentActionName = (String JavaDoc)pair.action.getValue(Action.NAME);
78               if (mCurrentActionName == null) {
79                 mCurrentActionName = "Unknown action";
80               }
81             }
82             pair.action.actionPerformed(pair.event);
83           }
84         } catch (Throwable JavaDoc e) {
85           e.printStackTrace();
86         } finally {
87           mCurrentActionName = "";
88         }
89         pair = popQueue();
90       }
91       try {
92         sleeping = true;
93         while (true)
94           Thread.sleep(100000000);
95       } catch (InterruptedException JavaDoc ie) {
96         sleeping = false;
97       }
98     }
99   }
100
101   /**
102    * This returns the top item in the action queue, if one is available.
103    * If not, the method returns null.
104    */

105   public synchronized ActionEventPair popQueue() {
106     if (actionQueue.size() > 0) {
107       return (ActionEventPair)actionQueue.remove(0);
108     }
109     else {
110       return null;
111     }
112   }
113
114   /**
115    * This adds an item to the queue. It also starts up the Thread if it's
116    * not already running.
117    */

118   public synchronized void addToQueue(Action JavaDoc action, ActionEvent event) {
119     addToQueue(action, event, PRIORITY_NORMAL);
120   }
121
122   /**
123    * This adds an item to the queue. It also starts up the Thread if it's
124    * not already running.
125    */

126   public synchronized void addToQueue(Action JavaDoc action, ActionEvent event, int priority) {
127     if (! stopMe) {
128       // see where this should go.
129
int index = 0;
130       boolean found = false;
131       while (! found && index < actionQueue.size()) {
132         ActionEventPair current = (ActionEventPair) actionQueue.elementAt(index);
133         if (current.priority < priority)
134           found = true;
135         else
136           index++;
137       }
138       actionQueue.add(index, new ActionEventPair(action, event, priority));
139       if (sleeping)
140         this.interrupt();
141     }
142   }
143
144   /**
145    * Stops the ActionThread.
146    */

147   public void setStop(boolean newValue) {
148     stopMe = newValue;
149     this.interrupt();
150   }
151
152   /**
153    * Returns whether or not this thread has been stopped.
154    */

155   public boolean getStopped() {
156     return stopMe;
157   }
158
159   /**
160    * Returns the run lock for this thread.
161    */

162   public Object JavaDoc getRunLock() {
163     return runLock;
164   }
165
166   /**
167    * Returns the queue size for this thread.
168    */

169   public int getQueueSize() {
170     return actionQueue.size();
171   }
172
173   /**
174    * Returns a copy of the current action queue.
175    */

176   public java.util.List JavaDoc getQueue() {
177     return new Vector JavaDoc(actionQueue);
178   }
179
180   /**
181    * Returns the name of the current action.
182    */

183   public String JavaDoc getCurrentActionName() {
184     return mCurrentActionName;
185   }
186
187   /**
188    * Sets the name of the current action.
189    */

190   public void setCurrentActionName(String JavaDoc pCurrentActionName) {
191     mCurrentActionName = pCurrentActionName;
192   }
193 }
194
Popular Tags