1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.clickable.action;
20
21 import net.sf.magicproject.action.Actiontype;
22 import net.sf.magicproject.action.InputChoice;
23 import net.sf.magicproject.action.MAction;
24 import net.sf.magicproject.action.Repeat;
25 import net.sf.magicproject.clickable.ability.Ability;
26 import net.sf.magicproject.event.context.ContextEventListener;
27 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
28
29 /***
30 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
31 * @since 0.93
32 */
33 public class ToStringHelper {
34
35 /***
36 * Return the HTML code representing an action list.
37 *
38 * @param ability
39 * the ability owning these actions.
40 * @param context
41 * the context needed by event activated
42 * @return the HTML code representing this ability.
43 */
44 public static String toHtmlString(Ability ability,
45 ContextEventListener context) {
46 final String defaultName = ability.getName();
47 StringBuilder buffer = new StringBuilder();
48 if (defaultName != null && defaultName.length() > 0) {
49 if (defaultName.indexOf("%a") == -1) {
50 return LanguageManagerMDB.getString(defaultName);
51 }
52
53 buffer.append(LanguageManagerMDB.getString(defaultName.replaceAll("%a",
54 "")));
55 }
56 toHtmlString(buffer, ability, ability.actionList(), context);
57 if (buffer.length() != 0) {
58 buffer.append(" : ");
59 }
60 toHtmlString(buffer, ability, ability.effectList(), context);
61 if (buffer.length() == 0) {
62 return null;
63 }
64 return buffer.toString();
65 }
66
67 /***
68 * Return the HTML code representing an action list.
69 *
70 * @param buffer
71 * the string buffer used to build the HTML code of this ability.
72 * @param ability
73 * the ability owning these actions.
74 * @param actions
75 * the actions to display.
76 * @param context
77 * the context needed by event activated
78 */
79 public static void toHtmlString(StringBuilder buffer, Ability ability,
80 MAction[] actions, ContextEventListener context) {
81 boolean emptyText = true;
82 for (int id = 0; id < actions.length; id++) {
83 final MAction action = actions[id];
84 String str = null;
85 if (action.getIdAction() == Actiontype.REPEAT_ACTION) {
86 final int times = ((Repeat) action).getPreemptionTimes(ability, null);
87 do {
88 str = actions[++id].toHtmlString(ability, times, context);
89 } while (str == null && id < actions.length - 1);
90 if (str == null) {
91 str = ((Repeat) action).toHtmlString(ability, context);
92 }
93 } else {
94 str = action.toHtmlString(ability, context);
95 if (action.getIdAction() == Actiontype.CHOICE) {
96 id += ((InputChoice) action).getSkipHop();
97 }
98 }
99 if (str != null && str.length() > 0) {
100 if (!emptyText) {
101 buffer.append(", ");
102 } else {
103 emptyText = false;
104 }
105 buffer.append(str);
106 }
107 }
108 }
109
110 /***
111 * Return the HTML code representing an action list.
112 *
113 * @param ability
114 * the ability owning these actions.
115 * @param actions
116 * the actions to display.
117 * @param context
118 * the context needed by event activated
119 * @return the HTML code representing this ability.
120 */
121 public static String toHtmlString(Ability ability, MAction[] actions,
122 ContextEventListener context) {
123 final StringBuilder buffer = new StringBuilder();
124 toHtmlString(buffer, ability, actions, context);
125 return buffer.toString();
126 }
127
128 /***
129 * Return the toString() of the given ability.
130 *
131 * @param ability
132 * the ability owning these actions.
133 * @return the toString() of the given ability.
134 */
135 public static String toString(Ability ability) {
136 final String defaultName = ability.getName();
137 StringBuilder buffer = new StringBuilder();
138 if (defaultName != null && defaultName.length() > 0) {
139 return defaultName;
140 }
141 toString(buffer, ability, ability.actionList());
142 if (buffer.length() != 0) {
143 buffer.append(" : ");
144 }
145 toString(buffer, ability, ability.effectList());
146 if (buffer.length() == 0) {
147 return null;
148 }
149 return buffer.toString();
150 }
151
152 /***
153 * Return the HTML code representing an action list.
154 *
155 * @param buffer
156 * the string buffer used to build the HTML code of this ability.
157 * @param ability
158 * the ability owning these actions.
159 * @param actions
160 * the actions to display.
161 */
162 public static void toString(StringBuilder buffer, Ability ability,
163 MAction[] actions) {
164 boolean emptyText = true;
165 for (int id = 0; id < actions.length; id++) {
166 final MAction action = actions[id];
167 String str = action.toString(ability);
168 if (str != null && str.length() > 0) {
169 if (action.getIdAction() == Actiontype.REPEAT_ACTION) {
170 String strTmp = null;
171 do {
172 strTmp = actions[++id].toString(ability);
173 } while ((strTmp == null || strTmp.length() == 0)
174 && id < actions.length - 1);
175 str = "[" + strTmp + "]" + str;
176 }
177 if (!emptyText) {
178 buffer.append(", ");
179 } else {
180 emptyText = false;
181 }
182 buffer.append(str);
183 if (actions[id].getIdAction() == Actiontype.CHOICE) {
184 id += ((InputChoice) actions[id]).getSkipHop();
185 }
186 }
187 }
188 if (emptyText) {
189 buffer.append("{no-text}");
190 }
191 }
192
193 }