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