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.deckbuilder;
20  
21  import java.awt.Image;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  
25  import net.sf.firemox.DeckBuilder;
26  import net.sf.firemox.token.IdConst;
27  import net.sf.firemox.tools.Configuration;
28  import net.sf.firemox.tools.Picture;
29  import net.sf.firemox.ui.TimerGlassPane;
30  
31  /***
32   * Internal implementation detail: we happen to use javax.swing.Timer currently,
33   * which sends its timing events to an ActionListener. This internal private
34   * class is our ActionListener that traps these calls and forwards them to the
35   * TimingController.timingEvent() method.
36   * 
37   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
38   * @since 0.95
39   */
40  public class CardLoader implements ActionListener {
41  
42  	private int deadlyCounter;
43  
44  	private final int thresholdCpu;
45  
46  	private Image playerThinkingPicture;
47  
48  	/***
49  	 * The progress bar pictures.
50  	 */
51  	private final Image[] byGifPictures;
52  
53  	private TimerGlassPane timerPanel;
54  
55  	/***
56  	 * Create a new instance of this class.
57  	 * 
58  	 * @param timerPanel
59  	 *          the associated timer panel.
60  	 */
61  	public CardLoader(TimerGlassPane timerPanel) {
62  		super();
63  		this.thresholdCpu = Configuration.getInt("timer.waiting-delay");
64  		this.byGifPictures = new Image[6];
65  		this.timerPanel = timerPanel;
66  		try {
67  			playerThinkingPicture = Picture.loadImage(IdConst.IMAGES_DIR
68  					+ "hourglass.jpg");
69  			for (int i = byGifPictures.length; i-- > 0;) {
70  				byGifPictures[i] = Picture.loadImage(IdConst.IMAGES_DIR + "progress"
71  						+ i + ".gif");
72  			}
73  			timerPanel.updatePicture(playerThinkingPicture, byGifPictures);
74  		} catch (Exception e) {
75  			// IGNORING
76  		}
77  	}
78  
79  	public void actionPerformed(ActionEvent e) {
80  		deadlyCounter++;
81  		if (deadlyCounter >= thresholdCpu) {
82  			timerPanel.updatePicture(playerThinkingPicture, byGifPictures);
83  			if (DeckBuilder.form.getGlassPane() == timerPanel
84  					&& !timerPanel.isVisible()) {
85  				timerPanel.setVisible(true);
86  				timerPanel.repaint();
87  			}
88  		}
89  	}
90  
91  	/***
92  	 * Reset counter
93  	 */
94  	public void resetCounter() {
95  		timerPanel.updatePicture(null, null);
96  		deadlyCounter = 0;
97  	}
98  
99  }