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.JCheckBoxMenuItem;
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 IdCheckBoxMenuItem
47 extends JCheckBoxMenuItem
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 IdCheckBoxMenuItem(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 public IdCheckBoxMenuItem(final IdentifiableAction action, final boolean state)
88 {
89 super();
90
91 if (action == null)
92 {
93 throw new IllegalArgumentException("action must not be null");
94 }
95 setAction(action);
96 setState(state);
97 }
98
99
100
101
102
103
104
105
106 public IdCheckBoxMenuItem(final IdentifiableAction action, final IconSize iconSize)
107 {
108 super();
109
110 if (action == null)
111 {
112 throw new IllegalArgumentException("action must not be null");
113 }
114 setIconSize(iconSize);
115 setAction(action);
116 }
117
118
119
120
121
122
123
124
125
126 public IdCheckBoxMenuItem(final IdentifiableAction action, final IconSize iconSize, final boolean state)
127 {
128 super();
129
130 if (action == null)
131 {
132 throw new IllegalArgumentException("action must not be null");
133 }
134 setIconSize(iconSize);
135 setAction(action);
136 setState(state);
137 }
138
139
140
141
142
143
144
145 public IconSize getIconSize()
146 {
147 return iconSize;
148 }
149
150
151
152
153
154
155
156
157 public void setIconSize(final IconSize iconSize)
158 {
159 if (iconSize == null)
160 {
161 throw new IllegalArgumentException("iconSize must not be null");
162 }
163 IconSize oldIconSize = this.iconSize;
164 this.iconSize = iconSize;
165
166 if (!this.iconSize.equals(oldIconSize))
167 {
168 firePropertyChange("iconSize", oldIconSize, this.iconSize);
169 setDirty(true);
170 }
171 }
172
173
174
175
176
177
178 public IconTextDirection getIconTextDirection()
179 {
180 return iconTextDirection;
181 }
182
183 @Override
184 public void setComponentOrientation(final ComponentOrientation orientation)
185 {
186 ComponentOrientation oldOrientation = getComponentOrientation();
187
188 if (!oldOrientation.equals(orientation))
189 {
190 if (orientation != null)
191 {
192 iconTextDirection = orientation.isLeftToRight()
193 ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
194
195 setDirty(true);
196 }
197 }
198
199 super.setComponentOrientation(orientation);
200 }
201
202 @Override
203 public void applyComponentOrientation(final ComponentOrientation orientation)
204 {
205 ComponentOrientation oldOrientation = getComponentOrientation();
206
207 if (!oldOrientation.equals(orientation))
208 {
209 if (orientation != null)
210 {
211 iconTextDirection = orientation.isLeftToRight()
212 ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
213
214 setDirty(true);
215 }
216 }
217
218 super.applyComponentOrientation(orientation);
219 }
220
221 @Override
222 protected void configurePropertiesFromAction(final Action action)
223 {
224 super.configurePropertiesFromAction(action);
225
226 if (isDirty())
227 {
228 rebuild();
229 }
230 }
231
232
233
234
235
236
237
238 private void setDirty(final boolean dirty)
239 {
240 this.dirty = (this.dirty || dirty);
241 }
242
243
244
245
246
247
248 private boolean isDirty()
249 {
250 return dirty;
251 }
252
253
254
255
256
257 private void rebuild()
258 {
259 Action action = getAction();
260 if ((action != null) && (action instanceof IdentifiableAction))
261 {
262 IdentifiableAction/identify/IdentifiableAction.html#IdentifiableAction">IdentifiableAction identifiableAction = (IdentifiableAction) action;
263 IconBundle bndl = identifiableAction.getIconBundle();
264 setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize)));
265 setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize)));
266 setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
267 setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize)));
268 setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
269
270 }
271 dirty = false;
272 }
273 }