1 7 8 package java.awt; 9 10 import java.awt.peer.ButtonPeer; 11 import java.util.EventListener ; 12 import java.awt.event.*; 13 import java.io.ObjectOutputStream ; 14 import java.io.ObjectInputStream ; 15 import java.io.IOException ; 16 import javax.accessibility.*; 17 18 70 public class Button extends Component implements Accessible { 71 72 78 String label; 79 80 87 String actionCommand; 88 89 transient ActionListener actionListener; 90 91 private static final String base = "button"; 92 private static int nameCounter = 0; 93 94 97 private static final long serialVersionUID = -8774683716313001058L; 98 99 100 static { 101 102 Toolkit.loadLibraries(); 103 if (!GraphicsEnvironment.isHeadless()) { 104 initIDs(); 105 } 106 } 107 108 112 private static native void initIDs(); 113 114 121 public Button() throws HeadlessException { 122 this(""); 123 } 124 125 134 public Button(String label) throws HeadlessException { 135 GraphicsEnvironment.checkHeadless(); 136 this.label = label; 137 } 138 139 143 String constructComponentName() { 144 synchronized (getClass()) { 145 return base + nameCounter++; 146 } 147 } 148 149 157 public void addNotify() { 158 synchronized(getTreeLock()) { 159 if (peer == null) 160 peer = getToolkit().createButton(this); 161 super.addNotify(); 162 } 163 } 164 165 172 public String getLabel() { 173 return label; 174 } 175 176 183 public void setLabel(String label) { 184 boolean testvalid = false; 185 186 synchronized (this) { 187 if (label != this.label && (this.label == null || 188 !this.label.equals(label))) { 189 this.label = label; 190 ButtonPeer peer = (ButtonPeer)this.peer; 191 if (peer != null) { 192 peer.setLabel(label); 193 } 194 testvalid = true; 195 } 196 } 197 198 if (testvalid && valid) { 200 invalidate(); 201 } 202 } 203 204 216 public void setActionCommand(String command) { 217 actionCommand = command; 218 } 219 220 225 public String getActionCommand() { 226 return (actionCommand == null? label : actionCommand); 227 } 228 229 241 public synchronized void addActionListener(ActionListener l) { 242 if (l == null) { 243 return; 244 } 245 actionListener = AWTEventMulticaster.add(actionListener, l); 246 newEventsOnly = true; 247 } 248 249 261 public synchronized void removeActionListener(ActionListener l) { 262 if (l == null) { 263 return; 264 } 265 actionListener = AWTEventMulticaster.remove(actionListener, l); 266 } 267 268 281 public synchronized ActionListener[] getActionListeners() { 282 return (ActionListener[]) (getListeners(ActionListener.class)); 283 } 284 285 318 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 319 EventListener l = null; 320 if (listenerType == ActionListener.class) { 321 l = actionListener; 322 } else { 323 return super.getListeners(listenerType); 324 } 325 return AWTEventMulticaster.getListeners(l, listenerType); 326 } 327 328 boolean eventEnabled(AWTEvent e) { 330 if (e.id == ActionEvent.ACTION_PERFORMED) { 331 if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 || 332 actionListener != null) { 333 return true; 334 } 335 return false; 336 } 337 return super.eventEnabled(e); 338 } 339 340 354 protected void processEvent(AWTEvent e) { 355 if (e instanceof ActionEvent) { 356 processActionEvent((ActionEvent)e); 357 return; 358 } 359 super.processEvent(e); 360 } 361 362 385 protected void processActionEvent(ActionEvent e) { 386 ActionListener listener = actionListener; 387 if (listener != null) { 388 listener.actionPerformed(e); 389 } 390 } 391 392 401 protected String paramString() { 402 return super.paramString() + ",label=" + label; 403 } 404 405 406 408 409 413 private int buttonSerializedDataVersion = 1; 414 415 434 private void writeObject(ObjectOutputStream s) 435 throws IOException 436 { 437 s.defaultWriteObject(); 438 439 AWTEventMulticaster.save(s, actionListenerK, actionListener); 440 s.writeObject(null); 441 } 442 443 459 private void readObject(ObjectInputStream s) 460 throws ClassNotFoundException , IOException , HeadlessException 461 { 462 GraphicsEnvironment.checkHeadless(); 463 s.defaultReadObject(); 464 465 Object keyOrNull; 466 while(null != (keyOrNull = s.readObject())) { 467 String key = ((String )keyOrNull).intern(); 468 469 if (actionListenerK == key) 470 addActionListener((ActionListener)(s.readObject())); 471 472 else s.readObject(); 474 } 475 } 476 477 478 482 496 public AccessibleContext getAccessibleContext() { 497 if (accessibleContext == null) { 498 accessibleContext = new AccessibleAWTButton(); 499 } 500 return accessibleContext; 501 } 502 503 508 protected class AccessibleAWTButton extends AccessibleAWTComponent 509 implements AccessibleAction, AccessibleValue 510 { 511 514 private static final long serialVersionUID = -5932203980244017102L; 515 516 522 public String getAccessibleName() { 523 if (accessibleName != null) { 524 return accessibleName; 525 } else { 526 if (getLabel() == null) { 527 return super.getAccessibleName(); 528 } else { 529 return getLabel(); 530 } 531 } 532 } 533 534 542 public AccessibleAction getAccessibleAction() { 543 return this; 544 } 545 546 554 public AccessibleValue getAccessibleValue() { 555 return this; 556 } 557 558 565 public int getAccessibleActionCount() { 566 return 1; 567 } 568 569 574 public String getAccessibleActionDescription(int i) { 575 if (i == 0) { 576 return new String ("click"); 578 } else { 579 return null; 580 } 581 } 582 583 589 public boolean doAccessibleAction(int i) { 590 if (i == 0) { 591 Toolkit.getEventQueue().postEvent( 593 new ActionEvent(Button.this, 594 ActionEvent.ACTION_PERFORMED, 595 Button.this.getActionCommand())); 596 return true; 597 } else { 598 return false; 599 } 600 } 601 602 609 public Number getCurrentAccessibleValue() { 610 return new Integer (0); 611 } 612 613 618 public boolean setCurrentAccessibleValue(Number n) { 619 return false; 620 } 621 622 627 public Number getMinimumAccessibleValue() { 628 return new Integer (0); 629 } 630 631 636 public Number getMaximumAccessibleValue() { 637 return new Integer (0); 638 } 639 640 647 public AccessibleRole getAccessibleRole() { 648 return AccessibleRole.PUSH_BUTTON; 649 } 650 } 652 } 653 | Popular Tags |