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.action;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import javax.swing.JOptionPane;
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.event.context.ContextEventListener;
31  import net.sf.firemox.expression.IntValue;
32  import net.sf.firemox.expression.ListExpression;
33  import net.sf.firemox.network.message.CoreMessageType;
34  import net.sf.firemox.stack.StackManager;
35  import net.sf.firemox.tools.Log;
36  import net.sf.firemox.ui.i18n.LanguageManagerMDB;
37  
38  /***
39   * Prompt to a player the zone to select.
40   * 
41   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
42   * @since 0.92
43   */
44  class InputZone extends MessagingAction {
45  
46  	/***
47  	 * Create an instance of InputZone by reading a file Offset's file must
48  	 * pointing on the first byte of this action. <br>
49  	 * <ul>
50  	 * Structure of InputStream : Data[size]
51  	 * <li>controller (idToken) [2]</li>
52  	 * <li>text to display +'/0' [...]</li>
53  	 * <li>register modification action [...]</li>
54  	 * <li>allowed zones : ListExpression [...]</li>
55  	 * </ul>
56  	 * 
57  	 * @param inputFile
58  	 *          file containing this action
59  	 * @throws IOException
60  	 *           if error occurred during the reading process from the specified
61  	 *           input stream
62  	 */
63  	InputZone(InputStream inputFile) throws IOException {
64  		super(inputFile);
65  		registerModifier = (ModifyRegister) ActionFactory.readAction(inputFile,
66  				null);
67  		allowedZones = new ListExpression(inputFile);
68  	}
69  
70  	@Override
71  	public final Actiontype getIdAction() {
72  		return Actiontype.INPUT_ZONE;
73  	}
74  
75  	@Override
76  	public boolean play(ContextEventListener context, Ability ability) {
77  		final Player controller = (Player) this.controller.getTargetable(ability,
78  				context, null);
79  		controller.setHandedPlayer();
80  		if (controller.isYou()) {
81  			Log.debug("Opponent is waiting for our answer");
82  			int[] zones = allowedZones.getList(ability, ability.getCard(), context);
83  			replayAction(context, ability,
84  					new net.sf.firemox.ui.wizard.InputZone(ability, text, zones));
85  		} else {
86  			Log.debug("Waiting for opponent's answer");
87  		}
88  		return false;
89  	}
90  
91  	@Override
92  	public final CoreMessageType getMessagingActionId() {
93  		return CoreMessageType.COLOR_ANSWER;
94  	}
95  
96  	@Override
97  	protected void setAnswer(int optionAnswer, int indexAnswer,
98  			ContextEventListener context, Ability ability,
99  			ActionContextWrapper actionContext) {
100 		if (optionAnswer == JOptionPane.NO_OPTION) {
101 			((IntValue) registerModifier.valueExpr).value = 0;
102 		} else {
103 			((IntValue) registerModifier.valueExpr).value = indexAnswer;
104 		}
105 		if (actionContext != null) {
106 			actionContext.actionContext = new Int(
107 					((IntValue) registerModifier.valueExpr).value);
108 		}
109 		if (registerModifier.play(context, ability)) {
110 			StackManager.resolveStack();
111 		}
112 	}
113 
114 	@Override
115 	public String toString(Ability ability) {
116 		return LanguageManagerMDB.getString("choosezone.action");
117 	}
118 
119 	@Override
120 	public final boolean replay(ActionContextWrapper actionContext,
121 			ContextEventListener context, Ability ability) {
122 		((IntValue) registerModifier.valueExpr).value = ((Int) actionContext.actionContext)
123 				.getInt();
124 		return registerModifier.play(context, ability);
125 	}
126 
127 	/***
128 	 * The register modifiaction action using the answer as input.
129 	 */
130 	private final ModifyRegister registerModifier;
131 
132 	/***
133 	 * The allowed zone
134 	 */
135 	private final ListExpression allowedZones;
136 
137 }