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.ui.component;
20  
21  import java.awt.Graphics;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  
25  import javax.swing.ImageIcon;
26  import javax.swing.JButton;
27  
28  import net.sf.magicproject.action.BackgroundMessaging;
29  import net.sf.magicproject.clickable.ability.Ability;
30  import net.sf.magicproject.event.context.ContextEventListener;
31  import net.sf.magicproject.ui.i18n.LanguageManager;
32  import net.sf.magicproject.ui.wizard.Wizard;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   * @since 0.90
37   */
38  public class MessageButton extends JButton implements ActionListener {
39  
40  	/***
41  	 * Create a new instance of this class.
42  	 * 
43  	 * @param enabledPicture1
44  	 * @param enabledPicture2
45  	 */
46  	public MessageButton(ImageIcon enabledPicture1, ImageIcon enabledPicture2) {
47  		this.enabledPicture1 = enabledPicture1;
48  		this.enabledPicture2 = enabledPicture2;
49  		addActionListener(this);
50  	}
51  
52  	/***
53  	 * Show the button enabling to restore the background wizard.
54  	 * 
55  	 * @param context
56  	 *          the context of wizard put in background.
57  	 * @param ability
58  	 *          the ability of wizard put in background.
59  	 * @param action
60  	 *          the action of wizard put in background.
61  	 * @param wizard
62  	 *          the wizard put in background.
63  	 */
64  	public void startButton(ContextEventListener context, Ability ability,
65  			BackgroundMessaging action, Wizard wizard) {
66  		this.context = context;
67  		this.ability = ability;
68  		this.action = action;
69  		this.wizard = wizard;
70  		state = 0;
71  		setVisible(true);
72  		setToolTipText(LanguageManager.getString("wiz_recallmsgTTenabled"));
73  	}
74  
75  	@Override
76  	public void update(Graphics g) {
77  		paint(g);
78  	}
79  
80  	/***
81  	 * Hidde the button put in background.
82  	 */
83  	public void stopButton() {
84  		state = 0;
85  		setVisible(false);
86  		setToolTipText(LanguageManager.getString("wiz_recallmsgTTdisabled"));
87  	}
88  
89  	public void actionPerformed(ActionEvent e) {
90  		// display hidden message box and disable this button
91  		stopButton();
92  		action.replayAction(context, ability, wizard);
93  	}
94  
95  	/***
96  	 * Display the next picture of this button.
97  	 */
98  	public void nextPicture() {
99  		state = ++state % 2;
100 		if (isVisible()) {
101 			if (state == 0) {
102 				setIcon(enabledPicture1);
103 			} else {
104 				setIcon(enabledPicture2);
105 			}
106 		}
107 	}
108 
109 	private ImageIcon enabledPicture1;
110 
111 	private ImageIcon enabledPicture2;
112 
113 	private int state;
114 
115 	private ContextEventListener context;
116 
117 	private Ability ability;
118 
119 	private BackgroundMessaging action;
120 
121 	private Wizard wizard;
122 }