1 7 package java.awt; 8 9 import java.awt.peer.CheckboxMenuItemPeer; 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 46 public class CheckboxMenuItem extends MenuItem implements ItemSelectable , Accessible { 47 48 static { 49 50 Toolkit.loadLibraries(); 51 if (!GraphicsEnvironment.isHeadless()) { 52 initIDs(); 53 } 54 } 55 56 62 boolean state = false; 63 64 transient ItemListener itemListener; 65 66 private static final String base = "chkmenuitem"; 67 private static int nameCounter = 0; 68 69 72 private static final long serialVersionUID = 6190621106981774043L; 73 74 82 public CheckboxMenuItem() throws HeadlessException { 83 this("", false); 84 } 85 86 96 public CheckboxMenuItem(String label) throws HeadlessException { 97 this(label, false); 98 } 99 100 112 public CheckboxMenuItem(String label, boolean state) 113 throws HeadlessException { 114 super(label); 115 this.state = state; 116 } 117 118 122 String constructComponentName() { 123 synchronized (getClass()) { 124 return base + nameCounter++; 125 } 126 } 127 128 136 public void addNotify() { 137 synchronized (getTreeLock()) { 138 if (peer == null) 139 peer = Toolkit.getDefaultToolkit().createCheckboxMenuItem(this); 140 super.addNotify(); 141 } 142 } 143 144 153 public boolean getState() { 154 return state; 155 } 156 157 173 public synchronized void setState(boolean b) { 174 state = b; 175 CheckboxMenuItemPeer peer = (CheckboxMenuItemPeer)this.peer; 176 if (peer != null) { 177 peer.setState(b); 178 } 179 } 180 181 186 public synchronized Object [] getSelectedObjects() { 187 if (state) { 188 Object [] items = new Object [1]; 189 items[0] = label; 190 return items; 191 } 192 return null; 193 } 194 195 209 public synchronized void addItemListener(ItemListener l) { 210 if (l == null) { 211 return; 212 } 213 itemListener = AWTEventMulticaster.add(itemListener, l); 214 newEventsOnly = true; 215 } 216 217 229 public synchronized void removeItemListener(ItemListener l) { 230 if (l == null) { 231 return; 232 } 233 itemListener = AWTEventMulticaster.remove(itemListener, l); 234 } 235 236 250 public synchronized ItemListener[] getItemListeners() { 251 return (ItemListener[])(getListeners(ItemListener.class)); 252 } 253 254 287 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 288 EventListener l = null; 289 if (listenerType == ItemListener.class) { 290 l = itemListener; 291 } else { 292 return super.getListeners(listenerType); 293 } 294 return AWTEventMulticaster.getListeners(l, listenerType); 295 } 296 297 boolean eventEnabled(AWTEvent e) { 299 if (e.id == ItemEvent.ITEM_STATE_CHANGED) { 300 if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 || 301 itemListener != null) { 302 return true; 303 } 304 return false; 305 } 306 return super.eventEnabled(e); 307 } 308 309 326 protected void processEvent(AWTEvent e) { 327 if (e instanceof ItemEvent) { 328 processItemEvent((ItemEvent)e); 329 return; 330 } 331 super.processEvent(e); 332 } 333 334 357 protected void processItemEvent(ItemEvent e) { 358 ItemListener listener = itemListener; 359 if (listener != null) { 360 listener.itemStateChanged(e); 361 } 362 } 363 364 367 void doMenuEvent(long when, int modifiers) { 368 setState(!state); 369 Toolkit.getEventQueue().postEvent( 370 new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, 371 getLabel(), 372 state ? ItemEvent.SELECTED : 373 ItemEvent.DESELECTED)); 374 } 375 376 386 public String paramString() { 387 return super.paramString() + ",state=" + state; 388 } 389 390 392 393 397 private int checkboxMenuItemSerializedDataVersion = 1; 398 399 418 private void writeObject(ObjectOutputStream s) 419 throws java.io.IOException 420 { 421 s.defaultWriteObject(); 422 423 AWTEventMulticaster.save(s, itemListenerK, itemListener); 424 s.writeObject(null); 425 } 426 427 439 private void readObject(ObjectInputStream s) 440 throws ClassNotFoundException , IOException 441 { 442 s.defaultReadObject(); 443 444 Object keyOrNull; 445 while(null != (keyOrNull = s.readObject())) { 446 String key = ((String )keyOrNull).intern(); 447 448 if (itemListenerK == key) 449 addItemListener((ItemListener)(s.readObject())); 450 451 else s.readObject(); 453 } 454 } 455 456 459 private static native void initIDs(); 460 461 462 466 475 public AccessibleContext getAccessibleContext() { 476 if (accessibleContext == null) { 477 accessibleContext = new AccessibleAWTCheckboxMenuItem(); 478 } 479 return accessibleContext; 480 } 481 482 493 protected class AccessibleAWTCheckboxMenuItem extends AccessibleAWTMenuItem 494 implements AccessibleAction, AccessibleValue 495 { 496 499 private static final long serialVersionUID = -1122642964303476L; 500 501 509 public AccessibleAction getAccessibleAction() { 510 return this; 511 } 512 513 521 public AccessibleValue getAccessibleValue() { 522 return this; 523 } 524 525 532 public int getAccessibleActionCount() { 533 return 0; } 535 536 541 public String getAccessibleActionDescription(int i) { 542 return null; } 544 545 551 public boolean doAccessibleAction(int i) { 552 return false; } 554 555 562 public Number getCurrentAccessibleValue() { 563 return null; } 565 566 572 public boolean setCurrentAccessibleValue(Number n) { 573 return false; } 575 576 583 public Number getMinimumAccessibleValue() { 584 return null; } 586 587 594 public Number getMaximumAccessibleValue() { 595 return null; } 597 598 604 public AccessibleRole getAccessibleRole() { 605 return AccessibleRole.CHECK_BOX; 606 } 607 608 } 610 } 611 | Popular Tags |