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 java.awt.Color;
22 import java.awt.Dimension;
23 import java.awt.Graphics;
24 import java.awt.Image;
25 import java.awt.Rectangle;
26 import java.awt.event.MouseEvent;
27 import java.net.MalformedURLException;
28
29 import javax.swing.ToolTipManager;
30 import javax.swing.plaf.basic.BasicHTML;
31 import javax.swing.text.View;
32
33 import net.sf.magicproject.action.context.ActionContextWrapper;
34 import net.sf.magicproject.action.handler.ChoosenAction;
35 import net.sf.magicproject.clickable.Clickable;
36 import net.sf.magicproject.stack.StackManager;
37 import net.sf.magicproject.token.IdConst;
38 import net.sf.magicproject.tools.Log;
39 import net.sf.magicproject.tools.Picture;
40 import net.sf.magicproject.ui.MagicUIComponents;
41
42 /***
43 * An implementation of a two-state button.
44 *
45 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
46 * @since 0.86
47 */
48 public class JChoosenAction extends Clickable {
49
50 private static final String CACHING_STR = "caching...";
51
52 /***
53 * The picture corresponding to the "completed" status of an action
54 */
55 private static Image completedImage = null;
56
57 /***
58 * The picture corresponding to the "is completing" status of an action
59 */
60 private static Image completingImage = null;
61
62 /***
63 * The picture corresponding to the "uncompleted" status of an action
64 */
65 private static Image uncompletedImage = null;
66
67 /***
68 * The context associated to this playable action
69 */
70 ActionContextWrapper context;
71
72 /***
73 * @param context
74 * The associated action context.
75 */
76 public JChoosenAction(ActionContextWrapper context) {
77 super();
78 this.context = context;
79 context.actionUI = this;
80 if (completedImage == null) {
81 try {
82 completedImage = Picture.loadImage(IdConst.IMAGES_DIR
83 + "completedaction.gif");
84 uncompletedImage = Picture.loadImage(IdConst.IMAGES_DIR
85 + "uncompletedaction.gif");
86 completingImage = Picture.loadImage(IdConst.IMAGES_DIR
87 + "completingaction.gif");
88
89 } catch (MalformedURLException e) {
90
91 }
92 }
93 addMouseListener(this);
94 setMaximumSize(new Dimension(2000, 25));
95 context.refreshText(StackManager.currentAbility, StackManager.getInstance()
96 .getAbilityContext());
97 putClientProperty(TOOL_TIP_TEXT_KEY, CACHING_STR);
98 ToolTipManager.sharedInstance().registerComponent(this);
99 repaint();
100 }
101
102 @Override
103 public void setToolTipText(String text) {
104 putClientProperty(TOOL_TIP_TEXT_KEY, text);
105 }
106
107 @Override
108 public String getToolTipText() {
109 String ttCache = (String) getClientProperty(TOOL_TIP_TEXT_KEY);
110 if (ttCache == CACHING_STR) {
111 ttCache = "<html>"
112 + context.toHtmlString(StackManager.currentAbility, StackManager
113 .getInstance().getAbilityContext());
114 setToolTipText("<html>" + ttCache + ChoosenCostPanel.ttClick);
115 }
116 return ttCache;
117 }
118
119 @Override
120 public void disHighLight() {
121
122 System.out.println("JCostAction : disHighLight");
123 ((ChoosenAction) context.action).disactivate(context, StackManager
124 .getInstance().getAbilityContext(), StackManager.currentAbility);
125 super.disHighLight();
126 }
127
128 @Override
129 public void mouseClicked(MouseEvent e) {
130 if (!isHighLighted && StackManager.actionManager.clickOn(this)) {
131
132 Log.debug("clickOn(Mouse):" + context.action);
133 sendClickToOpponent();
134 StackManager.actionManager.succeedClickOn(this);
135 }
136 }
137
138 /***
139 * Set this action as selected. This triggers this action to be choosen and
140 * also been played.
141 *
142 * @return true if the stack resolution can continue.
143 */
144 public boolean setSelected() {
145 StackManager.actionManager.currentAction = context.action;
146 StackManager.actionManager.currentIdAction = context.contextID;
147 context.refreshText(StackManager.currentAbility, StackManager.getInstance()
148 .getAbilityContext());
149 highLight(Color.ORANGE);
150 return ((ChoosenAction) context.action).choose(context, StackManager
151 .getInstance().getAbilityContext(), StackManager.currentAbility);
152 }
153
154 @Override
155 public void sendClickToOpponent() {
156 MagicUIComponents.choosenCostPanel.sendClickToOpponent(this);
157 }
158
159 @Override
160 public void paint(Graphics g) {
161
162 super.paint(g);
163 if (isCompleted()) {
164 g.setColor(Color.GREEN.darker());
165 g.fill3DRect(0, 0, getWidth() - 1, getHeight() - 1, true);
166 g.drawImage(completedImage, 5, 5, this);
167 } else if (isHighLighted) {
168 g.setColor(Color.ORANGE);
169 g.fill3DRect(0, 0, getWidth() - 1, getHeight() - 1, true);
170 g.drawImage(completingImage, 5, 5, null);
171 } else {
172 g.setColor(Color.RED.darker());
173 g.fill3DRect(0, 0, getWidth() - 1, getHeight() - 1, true);
174 g.drawImage(uncompletedImage, 5, 5, this);
175 }
176
177
178 if (context.isRefreshText()) {
179 context.toHtmlString(StackManager.currentAbility, StackManager
180 .getInstance().getAbilityContext());
181 }
182 g.setColor(Color.BLACK);
183 ((View) getClientProperty(BasicHTML.propertyKey)).paint(g,
184 getSharedRectangle());
185 }
186
187 /***
188 * Return the shared Rectangle object used for Painting operation of
189 * JChoosenAction.
190 *
191 * @return the shared Rectangle object used for Painting operation of
192 * JChoosenAction.
193 */
194 private Rectangle getSharedRectangle() {
195 if (sharedRectangle == null) {
196 sharedRectangle = new Rectangle(20, 5, getWidth() - 23, getHeight() - 2);
197 }
198 return sharedRectangle;
199 }
200
201 /***
202 * The shared Rectangle object used for Painting operation of JChoosenAction
203 * component.
204 */
205 private static Rectangle sharedRectangle = null;
206
207 /***
208 * Is this action is completed?
209 *
210 * @return true if this action is completed.
211 */
212 public boolean isCompleted() {
213 return context.isCompleted();
214 }
215 }