1 29 30 package nextapp.echo2.app.button; 31 32 import java.util.EventListener ; 33 34 import nextapp.echo2.app.Alignment; 35 import nextapp.echo2.app.Extent; 36 import nextapp.echo2.app.ImageReference; 37 import nextapp.echo2.app.event.ChangeEvent; 38 import nextapp.echo2.app.event.ChangeListener; 39 40 43 public abstract class ToggleButton extends AbstractButton { 44 45 public static final String PROPERTY_PRESSED_STATE_ICON = "pressedStateIcon"; 46 public static final String PROPERTY_PRESSED_SELECTED_STATE_ICON = "pressedSelectedStateIcon"; 47 public static final String PROPERTY_ROLLOVER_STATE_ICON = "rolloverStateIcon"; 48 public static final String PROPERTY_ROLLOVER_SELECTED_STATE_ICON = "rolloverSelectedStateIcon"; 49 public static final String PROPERTY_STATE_ALIGNMENT = "stateAlignment"; 50 public static final String PROPERTY_STATE_ICON = "stateIcon"; 51 public static final String PROPERTY_STATE_MARGIN = "stateMargin"; 52 public static final String PROPERTY_STATE_POSITION = "statePosition"; 53 public static final String PROPERTY_SELECTED_STATE_ICON = "selectedStateIcon"; 54 55 public static final String SELECTED_CHANGED_PROPERTY = "selected"; 56 57 61 private ChangeListener changeForwarder = new ChangeListener() { 62 63 66 public void stateChanged(ChangeEvent e) { 67 fireStateChanged(); 68 if (isSelected()) { 69 firePropertyChange(SELECTED_CHANGED_PROPERTY, Boolean.FALSE, Boolean.TRUE); 70 } else { 71 firePropertyChange(SELECTED_CHANGED_PROPERTY, Boolean.TRUE, Boolean.FALSE); 72 } 73 } 74 }; 75 76 82 public void addChangeListener(ChangeListener l) { 83 getEventListenerList().addListener(ChangeListener.class, l); 84 } 85 86 89 public void fireStateChanged() { 90 EventListener [] listeners = getEventListenerList().getListeners(ChangeListener.class); 91 ChangeEvent e = new ChangeEvent(this); 92 for (int index = 0; index < listeners.length; ++index) { 93 ((ChangeListener) listeners[index]).stateChanged(e); 94 } 95 } 96 97 103 public ImageReference getPressedSelectedStateIcon() { 104 return (ImageReference) getProperty(PROPERTY_PRESSED_SELECTED_STATE_ICON); 105 } 106 107 113 public ImageReference getPressedStateIcon() { 114 return (ImageReference) getProperty(PROPERTY_PRESSED_STATE_ICON); 115 } 116 117 123 public ImageReference getRolloverSelectedStateIcon() { 124 return (ImageReference) getProperty(PROPERTY_ROLLOVER_SELECTED_STATE_ICON); 125 } 126 127 133 public ImageReference getRolloverStateIcon() { 134 return (ImageReference) getProperty(PROPERTY_ROLLOVER_STATE_ICON); 135 } 136 137 142 public ImageReference getSelectedStateIcon() { 143 return (ImageReference) getProperty(PROPERTY_SELECTED_STATE_ICON); 144 } 145 146 152 public Alignment getStateAlignment() { 153 return (Alignment) getProperty(PROPERTY_STATE_ALIGNMENT); 154 } 155 156 161 public ImageReference getStateIcon() { 162 return (ImageReference) getProperty(PROPERTY_STATE_ICON); 163 } 164 165 175 public Extent getStateMargin() { 176 return (Extent) getProperty(PROPERTY_STATE_MARGIN); 177 } 178 179 185 public Alignment getStatePosition() { 186 return (Alignment) getProperty(PROPERTY_STATE_POSITION); 187 } 188 189 194 public boolean isSelected() { 195 return ((ToggleButtonModel) getModel()).isSelected(); 196 } 197 198 201 public void processInput(String name, Object value) { 202 super.processInput(name, value); 203 if (SELECTED_CHANGED_PROPERTY.equals(name)) { 204 setSelected(((Boolean ) value).booleanValue()); 205 } 206 } 207 208 214 public void removeChangeListener(ChangeListener l) { 215 getEventListenerList().removeListener(ChangeListener.class, l); 216 } 217 218 221 public void setModel(ButtonModel newValue) { 222 if (!(newValue instanceof ToggleButtonModel)) { 223 throw new IllegalArgumentException ("Model is not a ToggleButtonModel."); 224 } 225 226 ToggleButtonModel oldValue = (ToggleButtonModel) getModel(); 227 228 if (oldValue != null) { 229 oldValue.removeChangeListener(changeForwarder); 230 } 231 232 super.setModel(newValue); 233 234 ((ToggleButtonModel) newValue).addChangeListener(changeForwarder); 235 } 236 237 243 public void setPressedSelectedStateIcon(ImageReference newValue) { 244 setProperty(PROPERTY_PRESSED_SELECTED_STATE_ICON, newValue); 245 } 246 247 253 public void setPressedStateIcon(ImageReference newValue) { 254 setProperty(PROPERTY_PRESSED_STATE_ICON, newValue); 255 } 256 257 263 public void setRolloverSelectedStateIcon(ImageReference newValue) { 264 setProperty(PROPERTY_ROLLOVER_SELECTED_STATE_ICON, newValue); 265 } 266 267 273 public void setRolloverStateIcon(ImageReference newValue) { 274 setProperty(PROPERTY_ROLLOVER_STATE_ICON, newValue); 275 } 276 277 282 public void setSelectedStateIcon(ImageReference newValue) { 283 setProperty(PROPERTY_SELECTED_STATE_ICON, newValue); 284 } 285 286 291 public void setSelected(boolean newValue) { 292 ((ToggleButtonModel) getModel()).setSelected(newValue); 293 } 294 295 303 public void setStateAlignment(Alignment newValue) { 304 setProperty(PROPERTY_STATE_ALIGNMENT, newValue); 305 } 306 307 312 public void setStateIcon(ImageReference newValue) { 313 setProperty(PROPERTY_STATE_ICON, newValue); 314 } 315 316 326 public void setStateMargin(Extent newValue) { 327 setProperty(PROPERTY_STATE_MARGIN, newValue); 328 } 329 330 338 public void setStatePosition(Alignment newValue) { 339 setProperty(PROPERTY_STATE_POSITION, newValue); 340 } 341 } 342 | Popular Tags |