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 960 988 public void setAction(Action a) { 989 Action oldValue = getAction(); 990 if (action==null || !action.equals(a)) { 991 action = a; 992 if (oldValue!=null) { 993 removeActionListener(oldValue); 994 oldValue.removePropertyChangeListener(actionPropertyChangeListener); 995 actionPropertyChangeListener = null; 996 } 997 configurePropertiesFromAction(action); 998 if (action!=null) { 999 if (!isListener(ActionListener.class, action)) { 1001 addActionListener(action); 1002 } 1003 actionPropertyChangeListener = createActionPropertyChangeListener(action); 1005 action.addPropertyChangeListener(actionPropertyChangeListener); 1006 } 1007 firePropertyChange("action", oldValue, action); 1008 revalidate(); 1009 repaint(); 1010 } 1011 } 1012 1013 private boolean isListener(Class c, ActionListener a) { 1014 boolean isListener = false; 1015 Object [] listeners = listenerList.getListenerList(); 1016 for (int i = listeners.length-2; i>=0; i-=2) { 1017 if (listeners[i]==c && listeners[i+1]==a) { 1018 isListener=true; 1019 } 1020 } 1021 return isListener; 1022 } 1023 1024 1035 public Action getAction() { 1036 return action; 1037 } 1038 1039 1062 protected void configurePropertiesFromAction(Action a) { 1063 configurePropertiesFromAction(a, null); 1064 } 1065 1066 1105 void configurePropertiesFromAction(Action a, String [] types) { 1106 if (types == null) { 1107 String [] alltypes = { Action.MNEMONIC_KEY, Action.NAME, 1108 Action.SHORT_DESCRIPTION, Action.SMALL_ICON, 1109 Action.ACTION_COMMAND_KEY, "enabled" }; 1110 types = alltypes; 1111 } 1112 for (int i=0; i<types.length; i++) { 1113 String type = types[i]; 1114 if (type == null) continue; 1115 1116 if (type.equals(Action.MNEMONIC_KEY)) { 1117 Integer n = (a==null) ? null : (Integer )a.getValue(type); 1118 setMnemonic(n==null ? '\0' : n.intValue()); 1119 } else if (type.equals(Action.NAME)) { 1120 Boolean hide = (Boolean )getClientProperty("hideActionText"); 1123 setText(a != null && hide!=Boolean.TRUE ? 1124 (String )a.getValue(Action.NAME) : 1125 null); 1126 } else if (type.equals(Action.SHORT_DESCRIPTION)) { 1127 setToolTipText(a!=null ? (String )a.getValue(type) : null); 1128 } else if (type.equals(Action.SMALL_ICON)) { 1129 setIcon(a!=null ? (Icon )a.getValue(type) : null); 1130 } else if (type.equals(Action.ACTION_COMMAND_KEY)) { 1131 setActionCommand(a!=null? (String )a.getValue(type) : null); 1132 } else if (type.equals("enabled")) { 1133 setEnabled(a!=null ? a.isEnabled() : true); 1134 } 1135 } 1136 } 1137 1138 1159 protected PropertyChangeListener createActionPropertyChangeListener(Action a) { 1160 return new ButtonActionPropertyChangeListener(this, a); 1161 } 1162 1163 private static class ButtonActionPropertyChangeListener 1164 extends AbstractActionPropertyChangeListener 1165 implements Serializable { 1166 ButtonActionPropertyChangeListener(AbstractButton b, Action a) { 1167 super(b, a); 1168 } 1169 public void propertyChange(PropertyChangeEvent e) { 1170 String propertyName = e.getPropertyName(); 1171 AbstractButton button = (AbstractButton )getTarget(); 1172 if (button == null) { Action action = (Action )e.getSource(); 1174 action.removePropertyChangeListener(this); 1175 } else { 1176 if (e.getPropertyName().equals(Action.NAME)) { 1177 Boolean hide = (Boolean )button.getClientProperty("hideActionText"); 1178 if (hide == null || hide == Boolean.FALSE) { 1179 String text = (String ) e.getNewValue(); 1180 button.setText(text); 1181 button.repaint(); 1182 } 1183 } else if (e.getPropertyName().equals(Action.SHORT_DESCRIPTION)) { 1184 String text = (String ) e.getNewValue(); 1185 button.setToolTipText(text); 1186 } else if (propertyName.equals("enabled")) { 1187 Boolean enabledState = (Boolean ) e.getNewValue(); 1188 button.setEnabled(enabledState.booleanValue()); 1189 button.repaint(); 1190 } else if (e.getPropertyName().equals(Action.SMALL_ICON)) { 1191 Icon icon = (Icon ) e.getNewValue(); 1192 button.setIcon(icon); 1193 button.invalidate(); 1194 button.repaint(); 1195 } else if (e.getPropertyName().equals(Action.MNEMONIC_KEY)) { 1196 Integer mn = (Integer ) e.getNewValue(); 1197 button.setMnemonic(mn.intValue()); 1198 button.invalidate(); 1199 button.repaint(); 1200 } else if (e.getPropertyName().equals(Action.ACTION_COMMAND_KEY)) { 1201 button.setActionCommand((String )e.getNewValue()); 1202 } 1203 } 1204 } 1205 } 1206 1207 1213 public boolean isBorderPainted() { 1214 return paintBorder; 1215 } 1216 1217 1231 public void setBorderPainted(boolean b) { 1232 boolean oldValue = paintBorder; 1233 paintBorder = b; 1234 borderPaintedSet = true; 1235 firePropertyChange(BORDER_PAINTED_CHANGED_PROPERTY, oldValue, paintBorder); 1236 if (b != oldValue) { 1237 revalidate(); 1238 repaint(); 1239 } 1240 } 1241 1242 1250 protected void paintBorder(Graphics g) { 1251 if (isBorderPainted()) { 1252 super.paintBorder(g); 1253 } 1254 } 1255 1256 1262 public boolean isFocusPainted() { 1263 return paintFocus; 1264 } 1265 1266 1281 public void setFocusPainted(boolean b) { 1282 boolean oldValue = paintFocus; 1283 paintFocus = b; 1284 firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY, oldValue, paintFocus); 1285 if (b != oldValue && isFocusOwner()) { 1286 revalidate(); 1287 repaint(); 1288 } 1289 } 1290 1291 1297 public boolean isContentAreaFilled() { 1298 return contentAreaFilled; 1299 } 1300 1301 1325 public void setContentAreaFilled(boolean b) { 1326 boolean oldValue = contentAreaFilled; 1327 contentAreaFilled = b; 1328 contentAreaFilledSet = true; 1329 firePropertyChange(CONTENT_AREA_FILLED_CHANGED_PROPERTY, oldValue, contentAreaFilled); 1330 if (b != oldValue) { 1331 repaint(); 1332 } 1333 } 1334 1335 1341 public boolean isRolloverEnabled() { 1342 return rolloverEnabled; 1343 } 1344 1345 1360 public void setRolloverEnabled(boolean b) { 1361 boolean oldValue = rolloverEnabled; 1362 rolloverEnabled = b; 1363 rolloverEnabledSet = true; 1364 firePropertyChange(ROLLOVER_ENABLED_CHANGED_PROPERTY, oldValue, rolloverEnabled); 1365 if (b != oldValue) { 1366 repaint(); 1367 } 1368 } 1369 1370 1374 public int getMnemonic() { 1375 return mnemonic; 1376 } 1377 1378 1405 public void setMnemonic(int mnemonic) { 1406 int oldValue = getMnemonic(); 1407 model.setMnemonic(mnemonic); 1408 updateMnemonicProperties(); 1409 } 1410 1411 1424 public void setMnemonic(char mnemonic) { 1425 int vk = (int) mnemonic; 1426 if(vk >= 'a' && vk <='z') 1427 vk -= ('a' - 'A'); 1428 setMnemonic(vk); 1429 } 1430 1431 1459 public void setDisplayedMnemonicIndex(int index) 1460 throws IllegalArgumentException { 1461 int oldValue = mnemonicIndex; 1462 if (index == -1) { 1463 mnemonicIndex = -1; 1464 } else { 1465 String text = getText(); 1466 int textLength = (text == null) ? 0 : text.length(); 1467 if (index < -1 || index >= textLength) { throw new IllegalArgumentException ("index == " + index); 1469 } 1470 } 1471 mnemonicIndex = index; 1472 firePropertyChange("displayedMnemonicIndex", oldValue, index); 1473 if (index != oldValue) { 1474 revalidate(); 1475 repaint(); 1476 } 1477 } 1478 1479 1487 public int getDisplayedMnemonicIndex() { 1488 return mnemonicIndex; 1489 } 1490 1491 1497 private void updateDisplayedMnemonicIndex(String text, int mnemonic) { 1498 setDisplayedMnemonicIndex( 1499 SwingUtilities.findDisplayedMnemonicIndex(text, mnemonic)); 1500 } 1501 1502 1507 private void updateMnemonicProperties() { 1508 int newMnemonic = model.getMnemonic(); 1509 if (mnemonic != newMnemonic) { 1510 int oldValue = mnemonic; 1511 mnemonic = newMnemonic; 1512 firePropertyChange(MNEMONIC_CHANGED_PROPERTY, 1513 oldValue, mnemonic); 1514 updateDisplayedMnemonicIndex(getText(), mnemonic); 1515 revalidate(); 1516 repaint(); 1517 } 1518 } 1519 1520 1539 public void setMultiClickThreshhold(long threshhold) { 1540 if (threshhold < 0) { 1541 throw new IllegalArgumentException ("threshhold must be >= 0"); 1542 } 1543 this.multiClickThreshhold = threshhold; 1544 } 1545 1546 1556 public long getMultiClickThreshhold() { 1557 return multiClickThreshhold; 1558 } 1559 1560 1565 public ButtonModel getModel() { 1566 return model; 1567 } 1568 1569 1577 public void setModel(ButtonModel newModel) { 1578 1579 ButtonModel oldModel = getModel(); 1580 1581 if (oldModel != null) { 1582 oldModel.removeChangeListener(changeListener); 1583 oldModel.removeActionListener(actionListener); 1584 oldModel.removeItemListener(itemListener); 1585 changeListener = null; 1586 actionListener = null; 1587 itemListener = null; 1588 } 1589 1590 model = newModel; 1591 1592 if (newModel != null) { 1593 changeListener = createChangeListener(); 1594 actionListener = createActionListener(); 1595 itemListener = createItemListener(); 1596 newModel.addChangeListener(changeListener); 1597 newModel.addActionListener(actionListener); 1598 newModel.addItemListener(itemListener); 1599 1600 updateMnemonicProperties(); 1601 } else { 1602 mnemonic = '\0'; 1603 } 1604 1605 updateDisplayedMnemonicIndex(getText(), mnemonic); 1606 1607 firePropertyChange(MODEL_CHANGED_PROPERTY, oldModel, newModel); 1608 if (newModel != oldModel) { 1609 revalidate(); 1610 repaint(); 1611 } 1612 } 1613 1614 1615 1620 public ButtonUI getUI() { 1621 return (ButtonUI) ui; 1622 } 1623 1624 1625 1635 public void setUI(ButtonUI ui) { 1636 super.setUI(ui); 1637 if (disabledIcon instanceof UIResource) { 1639 setDisabledIcon(null); 1640 } 1641 if (disabledSelectedIcon instanceof UIResource) { 1642 setDisabledSelectedIcon(null); 1643 } 1644 } 1645 1646 1647 1657 public void updateUI() { 1658 } 1659 1660 1678 protected void addImpl(Component comp, Object constraints, int index) { 1679 if (!setLayout) { 1680 setLayout(new OverlayLayout (this)); 1681 } 1682 super.addImpl(comp, constraints, index); 1683 } 1684 1685 1693 public void setLayout(LayoutManager mgr) { 1694 setLayout = true; 1695 super.setLayout(mgr); 1696 } 1697 1698 1702 public void addChangeListener(ChangeListener l) { 1703 listenerList.add(ChangeListener.class, l); 1704 } 1705 1706 1710 public void removeChangeListener(ChangeListener l) { 1711 listenerList.remove(ChangeListener.class, l); 1712 } 1713 1714 1722 public ChangeListener[] getChangeListeners() { 1723 return (ChangeListener[])(listenerList.getListeners( 1724 ChangeListener.class)); 1725 } 1726 1727 1733 protected void fireStateChanged() { 1734 Object [] listeners = listenerList.getListenerList(); 1736 for (int i = listeners.length-2; i>=0; i-=2) { 1739 if (listeners[i]==ChangeListener.class) { 1740 if (changeEvent == null) 1742 changeEvent = new ChangeEvent(this); 1743 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent); 1744 } 1745 } 1746 } 1747 1748 1752 public void addActionListener(ActionListener l) { 1753 listenerList.add(ActionListener.class, l); 1754 } 1755 1756 1764 public void removeActionListener(ActionListener l) { 1765 if ((l != null) && (getAction() == l)) { 1766 setAction(null); 1767 } else { 1768 listenerList.remove(ActionListener.class, l); 1769 } 1770 } 1771 1772 1780 public ActionListener[] getActionListeners() { 1781 return (ActionListener[])(listenerList.getListeners( 1782 ActionListener.class)); 1783 } 1784 1785 1792 protected ChangeListener createChangeListener() { 1793 return getHandler(); 1794 } 1795 1796 1808 protected class ButtonChangeListener implements ChangeListener, Serializable { 1809 ButtonChangeListener() { 1812 } 1813 1814 public void stateChanged(ChangeEvent e) { 1815 getHandler().stateChanged(e); 1816 } 1817 } 1818 1819 1820 1829 protected void fireActionPerformed(ActionEvent event) { 1830 Object [] listeners = listenerList.getListenerList(); 1832 ActionEvent e = null; 1833 for (int i = listeners.length-2; i>=0; i-=2) { 1836 if (listeners[i]==ActionListener.class) { 1837 if (e == null) { 1839 String actionCommand = event.getActionCommand(); 1840 if(actionCommand == null) { 1841 actionCommand = getActionCommand(); 1842 } 1843 e = new ActionEvent(AbstractButton.this, 1844 ActionEvent.ACTION_PERFORMED, 1845 actionCommand, 1846 event.getWhen(), 1847 event.getModifiers()); 1848 } 1849 ((ActionListener)listeners[i+1]).actionPerformed(e); 1850 } 1851 } 1852 } 1853 1854 1862 protected void fireItemStateChanged(ItemEvent event) { 1863 Object [] listeners = listenerList.getListenerList(); 1865 ItemEvent e = null; 1866 for (int i = listeners.length-2; i>=0; i-=2) { 1869 if (listeners[i]==ItemListener.class) { 1870 if (e == null) { 1872 e = new ItemEvent(AbstractButton.this, 1873 ItemEvent.ITEM_STATE_CHANGED, 1874 AbstractButton.this, 1875 event.getStateChange()); 1876 } 1877 ((ItemListener)listeners[i+1]).itemStateChanged(e); 1878 } 1879 } 1880 if (accessibleContext != null) { 1881 if (event.getStateChange() == ItemEvent.SELECTED) { 1882 accessibleContext.firePropertyChange( 1883 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 1884 null, AccessibleState.SELECTED); 1885 accessibleContext.firePropertyChange( 1886 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, 1887 new Integer (0), new Integer (1)); 1888 } else { 1889 accessibleContext.firePropertyChange( 1890 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 1891 AccessibleState.SELECTED, null); 1892 accessibleContext.firePropertyChange( 1893 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, 1894 new Integer (1), new Integer (0)); 1895 } 1896 } 1897 } 1898 1899 1900 protected ActionListener createActionListener() { 1901 return getHandler(); 1902 } 1903 1904 1905 protected ItemListener createItemListener() { 1906 return getHandler(); 1907 } 1908 1909 1910 1914 public void setEnabled(boolean b) { 1915 if (!b && model.isRollover()) { 1916 model.setRollover(false); 1917 } 1918 super.setEnabled(b); 1919 model.setEnabled(b); 1920 } 1921 1922 1924 1930 @Deprecated 1931 public String getLabel() { 1932 return getText(); 1933 } 1934 1935 1944 @Deprecated 1945 public void setLabel(String label) { 1946 setText(label); 1947 } 1948 1949 1953 public void addItemListener(ItemListener l) { 1954 listenerList.add(ItemListener.class, l); 1955 } 1956 1957 1961 public void removeItemListener(ItemListener l) { 1962 listenerList.remove(ItemListener.class, l); 1963 } 1964 1965 1973 public ItemListener[] getItemListeners() { 1974 return (ItemListener[])listenerList.getListeners(ItemListener.class); 1975 } 1976 1977 1984 public Object [] getSelectedObjects() { 1985 if (isSelected() == false) { 1986 return null; 1987 } 1988 Object [] selectedObjects = new Object [1]; 1989 selectedObjects[0] = getText(); 1990 return selectedObjects; 1991 } 1992 1993 protected void init(String text, Icon icon) { 1994 if(text != null) { 1995 setText(text); 1996 } 1997 1998 if(icon != null) { 1999 setIcon(icon); 2000 } 2001 2002 updateUI(); 2004 2005 setAlignmentX(LEFT_ALIGNMENT); 2006 setAlignmentY(CENTER_ALIGNMENT); 2007 } 2008 2009 2010 2025 public boolean imageUpdate(Image img, int infoflags, 2026 int x, int y, int w, int h) { 2027 Icon iconDisplayed = getIcon(); 2028 if (iconDisplayed == null) { 2029 return false; 2030 } 2031 2032 if (!model.isEnabled()) { 2033 if (model.isSelected()) { 2034 iconDisplayed = getDisabledSelectedIcon(); 2035 } else { 2036 iconDisplayed = getDisabledIcon(); 2037 } 2038 } else if (model.isPressed() && model.isArmed()) { 2039 iconDisplayed = getPressedIcon(); 2040 } else if (isRolloverEnabled() && model.isRollover()) { 2041 if (model.isSelected()) { 2042 iconDisplayed = getRolloverSelectedIcon(); 2043 } else { 2044 iconDisplayed = getRolloverIcon(); 2045 } 2046 } else if (model.isSelected()) { 2047 iconDisplayed = getSelectedIcon(); 2048 } 2049 2050 if (!SwingUtilities.doesIconReferenceImage(iconDisplayed, img)) { 2051 return false; 2054 } 2055 return super.imageUpdate(img, infoflags, x, y, w, h); 2056 } 2057 2058 void setUIProperty(String propertyName, Object value) { 2059 if (propertyName == "borderPainted") { 2060 if (!borderPaintedSet) { 2061 setBorderPainted(((Boolean )value).booleanValue()); 2062 borderPaintedSet = false; 2063 } 2064 } else if (propertyName == "rolloverEnabled") { 2065 if (!rolloverEnabledSet) { 2066 setRolloverEnabled(((Boolean )value).booleanValue()); 2067 rolloverEnabledSet = false; 2068 } 2069 } else if (propertyName == "iconTextGap") { 2070 if (!iconTextGapSet) { 2071 setIconTextGap(((Number )value).intValue()); 2072 iconTextGapSet = false; 2073 } 2074 } else if (propertyName == "contentAreaFilled") { 2075 if (!contentAreaFilledSet) { 2076 setContentAreaFilled(((Boolean )value).booleanValue()); 2077 contentAreaFilledSet = false; 2078 } 2079 } else { 2080 super.setUIProperty(propertyName, value); 2081 } 2082 } 2083 2084 2097 protected String paramString() { 2098 String defaultIconString = ((defaultIcon != null) 2099 && (defaultIcon != this) ? 2100 defaultIcon.toString() : ""); 2101 String pressedIconString = ((pressedIcon != null) 2102 && (pressedIcon != this) ? 2103 pressedIcon.toString() : ""); 2104 String disabledIconString = ((disabledIcon != null) 2105 && (disabledIcon != this) ? 2106 disabledIcon.toString() : ""); 2107 String selectedIconString = ((selectedIcon != null) 2108 && (selectedIcon != this) ? 2109 selectedIcon.toString() : ""); 2110 String disabledSelectedIconString = ((disabledSelectedIcon != null) && 2111 (disabledSelectedIcon != this) ? 2112 disabledSelectedIcon.toString() 2113 : ""); 2114 String rolloverIconString = ((rolloverIcon != null) 2115 && (rolloverIcon != this) ? 2116 rolloverIcon.toString() : ""); 2117 String rolloverSelectedIconString = ((rolloverSelectedIcon != null) && 2118 (rolloverSelectedIcon != this) ? 2119 rolloverSelectedIcon.toString() 2120 : ""); 2121 String paintBorderString = (paintBorder ? "true" : "false"); 2122 String paintFocusString = (paintFocus ? "true" : "false"); 2123 String rolloverEnabledString = (rolloverEnabled ? "true" : "false"); 2124 2125 return super.paramString() + 2126 ",defaultIcon=" + defaultIconString + 2127 ",disabledIcon=" + disabledIconString + 2128 ",disabledSelectedIcon=" + disabledSelectedIconString + 2129 ",margin=" + margin + 2130 ",paintBorder=" + paintBorderString + 2131 ",paintFocus=" + paintFocusString + 2132 ",pressedIcon=" + pressedIconString + 2133 ",rolloverEnabled=" + rolloverEnabledString + 2134 ",rolloverIcon=" + rolloverIconString + 2135 ",rolloverSelectedIcon=" + rolloverSelectedIconString + 2136 ",selectedIcon=" + selectedIconString + 2137 ",text=" + text; 2138 } 2139 2140 2141 private Handler getHandler() { 2142 if (handler == null) { 2143 handler = new Handler(); 2144 } 2145 return handler; 2146 } 2147 2148 2149 class Handler implements ActionListener, ChangeListener, ItemListener, 2153 Serializable { 2154 public void stateChanged(ChangeEvent e) { 2158 Object source = e.getSource(); 2159 2160 updateMnemonicProperties(); 2161 fireStateChanged(); 2162 repaint(); 2163 } 2164 2165 public void actionPerformed(ActionEvent event) { 2169 fireActionPerformed(event); 2170 } 2171 2172 public void itemStateChanged(ItemEvent event) { 2176 fireItemStateChanged(event); 2177 } 2178 } 2179 2180 2198 protected abstract class AccessibleAbstractButton 2199 extends AccessibleJComponent implements AccessibleAction, 2200 AccessibleValue, AccessibleText, AccessibleExtendedComponent { 2201 2202 2209 public String getAccessibleName() { 2210 if (accessibleName != null) { 2211 return accessibleName; 2212 } else { 2213 if (AbstractButton.this.getText() == null) { 2214 return super.getAccessibleName(); 2215 } else { 2216 return AbstractButton.this.getText(); 2217 } 2218 } 2219 } 2220 2221 2225 public AccessibleIcon [] getAccessibleIcon() { 2226 Icon defaultIcon = getIcon(); 2227 2228 if (defaultIcon instanceof Accessible) { 2229 AccessibleContext ac = 2230 ((Accessible)defaultIcon).getAccessibleContext(); 2231 if (ac != null && ac instanceof AccessibleIcon) { 2232 return new AccessibleIcon[] { (AccessibleIcon)ac }; 2233 } 2234 } 2235 return null; 2236 } 2237 2238 2245 public AccessibleStateSet getAccessibleStateSet() { 2246 AccessibleStateSet states = super.getAccessibleStateSet(); 2247 if (getModel().isArmed()) { 2248 states.add(AccessibleState.ARMED); 2249 } 2250 if (isFocusOwner()) { 2251 states.add(AccessibleState.FOCUSED); 2252 } 2253 if (getModel().isPressed()) { 2254 states.add(AccessibleState.PRESSED); 2255 } 2256 if (isSelected()) { 2257 states.add(AccessibleState.CHECKED); 2258 } 2259 return states; 2260 } 2261 2262 2267 public AccessibleRelationSet getAccessibleRelationSet() { 2268 2269 AccessibleRelationSet relationSet 2272 = super.getAccessibleRelationSet(); 2273 2274 if (!relationSet.contains(AccessibleRelation.MEMBER_OF)) { 2275 ButtonModel model = getModel(); 2277 if (model != null && model instanceof DefaultButtonModel ) { 2278 ButtonGroup group = ((DefaultButtonModel )model).getGroup(); 2279 if (group != null) { 2280 int len = group.getButtonCount(); 2283 Object [] target = new Object [len]; 2284 Enumeration elem = group.getElements(); 2285 for (int i = 0; i < len; i++) { 2286 if (elem.hasMoreElements()) { 2287 target[i] = elem.nextElement(); 2288 } 2289 } 2290 AccessibleRelation relation = 2291 new AccessibleRelation(AccessibleRelation.MEMBER_OF); 2292 relation.setTarget(target); 2293 relationSet.add(relation); 2294 } 2295 } 2296 } 2297 return relationSet; 2298 } 2299 2300 2308 public AccessibleAction getAccessibleAction() { 2309 return this; 2310 } 2311 2312 2320 public AccessibleValue getAccessibleValue() { 2321 return this; 2322 } 2323 2324 2331 public int getAccessibleActionCount() { 2332 return 1; 2333 } 2334 2335 2340 public String getAccessibleActionDescription(int i) { 2341 if (i == 0) { 2342 return UIManager.getString("AbstractButton.clickText"); 2343 } else { 2344 return null; 2345 } 2346 } 2347 2348 2354 public boolean doAccessibleAction(int i) { 2355 if (i == 0) { 2356 doClick(); 2357 return true; 2358 } else { 2359 return false; 2360 } 2361 } 2362 2363 2370 public Number getCurrentAccessibleValue() { 2371 if (isSelected()) { 2372 return new Integer (1); 2373 } else { 2374 return new Integer (0); 2375 } 2376 } 2377 2378 2383 public boolean setCurrentAccessibleValue(Number n) { 2384 if (n == null) { 2386 return false; 2387 } 2388 int i = n.intValue(); 2389 if (i == 0) { 2390 setSelected(false); 2391 } else { 2392 setSelected(true); 2393 } 2394 return true; 2395 } 2396 2397 2402 public Number getMinimumAccessibleValue() { 2403 return new Integer (0); 2404 } 2405 2406 2411 public Number getMaximumAccessibleValue() { 2412 return new Integer (1); 2413 } 2414 2415 2416 2417 2418 public AccessibleText getAccessibleText() { 2419 View view = (View)AbstractButton.this.getClientProperty("html"); 2420 if (view != null) { 2421 return this; 2422 } else { 2423 return null; 2424 } 2425 } 2426 2427 2441 public int getIndexAtPoint(Point p) { 2442 View view = (View) AbstractButton.this.getClientProperty("html"); 2443 if (view != null) { 2444 Rectangle r = getTextRectangle(); 2445 if (r == null) { 2446 return -1; 2447 } 2448 Rectangle2D.Float shape = 2449 new Rectangle2D.Float(r.x, r.y, r.width, r.height); 2450 Position.Bias bias[] = new Position.Bias[1]; 2451 return view.viewToModel(p.x, p.y, shape, bias); 2452 } else { 2453 return -1; 2454 } 2455 } 2456 2457 2472 public Rectangle getCharacterBounds(int i) { 2473 View view = (View) AbstractButton.this.getClientProperty("html"); 2474 if (view != null) { 2475 Rectangle r = getTextRectangle(); 2476 if (r == null) { 2477 return null; 2478 } 2479 Rectangle2D.Float shape = 2480 new Rectangle2D.Float(r.x, r.y, r.width, r.height); 2481 try { 2482 Shape charShape = 2483 view.modelToView(i, shape, Position.Bias.Forward); 2484 return charShape.getBounds(); 2485 } catch (BadLocationException e) { 2486 return null; 2487 } 2488 } else { 2489 return null; 2490 } 2491 } 2492 2493 2498 public int getCharCount() { 2499 View view = (View) AbstractButton.this.getClientProperty("html"); 2500 if (view != null) { 2501 Document d = view.getDocument(); 2502 if (d instanceof StyledDocument) { 2503 StyledDocument doc = (StyledDocument)d; 2504 return doc.getLength(); 2505 } 2506 } 2507 return accessibleContext.getAccessibleName().length(); 2508 } 2509 2510 2517 public int getCaretPosition() { 2518 return -1; 2520 } 2521 2522 2531 public String getAtIndex(int part, int index) { 2532 if (index < 0 || index >= getCharCount()) { 2533 return null; 2534 } 2535 switch (part) { 2536 case AccessibleText.CHARACTER: 2537 try { 2538 return getText(index, 1); 2539 } catch (BadLocationException e) { 2540 return null; 2541 } 2542 case AccessibleText.WORD: 2543 try { 2544 String s = getText(0, getCharCount()); 2545 BreakIterator words = BreakIterator.getWordInstance(getLocale()); 2546 words.setText(s); 2547 int end = words.following(index); 2548 return s.substring(words.previous(), end); 2549 } catch (BadLocationException e) { 2550 return null; 2551 } 2552 case AccessibleText.SENTENCE: 2553 try { 2554 String s = getText(0, getCharCount()); 2555 BreakIterator sentence = 2556 BreakIterator.getSentenceInstance(getLocale()); 2557 sentence.setText(s); 2558 int end = sentence.following(index); 2559 return s.substring(sentence.previous(), end); 2560 } catch (BadLocationException e) { 2561 return null; 2562 } 2563 default: 2564 return null; 2565 } 2566 } 2567 2568 2577 public String getAfterIndex(int part, int index) { 2578 if (index < 0 || index >= getCharCount()) { 2579 return null; 2580 } 2581 switch (part) { 2582 case AccessibleText.CHARACTER: 2583 if (index+1 >= getCharCount()) { 2584 return null; 2585 } 2586 try { 2587 return getText(index+1, 1); 2588 } catch (BadLocationException e) { 2589 return null; 2590 } 2591 case AccessibleText.WORD: 2592 try { 2593 String s = getText(0, getCharCount()); 2594 BreakIterator words = BreakIterator.getWordInstance(getLocale()); 2595 words.setText(s); 2596 int start = words.following(index); 2597 if (start == BreakIterator.DONE || start >= s.length()) { 2598 return null; 2599 } 2600 int end = words.following(start); 2601 if (end == BreakIterator.DONE || end >= s.length()) { 2602 return null; 2603 } 2604 return s.substring(start, end); 2605 } catch (BadLocationException e) { 2606 return null; 2607 } 2608 case AccessibleText.SENTENCE: 2609 try { 2610 String s = getText(0, getCharCount()); 2611 BreakIterator sentence = 2612 BreakIterator.getSentenceInstance(getLocale()); 2613 sentence.setText(s); 2614 int start = sentence.following(index); 2615 if (start == BreakIterator.DONE || start >= s.length()) { 2616 return null; 2617 } 2618 int end = sentence.following(start); 2619 if (end == BreakIterator.DONE || end >= s.length()) { 2620 return null; 2621 } 2622 return s.substring(start, end); 2623 } catch (BadLocationException e) { 2624 return null; 2625 } 2626 default: 2627 return null; 2628 } 2629 } 2630 2631 2640 public String getBeforeIndex(int part, int index) { 2641 if (index < 0 || index > getCharCount()-1) { 2642 return null; 2643 } 2644 switch (part) { 2645 case AccessibleText.CHARACTER: 2646 if (index == 0) { 2647 return null; 2648 } 2649 try { 2650 return getText(index-1, 1); 2651 } catch (BadLocationException e) { 2652 return null; 2653 } 2654 case AccessibleText.WORD: 2655 try { 2656 String s = getText(0, getCharCount()); 2657 BreakIterator words = BreakIterator.getWordInstance(getLocale()); 2658 words.setText(s); 2659 int end = words.following(index); 2660 end = words.previous(); 2661 int start = words.previous(); 2662 if (start == BreakIterator.DONE) { 2663 return null; 2664 } 2665 return s.substring(start, end); 2666 } catch (BadLocationException e) { 2667 return null; 2668 } 2669 case AccessibleText.SENTENCE: 2670 try { 2671 String s = getText(0, getCharCount()); 2672 BreakIterator sentence = 2673 BreakIterator.getSentenceInstance(getLocale()); 2674 sentence.setText(s); 2675 int end = sentence.following(index); 2676 end = sentence.previous(); 2677 int start = sentence.previous(); 2678 if (start == BreakIterator.DONE) { 2679 return null; 2680 } 2681 return s.substring(start, end); 2682 } catch (BadLocationException e) { 2683 return null; 2684 } 2685 default: 2686 return null; 2687 } 2688 } 2689 2690 2696 public AttributeSet getCharacterAttribute(int i) { 2697 View view = (View) AbstractButton.this.getClientProperty("html"); 2698 if (view != null) { 2699 Document d = view.getDocument(); 2700 if (d instanceof StyledDocument) { 2701 StyledDocument doc = (StyledDocument)d; 2702 Element elem = doc.getCharacterElement(i); 2703 if (elem != null) { 2704 return elem.getAttributes(); 2705 } 2706 } 2707 } 2708 return null; 2709 } 2710 2711 2718 public int getSelectionStart() { 2719 return -1; 2721 } 2722 2723 2730 public int getSelectionEnd() { 2731 return -1; 2733 } 2734 2735 2740 public String getSelectedText() { 2741 return null; 2743 } 2744 2745 2749 private String getText(int offset, int length) 2750 throws BadLocationException { 2751 2752 View view = (View) AbstractButton.this.getClientProperty("html"); 2753 if (view != null) { 2754 Document d = view.getDocument(); 2755 if (d instanceof StyledDocument) { 2756 StyledDocument doc = (StyledDocument)d; 2757 return doc.getText(offset, length); 2758 } 2759 } 2760 return null; 2761 } 2762 2763 2766 private Rectangle getTextRectangle() { 2767 2768 String text = AbstractButton.this.getText(); 2769 Icon icon = (AbstractButton.this.isEnabled()) ? AbstractButton.this.getIcon() : AbstractButton.this.getDisabledIcon(); 2770 2771 if ((icon == null) && (text == null)) { 2772 return null; 2773 } 2774 2775 Rectangle paintIconR = new Rectangle(); 2776 Rectangle paintTextR = new Rectangle(); 2777 Rectangle paintViewR = new Rectangle(); 2778 Insets paintViewInsets = new Insets(0, 0, 0, 0); 2779 2780 paintViewInsets = AbstractButton.this.getInsets(paintViewInsets); 2781 paintViewR.x = paintViewInsets.left; 2782 paintViewR.y = paintViewInsets.top; 2783 paintViewR.width = AbstractButton.this.getWidth() - (paintViewInsets.left + paintViewInsets.right); 2784 paintViewR.height = AbstractButton.this.getHeight() - (paintViewInsets.top + paintViewInsets.bottom); 2785 2786 Graphics g = AbstractButton.this.getGraphics(); 2787 if (g == null) { 2788 return null; 2789 } 2790 String clippedText = SwingUtilities.layoutCompoundLabel( 2791 (JComponent )AbstractButton.this, 2792 g.getFontMetrics(), 2793 text, 2794 icon, 2795 AbstractButton.this.getVerticalAlignment(), 2796 AbstractButton.this.getHorizontalAlignment(), 2797 AbstractButton.this.getVerticalTextPosition(), 2798 AbstractButton.this.getHorizontalTextPosition(), 2799 paintViewR, 2800 paintIconR, 2801 paintTextR, 2802 0); 2803 2804 return paintTextR; 2805 } 2806 2807 2809 2814 AccessibleExtendedComponent getAccessibleExtendedComponent() { 2815 return this; 2816 } 2817 2818 2824 public String getToolTipText() { 2825 return AbstractButton.this.getToolTipText(); 2826 } 2827 2828 2834 public String getTitledBorderText() { 2835 return super.getTitledBorderText(); 2836 } 2837 2838 2845 public AccessibleKeyBinding getAccessibleKeyBinding() { 2846 int mnemonic = AbstractButton.this.getMnemonic(); 2847 if (mnemonic == 0) { 2848 return null; 2849 } 2850 return new ButtonKeyBinding(mnemonic); 2851 } 2852 2853 class ButtonKeyBinding implements AccessibleKeyBinding { 2854 int mnemonic; 2855 2856 ButtonKeyBinding(int mnemonic) { 2857 this.mnemonic = mnemonic; 2858 } 2859 2860 2865 public int getAccessibleKeyBindingCount() { 2866 return 1; 2867 } 2868 2869 2894 public java.lang.Object getAccessibleKeyBinding(int i) { 2895 if (i != 0) { 2896 throw new IllegalArgumentException (); 2897 } 2898 return KeyStroke.getKeyStroke(mnemonic, 0); 2899 } 2900 } 2901 } 2902} 2903 | Popular Tags |