1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.action;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import net.sf.magicproject.clickable.ability.Ability;
25 import net.sf.magicproject.clickable.targetable.Targetable;
26 import net.sf.magicproject.clickable.targetable.card.MCard;
27 import net.sf.magicproject.deckbuilder.MdbLoader;
28 import net.sf.magicproject.event.context.ContextEventListener;
29 import net.sf.magicproject.modifier.ObjectFactory;
30 import net.sf.magicproject.test.True;
31 import net.sf.magicproject.test.TestOn;
32 import net.sf.magicproject.tools.MToolKit;
33 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
34
35 /***
36 * Move an object from a component to another component.
37 *
38 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39 * @since 0.93
40 */
41 public class MoveObject extends UserAction implements LoopAction {
42
43 /***
44 * Create an instance of MoveObject
45 * <ul>
46 * Structure of stream : Data[size]
47 * <li>object name [String]</li>
48 * <li>from [TestOn]</li>
49 * <li>to [TestOn]</li>
50 * </ul>
51 *
52 * @param inputFile
53 * is ignored
54 * @param card
55 * owning this action
56 * @throws IOException
57 * If some other I/O error occurs
58 */
59 MoveObject(InputStream inputFile) throws IOException {
60 super(inputFile);
61 objectName = MToolKit.readString(inputFile).intern();
62 from = TestOn.deserialize(inputFile);
63 to = TestOn.deserialize(inputFile);
64 }
65
66 @Override
67 public Actiontype getIdAction() {
68 return Actiontype.MOVE_OBJECT;
69 }
70
71 @Override
72 public String toString(Ability ability) {
73 return "moveobject-" + objectName;
74 }
75
76 @Override
77 public String toHtmlString(Ability ability, int times,
78 ContextEventListener context) {
79 if (times == 1) {
80 return LanguageManagerMDB.getString("moveobject-1", new String[] {
81 objectName.replaceAll("/", ""), from.toString(), to.toString() });
82 }
83 if (times == -1) {
84
85 return LanguageManagerMDB.getString(
86 "moveobject-%n",
87 new String[] { objectName.replaceAll("/", ""), from.toString(),
88 to.toString() }).replaceAll("%n",
89 "" + MdbLoader.unknownSmlManaHtml);
90 }
91 return LanguageManagerMDB.getString(
92 "moveobject-%n",
93 new String[] { objectName.replaceAll("/", ""), from.toString(),
94 to.toString() }).replaceAll("%n", "" + times);
95 }
96
97 @Override
98 public String toHtmlString(Ability ability, ContextEventListener context) {
99 if (actionName != null) {
100 if (actionName.charAt(0) == '%') {
101 return "";
102 }
103 if (actionName.charAt(0) == '@') {
104 final String picture = ActionFactory.PICTURES.get(actionName);
105 if (picture != null) {
106 return toHtmlString(ability, picture);
107 }
108 } else {
109 return LanguageManagerMDB.getString(actionName);
110 }
111 }
112 return LanguageManagerMDB.getString("moveobject-1", new String[] {
113 objectName.replaceAll("/", ""), from.toString(), to.toString() });
114 }
115
116 public boolean continueLoop(ContextEventListener context, int loopingIndex,
117 Ability ability) {
118
119
120 final MCard from = this.from.getCard(ability, null);
121 if (!from.isCard()) {
122 throw new InternalError(
123 "TODO MoveObject action is only supported for Card component");
124 }
125 if (checkTimeStamp(context, from)) {
126 ObjectFactory.removeObjectModifier(objectName, from, True.getInstance());
127 MCard to = this.to.getCard(ability, null);
128 ObjectFactory.getObjectModifierModel(objectName).addModifierFromModel(
129 ability, to);
130 from.repaint();
131 to.repaint();
132 }
133 return true;
134 }
135
136 /***
137 * @param ability
138 * is the ability owning this test. The card component of this
139 * ability should correspond to the card owning this test too.
140 * @param target
141 * the component where objects will be removed from
142 * @param nbObjects
143 * amount of required object
144 * @return true if the specified targetable contains the required object(s)
145 */
146 public boolean checkObject(Ability ability, Targetable target, int nbObjects) {
147 if (!target.isCard()) {
148 throw new InternalError(
149 "TODO MmoveObject action is only supported for Card component");
150 }
151 return ObjectFactory.getNbObject(objectName, (MCard) target, True
152 .getInstance()) >= nbObjects;
153 }
154
155 public int getStartIndex() {
156 return 0;
157 }
158
159 /***
160 * The object's name to remove from the current target list.
161 */
162 private final String objectName;
163
164 /***
165 * The object's source of move.
166 */
167 private final TestOn from;
168
169 /***
170 * The object's destination of move.
171 */
172 private final TestOn to;
173
174 }