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.modifier.ObjectModifierModel;
31 import net.sf.magicproject.stack.StackManager;
32 import net.sf.magicproject.tools.MToolKit;
33 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
34
35 /***
36 *
37 */
38 class AddObject extends UserAction implements LoopAction {
39
40 /***
41 * <ul>
42 * Structure of stream : Data[size]
43 * <li>[super]</li>
44 * <li>object name [String]</li>
45 * <li>model [ObjectModel]</li>
46 * </ul>
47 *
48 * @param inputFile
49 * stream containing this action.
50 * @throws IOException
51 * If some other I/O error occurs
52 * @see Actiontype
53 */
54 AddObject(InputStream inputFile) throws IOException {
55 super(inputFile);
56 final String objectName = MToolKit.readString(inputFile).intern();
57 final ObjectModifierModel object = ObjectFactory
58 .getObjectModifierModel(objectName);
59 if (object == null) {
60 throw new RuntimeException("Unknown Object '" + objectName + "'");
61 }
62 objectModifierModel = (ObjectModifierModel) object.clone();
63 }
64
65 @Override
66 public Actiontype getIdAction() {
67 return Actiontype.ADD_OBJECT;
68 }
69
70 @Override
71 public String toString(Ability ability) {
72 return "addobject-" + objectModifierModel.getObjectName();
73 }
74
75 @Override
76 public String toHtmlString(Ability ability, int times,
77 ContextEventListener context) {
78 if (times == 1) {
79 return LanguageManagerMDB.getString("addobject-1-"
80 + objectModifierModel.getObjectName().replaceAll("/", ""));
81 }
82 if (times == -1) {
83
84 return LanguageManagerMDB.getString("addobject-%n",
85 objectModifierModel.getObjectName().replaceAll("/", "")).replaceAll(
86 "%n", "" + MdbLoader.unknownSmlManaHtml);
87 }
88 return LanguageManagerMDB.getString("addobject-%n",
89 objectModifierModel.getObjectName().replaceAll("/", "")).replaceAll(
90 "%n", "" + times);
91 }
92
93 @Override
94 public String toHtmlString(Ability ability, ContextEventListener context) {
95 if (actionName != null) {
96 if (actionName.charAt(0) == '%') {
97 return "";
98 }
99 if (actionName.charAt(0) == '@') {
100 final String picture = ActionFactory.PICTURES.get(actionName);
101 if (picture != null) {
102 return toHtmlString(ability, picture);
103 }
104 } else {
105 return LanguageManagerMDB.getString(actionName);
106 }
107 }
108 return LanguageManagerMDB.getString("addobject-1", objectModifierModel
109 .getObjectName().replaceAll("/", ""));
110 }
111
112 public boolean continueLoop(ContextEventListener context, int loopingIndex,
113 Ability ability) {
114 final Targetable target = StackManager.getInstance().getTargetedList().get(
115 loopingIndex);
116 if (!target.isCard()) {
117 throw new InternalError(
118 "AddObject action is only supported for Card component");
119 }
120 if (checkTimeStamp(context, (MCard) target)) {
121 objectModifierModel.addModifierFromModel(ability, (MCard) target);
122 target.repaint();
123 }
124 return true;
125 }
126
127 public int getStartIndex() {
128 return StackManager.getInstance().getTargetedList().size() - 1;
129 }
130
131 /***
132 * The object to add to the current target list.
133 */
134 private ObjectModifierModel objectModifierModel;
135 }