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.action;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import javax.swing.JOptionPane;
25  
26  import net.sf.magicproject.action.context.ActionContextWrapper;
27  import net.sf.magicproject.action.context.Int;
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.clickable.targetable.player.Player;
30  import net.sf.magicproject.event.context.ContextEventListener;
31  import net.sf.magicproject.expression.IntValue;
32  import net.sf.magicproject.expression.ListExpression;
33  import net.sf.magicproject.network.IdMessages;
34  import net.sf.magicproject.stack.StackManager;
35  import net.sf.magicproject.tools.Log;
36  import net.sf.magicproject.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.magicproject.ui.wizard.InputZone(context, ability, this,
85  							text, zones));
86  		} else {
87  			Log.debug("Waiting for opponent's answer");
88  		}
89  		return false;
90  	}
91  
92  	@Override
93  	public final int getMessagingActionId() {
94  		return IdMessages.COLOR_ANSWER;
95  	}
96  
97  	@Override
98  	protected void setAnswer(int optionAnswer, int indexAnswer,
99  			ContextEventListener context, Ability ability,
100 			ActionContextWrapper actionContext) {
101 		if (optionAnswer == JOptionPane.NO_OPTION) {
102 			((IntValue) registerModifier.valueExpr).value = 0;
103 		} else {
104 			((IntValue) registerModifier.valueExpr).value = indexAnswer;
105 		}
106 		if (actionContext != null) {
107 			actionContext.actionContext = new Int(
108 					((IntValue) registerModifier.valueExpr).value);
109 		}
110 		if (registerModifier.play(context, ability)) {
111 			StackManager.resolveStack();
112 		}
113 	}
114 
115 	@Override
116 	public String toString(Ability ability) {
117 		return LanguageManagerMDB.getString("choosezone.action");
118 	}
119 
120 	@Override
121 	public final boolean replay(ActionContextWrapper actionContext,
122 			ContextEventListener context, Ability ability) {
123 		((IntValue) registerModifier.valueExpr).value = ((Int) actionContext.actionContext)
124 				.getInt();
125 		return registerModifier.play(context, ability);
126 	}
127 
128 	/***
129 	 * The register modifiaction action using the answer as input.
130 	 */
131 	private final ModifyRegister registerModifier;
132 
133 	/***
134 	 * The allowed zone
135 	 */
136 	private final ListExpression allowedZones;
137 
138 }