1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.richclient.command.config;
17
18 import java.awt.Color;
19 import java.awt.Image;
20
21 import javax.swing.AbstractButton;
22 import javax.swing.Action;
23 import javax.swing.Icon;
24 import javax.swing.KeyStroke;
25
26 import org.springframework.binding.value.support.AbstractPropertyChangePublisher;
27 import org.springframework.core.style.ToStringCreator;
28 import org.springframework.richclient.command.AbstractCommand;
29 import org.springframework.richclient.core.ColorConfigurable;
30 import org.springframework.richclient.core.DescribedElement;
31 import org.springframework.richclient.core.DescriptionConfigurable;
32 import org.springframework.richclient.core.VisualizedElement;
33 import org.springframework.richclient.util.Assert;
34
35
36
37
38
39
40
41
42 public class CommandFaceDescriptor extends AbstractPropertyChangePublisher implements DescribedElement,
43 VisualizedElement, CommandLabelConfigurable, DescriptionConfigurable, CommandIconConfigurable, ColorConfigurable {
44
45
46 public static final String LABEL_INFO_PROPERTY = "labelInfo";
47
48
49 public static final String ICON_PROPERTY = "icon";
50
51
52 public static final String LARGE_ICON_PROPERTY = "largeIcon";
53
54
55 public static final String ICON_INFO_PROPERTY = "iconInfo";
56
57
58 public static final String LARGE_ICON_INFO_PROPERTY = "largeIconInfo";
59
60
61 public static final String BACKGROUND_PROPERTY = "background";
62
63
64 public static final String FOREGROUND_PROPERTY = "foreground";
65
66 private String caption;
67
68 private String description;
69
70 private Color background;
71
72 private Color foreground;
73
74 private CommandButtonLabelInfo labelInfo;
75
76 private CommandButtonIconInfo iconInfo = CommandButtonIconInfo.BLANK_ICON_INFO;
77
78 private CommandButtonIconInfo largeIconInfo = CommandButtonIconInfo.BLANK_ICON_INFO;
79
80
81
82
83
84
85
86
87
88 public CommandFaceDescriptor(String encodedLabel) {
89 this(encodedLabel, null, null);
90 }
91
92
93
94
95
96
97
98
99
100
101
102 public CommandFaceDescriptor(String encodedLabel, Icon icon, String caption) {
103 this.labelInfo = CommandButtonLabelInfo.valueOf(encodedLabel);
104 if (icon != null) {
105 this.iconInfo = new CommandButtonIconInfo(icon);
106 }
107 this.caption = caption;
108 }
109
110
111
112
113 public CommandFaceDescriptor() {
114 this(CommandButtonLabelInfo.BLANK_BUTTON_LABEL);
115 }
116
117
118
119
120
121
122
123 public CommandFaceDescriptor(CommandButtonLabelInfo labelInfo) {
124 Assert.notNull(labelInfo, "The labelInfo property is required");
125 this.labelInfo = labelInfo;
126 }
127
128
129
130
131
132
133 public boolean isBlank() {
134 return labelInfo == CommandButtonLabelInfo.BLANK_BUTTON_LABEL;
135 }
136
137
138
139
140
141
142 public String getText() {
143 return labelInfo.getText();
144 }
145
146
147
148
149 public String getDisplayName() {
150 return getText();
151 }
152
153
154
155
156 public String getCaption() {
157 return caption;
158 }
159
160
161
162
163 public String getDescription() {
164 return description;
165 }
166
167
168
169
170
171
172 public int getMnemonic() {
173 return labelInfo.getMnemonic();
174 }
175
176
177
178
179
180
181
182 public int getMnemonicIndex() {
183 return labelInfo.getMnemonicIndex();
184 }
185
186
187
188
189 public Image getImage() {
190
191 if (this.iconInfo == null) {
192 return null;
193 }
194 else {
195 return iconInfo.getImage();
196 }
197
198 }
199
200
201
202
203 public Icon getIcon() {
204
205 if (this.iconInfo == null) {
206 return null;
207 }
208 else {
209 return iconInfo.getIcon();
210 }
211
212 }
213
214
215
216
217
218
219 public Icon getLargeIcon() {
220
221 if (this.largeIconInfo == null) {
222 return null;
223 }
224 else {
225 return largeIconInfo.getIcon();
226 }
227
228 }
229
230
231
232
233
234
235 public KeyStroke getAccelerator() {
236 return labelInfo.getAccelerator();
237 }
238
239
240
241
242
243
244 protected CommandButtonLabelInfo getLabelInfo() {
245 return labelInfo;
246 }
247
248
249
250
251
252
253 protected CommandButtonIconInfo getIconInfo() {
254 return iconInfo;
255 }
256
257
258
259
260
261
262 protected CommandButtonIconInfo getLargeIconInfo() {
263 return largeIconInfo;
264 }
265
266
267
268
269 public void setCaption(String shortDescription) {
270 String old = this.caption;
271 this.caption = shortDescription;
272 firePropertyChange(DescribedElement.CAPTION_PROPERTY, old, this.caption);
273 }
274
275
276
277
278 public void setDescription(String longDescription) {
279 String old = this.description;
280 this.description = longDescription;
281 firePropertyChange(DescribedElement.DESCRIPTION_PROPERTY, old, this.description);
282 }
283
284
285
286
287 public void setBackground(Color background) {
288 Color old = this.background;
289 this.background = background;
290 firePropertyChange(BACKGROUND_PROPERTY, old, this.background);
291 }
292
293
294
295
296 public void setForeground(Color foreground) {
297 Color old = this.foreground;
298 this.foreground = foreground;
299 firePropertyChange(FOREGROUND_PROPERTY, old, this.foreground);
300 }
301
302
303
304
305
306
307
308
309 public void setButtonLabelInfo(String encodedLabelInfo) {
310 CommandButtonLabelInfo newLabelInfo = CommandButtonLabelInfo.valueOf(encodedLabelInfo);
311 setLabelInfo(newLabelInfo);
312 }
313
314
315
316
317 public void setLabelInfo(CommandButtonLabelInfo labelInfo) {
318 if (labelInfo == null) {
319 labelInfo = CommandButtonLabelInfo.BLANK_BUTTON_LABEL;
320 }
321 CommandButtonLabelInfo old = this.labelInfo;
322 this.labelInfo = labelInfo;
323 firePropertyChange(LABEL_INFO_PROPERTY, old, this.labelInfo);
324 }
325
326
327
328
329 public void setIconInfo(CommandButtonIconInfo iconInfo) {
330 if (iconInfo == null) {
331 iconInfo = CommandButtonIconInfo.BLANK_ICON_INFO;
332 }
333 CommandButtonIconInfo old = this.iconInfo;
334 this.iconInfo = iconInfo;
335 firePropertyChange(ICON_INFO_PROPERTY, old, this.iconInfo);
336 }
337
338
339
340
341 public void setLargeIconInfo(CommandButtonIconInfo largeIconInfo) {
342 if (largeIconInfo == null) {
343 largeIconInfo = CommandButtonIconInfo.BLANK_ICON_INFO;
344 }
345 CommandButtonIconInfo old = this.largeIconInfo;
346 this.largeIconInfo = largeIconInfo;
347 firePropertyChange(LARGE_ICON_INFO_PROPERTY, old, this.largeIconInfo);
348 }
349
350
351
352
353
354
355 public void setIcon(Icon icon) {
356 Icon old = null;
357 if (iconInfo == CommandButtonIconInfo.BLANK_ICON_INFO) {
358 if (icon != null) {
359
360 setIconInfo(new CommandButtonIconInfo(icon));
361 }
362 }
363 else {
364 old = iconInfo.getIcon();
365 this.iconInfo.setIcon(icon);
366 }
367 firePropertyChange(ICON_PROPERTY, old, icon);
368 }
369
370
371
372
373
374
375 public void setLargeIcon(Icon icon) {
376 Icon old = null;
377 if (largeIconInfo == CommandButtonIconInfo.BLANK_ICON_INFO) {
378 if (icon != null) {
379
380 setLargeIconInfo(new CommandButtonIconInfo(icon));
381 }
382 }
383 else {
384 old = largeIconInfo.getIcon();
385 this.largeIconInfo.setIcon(icon);
386 }
387 firePropertyChange(LARGE_ICON_PROPERTY, old, icon);
388 }
389
390
391
392
393
394
395
396
397 public void configureLabel(AbstractButton button) {
398 Assert.required(button, "button");
399 labelInfo.configure(button);
400 }
401
402
403
404
405
406
407
408
409 public void configureIcon(AbstractButton button) {
410 Assert.required(button, "button");
411 configureIconInfo(button, false);
412 }
413
414
415
416
417
418
419
420
421
422
423 public void configureIconInfo(AbstractButton button, boolean useLargeIcons) {
424
425 Assert.required(button, "button");
426
427 if (useLargeIcons) {
428 largeIconInfo.configure(button);
429 }
430 else {
431 iconInfo.configure(button);
432 }
433 }
434
435
436
437
438
439
440 public void configureColor(AbstractButton button) {
441 Assert.required(button, "button");
442 if (foreground != null) {
443 button.setForeground(foreground);
444 }
445 if (background != null) {
446 button.setBackground(background);
447 }
448 }
449
450
451
452
453
454
455
456
457
458
459
460 public void configure(AbstractButton button, AbstractCommand command, CommandButtonConfigurer configurer) {
461 Assert.required(button, "button");
462 Assert.required(configurer, "configurer");
463 configurer.configure(button, command, this);
464 }
465
466
467
468
469
470
471
472
473 public void configure(Action action) {
474 Assert.notNull(action, "The swing action to configure is required");
475 action.putValue(Action.NAME, getText());
476 action.putValue(Action.MNEMONIC_KEY, new Integer(getMnemonic()));
477 action.putValue(Action.SMALL_ICON, getIcon());
478 action.putValue("LargeIcon", getLargeIcon());
479 action.putValue(Action.ACCELERATOR_KEY, getAccelerator());
480 action.putValue(Action.SHORT_DESCRIPTION, caption);
481 action.putValue(Action.LONG_DESCRIPTION, description);
482 }
483
484
485
486
487 public String toString() {
488 return new ToStringCreator(this).append("caption", caption).append("description", description).append(
489 "buttonLabelInfo", labelInfo).append("buttonIconInfo", iconInfo).toString();
490 }
491 }