View Javadoc

1   /*
2    *   Firemox is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  			// IGNORING
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 }