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.ImageIcon;
29 import javax.swing.JMenu;
30
31 import org.dishevelled.iconbundle.IconBundle;
32 import org.dishevelled.iconbundle.IconSize;
33 import org.dishevelled.iconbundle.IconState;
34 import org.dishevelled.iconbundle.IconTextDirection;
35
36
37
38
39
40
41
42 public final class IdMenu
43 extends JMenu
44 {
45
46 public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
47
48
49 private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT;
50
51
52 private IconSize iconSize = DEFAULT_ICON_SIZE;
53
54
55 private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION;
56
57
58 private Object value;
59
60
61
62
63
64 public IdMenu()
65 {
66 this(null);
67 }
68
69
70
71
72
73
74 public IdMenu(final Object value)
75 {
76 super();
77 setValue(value);
78 }
79
80
81
82
83
84
85
86 public IdMenu(final Object value, final IconSize iconSize)
87 {
88 super();
89 setValue(value);
90
91 setIconSize(iconSize);
92 }
93
94
95
96
97
98
99
100 public Object getValue()
101 {
102 return value;
103 }
104
105
106
107
108
109
110
111
112 public void setValue(final Object value)
113 {
114 Object oldValue = this.value;
115 this.value = value;
116 firePropertyChange("value", oldValue, this.value);
117 rebuild();
118 }
119
120
121
122
123
124
125 public IconSize getIconSize()
126 {
127 return iconSize;
128 }
129
130
131
132
133
134
135
136
137 public void setIconSize(final IconSize iconSize)
138 {
139 if (iconSize == null)
140 {
141 throw new IllegalArgumentException("iconSize must not be null");
142 }
143 IconSize oldIconSize = this.iconSize;
144 this.iconSize = iconSize;
145
146 if (!this.iconSize.equals(oldIconSize))
147 {
148 firePropertyChange("iconSize", oldIconSize, this.iconSize);
149 rebuild();
150 }
151 }
152
153
154
155
156
157
158 public IconTextDirection getIconTextDirection()
159 {
160 return iconTextDirection;
161 }
162
163 @Override
164 public void setComponentOrientation(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 rebuild();
176 }
177 }
178
179 super.setComponentOrientation(orientation);
180 }
181
182 @Override
183 public void applyComponentOrientation(final ComponentOrientation orientation)
184 {
185 ComponentOrientation oldOrientation = getComponentOrientation();
186
187 if (!oldOrientation.equals(orientation))
188 {
189 if (orientation != null)
190 {
191 iconTextDirection = orientation.isLeftToRight()
192 ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
193
194 rebuild();
195 }
196 }
197
198 super.applyComponentOrientation(orientation);
199 }
200
201
202
203
204
205
206 private void rebuild()
207 {
208 String name = IdentifyUtils.getNameFor(value);
209 setText(name);
210 IconBundle bndl = IdentifyUtils.getIconBundleFor(value);
211
212 if (bndl == null)
213 {
214 setIcon(null);
215 }
216 else
217 {
218 setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize)));
219 setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize)));
220 setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
221 setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize)));
222 setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
223
224 }
225 }
226 }