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   * Created on 14 févr. 2004
20   */
21  package net.sf.magicproject.clickable.targetable.card;
22  
23  import java.awt.Image;
24  import java.awt.event.MouseEvent;
25  import java.util.List;
26  
27  import net.sf.magicproject.clickable.ability.Ability;
28  import net.sf.magicproject.clickable.targetable.Targetable;
29  import net.sf.magicproject.clickable.targetable.player.Player;
30  import net.sf.magicproject.database.DatabaseCard;
31  import net.sf.magicproject.database.DatabaseFactory;
32  import net.sf.magicproject.management.MonitorListener;
33  import net.sf.magicproject.stack.StackManager;
34  import net.sf.magicproject.stack.TargetHelper;
35  import net.sf.magicproject.test.Test;
36  import net.sf.magicproject.token.Visibility;
37  import net.sf.magicproject.ui.Reversable;
38  import net.sf.magicproject.ui.i18n.LanguageManager;
39  
40  /***
41   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
42   * @since 0.1
43   */
44  public abstract class AbstractCard extends Targetable implements
45  		MonitorListener {
46  
47  	/***
48  	 * Creates a new instance of AbstractCard <br>
49  	 */
50  	protected AbstractCard() {
51  		super();
52  	}
53  
54  	/***
55  	 * Creates a new instance of AbstractCard <br>
56  	 * 
57  	 * @param databaseCard
58  	 */
59  	protected AbstractCard(DatabaseCard databaseCard) {
60  		this();
61  		this.database = databaseCard;
62  	}
63  
64  	/***
65  	 * return true if this targetable is a card
66  	 * 
67  	 * @return true if this targetable is a card
68  	 */
69  	@Override
70  	public boolean isCard() {
71  		return true;
72  	}
73  
74  	/***
75  	 * Checks all cards corresponding to the specified constraints
76  	 * 
77  	 * @param test
78  	 *          applied to count valid cards
79  	 * @param ability
80  	 *          is the ability owning this test. The card component of this
81  	 *          ability should correspond to the card owning this test too.
82  	 * @param canBePreempted
83  	 *          <code>true</code> if the valid targets can be derterminated
84  	 *          before runtime.
85  	 * @return amount of card matching with the specified test
86  	 */
87  	public abstract int countAllCardsOf(Test test, Ability ability,
88  			boolean canBePreempted);
89  
90  	/***
91  	 * Checks all cards corresponding to this constraints
92  	 * 
93  	 * @param test
94  	 *          applied to count valid cards
95  	 * @param list
96  	 *          the list containing the founded cards
97  	 * @param ability
98  	 *          is the ability owning this test. The card component of this
99  	 *          ability should correspond to the card owning this test too.
100 	 */
101 	public abstract void checkAllCardsOf(Test test, List<Targetable> list,
102 			Ability ability);
103 
104 	/***
105 	 * Move this card to a new place tapped or not under the control of a player
106 	 * 
107 	 * @param newIdPlace
108 	 *          the new place for this card
109 	 * @param newController
110 	 *          new controler of this card. If null, the controler is the owner.
111 	 * @param newIsTapped
112 	 *          dertermines if this card will come tapped or not
113 	 * @param idPosition
114 	 *          from IdPositions
115 	 */
116 	public void moveCard(int newIdPlace, Player newController,
117 			boolean newIsTapped, int idPosition) {
118 		throw new InternalError("shouldn't be called");
119 	}
120 
121 	@Override
122 	public void mouseExited(MouseEvent e) {
123 		TargetHelper.getInstance().removeTargetedBy();
124 	}
125 
126 	/***
127 	 * Return HTML tooltip string of this card.
128 	 * 
129 	 * @return HTML tooltip string of this card.
130 	 */
131 	public abstract String getTooltipString();
132 
133 	/***
134 	 * Reverse this card if controller is not you
135 	 */
136 	public void reverseAsNeeded() {
137 		reverse(needReverse());
138 	}
139 
140 	/***
141 	 * Is this card need to be reversed to suit to the player view.
142 	 * 
143 	 * @return true if this card need to be reversed to suit to the player view.
144 	 */
145 	public boolean needReverse() {
146 		if (getParent() != null && getParent() instanceof MCard) {
147 			return !StackManager.isYou(((MCard) getParent()).controller);
148 		}
149 		return !StackManager.isYou(controller);
150 	}
151 
152 	/***
153 	 * Reverse this card if the specified parameter is true
154 	 * 
155 	 * @param reversed
156 	 *          if true the card will be tured as if your controled controls it
157 	 */
158 	public void reverse(boolean reversed) {
159 		this.reversed = reversed;
160 		for (int i = getComponentCount(); i-- > 1;) {
161 			((Reversable) getComponent(i)).reverse(reversed);
162 		}
163 	}
164 
165 	/***
166 	 * Return the card's picture
167 	 * 
168 	 * @return the card's picture
169 	 */
170 	public Image image() {
171 		return database.getImage(this);
172 	}
173 
174 	/***
175 	 * Return the scaled card's picture
176 	 * 
177 	 * @return the scaled card's picture
178 	 */
179 	public Image scaledImage() {
180 		return database.getScaledImage(this);
181 	}
182 
183 	public final void notifyChange() {
184 		ui.updateMUI();
185 		repaint();
186 	}
187 
188 	/***
189 	 * Return the card's picture as it would be displayed in the preview panel.
190 	 * May be different from the displayed picture.
191 	 * 
192 	 * @return the card's picture as it would be displayed in the preview panel.
193 	 */
194 	public final Image getPreviewImage() {
195 		if (visibility != null && visibility.isVisibleForYou()) {
196 			return image();
197 		}
198 		return DatabaseFactory.backImage;
199 	}
200 
201 	/***
202 	 * return the card's name
203 	 * 
204 	 * @return the card's name
205 	 */
206 	@Override
207 	public String toString() {
208 		return getCardName();
209 	}
210 
211 	/***
212 	 * Indicates if the current card in the stack is a copy or not
213 	 * 
214 	 * @return true if the current card in the stack is a copy or not
215 	 */
216 	public abstract boolean isACopy();
217 
218 	/***
219 	 * is called when mouse is on this card, will disp a preview
220 	 * 
221 	 * @param e
222 	 *          is the mouse event
223 	 */
224 	@Override
225 	public void mouseEntered(MouseEvent e) {
226 		StringBuilder toolTip = new StringBuilder(100);
227 		toolTip.append("<html><b>");
228 		toolTip.append(LanguageManager.getString("cardname"));
229 		toolTip.append(": </b>");
230 		toolTip.append(database.getLocalName());
231 		ui.setToolTipText(toolTip.toString());
232 	}
233 
234 	/***
235 	 * Return the controller of this card
236 	 * 
237 	 * @return the controller of this card
238 	 */
239 	public Player getController() {
240 		return controller == null ? SystemCard.instance.getController()
241 				: controller;
242 	}
243 
244 	/***
245 	 * This function return the result of Component#getName(). Return the english
246 	 * name, not the localized one.
247 	 * 
248 	 * @return the result of Component#getName()
249 	 */
250 	public final String getCardName() {
251 		return database.getCardName();
252 	}
253 
254 	/***
255 	 * Return the database configuration of this card : picture, database
256 	 * properties (credits, language,...)
257 	 * 
258 	 * @return database configuration of this card
259 	 */
260 	public final DatabaseCard getDatabase() {
261 		return database;
262 	}
263 
264 	/***
265 	 * Return UI of this card.
266 	 * 
267 	 * @return UI of this card.
268 	 */
269 	public VirtualCard getMUI() {
270 		return ui;
271 	}
272 
273 	/***
274 	 * The database configuration of this card : picture, database properties
275 	 * (credits, language,...)
276 	 */
277 	protected DatabaseCard database;
278 
279 	/***
280 	 * tell if this card is reversed or not, that means this card is controlled by
281 	 * opponent or not. A card in side, stack or under your control is not
282 	 * reversed.
283 	 */
284 	public boolean reversed;
285 
286 	/***
287 	 * Indicates if this card is returned or not, that means back image is visible
288 	 * or not.
289 	 */
290 	public Visibility visibility;
291 
292 	/***
293 	 * The UI of this card.
294 	 */
295 	protected VirtualCard ui;
296 
297 	/***
298 	 * Player controller
299 	 */
300 	public Player controller;
301 }