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.ui.wizard;
20  
21  import java.awt.Dimension;
22  import java.awt.event.ActionEvent;
23  
24  import javax.swing.BoxLayout;
25  import javax.swing.JFormattedTextField;
26  import javax.swing.JLabel;
27  import javax.swing.JTextField;
28  import javax.swing.WindowConstants;
29  import javax.swing.text.NumberFormatter;
30  
31  import net.sf.firemox.clickable.ability.Ability;
32  import net.sf.firemox.tools.MToolKit;
33  import net.sf.firemox.ui.i18n.LanguageManager;
34  
35  /***
36   * A message box prompting a number to a player. The value may have constraint.
37   * 
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @since 0.82
40   */
41  public class InputNumber extends YesNo {
42  
43  	/***
44  	 * Creates a new instance of InputNumber <br>
45  	 * 
46  	 * @param title
47  	 *          the title of this wizard.
48  	 * @param description
49  	 *          the description appended to the title of this wizard. This content
50  	 *          will be displayed as Html.
51  	 * @param min
52  	 *          the minimal value.
53  	 * @param max
54  	 *          the maximal value.
55  	 * @param defaultValue
56  	 *          the default value.
57  	 */
58  	public InputNumber(String title, String description, int min, int max,
59  			int defaultValue) {
60  		this(null, title, description, min, max, true, defaultValue, true);
61  	}
62  
63  	/***
64  	 * Creates a new instance of MInputIntegerFrame <br>
65  	 * 
66  	 * @param ability
67  	 *          ability to associate to this ability. If this ability has an
68  	 *          associated picture, it will be used instead of given picture.
69  	 *          Ability's name is also used to fill the title. This ability will
70  	 *          be used to restart this wizard in case of Background button is
71  	 *          used.
72  	 * @param description
73  	 *          the description appended to the title of this wizard. This content
74  	 *          will be displayed as Html.
75  	 * @param min
76  	 *          the minimal value.
77  	 * @param max
78  	 *          the maximal value.
79  	 * @param allowCancel
80  	 *          Is the cancel button is allowed.
81  	 * @param defaultValue
82  	 *          the default value.
83  	 * @param permissiveMax
84  	 *          if true, the max value is only an advise. Otherwise, a greater
85  	 *          value can be specified.
86  	 */
87  	public InputNumber(Ability ability, String description, int min, int max,
88  			boolean allowCancel, int defaultValue, boolean permissiveMax) {
89  		this(
90  				ability,
91  				LanguageManager.getString("wiz_input-number.title"),
92  				LanguageManager.getString("wiz_input-number.description") + description,
93  				min, max, allowCancel, defaultValue, permissiveMax);
94  	}
95  
96  	/***
97  	 * Creates a new instance of MInputIntegerFrame <br>
98  	 * 
99  	 * @param ability
100 	 *          ability to associate to this ability. If this ability has an
101 	 *          associated picture, it will be used instead of given picture.
102 	 *          Ability's name is also used to fill the title. This ability will
103 	 *          be used to restart this wizard in case of Background button is
104 	 *          used.
105 	 * @param title
106 	 *          the title of this wizard.
107 	 * @param description
108 	 *          the description appended to the title of this wizard. This content
109 	 *          will be displayed as Html.
110 	 * @param min
111 	 *          the minimal value.
112 	 * @param max
113 	 *          the maximal value.
114 	 * @param allowCancel
115 	 *          Is the cancel button is allowed.
116 	 * @param defaultValue
117 	 *          the default value.
118 	 * @param permissiveMax
119 	 *          if true, the max value is only an advise. Otherwise, a greater
120 	 *          value can be specified.
121 	 */
122 	private InputNumber(Ability ability, String title, String description,
123 			int min, int max, boolean allowCancel, int defaultValue,
124 			boolean permissiveMax) {
125 		super(ability, title, description, "wiz_input-number.gif", null,
126 				LanguageManager.getString("cancel"), 300, 200);
127 		if (allowCancel) {
128 			setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
129 		} else {
130 			setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
131 			this.cancelBtn.setEnabled(false);
132 		}
133 		this.min = min;
134 		this.max = max;
135 		this.permissiveMax = permissiveMax;
136 		this.defaultValue = defaultValue;
137 		gameParamPanel.setLayout(new BoxLayout(gameParamPanel, BoxLayout.X_AXIS));
138 		final NumberFormatter format = new NumberFormatter();
139 		format.setMinimum(min);
140 		if (permissiveMax) {
141 			gameParamPanel.add(new JLabel(LanguageManager.getString("value") + " ["
142 					+ min + " - " + max + "] : "));
143 			format.setMaximum(max);
144 		} else {
145 			gameParamPanel.add(new JLabel(LanguageManager.getString("value") + " ["
146 					+ min + " - " + max + "]+ : "));
147 		}
148 		format.setCommitsOnValidEdit(true);
149 		intText = new JFormattedTextField(format);
150 		intText.setMaximumSize(new Dimension(2000, 20));
151 		intText.setText(String.valueOf(defaultValue));
152 		addCheckValidity(intText);
153 		gameParamPanel.add(intText);
154 	}
155 
156 	@Override
157 	public void actionPerformed(ActionEvent event) {
158 		if (event.getSource() == cancelBtn) {
159 			// set the static integer token to the
160 			indexAnswer = defaultValue;
161 			super.actionPerformed(event);
162 		} else if (event.getSource() == okBtn) {
163 			// set the static integer token
164 			if (checkValidity()) {
165 				super.actionPerformed(event);
166 			} else {
167 				intText.selectAll();
168 			}
169 		} else {
170 			super.actionPerformed(event);
171 		}
172 	}
173 
174 	@Override
175 	protected boolean checkValidity() {
176 		try {
177 			indexAnswer = Integer.parseInt(MToolKit.replaceWhiteSpaces(intText
178 					.getText().replaceAll(",", "").replaceAll("//.", "")));
179 			if (indexAnswer >= min) {
180 				if (indexAnswer <= max) {
181 					okBtn.setEnabled(true);
182 					return true;
183 				}
184 				if (!permissiveMax) {
185 					wizardInfo.resetWarning(LanguageManager
186 							.getString("wiz_input-number.no-strict-bounds"));
187 					okBtn.setEnabled(true);
188 					return true;
189 				}
190 			}
191 		} catch (NumberFormatException e) {
192 			//
193 		}
194 		wizardInfo.resetError(LanguageManager
195 				.getString("wiz_input-number.strict-bounds"));
196 		okBtn.setEnabled(false);
197 		return false;
198 	}
199 
200 	/***
201 	 * The text field containing the integer answer
202 	 */
203 	private JTextField intText;
204 
205 	/***
206 	 * The default value
207 	 */
208 	private final int defaultValue;
209 
210 	/***
211 	 * The minimal accepted value.
212 	 */
213 	private final int min;
214 
215 	/***
216 	 * The maximal accepted value.
217 	 */
218 	private final int max;
219 
220 	/***
221 	 * If true, the max value is only an advise. Otherwise, a greater value can be
222 	 * specified.
223 	 */
224 	private boolean permissiveMax;
225 }