1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.aequo.action;
14
15 import java.awt.event.ActionEvent;
16 import java.net.URL;
17 import java.text.MessageFormat;
18
19 import javax.swing.AbstractAction;
20 import javax.swing.Action;
21 import javax.swing.Icon;
22 import javax.swing.ImageIcon;
23 import javax.swing.JOptionPane;
24 import javax.swing.ProgressMonitor;
25
26 import org.abstracthorizon.aequo.GlobalContext;
27 import org.abstracthorizon.aequo.util.TaskUtilities;
28
29
30
31
32
33
34
35 public abstract class BaseAction extends AbstractAction {
36
37
38 protected Object message;
39
40
41 protected GlobalContext context;
42
43
44 protected boolean confirmRequired;
45
46
47 protected boolean directionalAction;
48
49
50 protected int column = 0;
51
52
53 protected int destColumn = -1;
54
55
56 public static final Icon EMPTY_ICON = new ImageIcon(BaseAction.class.getResource("Empty.png"));
57
58
59 public static final String PREFIX = "<--";
60
61
62 public static final String SUFFIX = "-->";
63
64
65
66
67
68
69 public BaseAction() {
70 autoloadIcon();
71 }
72
73
74
75
76 protected void autoloadIcon() {
77 URL url = getIconURL();
78 if (url != null) {
79 Icon icon = new ImageIcon(url);
80 putValue(Action.SMALL_ICON, icon);
81 } else {
82 putValue(Action.SMALL_ICON, EMPTY_ICON);
83 }
84 }
85
86
87
88
89
90
91 protected URL getIconURL() {
92 String name = getClass().getName();
93 int i = name.lastIndexOf('.');
94 if (i > 0) {
95 name = name.substring(i + 1);
96 }
97 if (name.endsWith("Action")) {
98 name = name.substring(0, name.length() - 6);
99 }
100 URL url = getClass().getResource(name + ".png");
101
102 if (directionalAction && (url == null)) {
103 url = getClass().getResource(name + "_" + column + ".png");
104 }
105 return url;
106 }
107
108
109
110
111
112 public void setName(String name) {
113 putValue(Action.NAME, name);
114 }
115
116
117
118
119
120 public void setDescription(String description) {
121 putValue(Action.SHORT_DESCRIPTION, description);
122 putValue(Action.LONG_DESCRIPTION, description);
123 }
124
125
126
127
128
129 public void setGlobalContext(GlobalContext context) {
130 this.context = context;
131 }
132
133
134
135
136
137 public GlobalContext getGlobalContext() {
138 return context;
139 }
140
141
142
143
144
145 public void setConfirmRequired(boolean confirmRequired) {
146 this.confirmRequired = confirmRequired;
147 }
148
149
150
151
152
153 public boolean isConfirmRequired() {
154 return confirmRequired;
155 }
156
157
158
159
160
161 public void setDirectionalAction(boolean directional) {
162 this.directionalAction = directional;
163 }
164
165
166
167
168
169
170
171 public boolean isDirectionalAction() {
172 return directionalAction;
173 }
174
175
176
177
178
179
180 public void setMessage(Object message) {
181 this.message = message;
182 }
183
184
185
186
187
188
189 public Object getMessage() {
190 return message;
191 }
192
193
194
195
196
197
198 public void setColumn(int column) {
199 this.column = column;
200 if (directionalAction) {
201 destColumn = 1 - column;
202 autoloadIcon();
203 Object name = super.getValue(Action.NAME);
204 if ((name != null) && (changeSupport != null)) {
205 changeSupport.firePropertyChange(Action.NAME, name, getValue(Action.NAME));
206 }
207 }
208 }
209
210
211
212
213
214
215 public int getColumn() {
216 return column;
217 }
218
219
220
221
222
223
224 public void setDestinationColumn(int column) {
225 this.destColumn = column;
226 }
227
228
229
230
231
232 public int getDestinationColumn() {
233 return destColumn;
234 }
235
236
237
238
239
240
241
242
243
244 public Object getValue(String key) {
245 Object value = super.getValue(key);
246
247 if (directionalAction && key.equals(Action.NAME) && (value != null)) {
248 String prefix = "";
249 if (isEnabled() && (column > destColumn)) {
250 prefix = PREFIX;
251 }
252 String suffix = "";
253 if (isEnabled() && (destColumn > column)) {
254 suffix = SUFFIX;
255 }
256 value = MessageFormat.format(value.toString(), new Object[]{prefix, suffix});
257 }
258 return value;
259 }
260
261
262
263
264
265
266 public void actionPerformed(final ActionEvent e) {
267 if (isEnabled()) {
268 invokeAction(e);
269 }
270 }
271
272
273
274
275
276
277 protected void invokeAction(final ActionEvent e) {
278 int res = 0;
279 if (isConfirmRequired()) {
280 res = showConfirmDialog();
281 }
282 if (!confirmRequired || (res == JOptionPane.OK_OPTION)) {
283 final ProgressMonitor progressMonitor = new ProgressMonitor(null, message, "", 0, 1);
284 progressMonitor.setProgress(0);
285 progressMonitor.setMillisToDecideToPopup(500);
286 progressMonitor.setMillisToPopup(1000);
287 Runnable task = new Runnable() {
288 public void run() {
289 try {
290 perform(e, progressMonitor);
291 } finally {
292 progressMonitor.close();
293 }
294 }
295 };
296 TaskUtilities.invokeLater(task);
297 }
298 }
299
300
301
302
303
304 protected int showConfirmDialog() {
305 int res = JOptionPane.showConfirmDialog(null, updateMessage(), "Confirm", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null);
306 return res;
307 }
308
309
310
311
312
313
314
315 protected Object updateMessage() {
316 return message;
317 }
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332 public void perform(final ActionEvent e) {
333 }
334
335
336
337
338
339
340
341
342
343
344
345
346
347 public void perform(final ActionEvent e, ProgressMonitor monitor) {
348 perform(e);
349 }
350 }