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.stack.StackManager;
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 AttachList extends UserAction implements StandardAction {
43  
44  	/***
45  	 * Creates a new instance of AttachList <br>
46  	 * <ul>
47  	 * Structure of stream : Data[size]
48  	 * <li>idAction [1]</li>
49  	 * </ul>
50  	 * 
51  	 * @param inputFile
52  	 *          file containing this action
53  	 * @throws IOException
54  	 *           If some other I/O error occurs
55  	 */
56  	AttachList(InputStream inputFile) throws IOException {
57  		super(inputFile);
58  	}
59  
60  	@Override
61  	public final Actiontype getIdAction() {
62  		return Actiontype.ATTACH_LIST;
63  	}
64  
65  	public boolean play(ContextEventListener context, Ability ability) {
66  		// check action validity
67  		if (StackManager.getInstance().getTargetedList().size() < 2) {
68  			throw new InternalError(
69  					"For the action 'attachlist' the target list must contain at least 2"
70  							+ " targets : the first of list is the master card. The others are"
71  							+ " the cards to attach to this master card.");
72  		}
73  		final MCard to = (MCard) StackManager.getInstance().getTargetedList()
74  				.get(0);
75  		// add the components to the card
76  		for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 1;) {
77  			final MCard card = (MCard) StackManager.getInstance().getTargetedList()
78  					.get(i);
79  			Attachment attachment = card.getDatabase().getCardModel().getAttachment();
80  			if (attachment != null) {
81  				attachment.attach(context, ability, card, to);
82  			} else {
83  				to.add(card);
84  			}
85  			card.reverseAsNeeded();
86  		}
87  		to.getMUI().updateLayout();
88  		return true;
89  	}
90  
91  	@Override
92  	public String toString(Ability ability) {
93  		return "attach list";
94  	}
95  }