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   */
20  package net.sf.magicproject.action;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import net.sf.magicproject.action.handler.StandardAction;
26  import net.sf.magicproject.clickable.ability.Ability;
27  import net.sf.magicproject.clickable.targetable.card.Attachment;
28  import net.sf.magicproject.clickable.targetable.card.MCard;
29  import net.sf.magicproject.event.context.ContextEventListener;
30  import net.sf.magicproject.test.TestOn;
31  
32  /***
33   * Attach to the first card of the list, the other cards of the target list. So
34   * this action is valid only if there are at least 2 cards in the target list.
35   * The last card(s) are the card(s) to attach to the first one of the target
36   * list. <br>
37   * 
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @since 0.91 this action does not use attachment validation test which has
40   *        been moved to attachment card's member.
41   */
42  class Attach extends UserAction implements StandardAction {
43  
44  	private final TestOn from;
45  
46  	private final TestOn to;
47  
48  	/***
49  	 * Creates a new instance of Attach <br>
50  	 * <ul>
51  	 * Structure of stream : Data[size]
52  	 * <li>[super]</li>
53  	 * <li>from [TestOn]</li>
54  	 * <li>to [TestOn]</li>
55  	 * </ul>
56  	 * 
57  	 * @param inputFile
58  	 *          stream containing this action.
59  	 * @throws IOException
60  	 *           If some other I/O error occurs
61  	 */
62  	Attach(InputStream inputFile) throws IOException {
63  		super(inputFile);
64  		from = TestOn.deserialize(inputFile);
65  		to = TestOn.deserialize(inputFile);
66  	}
67  
68  	@Override
69  	public final Actiontype getIdAction() {
70  		return Actiontype.ATTACH_LIST;
71  	}
72  
73  	public boolean play(ContextEventListener context, Ability ability) {
74  		// check action validity
75  
76  		final MCard from = this.from.getCard(ability, ability.getCard());
77  		final MCard to = this.to.getCard(ability, ability.getCard());
78  		// add the components to the card
79  		Attachment attachment = from.getDatabase().getCardModel().getAttachment();
80  		if (attachment != null) {
81  			attachment.attach(context, ability, from, to);
82  		} else {
83  			to.add(from);
84  		}
85  		from.reverseAsNeeded();
86  		to.getMUI().updateLayout();
87  		// to.getContainer().repaint();
88  		return true;
89  	}
90  
91  	@Override
92  	public boolean equal(MAction constraintAction) {
93  		return constraintAction instanceof Attach
94  				&& constraintAction.getIdAction() == Actiontype.ATTACH
95  				&& actionName != null && actionName.length() > 0
96  				&& actionName.equals(((Attach) constraintAction).actionName);
97  	}
98  
99  	@Override
100 	public String toString(Ability ability) {
101 		return "attach[from/to]";
102 	}
103 }