1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 12 import javax.swing.event.*; 13 import javax.swing.plaf.*; 14 import javax.accessibility.*; 15 16 import java.io.ObjectOutputStream ; 17 import java.io.ObjectInputStream ; 18 import java.io.IOException ; 19 20 21 48 public class JToggleButton extends AbstractButton implements Accessible { 49 50 54 private static final String uiClassID = "ToggleButtonUI"; 55 56 60 public JToggleButton () { 61 this(null, null, false); 62 } 63 64 70 public JToggleButton(Icon icon) { 71 this(null, icon, false); 72 } 73 74 82 public JToggleButton(Icon icon, boolean selected) { 83 this(null, icon, selected); 84 } 85 86 91 public JToggleButton (String text) { 92 this(text, null, false); 93 } 94 95 103 public JToggleButton (String text, boolean selected) { 104 this(text, null, selected); 105 } 106 107 113 public JToggleButton(Action a) { 114 this(); 115 setAction(a); 116 } 117 118 125 public JToggleButton(String text, Icon icon) { 126 this(text, icon, false); 127 } 128 129 138 public JToggleButton (String text, Icon icon, boolean selected) { 139 setModel(new ToggleButtonModel()); 141 142 model.setSelected(selected); 143 144 init(text, icon); 146 } 147 148 153 public void updateUI() { 154 setUI((ButtonUI)UIManager.getUI(this)); 155 } 156 157 167 public String getUIClassID() { 168 return uiClassID; 169 } 170 171 172 174 186 public static class ToggleButtonModel extends DefaultButtonModel { 187 188 191 public ToggleButtonModel () { 192 } 193 194 197 public boolean isSelected() { 198 return (stateMask & SELECTED) != 0; 202 } 204 205 206 211 public void setSelected(boolean b) { 212 ButtonGroup group = getGroup(); 213 if (group != null) { 214 group.setSelected(this, b); 216 b = group.isSelected(this); 217 } 218 219 if (isSelected() == b) { 220 return; 221 } 222 223 if (b) { 224 stateMask |= SELECTED; 225 } else { 226 stateMask &= ~SELECTED; 227 } 228 229 fireStateChanged(); 231 232 fireItemStateChanged( 234 new ItemEvent(this, 235 ItemEvent.ITEM_STATE_CHANGED, 236 this, 237 this.isSelected() ? ItemEvent.SELECTED : ItemEvent.DESELECTED)); 238 239 } 240 241 244 public void setPressed(boolean b) { 245 if ((isPressed() == b) || !isEnabled()) { 246 return; 247 } 248 249 if (b == false && isArmed()) { 250 setSelected(!this.isSelected()); 251 } 252 253 if (b) { 254 stateMask |= PRESSED; 255 } else { 256 stateMask &= ~PRESSED; 257 } 258 259 fireStateChanged(); 260 261 if(!isPressed() && isArmed()) { 262 int modifiers = 0; 263 AWTEvent currentEvent = EventQueue.getCurrentEvent(); 264 if (currentEvent instanceof InputEvent) { 265 modifiers = ((InputEvent)currentEvent).getModifiers(); 266 } else if (currentEvent instanceof ActionEvent) { 267 modifiers = ((ActionEvent)currentEvent).getModifiers(); 268 } 269 fireActionPerformed( 270 new ActionEvent(this, ActionEvent.ACTION_PERFORMED, 271 getActionCommand(), 272 EventQueue.getMostRecentEventTime(), 273 modifiers)); 274 } 275 276 } 277 } 278 279 280 284 private void writeObject(ObjectOutputStream s) throws IOException { 285 s.defaultWriteObject(); 286 if (getUIClassID().equals(uiClassID)) { 287 byte count = JComponent.getWriteObjCounter(this); 288 JComponent.setWriteObjCounter(this, --count); 289 if (count == 0 && ui != null) { 290 ui.installUI(this); 291 } 292 } 293 } 294 295 296 305 protected String paramString() { 306 return super.paramString(); 307 } 308 309 310 314 326 public AccessibleContext getAccessibleContext() { 327 if (accessibleContext == null) { 328 accessibleContext = new AccessibleJToggleButton(); 329 } 330 return accessibleContext; 331 } 332 333 348 protected class AccessibleJToggleButton extends AccessibleAbstractButton 349 implements ItemListener { 350 351 public AccessibleJToggleButton() { 352 super(); 353 JToggleButton.this.addItemListener(this); 354 } 355 356 360 public void itemStateChanged(ItemEvent e) { 361 JToggleButton tb = (JToggleButton ) e.getSource(); 362 if (JToggleButton.this.accessibleContext != null) { 363 if (tb.isSelected()) { 364 JToggleButton.this.accessibleContext.firePropertyChange( 365 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 366 null, AccessibleState.CHECKED); 367 } else { 368 JToggleButton.this.accessibleContext.firePropertyChange( 369 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 370 AccessibleState.CHECKED, null); 371 } 372 } 373 } 374 375 381 public AccessibleRole getAccessibleRole() { 382 return AccessibleRole.TOGGLE_BUTTON; 383 } 384 } } 386 387 | Popular Tags |