1 7 package java.awt; 8 9 import java.awt.peer.CheckboxPeer; 10 import java.awt.event.*; 11 import java.util.EventListener ; 12 import java.io.ObjectOutputStream ; 13 import java.io.ObjectInputStream ; 14 import java.io.IOException ; 15 import javax.accessibility.*; 16 17 18 59 public class Checkbox extends Component implements ItemSelectable , Accessible { 60 61 static { 62 63 Toolkit.loadLibraries(); 64 if (!GraphicsEnvironment.isHeadless()) { 65 initIDs(); 66 } 67 } 68 69 76 String label; 77 78 84 boolean state; 85 86 94 CheckboxGroup group; 95 96 transient ItemListener itemListener; 97 98 private static final String base = "checkbox"; 99 private static int nameCounter = 0; 100 101 104 private static final long serialVersionUID = 7270714317450821763L; 105 106 110 void setStateInternal(boolean state) { 111 this.state = state; 112 CheckboxPeer peer = (CheckboxPeer)this.peer; 113 if (peer != null) { 114 peer.setState(state); 115 } 116 } 117 118 126 public Checkbox() throws HeadlessException { 127 this("", false, null); 128 } 129 130 142 public Checkbox(String label) throws HeadlessException { 143 this(label, false, null); 144 } 145 146 159 public Checkbox(String label, boolean state) throws HeadlessException { 160 this(label, state, null); 161 } 162 163 178 public Checkbox(String label, boolean state, CheckboxGroup group) 179 throws HeadlessException { 180 GraphicsEnvironment.checkHeadless(); 181 this.label = label; 182 this.state = state; 183 this.group = group; 184 if (state && (group != null)) { 185 group.setSelectedCheckbox(this); 186 } 187 } 188 189 204 public Checkbox(String label, CheckboxGroup group, boolean state) 205 throws HeadlessException { 206 this(label, state, group); 207 } 208 209 215 String constructComponentName() { 216 synchronized (getClass()) { 217 return base + nameCounter++; 218 } 219 } 220 221 228 public void addNotify() { 229 synchronized (getTreeLock()) { 230 if (peer == null) 231 peer = getToolkit().createCheckbox(this); 232 super.addNotify(); 233 } 234 } 235 236 243 public String getLabel() { 244 return label; 245 } 246 247 254 public void setLabel(String label) { 255 boolean testvalid = false; 256 257 synchronized (this) { 258 if (label != this.label && (this.label == null || 259 !this.label.equals(label))) { 260 this.label = label; 261 CheckboxPeer peer = (CheckboxPeer)this.peer; 262 if (peer != null) { 263 peer.setLabel(label); 264 } 265 testvalid = true; 266 } 267 } 268 269 if (testvalid && valid) { 271 invalidate(); 272 } 273 } 274 275 283 public boolean getState() { 284 return state; 285 } 286 287 301 public void setState(boolean state) { 302 303 CheckboxGroup group = this.group; 304 if (group != null) { 305 if (state) { 306 group.setSelectedCheckbox(this); 307 } else if (group.getSelectedCheckbox() == this) { 308 state = true; 309 } 310 } 311 setStateInternal(state); 312 } 313 314 319 public Object [] getSelectedObjects() { 320 if (state) { 321 Object [] items = new Object [1]; 322 items[0] = label; 323 return items; 324 } 325 return null; 326 } 327 328 334 public CheckboxGroup getCheckboxGroup() { 335 return group; 336 } 337 338 354 public void setCheckboxGroup(CheckboxGroup g) { 355 CheckboxGroup oldGroup; 356 boolean oldState; 357 358 361 if (this.group == g) { 362 return; 363 } 364 365 synchronized (this) { 366 oldGroup = this.group; 367 oldState = getState(); 368 369 this.group = g; 370 CheckboxPeer peer = (CheckboxPeer)this.peer; 371 if (peer != null) { 372 peer.setCheckboxGroup(g); 373 } 374 if (this.group != null && getState()) { 375 if (this.group.getSelectedCheckbox() != null) { 376 setState(false); 377 } else { 378 this.group.setSelectedCheckbox(this); 379 } 380 } 381 } 382 383 391 if (oldGroup != null && oldState) { 392 oldGroup.setSelectedCheckbox(null); 393 } 394 } 395 396 410 public synchronized void addItemListener(ItemListener l) { 411 if (l == null) { 412 return; 413 } 414 itemListener = AWTEventMulticaster.add(itemListener, l); 415 newEventsOnly = true; 416 } 417 418 430 public synchronized void removeItemListener(ItemListener l) { 431 if (l == null) { 432 return; 433 } 434 itemListener = AWTEventMulticaster.remove(itemListener, l); 435 } 436 437 451 public synchronized ItemListener[] getItemListeners() { 452 return (ItemListener[]) (getListeners(ItemListener.class)); 453 } 454 455 488 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 489 EventListener l = null; 490 if (listenerType == ItemListener.class) { 491 l = itemListener; 492 } else { 493 return super.getListeners(listenerType); 494 } 495 return AWTEventMulticaster.getListeners(l, listenerType); 496 } 497 498 boolean eventEnabled(AWTEvent e) { 500 if (e.id == ItemEvent.ITEM_STATE_CHANGED) { 501 if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 || 502 itemListener != null) { 503 return true; 504 } 505 return false; 506 } 507 return super.eventEnabled(e); 508 } 509 510 524 protected void processEvent(AWTEvent e) { 525 if (e instanceof ItemEvent) { 526 processItemEvent((ItemEvent)e); 527 return; 528 } 529 super.processEvent(e); 530 } 531 532 556 protected void processItemEvent(ItemEvent e) { 557 ItemListener listener = itemListener; 558 if (listener != null) { 559 listener.itemStateChanged(e); 560 } 561 } 562 563 572 protected String paramString() { 573 String str = super.paramString(); 574 String label = this.label; 575 if (label != null) { 576 str += ",label=" + label; 577 } 578 return str + ",state=" + state; 579 } 580 581 582 584 585 589 private int checkboxSerializedDataVersion = 1; 590 591 610 private void writeObject(ObjectOutputStream s) 611 throws java.io.IOException 612 { 613 s.defaultWriteObject(); 614 615 AWTEventMulticaster.save(s, itemListenerK, itemListener); 616 s.writeObject(null); 617 } 618 619 635 private void readObject(ObjectInputStream s) 636 throws ClassNotFoundException , IOException , HeadlessException 637 { 638 GraphicsEnvironment.checkHeadless(); 639 s.defaultReadObject(); 640 641 Object keyOrNull; 642 while(null != (keyOrNull = s.readObject())) { 643 String key = ((String )keyOrNull).intern(); 644 645 if (itemListenerK == key) 646 addItemListener((ItemListener)(s.readObject())); 647 648 else s.readObject(); 650 } 651 } 652 653 656 private static native void initIDs(); 657 658 659 663 664 673 public AccessibleContext getAccessibleContext() { 674 if (accessibleContext == null) { 675 accessibleContext = new AccessibleAWTCheckbox(); 676 } 677 return accessibleContext; 678 } 679 680 685 protected class AccessibleAWTCheckbox extends AccessibleAWTComponent 686 implements ItemListener, AccessibleAction, AccessibleValue 687 { 688 691 private static final long serialVersionUID = 7881579233144754107L; 692 693 public AccessibleAWTCheckbox() { 694 super(); 695 Checkbox.this.addItemListener(this); 696 } 697 698 702 public void itemStateChanged(ItemEvent e) { 703 Checkbox cb = (Checkbox ) e.getSource(); 704 if (Checkbox.this.accessibleContext != null) { 705 if (cb.getState()) { 706 Checkbox.this.accessibleContext.firePropertyChange( 707 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 708 null, AccessibleState.CHECKED); 709 } else { 710 Checkbox.this.accessibleContext.firePropertyChange( 711 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 712 AccessibleState.CHECKED, null); 713 } 714 } 715 } 716 717 725 public AccessibleAction getAccessibleAction() { 726 return this; 727 } 728 729 737 public AccessibleValue getAccessibleValue() { 738 return this; 739 } 740 741 748 public int getAccessibleActionCount() { 749 return 0; } 751 752 757 public String getAccessibleActionDescription(int i) { 758 return null; } 760 761 767 public boolean doAccessibleAction(int i) { 768 return false; } 770 771 778 public Number getCurrentAccessibleValue() { 779 return null; } 781 782 788 public boolean setCurrentAccessibleValue(Number n) { 789 return false; } 791 792 799 public Number getMinimumAccessibleValue() { 800 return null; } 802 803 810 public Number getMaximumAccessibleValue() { 811 return null; } 813 814 821 public AccessibleRole getAccessibleRole() { 822 return AccessibleRole.CHECK_BOX; 823 } 824 825 832 public AccessibleStateSet getAccessibleStateSet() { 833 AccessibleStateSet states = super.getAccessibleStateSet(); 834 if (getState()) { 835 states.add(AccessibleState.CHECKED); 836 } 837 return states; 838 } 839 840 841 } 843 } 844 | Popular Tags |