1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.console.swingui;
23
24 import java.beans.PropertyChangeEvent;
25 import java.beans.PropertyChangeListener;
26 import java.util.HashSet;
27 import java.util.Set;
28 import javax.swing.AbstractAction;
29 import javax.swing.AbstractButton;
30 import javax.swing.Action;
31 import javax.swing.ImageIcon;
32
33 import net.grinder.console.common.Resources;
34
35
36
37
38
39
40
41 abstract class CustomAction extends AbstractAction {
42
43
44 protected static final String SET_ACTION_PROPERTY = "setAction";
45
46
47 public static final String ROLLOVER_ICON = "RolloverIcon";
48
49
50
51
52
53 public static final String RELEVANT_TO_SELECTION = "RelevantToSelection";
54
55 private final String m_key;
56 private final Set<AbstractButton> m_buttonsWithRegisteredListeners =
57 new HashSet<AbstractButton>();
58
59 protected CustomAction(Resources resources, String key) {
60 this(resources, key, false);
61 }
62
63 protected CustomAction(Resources resources, String key,
64 boolean isDialogAction) {
65 super();
66
67 m_key = key;
68
69 final String label = resources.getString(m_key + ".label", false);
70
71 if (label != null) {
72 if (isDialogAction) {
73 putValue(Action.NAME, label + "...");
74 }
75 else {
76 putValue(Action.NAME, label);
77 }
78 }
79
80 final String tip = resources.getString(m_key + ".tip", false);
81
82 if (tip != null) {
83 putValue(Action.SHORT_DESCRIPTION, tip);
84 }
85
86 final ImageIcon imageIcon = resources.getImageIcon(m_key + ".image");
87
88 if (imageIcon != null) {
89 putValue(Action.SMALL_ICON, imageIcon);
90 }
91
92 final ImageIcon rolloverImageIcon =
93 resources.getImageIcon(m_key + ".rollover-image");
94
95 if (rolloverImageIcon != null) {
96 putValue(CustomAction.ROLLOVER_ICON, rolloverImageIcon);
97 }
98 }
99
100 public final String getKey() {
101 return m_key;
102 }
103
104 public final void registerButton(final AbstractButton button) {
105 if (!m_buttonsWithRegisteredListeners.contains(button)) {
106 addPropertyChangeListener(
107 new PropertyChangeListener() {
108 public void propertyChange(PropertyChangeEvent e) {
109 if (e.getPropertyName().equals(SET_ACTION_PROPERTY)) {
110
111 final CustomAction newAction = (CustomAction)e.getNewValue();
112
113 button.setAction(newAction);
114 newAction.registerButton(button);
115 }
116 }
117 }
118 );
119
120 m_buttonsWithRegisteredListeners.add(button);
121 }
122 }
123
124 public void setRelevantToSelection(boolean b) {
125 putValue(RELEVANT_TO_SELECTION, Boolean.valueOf(b));
126 }
127
128 public boolean isRelevantToSelection() {
129 final Boolean b = (Boolean) getValue(RELEVANT_TO_SELECTION);
130 return b != null && b.booleanValue();
131 }
132 }