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