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.JButton;
31 import javax.swing.UIManager;
32
33 import org.dishevelled.iconbundle.IconBundle;
34 import org.dishevelled.iconbundle.IconSize;
35 import org.dishevelled.iconbundle.IconState;
36 import org.dishevelled.iconbundle.IconTextDirection;
37
38
39
40
41
42
43
44
45
46
47
48 public final class IdButton
49 extends JButton
50 {
51
52 public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
53
54
55 private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT;
56
57
58 private IconSize iconSize = DEFAULT_ICON_SIZE;
59
60
61 private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION;
62
63
64
65
66
67
68
69 public IdButton(final IdentifiableAction action)
70 {
71 super();
72
73 UIManager.put("Button.gradient", null);
74 UIManager.getDefaults().put("Button.gradient", null);
75 UIManager.getLookAndFeelDefaults().put("Button.gradient", null);
76
77 if (action == null)
78 {
79 throw new IllegalArgumentException("action must not be null");
80 }
81 setAction(action);
82 }
83
84
85
86
87
88
89
90
91 public IdButton(final IdentifiableAction action, final IconSize iconSize)
92 {
93 super();
94
95 UIManager.put("Button.gradient", null);
96 UIManager.getDefaults().put("Button.gradient", null);
97 UIManager.getLookAndFeelDefaults().put("Button.gradient", null);
98
99 if (action == null)
100 {
101 throw new IllegalArgumentException("action must not be null");
102 }
103 setIconSize(iconSize);
104 setAction(action);
105 }
106
107
108
109
110
111
112
113 public IconSize getIconSize()
114 {
115 return iconSize;
116 }
117
118
119
120
121
122
123
124
125 public void setIconSize(final IconSize iconSize)
126 {
127 if (iconSize == null)
128 {
129 throw new IllegalArgumentException("iconSize must not be null");
130 }
131 IconSize oldIconSize = this.iconSize;
132 this.iconSize = iconSize;
133
134 if (!this.iconSize.equals(oldIconSize))
135 {
136 firePropertyChange("iconSize", oldIconSize, this.iconSize);
137 rebuild();
138 }
139 }
140
141
142
143
144
145
146 IconTextDirection getIconTextDirection()
147 {
148 return iconTextDirection;
149 }
150
151
152
153
154 public void displayIcon()
155 {
156
157 setText(null);
158 rebuild();
159 }
160
161
162
163
164 public void displayText()
165 {
166
167 Action action = getAction();
168 if ((action != null) && (action instanceof IdentifiableAction))
169 {
170 IdentifiableAction identifiableAction = (IdentifiableAction) action;
171 setText(identifiableAction.getName());
172 }
173 setIcon(null);
174 setPressedIcon(null);
175 setSelectedIcon(null);
176 setRolloverIcon(null);
177 setRolloverSelectedIcon(null);
178 setDisabledIcon(null);
179 }
180
181
182
183
184 public void displayIconAndText()
185 {
186
187 Action action = getAction();
188 if ((action != null) && (action instanceof IdentifiableAction))
189 {
190 IdentifiableAction identifiableAction = (IdentifiableAction) action;
191 setText(identifiableAction.getName());
192 }
193 rebuild();
194 }
195
196
197 public void setComponentOrientation(final ComponentOrientation orientation)
198 {
199 ComponentOrientation oldOrientation = getComponentOrientation();
200
201 if (!oldOrientation.equals(orientation))
202 {
203 if (orientation != null)
204 {
205 iconTextDirection = orientation.isLeftToRight()
206 ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
207
208 rebuild();
209 }
210 }
211
212 super.setComponentOrientation(orientation);
213 }
214
215
216 public void applyComponentOrientation(final ComponentOrientation orientation)
217 {
218 ComponentOrientation oldOrientation = getComponentOrientation();
219
220 if (!oldOrientation.equals(orientation))
221 {
222 if (orientation != null)
223 {
224 iconTextDirection = orientation.isLeftToRight()
225 ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
226
227 rebuild();
228 }
229 }
230
231 super.applyComponentOrientation(orientation);
232 }
233
234
235 protected void configurePropertiesFromAction(final Action action)
236 {
237 super.configurePropertiesFromAction(action);
238 rebuild();
239 }
240
241
242
243
244
245 private void rebuild()
246 {
247 Action action = getAction();
248 if ((action != null) && (action instanceof IdentifiableAction))
249 {
250 IdentifiableAction identifiableAction = (IdentifiableAction) action;
251 IconBundle bndl = identifiableAction.getIconBundle();
252 setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize)));
253 setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize)));
254 setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
255 setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize)));
256 setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
257
258 }
259 }
260 }