1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dishevelled.identify;
25
26 import java.awt.ComponentOrientation;
27
28 import javax.swing.Action;
29 import javax.swing.ImageIcon;
30 import javax.swing.JMenuItem;
31
32 import org.dishevelled.iconbundle.IconBundle;
33 import org.dishevelled.iconbundle.IconSize;
34 import org.dishevelled.iconbundle.IconState;
35 import org.dishevelled.iconbundle.IconTextDirection;
36
37
38
39
40
41
42
43
44
45
46
47 public final class IdMenuItem
48 extends JMenuItem
49 {
50
51 public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
52
53
54 private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT;
55
56
57 private IconSize iconSize = DEFAULT_ICON_SIZE;
58
59
60 private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION;
61
62
63 private transient boolean dirty = true;
64
65
66
67
68
69
70
71 public IdMenuItem(final IdentifiableAction action)
72 {
73 super();
74
75 if (action == null)
76 {
77 throw new IllegalArgumentException("action must not be null");
78 }
79 setAction(action);
80 }
81
82
83
84
85
86
87
88
89 public IdMenuItem(final IdentifiableAction action, final IconSize iconSize)
90 {
91 super();
92
93 if (action == null)
94 {
95 throw new IllegalArgumentException("action must not be null");
96 }
97 setIconSize(iconSize);
98 setAction(action);
99 }
100
101
102
103
104
105
106
107 public IconSize getIconSize()
108 {
109 return iconSize;
110 }
111
112
113
114
115
116
117
118
119 public void setIconSize(final IconSize iconSize)
120 {
121 if (iconSize == null)
122 {
123 throw new IllegalArgumentException("iconSize must not be null");
124 }
125 IconSize oldIconSize = this.iconSize;
126 this.iconSize = iconSize;
127
128 if (!this.iconSize.equals(oldIconSize))
129 {
130 firePropertyChange("iconSize", oldIconSize, this.iconSize);
131 setDirty(true);
132 }
133 }
134
135
136
137
138
139
140 public IconTextDirection getIconTextDirection()
141 {
142 return iconTextDirection;
143 }
144
145
146 public void setComponentOrientation(final ComponentOrientation orientation)
147 {
148 ComponentOrientation oldOrientation = getComponentOrientation();
149
150 if (!oldOrientation.equals(orientation))
151 {
152 if (orientation != null)
153 {
154 iconTextDirection = orientation.isLeftToRight()
155 ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
156
157 setDirty(true);
158 }
159 }
160
161 super.setComponentOrientation(orientation);
162 }
163
164
165 public void applyComponentOrientation(final ComponentOrientation orientation)
166 {
167 ComponentOrientation oldOrientation = getComponentOrientation();
168
169 if (!oldOrientation.equals(orientation))
170 {
171 if (orientation != null)
172 {
173 iconTextDirection = orientation.isLeftToRight()
174 ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
175
176 setDirty(true);
177 }
178 }
179
180 super.applyComponentOrientation(orientation);
181 }
182
183
184 protected void configurePropertiesFromAction(final Action action)
185 {
186 super.configurePropertiesFromAction(action);
187
188 if (isDirty())
189 {
190 rebuild();
191 }
192 }
193
194
195
196
197
198
199
200 private void setDirty(final boolean dirty)
201 {
202 this.dirty = (this.dirty || dirty);
203 }
204
205
206
207
208
209
210 private boolean isDirty()
211 {
212 return dirty;
213 }
214
215
216
217
218
219 private void rebuild()
220 {
221 Action action = getAction();
222 if ((action != null) && (action instanceof IdentifiableAction))
223 {
224 IdentifiableAction identifiableAction = (IdentifiableAction) action;
225 IconBundle bndl = identifiableAction.getIconBundle();
226 setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize)));
227 setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize)));
228 setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
229 setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize)));
230 setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
231 setDisabledIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.DISABLED, iconSize)));
232 }
233 dirty = false;
234 }
235 }