View Javadoc

1   /* 
2    * WaitChoosenActionChoice.java
3    * Created on 26 févr. 2004
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package net.sf.magicproject.action;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import javax.swing.JTabbedPane;
28  
29  import net.sf.magicproject.action.listener.WaitingAbility;
30  import net.sf.magicproject.action.listener.WaitingAction;
31  import net.sf.magicproject.clickable.ability.Ability;
32  import net.sf.magicproject.clickable.action.JChoosenAction;
33  import net.sf.magicproject.clickable.targetable.card.MCard;
34  import net.sf.magicproject.clickable.targetable.player.Player;
35  import net.sf.magicproject.event.CanICast;
36  import net.sf.magicproject.stack.ActivatedChoiceList;
37  import net.sf.magicproject.stack.StackManager;
38  import net.sf.magicproject.ui.MagicUIComponents;
39  import net.sf.magicproject.zone.MZone;
40  
41  /***
42   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
43   * @since 0.86
44   */
45  public final class WaitChoosenActionChoice extends MAction implements
46  		WaitingAction, WaitingAbility {
47  
48  	/***
49  	 * Create a new instance of WaitTriggeredBufferChoice
50  	 */
51  	private WaitChoosenActionChoice() {
52  		super();
53  	}
54  
55  	/***
56  	 * This action activate the panel containing the ChoosenAction, highlights the
57  	 * playable mana-source abilities, and the the spell's controller as the
58  	 * handed player.
59  	 * 
60  	 * @param controller
61  	 *          is the player waiting the action choice.
62  	 * @TODO enable advaned abilities to be played
63  	 */
64  	protected void play(Player controller) {
65  		final List<Ability> res = new ArrayList<Ability>();
66  		final List<Ability> advRes = new ArrayList<Ability>();
67  		CanICast.dispatchManaAbilityEvent(controller.idPlayer, res);
68  		finished();
69  		if (!res.isEmpty()) { // TODO Enable advanced too : || !advRes.isEmpty()){
70  			activChoiceList = new ActivatedChoiceList(res, advRes);
71  			res.clear();
72  			advRes.clear();
73  			zoneList = activChoiceList.highLight();
74  
75  			// bring to front the ChoosenAction panel
76  			((JTabbedPane) MagicUIComponents.choosenCostPanel.getParent())
77  					.setSelectedComponent(MagicUIComponents.choosenCostPanel);
78  		}
79  		StackManager.enableAbort();
80  		controller.setActivePlayer();
81  	}
82  
83  	public boolean clickOn(JChoosenAction action) {
84  		// TODO Modifying choosenAction order is not yet implemented
85  		// return true;
86  		return false;
87  	}
88  
89  	public boolean clickOn(Ability ability) {
90  		return StackManager.idHandedPlayer == 0;
91  	}
92  
93  	public boolean succeedClickOn(JChoosenAction action) {
94  		finished();
95  		return action.setSelected();
96  	}
97  
98  	public boolean succeedClickOn(Ability ability) {
99  		finished();
100 		StackManager.newSpell(ability, ability.isMatching());
101 		return true;
102 	}
103 
104 	@Override
105 	public Actiontype getIdAction() {
106 		return Actiontype.WAIT_ACTIVATED_CHOICE;
107 	}
108 
109 	public boolean manualSkip() {
110 		finished();
111 		// activate automatically the first uncompleted ChoosenAction
112 		return MagicUIComponents.choosenCostPanel.playFirstUncompleted();
113 	}
114 
115 	public void finished() {
116 		if (activChoiceList != null) {
117 			activChoiceList.disHighLight();
118 			activChoiceList.clear();
119 			activChoiceList = null;
120 		}
121 		if (zoneList != null) {
122 			for (MZone zone : zoneList) {
123 				zone.disHighLight();
124 			}
125 			zoneList.clear();
126 			zoneList = null;
127 			StackManager.PLAYERS[0].disHighLight();
128 			StackManager.PLAYERS[1].disHighLight();
129 		}
130 		StackManager.disableAbort();
131 	}
132 
133 	public List<Ability> abilitiesOf(MCard card) {
134 		return activChoiceList == null ? null : activChoiceList.abilitiesOf(card);
135 	}
136 
137 	public List<Ability> advancedAbilitiesOf(MCard card) {
138 		return activChoiceList == null ? null : activChoiceList
139 				.advancedAbilitiesOf(card);
140 	}
141 
142 	@Override
143 	public String toString(Ability ability) {
144 		return "Wait mana source choice";
145 	}
146 
147 	/***
148 	 * Return the unique instance of this class.
149 	 * 
150 	 * @return the unique instance of this class.
151 	 */
152 	public static WaitChoosenActionChoice getInstance() {
153 		if (instance == null)
154 			instance = new WaitChoosenActionChoice();
155 		return instance;
156 	}
157 
158 	/***
159 	 * This is the list of zone concerned by highlighted cards
160 	 */
161 	private List<MZone> zoneList;
162 
163 	/***
164 	 * the current list of choices of active player
165 	 */
166 	private ActivatedChoiceList activChoiceList;
167 
168 	/***
169 	 * The unique instance of this class
170 	 */
171 	private static WaitChoosenActionChoice instance;
172 
173 }