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 InputProperty extends MessagingAction {
45  
46  	/***
47  	 * Create an instance of InputProperty 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 properties : 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  	InputProperty(InputStream inputFile) throws IOException {
64  		super(inputFile);
65  		registerModifier = (ModifyRegister) ActionFactory.readAction(inputFile,
66  				null);
67  		allowedProperties = 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[] properties = allowedProperties.getList(ability, ability.getCard(),
83  					context);
84  			replayAction(context, ability,
85  					new net.sf.magicproject.ui.wizard.InputProperty(context, ability,
86  							this, text, properties));
87  		} else {
88  			Log.debug("Waiting for opponent's answer");
89  		}
90  		return false;
91  	}
92  
93  	@Override
94  	public final int getMessagingActionId() {
95  		return IdMessages.COLOR_ANSWER;
96  	}
97  
98  	@Override
99  	protected void setAnswer(int optionAnswer, int indexAnswer,
100 			ContextEventListener context, Ability ability,
101 			ActionContextWrapper actionContext) {
102 		if (optionAnswer == JOptionPane.NO_OPTION) {
103 			((IntValue) registerModifier.valueExpr).value = 0;
104 		} else {
105 			((IntValue) registerModifier.valueExpr).value = indexAnswer;
106 		}
107 		if (actionContext != null) {
108 			actionContext.actionContext = new Int(
109 					((IntValue) registerModifier.valueExpr).value);
110 		}
111 		if (registerModifier.play(context, ability)) {
112 			StackManager.resolveStack();
113 		}
114 	}
115 
116 	@Override
117 	public String toString(Ability ability) {
118 		return LanguageManagerMDB.getString("choosezone.action");
119 	}
120 
121 	@Override
122 	public final boolean replay(ActionContextWrapper actionContext,
123 			ContextEventListener context, Ability ability) {
124 		((IntValue) registerModifier.valueExpr).value = ((Int) actionContext.actionContext)
125 				.getInt();
126 		return registerModifier.play(context, ability);
127 	}
128 
129 	/***
130 	 * The register modifiaction action using the answer as input.
131 	 */
132 	private final ModifyRegister registerModifier;
133 
134 	/***
135 	 * The allowed properties
136 	 */
137 	private final ListExpression allowedProperties;
138 
139 }