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.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
75
76 final MCard from = this.from.getCard(ability, ability.getCard());
77 final MCard to = this.to.getCard(ability, ability.getCard());
78
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
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 }