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 net.sf.magicproject.clickable.ability.Ability;
22 import net.sf.magicproject.event.context.ContextEventListener;
23 import net.sf.magicproject.test.Test;
24 import net.sf.magicproject.token.IdConst;
25
26 /***
27 * This class is representing an atom action. Each action can generate more than
28 * an event.
29 *
30 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
31 * @since 0.3
32 * @since 0.85 named action, and complex constraints are supported.
33 */
34 public abstract class MAction {
35
36 /***
37 * The debug data, may be empty.
38 */
39 protected final String debugData;
40
41 /***
42 * Creates a new instance of MAction
43 *
44 * @param debugData
45 * optional debug info attached to this action.
46 */
47 protected MAction(String debugData) {
48 super();
49 this.debugData = debugData;
50 }
51
52 /***
53 * Creates a new instance of MAction
54 */
55 protected MAction() {
56 this(null);
57 }
58
59 /***
60 * Return the index of this action. As default, this is a zero id
61 *
62 * @return the index of this action.
63 * @see net.sf.magicproject.action.Actiontype
64 */
65 public abstract Actiontype getIdAction();
66
67 /***
68 * Return the amount of mana needed (constant part only) to play this ability
69 * As default, we return an empty number for all manas.
70 *
71 * @param ability
72 * is the ability owning this test. The card component of this
73 * ability should correspond to the card owning this test too.
74 * @param context
75 * the context of playing ability.
76 * @return array of mana needed to play this ability
77 */
78 public int[] manaNeeded(Ability ability, ContextEventListener context) {
79 return IdConst.EMPTY_CODE;
80 }
81
82 /***
83 * Return the HTML code representing this action. If no picture is associated
84 * to this action, only text will be returned.
85 *
86 * @param ability
87 * is the ability owning this test. The card component of this
88 * ability should correspond to the card owning this test too.
89 * @param context
90 * is the context attached to this action.
91 * @return the HTML code representing this action. If no picture is associated
92 * to this action, only text will be returned.
93 * @see #toString(Ability)
94 * @see #toHtmlString(Ability, int, ContextEventListener)
95 * @since 0.85
96 */
97 public String toHtmlString(Ability ability, ContextEventListener context) {
98 return toString(ability);
99 }
100
101 /***
102 * Return the HTML code representing this action. If no picture is associated
103 * to this action, only text will be returned.
104 *
105 * @param ability
106 * is the ability owning this test. The card component of this
107 * ability should correspond to the card owning this test too.
108 * @param htmlString
109 * the HTML code defined for this string.
110 * @return the HTML code representing this action. If no picture is associated
111 * to this action, only text will be returned.
112 * @see #toString(Ability)
113 * @since 0.85
114 */
115 protected String toHtmlString(Ability ability, String htmlString) {
116 return htmlString;
117 }
118
119 /***
120 * Return the HTML code representing this action. If this action is named,
121 * it's name will be returned. If not, if existing, the picture associated to
122 * this action is returned. Otherwise, built-in action's text will be
123 * returned.
124 *
125 * @param ability
126 * is the ability owning this test. The card component of this
127 * ability should correspond to the card owning this test too.
128 * @param times
129 * the times to repeat this action.
130 * @param context
131 * is the context attached to this action.
132 * @return the HTML code representing this action. If this action is named,
133 * it's name will be returned. If not, if existing, the picture
134 * associated to this action is returned. Otherwise, built-in action's
135 * text will be returned.
136 * @see #toString(Ability)
137 * @see #toHtmlString(Ability, ContextEventListener)
138 * @since 0.85
139 */
140 public String toHtmlString(Ability ability, int times,
141 ContextEventListener context) {
142 if (times > 1) {
143 final String res = toHtmlString(ability, context);
144 if (res.length() == 0) {
145 return null;
146 }
147 return "[" + toHtmlString(ability, context) + "] x " + times;
148 }
149 return toHtmlString(ability, context);
150 }
151
152 /***
153 * String representation of this action.
154 *
155 * @param ability
156 * is the ability owning this test. The card component of this
157 * ability should correspond to the card owning this test too.
158 * @return action name.
159 * @see Object#toString()
160 */
161 public abstract String toString(Ability ability);
162
163 @Override
164 public String toString() {
165 if (debugData != null && debugData.length() > 0)
166 return debugData;
167 return super.toString();
168 }
169
170 /***
171 * Return the given test where values depending on values of this action have
172 * been replaced.
173 *
174 * @param test
175 * the test containing eventually some values depending on parameters
176 * of this action.
177 * @return a parsed test.
178 * @since 0.85
179 */
180 public Test parseTest(Test test) {
181 return test;
182 }
183
184 /***
185 * Return true if this action matches with the given action.
186 *
187 * @param constraintAction
188 * the constraint action.
189 * @return true if this action matches with the given action.
190 * @since 0.85
191 */
192 public boolean equal(MAction constraintAction) {
193 return false;
194 }
195
196 /***
197 * Return the name of this action
198 *
199 * @return the name of this action
200 */
201 public String getActionName() {
202 return toString();
203 }
204
205 }