1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.firemox.tools;
20
21 import java.awt.Image;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24
25 import net.sf.firemox.stack.StackManager;
26 import net.sf.firemox.token.IdConst;
27 import net.sf.firemox.ui.MagicUIComponents;
28 import net.sf.firemox.ui.TimerGlassPane;
29
30 /***
31 * Internal implementation detail: we happen to use javax.swing.Timer currently,
32 * which sends its timing events to an ActionListener. This internal private
33 * class is our ActionListener that traps these calls and forwards them to the
34 * TimingController.timingEvent() method.
35 *
36 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37 * @since 0.84
38 */
39 public class TimerTarget implements ActionListener {
40
41 private TimerGlassPane timerPanel;
42
43 /***
44 * Create a new instance of this class.
45 *
46 * @param timerPanel
47 * the associated timer panel.
48 */
49 public TimerTarget(TimerGlassPane timerPanel) {
50 super();
51 this.thresholdBusyCpu = Configuration.getInt("timer.hourglass-delay");
52 this.thresholdDeadlock = Configuration.getInt("timer.deadlock-delay");
53 this.thresholdPlayerThinking = Configuration.getInt("timer.waiting-delay");
54 this.timerPanel = timerPanel;
55 try {
56 busyCpuPicture = Picture.loadImage(IdConst.IMAGES_DIR + "hourglass.jpg");
57 playerThinkingPicture = busyCpuPicture;
58 deadlockPicture = Picture.loadImage(IdConst.IMAGES_DIR + "deadlock.gif");
59 byGifPictures = new Image[6];
60 for (int i = byGifPictures.length; i-- > 0;) {
61 byGifPictures[i] = Picture.loadImage(IdConst.IMAGES_DIR + "progress"
62 + i + ".gif");
63 }
64 } catch (Exception e) {
65
66 }
67 }
68
69 public void actionPerformed(ActionEvent e) {
70 deadlyCounter++;
71 if (StackManager.idHandedPlayer == 1) {
72 if (deadlyCounter >= thresholdPlayerThinking) {
73 timerPanel.updatePicture(playerThinkingPicture, null);
74 if (MagicUIComponents.magicForm.getGlassPane() == timerPanel
75 && !timerPanel.isVisible()) {
76 timerPanel.setVisible(true);
77 timerPanel.repaint();
78 }
79 }
80 } else if (StackManager.idHandedPlayer == -1) {
81 if (deadlyCounter >= thresholdBusyCpu
82 && deadlyCounter < thresholdDeadlock) {
83 timerPanel.updatePicture(busyCpuPicture, byGifPictures);
84 } else if (deadlyCounter == thresholdDeadlock) {
85 timerPanel.updatePicture(deadlockPicture, null);
86 }
87 }
88 if (MagicUIComponents.backgroundBtn.isEnabled()) {
89 MagicUIComponents.backgroundBtn.nextPicture();
90 }
91 }
92
93 /***
94 * Reset counter
95 */
96 public void resetCounter() {
97 timerPanel.updatePicture(null, null);
98 deadlyCounter = 0;
99 }
100
101 private int deadlyCounter;
102
103 private int thresholdBusyCpu;
104
105 private int thresholdDeadlock;
106
107 private int thresholdPlayerThinking;
108
109 private Image busyCpuPicture;
110
111 private Image deadlockPicture;
112
113 private Image playerThinkingPicture;
114
115 /***
116 * The progress bar pictures.
117 */
118 public Image[] byGifPictures;
119
120 /***
121 * Save editable string settings of timer.
122 */
123 public void saveSettings() {
124 Configuration.setProperty("timer.hourglass-delay", thresholdBusyCpu);
125 Configuration.setProperty("timer.deadlock-delay", thresholdDeadlock);
126 Configuration.setProperty("timer.waiting-delay", thresholdPlayerThinking);
127 }
128
129 }