1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }