View Javadoc

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