View Javadoc

1   /*
2    * Created on 2005/0824
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  		// There is at least two abilities, show the chooser wizard
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  			// opponent waits for our answer
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 			// valid answer
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 		// html header and card name
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 			// credits
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 }