1 package net.suberic.util.thread; 2 import java.util.HashMap ; 3 import javax.swing.Action ; 4 import java.awt.event.*; 5 import java.util.Vector ; 6 7 12 public class ActionThread extends Thread { 13 14 public static int PRIORITY_HIGH = 10; 16 17 public static int PRIORITY_NORMAL = 5; 19 20 public static int PRIORITY_LOW = 0; 22 23 boolean stopMe = false; 24 25 Object runLock = new Object (); 26 27 String mCurrentActionName = ""; 28 29 32 public ActionThread(String threadName) { 33 super(threadName); 34 setPriority(Thread.NORM_PRIORITY); 35 } 36 37 41 public class ActionEventPair { 42 public Action action; 43 public ActionEvent event; 44 public int priority = PRIORITY_NORMAL; 45 46 49 public ActionEventPair(Action newAction, ActionEvent newEvent) { 50 this(newAction, newEvent, PRIORITY_NORMAL); 51 } 52 53 56 public ActionEventPair(Action newAction, ActionEvent newEvent, int newPriority) { 57 action=newAction; 58 event=newEvent; 59 priority = newPriority; 60 } 61 } 62 63 private Vector actionQueue = new Vector (); 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 )pair.action.getValue(Action.SHORT_DESCRIPTION); 76 if (mCurrentActionName == null) { 77 mCurrentActionName = (String )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 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 ie) { 96 sleeping = false; 97 } 98 } 99 } 100 101 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 118 public synchronized void addToQueue(Action action, ActionEvent event) { 119 addToQueue(action, event, PRIORITY_NORMAL); 120 } 121 122 126 public synchronized void addToQueue(Action action, ActionEvent event, int priority) { 127 if (! stopMe) { 128 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 147 public void setStop(boolean newValue) { 148 stopMe = newValue; 149 this.interrupt(); 150 } 151 152 155 public boolean getStopped() { 156 return stopMe; 157 } 158 159 162 public Object getRunLock() { 163 return runLock; 164 } 165 166 169 public int getQueueSize() { 170 return actionQueue.size(); 171 } 172 173 176 public java.util.List getQueue() { 177 return new Vector (actionQueue); 178 } 179 180 183 public String getCurrentActionName() { 184 return mCurrentActionName; 185 } 186 187 190 public void setCurrentActionName(String pCurrentActionName) { 191 mCurrentActionName = pCurrentActionName; 192 } 193 } 194 | Popular Tags |