1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.action.context;
20
21 import javax.swing.plaf.basic.BasicHTML;
22
23 import net.sf.magicproject.action.MAction;
24 import net.sf.magicproject.action.Repeat;
25 import net.sf.magicproject.action.handler.ChoosenAction;
26 import net.sf.magicproject.action.handler.InitAction;
27 import net.sf.magicproject.clickable.ability.Ability;
28 import net.sf.magicproject.clickable.action.JChoosenAction;
29 import net.sf.magicproject.event.context.ContextEventListener;
30 import net.sf.magicproject.stack.StackManager;
31
32 /***
33 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
34 * @since 0.86
35 */
36 public final class ActionContextWrapper {
37
38 /***
39 * Action owning this context
40 */
41 public MAction action;
42
43 /***
44 * The JCostAction object associated to this context. Is null if the action is
45 * not a ChoosenAction class.
46 */
47 public JChoosenAction actionUI;
48
49 /***
50 * Specific data saved during the init/... steps.
51 */
52 public ActionContext actionContext;
53
54 /***
55 * Action index within the chain of cost
56 */
57 public int contextID;
58
59 /***
60 * Times to repeat this action
61 */
62 public int repeat;
63
64 /***
65 * Times already done. In range [0,repeat].
66 */
67 public int done;
68
69 /***
70 * Is <code>-1</code> if this action has not been completed. Otherwise this
71 * corresponds to the record id. Start from <code>0</code>.
72 */
73 public int recordIndex = -1;
74
75 /***
76 * Cached toString() value of this action. Initialize at the first use.
77 */
78 private String actionStr = null;
79
80 /***
81 * Is this action is completed?
82 *
83 * @return true if this action is completed.
84 */
85 public boolean isCompleted() {
86 return recordIndex != -1;
87 }
88
89 /***
90 * Return the HTML code representing this action. If no picture is associated
91 * to this action, only text will be returned.
92 *
93 * @param ability
94 * is the ability owning this test. The card component of this
95 * ability should correspond to the card owning this test too.
96 * @param context
97 * is the context attached to this action.
98 * @return the HTML code representing this action. If no picture is associated
99 * to this action, only text will be returned.
100 * @since 0.85
101 */
102 public String toHtmlString(Ability ability, ContextEventListener context) {
103 if (isRefreshText()) {
104 actionStr = null;
105 final MAction[] actions = StackManager.actionManager.actionList();
106 int id = contextID;
107 do {
108 final MAction action = actions[id];
109 String str = null;
110 if (action instanceof ChoosenAction) {
111 str = ((ChoosenAction) action).toHtmlString(ability, context, this);
112 } else {
113 str = action.toHtmlString(ability, context);
114 }
115 if (str != null && str.length() > 0) {
116 if (actionStr == null) {
117 actionStr = str;
118 } else {
119 actionStr += ", " + str;
120 }
121 }
122 } while (++id < actions.length && !(actions[id] instanceof ChoosenAction)
123 && !(actions[id] instanceof InitAction)
124 && !(actions[id] instanceof Repeat));
125 if (actionStr == null) {
126 actionStr = "{unknown}";
127 }
128 BasicHTML.updateRenderer(actionUI, "<html>" + actionStr);
129 }
130
131
132 return actionStr;
133 }
134
135 /***
136 * Refresh the text of associated action UI component.
137 *
138 * @param ability
139 * is the ability owning this test. The card component of this
140 * ability should correspond to the card owning this test too.
141 * @param context
142 * is the context attached to this action.
143 */
144 public void refreshText(Ability ability, ContextEventListener context) {
145 actionStr = null;
146 if (actionUI != null) {
147
148 toHtmlString(ability, context);
149 actionUI.repaint();
150 }
151 }
152
153 /***
154 * Is the text is being to be refreshed.
155 *
156 * @return true if the text is being to be refreshed.
157 */
158 public boolean isRefreshText() {
159 return actionStr == null;
160 }
161
162 /***
163 * Create a new instance of this class.
164 *
165 * @param contextID
166 * the action ID.
167 * @param action
168 * the action.
169 * @param actionContext
170 * the context of this action
171 * @param repeat
172 * times this action has to be refreshed.
173 */
174 public ActionContextWrapper(int contextID, MAction action,
175 ActionContext actionContext, int repeat) {
176 this.contextID = contextID;
177 this.actionContext = actionContext;
178 this.action = action;
179 this.repeat = repeat;
180 done = 0;
181 }
182
183 }