001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.springframework.richclient.command.config; 017 018 import java.awt.Image; 019 020 import javax.swing.AbstractButton; 021 import javax.swing.Icon; 022 import javax.swing.ImageIcon; 023 024 import org.springframework.core.style.ToStringCreator; 025 import org.springframework.richclient.core.VisualizedElement; 026 import org.springframework.richclient.factory.ButtonConfigurer; 027 import org.springframework.util.Assert; 028 import org.springframework.util.ObjectUtils; 029 030 /** 031 * A parameter object consisting of the various icons that may be displayed on a 032 * single command button depending on its state. 033 * 034 * <p> 035 * The set of states for which this object maintains icons are as follows: 036 * 037 * <ul> 038 * <li>default</li> 039 * <li>selected</li> 040 * <li>disabled</li> 041 * <li>pressed</li> 042 * <li>rollover</li> 043 * </ul> 044 * 045 * </p> 046 * 047 * @author Keith Donald 048 * 049 */ 050 public class CommandButtonIconInfo implements ButtonConfigurer, VisualizedElement { 051 052 /** 053 * A {@code CommandButtonIconInfo} instance that can be used for command 054 * buttons that have no icon information associated with them. 055 */ 056 // FIXME this is a mutable object so we probably shouldn't be reusing the 057 // same instance for 058 // a blank IconInfo. What happens if multiple blanks have been dished out 059 // and one of them is modified? 060 public static final CommandButtonIconInfo BLANK_ICON_INFO = new CommandButtonIconInfo(null); 061 062 private Icon icon; 063 064 private Icon selectedIcon; 065 066 private Icon disabledIcon; 067 068 private Icon pressedIcon; 069 070 private Icon rolloverIcon; 071 072 /** 073 * Creates a new {@code CommandButtonIconInfo} with the given icon. No 074 * optional icons will be associated with this instance. 075 * 076 * @param icon The main displayable icon. 077 */ 078 public CommandButtonIconInfo(Icon icon) { 079 this.icon = icon; 080 } 081 082 /** 083 * Creates a new {@code CommandButtonIconInfo} with the given icons. 084 * 085 * @param icon The main default icon. May be null. 086 * @param selectedIcon The icon to be displayed when the command button is 087 * in a selected state. May be null. 088 */ 089 public CommandButtonIconInfo(Icon icon, Icon selectedIcon) { 090 this.icon = icon; 091 this.selectedIcon = selectedIcon; 092 } 093 094 /** 095 * Creates a new {@code CommandButtonIconInfo} with the given icons. 096 * 097 * @param icon The main default icon. May be null. 098 * @param selectedIcon The icon to be displayed when the command button is 099 * 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 }