1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.magicproject.action.objectmap;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import net.sf.magicproject.action.context.ActionContextWrapper;
27 import net.sf.magicproject.action.context.Wrapper;
28 import net.sf.magicproject.action.handler.FollowAction;
29 import net.sf.magicproject.clickable.ability.Ability;
30 import net.sf.magicproject.clickable.targetable.Targetable;
31 import net.sf.magicproject.event.context.ContextEventListener;
32 import net.sf.magicproject.test.TestOn;
33 import net.sf.magicproject.tools.MToolKit;
34
35 /***
36 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37 * @since 0.90
38 */
39 class ObjectMapSave extends ObjectMap implements FollowAction {
40
41 /***
42 * Create an instance of ObjectMapSave by reading a file Offset's file must
43 * pointing on the first byte of this action <br>
44 * <ul>
45 * Structure of InputStream : Data[size]
46 * <li>super [ObjectMap]</li>
47 * <li>testOn : the component this action is applyed on [TestOn]</li>
48 * <li>object's name [String]</li>
49 * </ul>
50 *
51 * @param inputStream
52 * file containing this action
53 * @throws IOException
54 * If some other I/O error occurs
55 */
56 ObjectMapSave(InputStream inputStream) throws IOException {
57 super(inputStream);
58 testOn = TestOn.deserialize(inputStream);
59 objectName = MToolKit.readString(inputStream);
60 }
61
62 @Override
63 public boolean play(ContextEventListener context, Ability ability) {
64 ability.getTargetable().addPrivateNamedObject(objectName,
65 testOn.getTargetable(ability, null));
66 return true;
67 }
68
69 @Override
70 public String toString(Ability ability) {
71 return "";
72 }
73
74 public void simulate(ActionContextWrapper actionContext,
75 ContextEventListener context, Ability ability) {
76 final Wrapper<Targetable> mapContext = new Wrapper<Targetable>();
77 actionContext.actionContext = mapContext;
78 mapContext.setObject(ability.getTargetable().getPrivateNamedObject(
79 objectName));
80 play(context, ability);
81 }
82
83 public void rollback(ActionContextWrapper actionContext,
84 ContextEventListener context, Ability ability) {
85 final Targetable oldTargetable = ((Wrapper<Targetable>) actionContext.actionContext)
86 .getObject();
87 if (oldTargetable == null) {
88 ability.getTargetable().removePrivateNamedObject(objectName);
89 } else {
90 ability.getTargetable().addPrivateNamedObject(objectName, oldTargetable);
91 }
92 }
93
94 /***
95 * IdToken indentifying the first targetable to save. If is 0, no card is
96 * saved.
97 */
98 private TestOn testOn;
99
100 /***
101 * The object's name to manipulate.
102 */
103 private String objectName;
104 }