1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sf.magicproject.clickable.targetable.card;
23
24 import java.awt.event.MouseListener;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import net.sf.magicproject.action.BackgroundMessaging;
29 import net.sf.magicproject.clickable.ability.Ability;
30 import net.sf.magicproject.event.context.ContextEventListener;
31 import net.sf.magicproject.network.ConnectionManager;
32 import net.sf.magicproject.network.IdMessages;
33 import net.sf.magicproject.stack.StackManager;
34 import net.sf.magicproject.tools.Log;
35 import net.sf.magicproject.ui.i18n.LanguageManager;
36 import net.sf.magicproject.ui.wizard.Wizard;
37
38 /***
39 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40 * @since 0.86
41 */
42 public class TriggeredCardChoice extends TriggeredCard implements
43 MouseListener, BackgroundMessaging {
44
45 /***
46 * @param triggeredAbility
47 * the triggered ability associated to this card
48 * @param context
49 * the context of the associated triggered ability
50 * @param abilityID
51 * is the ability's Id making this triggered ability to be created.
52 */
53 public TriggeredCardChoice(Ability triggeredAbility,
54 ContextEventListener context, long abilityID) {
55 super(triggeredAbility, context, abilityID);
56 }
57
58 /***
59 * Add an alternative ability to the associated triggered ability
60 *
61 * @param ability
62 * the ability to add to the choice list.
63 * @param context
64 * the associated context of added ability.
65 */
66 public void addChoice(Ability ability, ContextEventListener context) {
67 if (either == null) {
68 either = new ArrayList<Ability>();
69 contexts = new ArrayList<ContextEventListener>();
70 }
71 either.add(ability);
72 }
73
74 @Override
75 public boolean newSpell() {
76 if (either == null) {
77 return super.newSpell();
78 }
79
80 final List<String> actionListStr = getActionList();
81 StackManager.spellController.setActivePlayer();
82 if (StackManager.spellController.isYou()) {
83 replayAction(context, triggeredAbility,
84 new net.sf.magicproject.ui.wizard.Choice(context, triggeredAbility,
85 this, false, actionListStr));
86 } else {
87
88 waitingTriggered = this;
89 }
90 return false;
91 }
92
93 /***
94 * Replay the current action as it was when it has been suspended.
95 *
96 * @param context
97 * is the context attached to this action.
98 * @param ability
99 * is the ability owning this test. The card component of this
100 * ability should correspond to the card owning this test too.
101 * @param wizard
102 * the hidden wizard frame
103 */
104 public void replayAction(ContextEventListener context, Ability ability,
105 Wizard wizard) {
106 wizard.setVisible(true);
107 if (Wizard.optionAnswer != Wizard.BACKGROUND_OPTION) {
108
109 final int res = Wizard.optionAnswer * 256 + Wizard.indexAnswer;
110 Log.debug("answer was " + res);
111 ConnectionManager.sendToOpponent(IdMessages.TRIGGERED_CARD_CHOICE,
112 Wizard.indexAnswer);
113 setAnswer(Wizard.indexAnswer);
114 }
115 }
116
117 private List<String> getActionList() {
118 final List<String> res = new ArrayList<String>();
119 res.add(triggeredAbility.toHtmlString(context));
120 for (int i = 0; i < either.size(); i++) {
121 res.add(either.get(i).toHtmlString(contexts.get(i)));
122 }
123 return res;
124 }
125
126 /***
127 * The value is piped to the 'modifyregister' action.
128 *
129 * @param idAnswer
130 * the answer value.
131 */
132 protected void setAnswer(int idAnswer) {
133 if (idAnswer > 0) {
134 triggeredAbility = either.get(idAnswer - 1);
135 context = contexts.get(idAnswer - 1);
136 }
137 if (StackManager.newSpell(this)) {
138 StackManager.resolveStack();
139 }
140
141 }
142
143 /***
144 * The callback method when opponent as made the triggered card choice.
145 *
146 * @param idAnswer
147 * the triggered card id.
148 */
149 public static void finishedMessage(int idAnswer) {
150 waitingTriggered.setAnswer(idAnswer);
151 }
152
153 @Override
154 public String getTooltipString() {
155 StringBuilder toolTip = new StringBuilder(300);
156
157
158 toolTip.append("<br><b>");
159 toolTip.append(LanguageManager.getString("source"));
160 toolTip.append(": </b>");
161 if (visibility.isVisibleForYou()) {
162 toolTip.append(database.getLocalName());
163 toolTip.append("<br><b>");
164 toolTip.append(LanguageManager.getString("triggeredability"));
165 toolTip.append(": </b>");
166 toolTip.append(triggeredAbility.toHtmlString(context));
167 if (either != null) {
168 for (int i = 0; i < either.size(); i++) {
169 toolTip.append(either.get(i).toHtmlString(context));
170 }
171 }
172 toolTip.append(CardFactory.ttSource);
173 toolTip.append(triggeredAbility.getCard().toString());
174
175
176 if (database.getRulesCredit() != null) {
177 toolTip.append(CardFactory.ttRulesAuthor);
178 toolTip.append(database.getRulesCredit());
179 }
180 } else {
181 toolTip.append("??");
182 }
183 toolTip.append("</html>");
184 return toolTip.toString();
185 }
186
187 @Override
188 public String toString() {
189 if (either == null)
190 return new StringBuilder(super.toString()).append("(++ choices ++ :")
191 .append(either.size()).append(")").toString();
192 return super.toString() + "(++ choices ++ : 0)";
193 }
194
195 /***
196 * List of alternatives of triggered abilities. Is null is there is no choice.
197 */
198 private List<Ability> either;
199
200 /***
201 * List of contextes associated to alternatives of triggered abilities. Is
202 * null is there is no choice.
203 */
204 private List<ContextEventListener> contexts;
205
206 private static TriggeredCardChoice waitingTriggered;
207 }