1 29 30 package nextapp.echo2.app; 31 32 import nextapp.echo2.app.button.ButtonGroup; 33 import nextapp.echo2.app.button.ButtonModel; 34 import nextapp.echo2.app.button.DefaultToggleButtonModel; 35 import nextapp.echo2.app.button.ToggleButton; 36 import nextapp.echo2.app.button.ToggleButtonModel; 37 import nextapp.echo2.app.event.ChangeEvent; 38 import nextapp.echo2.app.event.ChangeListener; 39 40 43 public class RadioButton extends ToggleButton { 44 45 public static final String BUTTON_GROUP_CHANGED_PROPERTY = "buttonGroup"; 46 47 50 private ChangeListener changeMonitor = new ChangeListener() { 51 52 55 public void stateChanged(ChangeEvent e) { 56 if (buttonGroup != null) { 57 buttonGroup.updateSelection(RadioButton.this); 58 } 59 } 60 }; 61 62 private ButtonGroup buttonGroup; 63 64 67 public RadioButton() { 68 this(null, null); 69 } 70 71 76 public RadioButton(String text) { 77 this(text, null); 78 } 79 80 85 public RadioButton(ImageReference icon) { 86 this(null, icon); 87 } 88 89 95 public RadioButton(String text, ImageReference icon) { 96 super(); 97 98 setModel(new DefaultToggleButtonModel()); 99 100 setIcon(icon); 101 setText(text); 102 } 103 104 111 public ButtonGroup getGroup() { 112 return buttonGroup; 113 } 114 115 122 public void setGroup(ButtonGroup newValue) { 123 ButtonGroup oldValue = buttonGroup; 124 buttonGroup = newValue; 125 126 if (oldValue != null) { 127 oldValue.removeButton(this); 128 } 129 if (newValue != null) { 130 newValue.addButton(this); 131 } 132 133 firePropertyChange(BUTTON_GROUP_CHANGED_PROPERTY, oldValue, newValue); 134 } 135 136 139 public void setModel(ButtonModel newValue) { 140 ButtonModel oldValue = getModel(); 141 super.setModel(newValue); 142 if (oldValue != null) { 143 ((ToggleButtonModel) oldValue).removeChangeListener(changeMonitor); 144 } 145 ((ToggleButtonModel) newValue).addChangeListener(changeMonitor); 146 if (buttonGroup != null) { 147 buttonGroup.updateSelection(this); 148 } 149 } 150 } 151 | Popular Tags |