1 /*
2 * Copyright 2002-2004 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.springframework.richclient.command.config;
17
18 import java.awt.Image;
19
20 import javax.swing.AbstractButton;
21 import javax.swing.Icon;
22 import javax.swing.ImageIcon;
23
24 import org.springframework.core.style.ToStringCreator;
25 import org.springframework.richclient.core.VisualizedElement;
26 import org.springframework.richclient.factory.ButtonConfigurer;
27 import org.springframework.util.Assert;
28 import org.springframework.util.ObjectUtils;
29
30 /**
31 * A parameter object consisting of the various icons that may be displayed on a
32 * single command button depending on its state.
33 *
34 * <p>
35 * The set of states for which this object maintains icons are as follows:
36 *
37 * <ul>
38 * <li>default</li>
39 * <li>selected</li>
40 * <li>disabled</li>
41 * <li>pressed</li>
42 * <li>rollover</li>
43 * </ul>
44 *
45 * </p>
46 *
47 * @author Keith Donald
48 *
49 */
50 public class CommandButtonIconInfo implements ButtonConfigurer, VisualizedElement {
51
52 /**
53 * A {@code CommandButtonIconInfo} instance that can be used for command
54 * buttons that have no icon information associated with them.
55 */
56 // FIXME this is a mutable object so we probably shouldn't be reusing the
57 // same instance for
58 // a blank IconInfo. What happens if multiple blanks have been dished out
59 // and one of them is modified?
60 public static final CommandButtonIconInfo BLANK_ICON_INFO = new CommandButtonIconInfo(null);
61
62 private Icon icon;
63
64 private Icon selectedIcon;
65
66 private Icon disabledIcon;
67
68 private Icon pressedIcon;
69
70 private Icon rolloverIcon;
71
72 /**
73 * Creates a new {@code CommandButtonIconInfo} with the given icon. No
74 * optional icons will be associated with this instance.
75 *
76 * @param icon The main displayable icon.
77 */
78 public CommandButtonIconInfo(Icon icon) {
79 this.icon = icon;
80 }
81
82 /**
83 * Creates a new {@code CommandButtonIconInfo} with the given icons.
84 *
85 * @param icon The main default icon. May be null.
86 * @param selectedIcon The icon to be displayed when the command button is
87 * in a selected state. May be null.
88 */
89 public CommandButtonIconInfo(Icon icon, Icon selectedIcon) {
90 this.icon = icon;
91 this.selectedIcon = selectedIcon;
92 }
93
94 /**
95 * Creates a new {@code CommandButtonIconInfo} with the given icons.
96 *
97 * @param icon The main default icon. May be null.
98 * @param selectedIcon The icon to be displayed when the command button is
99 * in a selected state.
100 * @param rolloverIcon The icon to be displayed when the mouse pointer rolls
101 * over the command button. May be null.
102 */
103 public CommandButtonIconInfo(Icon icon, Icon selectedIcon, Icon rolloverIcon) {
104 this.icon = icon;
105 this.selectedIcon = selectedIcon;
106 this.rolloverIcon = rolloverIcon;
107 }
108
109 /**
110 * Creates a new {@code CommandButtonIconInfo} with the given icons.
111 *
112 * @param icon The main default icon. May be null.
113 * @param selectedIcon The icon to be displayed when the command button is
114 * in a selected state.
115 * @param rolloverIcon The icon to be displayed when the mouse pointer rolls
116 * over the command button. May be null.
117 * @param disabledIcon The icon to be displayed when the command button is
118 * in a disabled state. May be null.
119 * @param pressedIcon The icon to be displayed when the command button is in
120 * a pressed state. May be null.
121 */
122 public CommandButtonIconInfo(Icon icon, Icon selectedIcon, Icon rolloverIcon, Icon disabledIcon, Icon pressedIcon) {
123 this.icon = icon;
124 this.selectedIcon = selectedIcon;
125 this.rolloverIcon = rolloverIcon;
126 this.disabledIcon = disabledIcon;
127 this.pressedIcon = pressedIcon;
128 }
129
130 /**
131 * Configures the given command button with the icon values from this
132 * instance.
133 *
134 * @param button The button to be configured with icons. Must not be null.
135 *
136 * @throws IllegalArgumentException if {@code button} is null.
137 */
138 public AbstractButton configure(AbstractButton button) {
139 Assert.notNull(button, "The button to configure is required");
140
141 button.setIcon(icon);
142 button.setSelectedIcon(selectedIcon);
143 button.setDisabledIcon(disabledIcon);
144 button.setPressedIcon(pressedIcon);
145 button.setRolloverIcon(rolloverIcon);
146
147 return button;
148 }
149
150 /**
151 * Returns the image from the main default icon of this instance if it is
152 * actually an instance of an {@link ImageIcon}.
153 *
154 * @return The image from the main default icon, or null if there is no
155 * default icon or it is not an {@link ImageIcon}.
156 */
157 public Image getImage() {
158 if (getIcon() instanceof ImageIcon) {
159 return ((ImageIcon) getIcon()).getImage();
160 }
161
162 return null;
163 }
164
165 /**
166 * {@inheritDoc}
167 */
168 public Icon getIcon() {
169 return icon;
170 }
171
172 /**
173 * Returns the icon to be displayed when the command button is in a disabled
174 * state.
175 *
176 * @return The icon for the command button in disabled state, or null.
177 */
178 public Icon getDisabledIcon() {
179 return disabledIcon;
180 }
181
182 /**
183 * Returns the icon to be displayed when the command button is in a pressed
184 * state.
185 *
186 * @return The icon for the command button in pressed state, or null.
187 */
188 public Icon getPressedIcon() {
189 return pressedIcon;
190 }
191
192 /**
193 * Returns the icon to be displayed when the mouse pointer rolls over the
194 * command button.
195 *
196 * @return The icon for the command button when rolled over by the mouse
197 * pointer, or null.
198 */
199 public Icon getRolloverIcon() {
200 return rolloverIcon;
201 }
202
203 /**
204 * Returns the icon to be displayed when the command button is in a selected
205 * state.
206 *
207 * @return The icon for the command button in selected state, or null.
208 */
209 public Icon getSelectedIcon() {
210 return selectedIcon;
211 }
212
213 /**
214 * Sets the main default icon for the command button.
215 *
216 * @param icon The main default icon for the command button. May be null.
217 */
218 public void setIcon(Icon icon) {
219 this.icon = icon;
220 }
221
222 /**
223 * Sets the icon to be displayed when the command button is in a disabled
224 * state.
225 *
226 * @param disabledIcon The icon for the button in a disabled state. May be
227 * null.
228 */
229 public void setDisabledIcon(Icon disabledIcon) {
230 this.disabledIcon = disabledIcon;
231 }
232
233 /**
234 * Sets the icon to be displayed when the command button is in a pressed
235 * state.
236 *
237 * @param pressedIcon The icon for the button in a pressed state. May be
238 * null.
239 */
240 public void setPressedIcon(Icon pressedIcon) {
241 this.pressedIcon = pressedIcon;
242 }
243
244 /**
245 * Sets the icon to be displayed when the mouse pointer rolls over the
246 * command button.
247 *
248 * @param rolloverIcon The icon for the button in a rolled over. May be
249 * null.
250 */
251 public void setRolloverIcon(Icon rolloverIcon) {
252 this.rolloverIcon = rolloverIcon;
253 }
254
255 /**
256 * Sets the icon to be displayed when the command button is in a pressed
257 * state.
258 *
259 * @param selectedIcon The icon for the button in a pressed state. May be
260 * null.
261 */
262 public void setSelectedIcon(Icon selectedIcon) {
263 this.selectedIcon = selectedIcon;
264 }
265
266 /**
267 * {@inheritDoc}
268 */
269 public String toString() {
270 return new ToStringCreator(this).toString();
271 }
272
273 /**
274 * {@inheritDoc}
275 */
276 public boolean equals(Object o) {
277 if (!(o instanceof CommandButtonIconInfo)) {
278 return false;
279 }
280 CommandButtonIconInfo other = (CommandButtonIconInfo) o;
281 return ObjectUtils.nullSafeEquals(icon, other.icon)
282 && ObjectUtils.nullSafeEquals(disabledIcon, other.disabledIcon)
283 && ObjectUtils.nullSafeEquals(pressedIcon, other.pressedIcon)
284 && ObjectUtils.nullSafeEquals(rolloverIcon, other.rolloverIcon)
285 && ObjectUtils.nullSafeEquals(selectedIcon, other.selectedIcon);
286 }
287
288 /**
289 * {@inheritDoc}
290 */
291 public int hashCode() {
292 int hash = 1;
293 hash = hash * 31 + (icon == null ? 0 : icon.hashCode());
294 hash = hash * 31 + (disabledIcon == null ? 0 : disabledIcon.hashCode());
295 hash = hash * 31 + (pressedIcon == null ? 0 : pressedIcon.hashCode());
296 hash = hash * 31 + (rolloverIcon == null ? 0 : rolloverIcon.hashCode());
297 hash = hash * 31 + (selectedIcon == null ? 0 : selectedIcon.hashCode());
298 return hash;
299 }
300
301 }