1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }