| 1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.awt.image.*; 12 import java.text.*; 13 import java.awt.geom.*; 14 import java.beans.*; 15 import java.util.Enumeration ; 16 import java.util.Vector ; 17 import java.io.Serializable ; 18 import javax.swing.event.*; 19 import javax.swing.border.*; 20 import javax.swing.plaf.*; 21 import javax.accessibility.*; 22 import javax.swing.text.*; 23 import javax.swing.text.html.*; 24 import javax.swing.plaf.basic.*; 25 import java.util.*; 26 27 48 public abstract class AbstractButton extends JComponent implements ItemSelectable, SwingConstants { 49 50 54 55 public static final String MODEL_CHANGED_PROPERTY = "model"; 56 57 public static final String TEXT_CHANGED_PROPERTY = "text"; 58 59 public static final String MNEMONIC_CHANGED_PROPERTY = "mnemonic"; 60 61 63 public static final String MARGIN_CHANGED_PROPERTY = "margin"; 64 65 public static final String VERTICAL_ALIGNMENT_CHANGED_PROPERTY = "verticalAlignment"; 66 67 public static final String HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY = "horizontalAlignment"; 68 69 70 public static final String VERTICAL_TEXT_POSITION_CHANGED_PROPERTY = "verticalTextPosition"; 71 72 public static final String HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY = "horizontalTextPosition"; 73 74 79 public static final String BORDER_PAINTED_CHANGED_PROPERTY = "borderPainted"; 80 84 public static final String FOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted"; 85 89 public static final String ROLLOVER_ENABLED_CHANGED_PROPERTY = "rolloverEnabled"; 90 93 public static final String CONTENT_AREA_FILLED_CHANGED_PROPERTY = "contentAreaFilled"; 94 95 97 public static final String ICON_CHANGED_PROPERTY = "icon"; 98 99 103 public static final String PRESSED_ICON_CHANGED_PROPERTY = "pressedIcon"; 104 108 public static final String SELECTED_ICON_CHANGED_PROPERTY = "selectedIcon"; 109 110 114 public static final String ROLLOVER_ICON_CHANGED_PROPERTY = "rolloverIcon"; 115 119 public static final String ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY = "rolloverSelectedIcon"; 120 121 125 public static final String DISABLED_ICON_CHANGED_PROPERTY = "disabledIcon"; 126 130 public static final String DISABLED_SELECTED_ICON_CHANGED_PROPERTY = "disabledSelectedIcon"; 131 132 133 134 protected ButtonModel model = null; 135 136 private String text = ""; private Insets margin = null; 138 private Insets defaultMargin = null; 139 140 private Icon defaultIcon = null; 143 private Icon pressedIcon = null; 144 private Icon disabledIcon = null; 145 146 private Icon selectedIcon = null; 147 private Icon disabledSelectedIcon = null; 148 149 private Icon rolloverIcon = null; 150 private Icon rolloverSelectedIcon = null; 151 152 private boolean paintBorder = true; 154 private boolean paintFocus = true; 155 private boolean rolloverEnabled = false; 156 private boolean contentAreaFilled = true; 157 158 private int verticalAlignment = CENTER; 160 private int horizontalAlignment = CENTER; 161 162 private int verticalTextPosition = CENTER; 163 private int horizontalTextPosition = TRAILING; 164 165 private int iconTextGap = 4; 166 167 private int mnemonic; 168 private int mnemonicIndex = -1; 169 170 private long multiClickThreshhold = 0; 171 172 private boolean borderPaintedSet = false; 173 private boolean rolloverEnabledSet = false; 174 private boolean iconTextGapSet = false; 175 private boolean contentAreaFilledSet = false; 176 177 private boolean setLayout = false; 179 180 boolean defaultCapable = true; 183 184 187 private Handler handler; 188 189 192 protected ChangeListener changeListener = null; 193 196 protected ActionListener actionListener = null; 197 200 protected ItemListener itemListener = null; 201 202 208 protected transient ChangeEvent changeEvent; 209 210 215 public String getText() { 216 return text; 217 } 218 219 229 public void setText(String text) { 230 String oldValue = this.text; 231 this.text = text; 232 firePropertyChange(TEXT_CHANGED_PROPERTY, oldValue, text); 233 updateDisplayedMnemonicIndex(text, getMnemonic()); 234 235 if (accessibleContext != null) { 236 accessibleContext.firePropertyChange( 237 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 238 oldValue, text); 239 } 240 if (text == null || oldValue == null || !text.equals(oldValue)) { 241 revalidate(); 242 repaint(); 243 } 244 } 245 246 247 252 public boolean isSelected() { 253 return model.isSelected(); 254 } 255 256 263 public void setSelected(boolean b) { 264 boolean oldValue = isSelected(); 265 266 274 model.setSelected(b); 275 } 276 277 281 public void doClick() { 282 doClick(68); 283 } 284 285 293 public void doClick(int pressTime) { 294 Dimension size = getSize(); 295 model.setArmed(true); 296 model.setPressed(true); 297 paintImmediately(new Rectangle(0,0, size.width, size.height)); 298 try { 299 Thread.currentThread().sleep(pressTime); 300 } catch(InterruptedException ie) { 301 } 302 model.setPressed(false); 303 model.setArmed(false); 304 } 305 306 323 public void setMargin(Insets m) { 324 if(m instanceof UIResource) { 326 defaultMargin = m; 327 } else if(margin instanceof UIResource) { 328 defaultMargin = margin; 329 } 330 331 if(m == null && defaultMargin != null) { 334 m = defaultMargin; 335 } 336 337 Insets old = margin; 338 margin = m; 339 firePropertyChange(MARGIN_CHANGED_PROPERTY, old, m); 340 if (old == null || !old.equals(m)) { 341 revalidate(); 342 repaint(); 343 } 344 } 345 346 354 public Insets getMargin() { 355 return (margin == null) ? null : (Insets) margin.clone(); 356 } 357 358 363 public Icon getIcon() { 364 return defaultIcon; 365 } 366 367 380 public void setIcon(Icon defaultIcon) { 381 Icon oldValue = this.defaultIcon; 382 this.defaultIcon = defaultIcon; 383 384 389 if (defaultIcon != oldValue && (disabledIcon instanceof UIResource)) { 390 disabledIcon = null; 391 } 392 393 firePropertyChange(ICON_CHANGED_PROPERTY, oldValue, defaultIcon); 394 if (accessibleContext != null) { 395 accessibleContext.firePropertyChange( 396 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 397 oldValue, defaultIcon); 398 } 399 if (defaultIcon != oldValue) { 400 if (defaultIcon == null || oldValue == null || 401 defaultIcon.getIconWidth() != oldValue.getIconWidth() || 402 defaultIcon.getIconHeight() != oldValue.getIconHeight()) { 403 revalidate(); 404 } 405 repaint(); 406 } 407 } 408 409 414 public Icon getPressedIcon() { 415 return pressedIcon; 416 } 417 418 427 public void setPressedIcon(Icon pressedIcon) { 428 Icon oldValue = this.pressedIcon; 429 this.pressedIcon = pressedIcon; 430 firePropertyChange(PRESSED_ICON_CHANGED_PROPERTY, oldValue, pressedIcon); 431 if (accessibleContext != null) { 432 accessibleContext.firePropertyChange( 433 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 434 oldValue, pressedIcon); 435 } 436 if (pressedIcon != oldValue) { 437 if (getModel().isPressed()) { 438 repaint(); 439 } 440 } 441 } 442 443 448 public Icon getSelectedIcon() { 449 return selectedIcon; 450 } 451 452 461 public void setSelectedIcon(Icon selectedIcon) { 462 Icon oldValue = this.selectedIcon; 463 this.selectedIcon = selectedIcon; 464 465 470 if (selectedIcon != oldValue && 471 disabledSelectedIcon instanceof UIResource) { 472 473 disabledSelectedIcon = null; 474 } 475 476 firePropertyChange(SELECTED_ICON_CHANGED_PROPERTY, oldValue, selectedIcon); 477 if (accessibleContext != null) { 478 accessibleContext.firePropertyChange( 479 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 480 oldValue, selectedIcon); 481 } 482 if (selectedIcon != oldValue) { 483 if (isSelected()) { 484 repaint(); 485 } 486 } 487 } 488 489 494 public Icon getRolloverIcon() { 495 return rolloverIcon; 496 } 497 498 507 public void setRolloverIcon(Icon rolloverIcon) { 508 Icon oldValue = this.rolloverIcon; 509 this.rolloverIcon = rolloverIcon; 510 firePropertyChange(ROLLOVER_ICON_CHANGED_PROPERTY, oldValue, rolloverIcon); 511 if (accessibleContext != null) { 512 accessibleContext.firePropertyChange( 513 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 514 oldValue, rolloverIcon); 515 } 516 setRolloverEnabled(true); 517 if (rolloverIcon != oldValue) { 518 repaint(); 521 } 522 523 } 524 525 530 public Icon getRolloverSelectedIcon() { 531 return rolloverSelectedIcon; 532 } 533 534 544 public void setRolloverSelectedIcon(Icon rolloverSelectedIcon) { 545 Icon oldValue = this.rolloverSelectedIcon; 546 this.rolloverSelectedIcon = rolloverSelectedIcon; 547 firePropertyChange(ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY, oldValue, rolloverSelectedIcon); 548 if (accessibleContext != null) { 549 accessibleContext.firePropertyChange( 550 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 551 oldValue, rolloverSelectedIcon); 552 } 553 setRolloverEnabled(true); 554 if (rolloverSelectedIcon != oldValue) { 555 if (isSelected()) { 558 repaint(); 559 } 560 } 561 } 562 563 576 public Icon getDisabledIcon() { 577 if (disabledIcon == null) { 578 disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(this, getIcon()); 579 if (disabledIcon != null) { 580 firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, null, disabledIcon); 581 } 582 } 583 return disabledIcon; 584 } 585 586 595 public void setDisabledIcon(Icon disabledIcon) { 596 Icon oldValue = this.disabledIcon; 597 this.disabledIcon = disabledIcon; 598 firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, oldValue, disabledIcon); 599 if (accessibleContext != null) { 600 accessibleContext.firePropertyChange( 601 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 602 oldValue, disabledIcon); 603 } 604 if (disabledIcon != oldValue) { 605 if (!isEnabled()) { 606 repaint(); 607 } 608 } 609 } 610 611 626 public Icon getDisabledSelectedIcon() { 627 if (disabledSelectedIcon == null) { 628 if (selectedIcon != null) { 629 disabledSelectedIcon = UIManager.getLookAndFeel(). 630 getDisabledSelectedIcon(this, getSelectedIcon()); 631 } else { 632 return getDisabledIcon(); 633 } 634 } 635 return disabledSelectedIcon; 636 } 637 638 648 public void setDisabledSelectedIcon(Icon disabledSelectedIcon) { 649 Icon oldValue = this.disabledSelectedIcon; 650 this.disabledSelectedIcon = disabledSelectedIcon; 651 firePropertyChange(DISABLED_SELECTED_ICON_CHANGED_PROPERTY, oldValue, disabledSelectedIcon); 652 if (accessibleContext != null) { 653 accessibleContext.firePropertyChange( 654 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 655 oldValue, disabledSelectedIcon); 656 } 657 if (disabledSelectedIcon != oldValue) { 658 if (disabledSelectedIcon == null || oldValue == null || 659 disabledSelectedIcon.getIconWidth() != oldValue.getIconWidth() || 660 disabledSelectedIcon.getIconHeight() != oldValue.getIconHeight()) { 661 revalidate(); 662 } 663 if (!isEnabled() && isSelected()) { 664 repaint(); 665 } 666 } 667 } 668 669 680 public int getVerticalAlignment() { 681 return verticalAlignment; 682 } 683 684 700 public void setVerticalAlignment(int alignment) { 701 if (alignment == verticalAlignment) return; 702 int oldValue = verticalAlignment; 703 verticalAlignment = checkVerticalKey(alignment, "verticalAlignment"); 704 firePropertyChange(VERTICAL_ALIGNMENT_CHANGED_PROPERTY, oldValue, verticalAlignment); repaint(); 705 } 706 707 719 public int getHorizontalAlignment() { 720 return horizontalAlignment; 721 } 722 723 743 public void setHorizontalAlignment(int alignment) { 744 if (alignment == horizontalAlignment) return; 745 int oldValue = horizontalAlignment; 746 horizontalAlignment = checkHorizontalKey(alignment, 747 "horizontalAlignment"); 748 firePropertyChange(HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY, 749 oldValue, horizontalAlignment); 750 repaint(); 751 } 752 753 754 764 public int getVerticalTextPosition() { 765 return verticalTextPosition; 766 } 767 768 784 public void setVerticalTextPosition(int textPosition) { 785 if (textPosition == verticalTextPosition) return; 786 int oldValue = verticalTextPosition; 787 verticalTextPosition = checkVerticalKey(textPosition, "verticalTextPosition"); 788 firePropertyChange(VERTICAL_TEXT_POSITION_CHANGED_PROPERTY, oldValue, verticalTextPosition); 789 repaint(); 790 } 791 792 804 public int getHorizontalTextPosition() { 805 return horizontalTextPosition; 806 } 807 808 830 public void setHorizontalTextPosition(int textPosition) { 831 if (textPosition == horizontalTextPosition) return; 832 int oldValue = horizontalTextPosition; 833 horizontalTextPosition = checkHorizontalKey(textPosition, 834 "horizontalTextPosition"); 835 firePropertyChange(HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY, 836 oldValue, 837 horizontalTextPosition); 838 repaint(); 839 } 840 841 850 public int getIconTextGap() { 851 return iconTextGap; 852 } 853 854 870 public void setIconTextGap(int iconTextGap) { 871 int oldValue = this.iconTextGap; 872 this.iconTextGap = iconTextGap; 873 iconTextGapSet = true; 874 firePropertyChange("iconTextGap", oldValue, iconTextGap); 875 if (iconTextGap != oldValue) { 876 revalidate(); 877 repaint(); 878 } 879 } 880 881 900 protected int checkHorizontalKey(int key, String exception) { 901 if ((key == LEFT) || 902 (key == CENTER) || 903 (key == RIGHT) || 904 (key == LEADING) || 905 (key == TRAILING)) { 906 return key; 907 } else { 908 throw new IllegalArgumentException (exception); 909 } 910 } 911 912 929 protected int checkVerticalKey(int key, String exception) { 930 if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) { 931 return key; 932 } else { 933 throw new IllegalArgumentException (exception); 934 } 935 } 936 937 941 public void setActionCommand(String actionCommand) { 942 getModel().setActionCommand(actionCommand); 943 } 944 945 949 public String getActionCommand() { 950 String ac = getModel().getActionCommand(); 951 if(ac == null) { 952 ac = getText(); 953 } 954 return ac; 955 } 956 957 private Action action; 958 private PropertyChangeListener actionPropertyChangeListener; 959 |