View Javadoc

1   /*
2    * Created on Dec 31, 2004
3    * 
4    *   Firemox is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package net.sf.firemox.action;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import net.sf.firemox.action.context.ActionContextWrapper;
27  import net.sf.firemox.action.context.Int;
28  import net.sf.firemox.clickable.ability.Ability;
29  import net.sf.firemox.clickable.target.player.Player;
30  import net.sf.firemox.database.propertyconfig.PropertyProxyConfig;
31  import net.sf.firemox.event.context.ContextEventListener;
32  import net.sf.firemox.expression.Expression;
33  import net.sf.firemox.expression.ExpressionFactory;
34  import net.sf.firemox.expression.IntValue;
35  import net.sf.firemox.network.message.CoreMessageType;
36  import net.sf.firemox.stack.StackManager;
37  import net.sf.firemox.tools.Log;
38  import net.sf.firemox.tools.MToolKit;
39  
40  /***
41   * Display an input box waiting for an integer answer. It's possible to use the
42   * multi-language ability for text if the string start with the '$' character.
43   * The entered integer is used as 'value' for 'modifyregister'.
44   * 
45   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
46   * @since 0.82
47   * @since 0.91 use 'default' value and 'stric-max' constraint.
48   */
49  class InputNumber extends MessagingAction {
50  
51  	/***
52  	 * Create an instance of InputInteger by reading a file Offset's file must
53  	 * pointing on the first byte of this action. <br>
54  	 * <ul>
55  	 * Structure of InputStream : Data[size]
56  	 * <li>[super]</li>
57  	 * <li>modifier [ModifyRegister]</li>
58  	 * <li>min expression [...]</li>
59  	 * <li>max expression [...]</li>
60  	 * <li>default expression [...]</li>
61  	 * <li>strict-max boolean 1/0 [1]</li>
62  	 * </ul>
63  	 * 
64  	 * @param inputFile
65  	 *          file containing this action
66  	 * @throws IOException
67  	 *           if error occurred during the reading process from the specified
68  	 *           input stream
69  	 */
70  	InputNumber(InputStream inputFile) throws IOException {
71  		super(inputFile);
72  		registerModifier = (ModifyRegister) ActionFactory.readAction(inputFile,
73  				null);
74  		minExpr = ExpressionFactory.readNextExpression(inputFile);
75  		maxExpr = ExpressionFactory.readNextExpression(inputFile);
76  		defaultExpr = ExpressionFactory.readNextExpression(inputFile);
77  		strictMax = inputFile.read() != 0;
78  	}
79  
80  	@Override
81  	public final Actiontype getIdAction() {
82  		return Actiontype.INPUT_NUMBER;
83  	}
84  
85  	@Override
86  	public boolean play(ContextEventListener context, Ability ability) {
87  		final Player controller = (Player) this.controller.getTargetable(ability,
88  				context, null);
89  		controller.setHandedPlayer();
90  		if (controller.isYou()) {
91  			Log.debug("Opponent is waiting for our answer");
92  			PropertyProxyConfig.values.put("%maximum", maxExpr);
93  			PropertyProxyConfig.values.put("%minimum", minExpr);
94  			replayAction(context, ability,
95  					new net.sf.firemox.ui.wizard.InputNumber(ability, text, minExpr
96  							.getValue(ability, ability.getCard(), context), maxExpr.getValue(
97  							ability, ability.getCard(), context), false, defaultExpr
98  							.getValue(ability, ability.getCard(), context), strictMax));
99  		} else {
100 			Log.debug("Waiting for opponent's answer");
101 		}
102 		return false;
103 	}
104 
105 	@Override
106 	public String toString(Ability ability) {
107 		// TODO Translate 'integer input box'
108 		return "integer inputbox";
109 	}
110 
111 	@Override
112 	public String toHtmlString(Ability ability, ContextEventListener context) {
113 		return "<img src='file:///"
114 				+ MToolKit.getTbsHtmlPicture("actions/input-number.gif") + "'>&nbsp;";
115 	}
116 
117 	@Override
118 	protected final CoreMessageType getMessagingActionId() {
119 		return CoreMessageType.INTEGER_ANSWER;
120 	}
121 
122 	@Override
123 	protected void setAnswer(int optionAnswer, int indexAnswer,
124 			ContextEventListener context, Ability ability,
125 			ActionContextWrapper actionContext) {
126 		((IntValue) registerModifier.valueExpr).value = indexAnswer;
127 		if (actionContext != null) {
128 			actionContext.actionContext = new Int(
129 					((IntValue) registerModifier.valueExpr).value);
130 		}
131 		if (registerModifier.play(context, ability)) {
132 			StackManager.resolveStack();
133 		}
134 	}
135 
136 	@Override
137 	public final boolean replay(ActionContextWrapper actionContext,
138 			ContextEventListener context, Ability ability) {
139 		((IntValue) registerModifier.valueExpr).value = ((Int) actionContext.actionContext)
140 				.getInt();
141 		return registerModifier.play(context, ability);
142 	}
143 
144 	/***
145 	 * The register modification action using the answer as input.
146 	 */
147 	private final ModifyRegister registerModifier;
148 
149 	/***
150 	 * The maximal value accepted for the displayed wizard.
151 	 */
152 	private final Expression maxExpr;
153 
154 	/***
155 	 * The minimal value accepted for the displayed wizard.
156 	 */
157 	private final Expression minExpr;
158 
159 	/***
160 	 * The default value displayed in the diaLog. By default, the maximal allowed
161 	 * value is used (%max).
162 	 */
163 	private final Expression defaultExpr;
164 
165 	/***
166 	 * Allow or not to enter a value exceeding the maximal value. If false
167 	 * (default), the user can enter a higher value with a warning displayed in
168 	 * the diaLog.
169 	 */
170 	private final boolean strictMax;
171 }