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