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