1. /*
  2. * @(#)AbstractButton.java 1.133 01/02/09
  3. *
  4. * Copyright 1997-2001 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import java.awt.image.*;
  14. import java.text.*;
  15. import java.awt.geom.*;
  16. import java.beans.*;
  17. import java.util.Enumeration;
  18. import java.util.Vector;
  19. import java.io.Serializable;
  20. import javax.swing.event.*;
  21. import javax.swing.border.*;
  22. import javax.swing.plaf.*;
  23. import javax.accessibility.*;
  24. import javax.swing.text.*;
  25. import javax.swing.text.html.*;
  26. import javax.swing.plaf.basic.*;
  27. import java.util.*;
  28. /**
  29. * Defines common behaviors for buttons and menu items.
  30. * For further information see
  31. * <a
  32. href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>,
  33. * a section in <em>The Java Tutorial</em>.
  34. *
  35. * <p>
  36. *
  37. * <strong>Warning:</strong>
  38. * Serialized objects of this class will not be compatible with
  39. * future Swing releases. The current serialization support is appropriate
  40. * for short term storage or RMI between applications running the same
  41. * version of Swing. A future release of Swing will provide support for
  42. * long term persistence.
  43. *
  44. * @version 1.133 02/09/01
  45. * @author Jeff Dinkins
  46. */
  47. public abstract class AbstractButton extends JComponent implements ItemSelectable, SwingConstants {
  48. // *********************************
  49. // ******* Button properties *******
  50. // *********************************
  51. /** Identifies a change in the button model. */
  52. public static final String MODEL_CHANGED_PROPERTY = "model";
  53. /** Identifies a change in the button's text. */
  54. public static final String TEXT_CHANGED_PROPERTY = "text";
  55. /** Identifies a change to the button's mnemonic. */
  56. public static final String MNEMONIC_CHANGED_PROPERTY = "mnemonic";
  57. // Text positioning and alignment
  58. /** Identifies a change in the button's margins. */
  59. public static final String MARGIN_CHANGED_PROPERTY = "margin";
  60. /** Identifies a change in the button's vertical alignment. */
  61. public static final String VERTICAL_ALIGNMENT_CHANGED_PROPERTY = "verticalAlignment";
  62. /** Identifies a change in the button's horizontal alignment. */
  63. public static final String HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY = "horizontalAlignment";
  64. /** Identifies a change in the button's vertical text position. */
  65. public static final String VERTICAL_TEXT_POSITION_CHANGED_PROPERTY = "verticalTextPosition";
  66. /** Identifies a change in the button's horizontal text position. */
  67. public static final String HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY = "horizontalTextPosition";
  68. // Paint options
  69. /**
  70. * Identifies a change to having the border drawn,
  71. * or having it not drawn.
  72. */
  73. public static final String BORDER_PAINTED_CHANGED_PROPERTY = "borderPainted";
  74. /**
  75. * Identifies a change to having the border highlighted when focused,
  76. * or not.
  77. */
  78. public static final String FOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted";
  79. /** Identifies a change in the button's */
  80. public static final String ROLLOVER_ENABLED_CHANGED_PROPERTY = "rolloverEnabled";
  81. /**
  82. * Identifies a change from rollover enabled to disabled or back
  83. * to enabled.
  84. */
  85. public static final String CONTENT_AREA_FILLED_CHANGED_PROPERTY = "contentAreaFilled";
  86. // Icons
  87. /** Identifies a change to the icon that represents the button. */
  88. public static final String ICON_CHANGED_PROPERTY = "icon";
  89. /**
  90. * Identifies a change to the icon used when the button has been
  91. * pressed.
  92. */
  93. public static final String PRESSED_ICON_CHANGED_PROPERTY = "pressedIcon";
  94. /**
  95. * Identifies a change to the icon used when the button has
  96. * been selected.
  97. */
  98. public static final String SELECTED_ICON_CHANGED_PROPERTY = "selectedIcon";
  99. /**
  100. * Identifies a change to the icon used when the cursor is over
  101. * the button.
  102. */
  103. public static final String ROLLOVER_ICON_CHANGED_PROPERTY = "rolloverIcon";
  104. /**
  105. * Identifies a change to the icon used when the cursor is
  106. * over the button and it has been selected.
  107. */
  108. public static final String ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY = "rolloverSelectedIcon";
  109. /**
  110. * Identifies a change to the icon used when the button has
  111. * been disabled.
  112. */
  113. public static final String DISABLED_ICON_CHANGED_PROPERTY = "disabledIcon";
  114. /**
  115. * Identifies a change to the icon used when the button has been
  116. * disabled and selected.
  117. */
  118. public static final String DISABLED_SELECTED_ICON_CHANGED_PROPERTY = "disabledSelectedIcon";
  119. /** The data model that determines the button's state. */
  120. protected ButtonModel model = null;
  121. private String text = ""; // for BeanBox
  122. private Insets margin = null;
  123. private Insets defaultMargin = null;
  124. // Button icons
  125. // PENDING(jeff) - hold icons in an array
  126. private Icon defaultIcon = null;
  127. private Icon pressedIcon = null;
  128. private Icon disabledIcon = null;
  129. private Icon selectedIcon = null;
  130. private Icon disabledSelectedIcon = null;
  131. private Icon rolloverIcon = null;
  132. private Icon rolloverSelectedIcon = null;
  133. // Display properties
  134. private boolean paintBorder = true;
  135. private boolean paintFocus = true;
  136. private boolean rolloverEnabled = false;
  137. private boolean contentAreaFilled = true;
  138. // Icon/Label Alignment
  139. private int verticalAlignment = CENTER;
  140. private int horizontalAlignment = CENTER;
  141. private int verticalTextPosition = CENTER;
  142. private int horizontalTextPosition = TRAILING;
  143. private AccessibleIcon accessibleIcon = null;
  144. /**
  145. * The button model's <code>changeListener</code>.
  146. */
  147. protected ChangeListener changeListener = null;
  148. /**
  149. * The button model's <code>ActionListener</code>.
  150. */
  151. protected ActionListener actionListener = null;
  152. /**
  153. * The button model's <code>ItemListener</code>.
  154. */
  155. protected ItemListener itemListener = null;
  156. /**
  157. * Only one <code>ChangeEvent</code> is needed per button
  158. * instance since the
  159. * event's only state is the source property. The source of events
  160. * generated is always "this".
  161. */
  162. protected transient ChangeEvent changeEvent;
  163. /**
  164. * Returns the button's text.
  165. * @return the buttons text
  166. * @see #setText
  167. */
  168. public String getText() {
  169. return text;
  170. }
  171. /**
  172. * Sets the button's text.
  173. * @param t the string used to set the text
  174. * @see #getText
  175. * @beaninfo
  176. * bound: true
  177. * preferred: true
  178. * attribute: visualUpdate true
  179. * description: The button's text.
  180. */
  181. public void setText(String text) {
  182. String oldValue = this.text;
  183. this.text = text;
  184. firePropertyChange(TEXT_CHANGED_PROPERTY, oldValue, text);
  185. if (accessibleContext != null) {
  186. accessibleContext.firePropertyChange(
  187. AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
  188. oldValue, text);
  189. }
  190. if (text == null || oldValue == null || !text.equals(oldValue)) {
  191. revalidate();
  192. repaint();
  193. }
  194. }
  195. /**
  196. * Returns the state of the button. True if the
  197. * toggle button is selected, false if it's not.
  198. * @return true if the toggle button is selected, otherwise false
  199. */
  200. public boolean isSelected() {
  201. return model.isSelected();
  202. }
  203. /**
  204. * Sets the state of the button. Note that this method does not
  205. * trigger an <code>actionEvent</code>.
  206. * Call <code>doClick</code> to perform a programatic action change.
  207. *
  208. * @param b true if the button is selected, otherwise false
  209. */
  210. public void setSelected(boolean b) {
  211. boolean oldValue = isSelected();
  212. if (accessibleContext != null && oldValue != b) {
  213. if (b) {
  214. accessibleContext.firePropertyChange(
  215. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  216. null, AccessibleState.SELECTED);
  217. } else {
  218. accessibleContext.firePropertyChange(
  219. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  220. AccessibleState.SELECTED, null);
  221. }
  222. }
  223. model.setSelected(b);
  224. }
  225. /**
  226. * Programmatically perform a "click". This does the same
  227. * thing as if the user had pressed and released the button.
  228. */
  229. public void doClick() {
  230. doClick(68);
  231. }
  232. /**
  233. * Programmatically perform a "click". This does the same
  234. * thing as if the user had pressed and released the button.
  235. * The button stays visually "pressed" for <code>pressTime</code>
  236. * milliseconds.
  237. *
  238. * @param pressTime the time to "hold down" the button, in milliseconds
  239. */
  240. public void doClick(int pressTime) {
  241. Dimension size = getSize();
  242. model.setArmed(true);
  243. model.setPressed(true);
  244. paintImmediately(new Rectangle(0,0, size.width, size.height));
  245. try {
  246. Thread.currentThread().sleep(pressTime);
  247. } catch(InterruptedException ie) {
  248. }
  249. model.setPressed(false);
  250. model.setArmed(false);
  251. }
  252. /**
  253. * Sets space for margin between the button's border and
  254. * the label. Setting to <code>null</code> will cause the button to
  255. * use the default margin. The button's default <code>Border</code>
  256. * object will use this value to create the proper margin.
  257. * However, if a non-default border is set on the button,
  258. * it is that <code>Border</code> object's responsibility to create the
  259. * appropriate margin space (else this property will
  260. * effectively be ignored).
  261. *
  262. * @param m the space between the border and the label
  263. *
  264. * @beaninfo
  265. * bound: true
  266. * attribute: visualUpdate true
  267. * description: The space between the button's border and the label.
  268. */
  269. public void setMargin(Insets m) {
  270. // Cache the old margin if it comes from the UI
  271. if(m instanceof UIResource) {
  272. defaultMargin = m;
  273. } else if(margin instanceof UIResource) {
  274. defaultMargin = margin;
  275. }
  276. // If the client passes in a null insets, restore the margin
  277. // from the UI if possible
  278. if(m == null && defaultMargin != null) {
  279. m = defaultMargin;
  280. }
  281. Insets old = margin;
  282. margin = m;
  283. firePropertyChange(MARGIN_CHANGED_PROPERTY, old, m);
  284. if (old == null || !m.equals(old)) {
  285. revalidate();
  286. repaint();
  287. }
  288. }
  289. /**
  290. * Returns the margin between the button's border and
  291. * the label.
  292. *
  293. * @return an <code>Insets</code> object specifying the margin
  294. * between the botton's border and the label
  295. * @see #setMargin
  296. */
  297. public Insets getMargin() {
  298. return (margin == null) ? null : (Insets) margin.clone();
  299. }
  300. /**
  301. * Returns the default icon.
  302. * @return the default <code>Icon</code>
  303. * @see #setIcon
  304. */
  305. public Icon getIcon() {
  306. return defaultIcon;
  307. }
  308. /**
  309. * Sets the button's default icon. This icon is
  310. * also used as the "pressed" and "disabled" icon if
  311. * there is no explicitly set pressed icon.
  312. *
  313. * @param defaultIcon the icon used as the default image
  314. * @see #getIcon
  315. * @see #setPressedIcon
  316. * @beaninfo
  317. * bound: true
  318. * attribute: visualUpdate true
  319. * description: The button's default icon
  320. */
  321. public void setIcon(Icon defaultIcon) {
  322. Icon oldValue = this.defaultIcon;
  323. this.defaultIcon = defaultIcon;
  324. firePropertyChange(ICON_CHANGED_PROPERTY, oldValue, defaultIcon);
  325. if (accessibleContext != null) {
  326. accessibleContext.firePropertyChange(
  327. AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
  328. oldValue, pressedIcon);
  329. }
  330. if (defaultIcon != oldValue) {
  331. if (defaultIcon == null || oldValue == null ||
  332. defaultIcon.getIconWidth() != oldValue.getIconWidth() ||
  333. defaultIcon.getIconHeight() != oldValue.getIconHeight()) {
  334. revalidate();
  335. }
  336. repaint();
  337. }
  338. // set the accessible icon
  339. accessibleIcon = null;
  340. if (defaultIcon instanceof Accessible) {
  341. AccessibleContext ac =
  342. ((Accessible)defaultIcon).getAccessibleContext();
  343. if (ac != null && ac instanceof AccessibleIcon) {
  344. accessibleIcon = (AccessibleIcon)ac;
  345. }
  346. }
  347. }
  348. /**
  349. * Returns the pressed icon for the button.
  350. * @return the <code>pressedIcon</code> property
  351. * @see #setPressedIcon
  352. */
  353. public Icon getPressedIcon() {
  354. return pressedIcon;
  355. }
  356. /**
  357. * Sets the pressed icon for the button.
  358. * @param pressedIcon the icon used as the "pressed" image
  359. * @see #getPressedIcon
  360. * @beaninfo
  361. * bound: true
  362. * attribute: visualUpdate true
  363. * description: The pressed icon for the button.
  364. */
  365. public void setPressedIcon(Icon pressedIcon) {
  366. Icon oldValue = this.pressedIcon;
  367. this.pressedIcon = pressedIcon;
  368. firePropertyChange(PRESSED_ICON_CHANGED_PROPERTY, oldValue, pressedIcon);
  369. if (accessibleContext != null) {
  370. accessibleContext.firePropertyChange(
  371. AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
  372. oldValue, defaultIcon);
  373. }
  374. if (pressedIcon != oldValue) {
  375. if (getModel().isPressed()) {
  376. repaint();
  377. }
  378. }
  379. }
  380. /**
  381. * Returns the selected icon for the button.
  382. * @return the <code>selectedIcon</code> property
  383. * @see #setSelectedIcon
  384. */
  385. public Icon getSelectedIcon() {
  386. return selectedIcon;
  387. }
  388. /**
  389. * Sets the selected icon for the button.
  390. * @param selectedIcon the icon used as the "selected" image
  391. * @see #getSelectedIcon
  392. * @beaninfo
  393. * bound: true
  394. * attribute: visualUpdate true
  395. * description: The selected icon for the button.
  396. */
  397. public void setSelectedIcon(Icon selectedIcon) {
  398. Icon oldValue = this.selectedIcon;
  399. this.selectedIcon = selectedIcon;
  400. firePropertyChange(SELECTED_ICON_CHANGED_PROPERTY, oldValue, selectedIcon);
  401. if (accessibleContext != null) {
  402. accessibleContext.firePropertyChange(
  403. AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
  404. oldValue, selectedIcon);
  405. }
  406. if (selectedIcon != oldValue) {
  407. if (isSelected()) {
  408. repaint();
  409. }
  410. }
  411. }
  412. /**
  413. * Returns the rollover icon for the button.
  414. * @return the <code>rolloverIcon</code> property
  415. * @see #setRolloverIcon
  416. */
  417. public Icon getRolloverIcon() {
  418. return rolloverIcon;
  419. }
  420. /**
  421. * Sets the rollover icon for the button.
  422. * @param rolloverIcon the icon used as the "rollover" image
  423. * @see #getRolloverIcon
  424. * @beaninfo
  425. * bound: true
  426. * attribute: visualUpdate true
  427. * description: The rollover icon for the button.
  428. */
  429. public void setRolloverIcon(Icon rolloverIcon) {
  430. Icon oldValue = this.rolloverIcon;
  431. this.rolloverIcon = rolloverIcon;
  432. firePropertyChange(ROLLOVER_ICON_CHANGED_PROPERTY, oldValue, rolloverIcon);
  433. if (accessibleContext != null) {
  434. accessibleContext.firePropertyChange(
  435. AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
  436. oldValue, rolloverIcon);
  437. }
  438. setRolloverEnabled(true);
  439. if (rolloverIcon != oldValue) {
  440. // No way to determine whether we are currently in
  441. // a rollover state, so repaint regardless
  442. repaint();
  443. }
  444. }
  445. /**
  446. * Returns the rollover selection icon for the button.
  447. * @return the <code>rolloverSelectedIcon</code> property
  448. * @see #setRolloverSelectedIcon
  449. */
  450. public Icon getRolloverSelectedIcon() {
  451. return rolloverSelectedIcon;
  452. }
  453. /**
  454. * Sets the rollover selected icon for the button.
  455. * @param rolloverSelectedIcon the icon used as the
  456. * "selected rollover" image
  457. * @see #getRolloverSelectedIcon
  458. * @beaninfo
  459. * bound: true
  460. * attribute: visualUpdate true
  461. * description: The rollover selected icon for the button.
  462. */
  463. public void setRolloverSelectedIcon(Icon rolloverSelectedIcon) {
  464. Icon oldValue = this.rolloverSelectedIcon;
  465. this.rolloverSelectedIcon = rolloverSelectedIcon;
  466. firePropertyChange(ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY, oldValue, rolloverSelectedIcon);
  467. if (accessibleContext != null) {
  468. accessibleContext.firePropertyChange(
  469. AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
  470. oldValue, rolloverSelectedIcon);
  471. }
  472. if (rolloverSelectedIcon != oldValue) {
  473. // No way to determine whether we are currently in
  474. // a rollover state, so repaint regardless
  475. if (isSelected()) {
  476. repaint();
  477. }
  478. }
  479. }
  480. /**
  481. * Returns the icon used by the button when it's disabled.
  482. * If no disabled icon has been set, the button constructs
  483. * one from the default icon.
  484. * <!-- PENDING(jeff): the disabled icon really should be created
  485. * (if necessary) by the L&F.-->
  486. *
  487. * @return the <code>disabledIcon</code> property
  488. * @see #getPressedIcon
  489. * @see #setDisabledIcon
  490. */
  491. public Icon getDisabledIcon() {
  492. if(disabledIcon == null) {
  493. if(defaultIcon != null
  494. && defaultIcon instanceof ImageIcon) {
  495. disabledIcon = new ImageIcon(
  496. GrayFilter.createDisabledImage(
  497. ((ImageIcon)defaultIcon).getImage()));
  498. }
  499. }
  500. return disabledIcon;
  501. }
  502. /**
  503. * Sets the disabled icon for the button.
  504. * @param disabledIcon the icon used as the disabled image
  505. * @see #getDisabledIcon
  506. * @beaninfo
  507. * bound: true
  508. * attribute: visualUpdate true
  509. * description: The disabled icon for the button.
  510. */
  511. public void setDisabledIcon(Icon disabledIcon) {
  512. Icon oldValue = this.disabledIcon;
  513. this.disabledIcon = disabledIcon;
  514. firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, oldValue, disabledIcon);
  515. if (accessibleContext != null) {
  516. accessibleContext.firePropertyChange(
  517. AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
  518. oldValue, disabledIcon);
  519. }
  520. if (disabledIcon != oldValue) {
  521. if (!isEnabled()) {
  522. repaint();
  523. }
  524. }
  525. }
  526. /**
  527. * Returns the icon used by the button when it's disabled and selected.
  528. * If not no disabled selection icon has been set, the button constructs
  529. * one from the selection icon.
  530. * <!-- PENDING(jeff): the disabled selection icon really should be
  531. * created (if necesary) by the L&F. -->
  532. *
  533. * @return the <code>disabledSelectedIcon</code> property
  534. * @see #getPressedIcon
  535. * @see #setDisabledIcon
  536. */
  537. public Icon getDisabledSelectedIcon() {
  538. if(disabledSelectedIcon == null) {
  539. if(selectedIcon != null && selectedIcon instanceof ImageIcon) {
  540. disabledSelectedIcon = new ImageIcon(
  541. GrayFilter.createDisabledImage(((ImageIcon)selectedIcon).getImage()));
  542. } else {
  543. return disabledIcon;
  544. }
  545. }
  546. return disabledSelectedIcon;
  547. }
  548. /**
  549. * Sets the disabled selection icon for the button.
  550. * @param disabledSelectedIcon the icon used as the disabled
  551. * selection image
  552. * @see #getDisabledSelectedIcon
  553. * @beaninfo
  554. * bound: true
  555. * attribute: visualUpdate true
  556. * description: The disabled selection icon for the button.
  557. */
  558. public void setDisabledSelectedIcon(Icon disabledSelectedIcon) {
  559. Icon oldValue = this.disabledSelectedIcon;
  560. this.disabledSelectedIcon = disabledSelectedIcon;
  561. firePropertyChange(DISABLED_SELECTED_ICON_CHANGED_PROPERTY, oldValue, disabledSelectedIcon);
  562. if (accessibleContext != null) {
  563. accessibleContext.firePropertyChange(
  564. AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
  565. oldValue, disabledSelectedIcon);
  566. }
  567. if (disabledSelectedIcon != oldValue) {
  568. if (disabledSelectedIcon == null || oldValue == null ||
  569. disabledSelectedIcon.getIconWidth() != oldValue.getIconWidth() ||
  570. disabledSelectedIcon.getIconHeight() != oldValue.getIconHeight()) {
  571. revalidate();
  572. }
  573. if (!isEnabled() && isSelected()) {
  574. repaint();
  575. }
  576. }
  577. }
  578. /**
  579. * Returns the vertical alignment of the text and icon.
  580. *
  581. * @return the <code>verticalAlignment</code> property, one of the
  582. * following values:
  583. * <ul>
  584. * <li>SwingConstants.CENTER (the default)
  585. * <li>SwingConstants.TOP
  586. * <li>SwingConstants.BOTTOM
  587. * </ul>
  588. */
  589. public int getVerticalAlignment() {
  590. return verticalAlignment;
  591. }
  592. /**
  593. * Sets the vertical alignment of the icon and text.
  594. * @param alignment one of the following values:
  595. * <ul>
  596. * <li>SwingConstants.CENTER (the default)
  597. * <li>SwingConstants.TOP
  598. * <li>SwingConstants.BOTTOM
  599. * </ul>
  600. * @beaninfo
  601. * bound: true
  602. * enum: TOP SwingConstants.TOP
  603. * CENTER SwingConstants.CENTER
  604. * BOTTOM SwingConstants.BOTTOM
  605. * attribute: visualUpdate true
  606. * description: The vertical alignment of the icon and text.
  607. */
  608. public void setVerticalAlignment(int alignment) {
  609. if (alignment == verticalAlignment) return;
  610. int oldValue = verticalAlignment;
  611. verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
  612. firePropertyChange(VERTICAL_ALIGNMENT_CHANGED_PROPERTY, oldValue, verticalAlignment); repaint();
  613. }
  614. /**
  615. * Returns the horizontal alignment of the icon and text.
  616. * @return the <code>horizontalAlignment</code> property,
  617. * one of the following values:
  618. * <ul>
  619. * <li>SwingConstants.RIGHT (the default)
  620. * <li>SwingConstants.LEFT
  621. * <li>SwingConstants.CENTER
  622. * <li>SwingConstants.LEADING
  623. * <li>SwingConstants.TRAILING
  624. * </ul>
  625. */
  626. public int getHorizontalAlignment() {
  627. return horizontalAlignment;
  628. }
  629. /**
  630. * Sets the horizontal alignment of the icon and text.
  631. * @param alignment one of the following values:
  632. * <ul>
  633. * <li>SwingConstants.RIGHT (the default)
  634. * <li>SwingConstants.LEFT
  635. * <li>SwingConstants.CENTER
  636. * <li>SwingConstants.LEADING
  637. * <li>SwingConstants.TRAILING
  638. * </ul>
  639. * @beaninfo
  640. * bound: true
  641. * enum: LEFT SwingConstants.LEFT
  642. * CENTER SwingConstants.CENTER
  643. * RIGHT SwingConstants.RIGHT
  644. * LEADING SwingConstants.LEADING
  645. * TRAILING SwingConstants.TRAILING
  646. * attribute: visualUpdate true
  647. * description: The horizontal alignment of the icon and text.
  648. */
  649. public void setHorizontalAlignment(int alignment) {
  650. if (alignment == horizontalAlignment) return;
  651. int oldValue = horizontalAlignment;
  652. horizontalAlignment = checkHorizontalKey(alignment,
  653. "horizontalAlignment");
  654. firePropertyChange(HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY,
  655. oldValue, horizontalAlignment);
  656. repaint();
  657. }
  658. /**
  659. * Returns the vertical position of the text relative to the icon.
  660. * @return the <code>verticalTextPosition</code> property,
  661. * one of the following values:
  662. * <ul>
  663. * <li>SwingConstants.CENTER (the default)
  664. * <li>SwingConstants.TOP
  665. * <li>SwingConstants.BOTTOM
  666. * </ul>
  667. */
  668. public int getVerticalTextPosition() {
  669. return verticalTextPosition;
  670. }
  671. /**
  672. * Sets the vertical position of the text relative to the icon.
  673. * @param alignment one of the following values:
  674. * <ul>
  675. * <li>SwingConstants.CENTER (the default)
  676. * <li>SwingConstants.TOP
  677. * <li>SwingConstants.BOTTOM
  678. * </ul>
  679. * @beaninfo
  680. * bound: true
  681. * enum: TOP SwingConstants.TOP
  682. * CENTER SwingConstants.CENTER
  683. * BOTTOM SwingConstants.BOTTOM
  684. * attribute: visualUpdate true
  685. * description: The vertical position of the text relative to the icon.
  686. */
  687. public void setVerticalTextPosition(int textPosition) {
  688. if (textPosition == verticalTextPosition) return;
  689. int oldValue = verticalTextPosition;
  690. verticalTextPosition = checkVerticalKey(textPosition, "verticalTextPosition");
  691. firePropertyChange(VERTICAL_TEXT_POSITION_CHANGED_PROPERTY, oldValue, verticalTextPosition);
  692. repaint();
  693. }
  694. /**
  695. * Returns the horizontal position of the text relative to the icon.
  696. * @return the <code>horizontalTextPosition</code> property,
  697. * one of the following values:
  698. * <ul>
  699. * <li>SwingConstants.RIGHT (the default)
  700. * <li>SwingConstants.LEFT
  701. * <li>SwingConstants.CENTER
  702. * <li>SwingConstants.LEADING
  703. * <li>SwingConstants.TRAILING
  704. * </ul>
  705. */
  706. public int getHorizontalTextPosition() {
  707. return horizontalTextPosition;
  708. }
  709. /**
  710. * Sets the horizontal position of the text relative to the icon.
  711. * @param textPosition one of the following values:
  712. * <ul>
  713. * <li>SwingConstants.RIGHT (the default)
  714. * <li>SwingConstants.LEFT
  715. * <li>SwingConstants.CENTER
  716. * <li>SwingConstants.LEADING
  717. * <li>SwingConstants.TRAILING
  718. * </ul>
  719. * @exception IllegalArgumentException if <code>textPosition</code.
  720. * is not one of the legal values listed above
  721. * @beaninfo
  722. * bound: true
  723. * enum: LEFT SwingConstants.LEFT
  724. * CENTER SwingConstants.CENTER
  725. * RIGHT SwingConstants.RIGHT
  726. * LEADING SwingConstants.LEADING
  727. * TRAILING SwingConstants.TRAILING
  728. * attribute: visualUpdate true
  729. * description: The horizontal position of the text relative to the icon.
  730. */
  731. public void setHorizontalTextPosition(int textPosition) {
  732. if (textPosition == horizontalTextPosition) return;
  733. int oldValue = horizontalTextPosition;
  734. horizontalTextPosition = checkHorizontalKey(textPosition,
  735. "horizontalTextPosition");
  736. firePropertyChange(HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY,
  737. oldValue,
  738. horizontalTextPosition);
  739. repaint();
  740. }
  741. /**
  742. * Verify that key is a legal value for the
  743. * <code>horizontalAlignment</code> properties.
  744. *
  745. * @param key the property value to check, one of the following values:
  746. * <ul>
  747. * <li>SwingConstants.RIGHT (the default)
  748. * <li>SwingConstants.LEFT
  749. * <li>SwingConstants.CENTER
  750. * <li>SwingConstants.LEADING
  751. * <li>SwingConstants.TRAILING
  752. * </ul>
  753. * @param exception the <code>IllegalArgumentException</code>
  754. * detail message
  755. * @exception IllegalArgumentException if key is not one of the legal
  756. * values listed above
  757. * @see #setHorizontalTextPosition
  758. * @see #setHorizontalAlignment
  759. */
  760. protected int checkHorizontalKey(int key, String exception) {
  761. if ((key == LEFT) ||
  762. (key == CENTER) ||
  763. (key == RIGHT) ||
  764. (key == LEADING) ||
  765. (key == TRAILING)) {
  766. return key;
  767. } else {
  768. throw new IllegalArgumentException(exception);
  769. }
  770. }
  771. /**
  772. * Ensures that the key is a valid. Throws an
  773. * <code>IllegalArgumentException</code>
  774. * exception otherwise.
  775. *
  776. * @param key the value to check, one of the following values:
  777. * <ul>
  778. * <li>SwingConstants.CENTER (the default)
  779. * <li>SwingConstants.TOP
  780. * <li>SwingConstants.BOTTOM
  781. * </ul>
  782. * @param exception a string to be passed to the
  783. * <code>IllegalArgumentException</code> call if key
  784. * is not one of the valid values listed above
  785. * @exception IllegalArgumentException if key is not one of the legal
  786. * values listed above
  787. */
  788. protected int checkVerticalKey(int key, String exception) {
  789. if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) {
  790. return key;
  791. } else {
  792. throw new IllegalArgumentException(exception);
  793. }
  794. }
  795. /**
  796. * Sets the action command for this button.
  797. * @param actionCommand the action command for this button
  798. */
  799. public void setActionCommand(String actionCommand) {
  800. getModel().setActionCommand(actionCommand);
  801. }
  802. /**
  803. * Returns the action command for this button.
  804. * @return the action command for this button
  805. */
  806. public String getActionCommand() {
  807. String ac = getModel().getActionCommand();
  808. if(ac == null) {
  809. ac = getText();
  810. }
  811. return ac;
  812. }
  813. private Action action;
  814. private PropertyChangeListener actionPropertyChangeListener;
  815. /**
  816. * Sets the <code>Action</code> for the <code>ActionEvent</code> source.
  817. * The new <code>Action</code> replaces any previously set
  818. * <code>Action</code> but does not affect <code>ActionListeners</code>
  819. * independently added with <code>addActionListener</code>.
  820. * If the <code>Action</code> is already a registered
  821. * <code>ActionListener</code> for the button, it is not re-registered.
  822. * <p>
  823. * A side-effect of setting the <code>Action</code> is that the
  824. * <code>ActionEvent</code> source's properties are immediately
  825. * set from the values in the <code>Action</code> (performed by the
  826. * method <code>configurePropertiesFromAction</code>) and
  827. * subsequently updated as the <code>Action</code>'s properties change
  828. * (via a <code>PropertyChangeListener</code> created by the method
  829. * <code>createActionPropertyChangeListener</code>.
  830. *
  831. * @param a the <code>Action</code> for the <code>AbstractButton</code>,
  832. * or <code>null</code>
  833. * @since 1.3
  834. * @see Action
  835. * @see #getAction
  836. * @see #configurePropertiesFromAction
  837. * @see #createActionPropertyChangeListener
  838. * @beaninfo
  839. * bound: true
  840. * attribute: visualUpdate true
  841. * description: the Action instance connected with this ActionEvent source
  842. */
  843. public void setAction(Action a) {
  844. Action oldValue = getAction();
  845. if (action==null || !action.equals(a)) {
  846. action = a;
  847. if (oldValue!=null) {
  848. removeActionListener(oldValue);
  849. oldValue.removePropertyChangeListener(actionPropertyChangeListener);
  850. actionPropertyChangeListener = null;
  851. }
  852. configurePropertiesFromAction(action);
  853. if (action!=null) {
  854. // Don't add if it is already a listener
  855. if (!isListener(ActionListener.class, action)) {
  856. addActionListener(action);
  857. }
  858. // Reverse linkage:
  859. actionPropertyChangeListener = createActionPropertyChangeListener(action);
  860. action.addPropertyChangeListener(actionPropertyChangeListener);
  861. }
  862. firePropertyChange("action", oldValue, action);
  863. revalidate();
  864. repaint();
  865. }
  866. }
  867. private boolean isListener(Class c, ActionListener a) {
  868. boolean isListener = false;
  869. Object[] listeners = listenerList.getListenerList();
  870. for (int i = listeners.length-2; i>=0; i-=2) {
  871. if (listeners[i]==c && listeners[i+1]==a) {
  872. isListener=true;
  873. }
  874. }
  875. return isListener;
  876. }
  877. /**
  878. * Returns the currently set <code>Action</code> for this
  879. * <code>ActionEvent</code> source, or <code>null</code>
  880. * if no <code>Action</code> is set.
  881. *
  882. * @return the <code>Action</code> for this <code>ActionEvent</code>
  883. * source, or <code>null</code>
  884. * @since 1.3
  885. * @see Action
  886. * @see #setAction
  887. */
  888. public Action getAction() {
  889. return action;
  890. }
  891. /**
  892. * Factory method which sets the <code>ActionEvent</code>
  893. * source's properties according to values from the
  894. * <code>Action</code> instance. The properties
  895. * which are set may differ for subclasses. By default,
  896. * the properties which get set are <code>Text</code>, <code>Icon
  897. * Enabled</code>, <code>ToolTipText</code> and <code>Mnemonic</code>.
  898. * <p>
  899. * If the <code>Action</code> passed in is <code>null</code>,
  900. * the following things will occur:
  901. * <ul>
  902. * <li>the text is set to <code>null</code>,
  903. * <li>the icon is set to <code>null</code>,
  904. * <li>enabled is set to true,
  905. * <li>the tooltip text is set to <code>null</code>
  906. * </ul>
  907. *
  908. * @param a the <code>Action</code> from which to get the properties,
  909. * or <code>null</code>
  910. * @since 1.3
  911. * @see Action
  912. * @see #setAction
  913. */
  914. protected void configurePropertiesFromAction(Action a) {
  915. setText((a!=null?(String)a.getValue(Action.NAME):null));
  916. setIcon((a!=null?(Icon)a.getValue(Action.SMALL_ICON):null));
  917. setEnabled((a!=null?a.isEnabled():true));
  918. setToolTipText((a!=null?(String)a.getValue(Action.SHORT_DESCRIPTION):null));
  919. if (a != null) {
  920. Integer i = (Integer)a.getValue(Action.MNEMONIC_KEY);
  921. if (i != null)
  922. setMnemonic(i.intValue());
  923. }
  924. }
  925. /**
  926. * Factory method which creates the <code>PropertyChangeListener</code>
  927. * used to update the <code>ActionEvent</code> source as properties
  928. * change on its <code>Action</code> instance. Subclasses may
  929. * override this in order to provide their own
  930. * <code>PropertyChangeListener</code> if the set of
  931. * properties which should be kept up to date differs from the
  932. * default properties (<code>Text, Icon, Enabled, ToolTipText,
  933. * Mnemonic</code>).
  934. * <p>
  935. * Note that <code>PropertyChangeListeners</code> should avoid holding
  936. * strong references to the <code>ActionEvent</code> source,
  937. * as this may hinder garbage collection of the
  938. * <code>ActionEvent</code> source and all components
  939. * in its containment hierarchy.
  940. *
  941. * @param a the new action for the button
  942. * @since 1.3
  943. * @see Action
  944. * @see #setAction
  945. */
  946. protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
  947. return new ButtonActionPropertyChangeListener(this, a);
  948. }
  949. private static class ButtonActionPropertyChangeListener extends AbstractActionPropertyChangeListener {
  950. ButtonActionPropertyChangeListener(AbstractButton b, Action a) {
  951. super(b, a);
  952. }
  953. public void propertyChange(PropertyChangeEvent e) {
  954. String propertyName = e.getPropertyName();
  955. AbstractButton button = (AbstractButton)getTarget();
  956. if (button == null) { //WeakRef GC'ed in 1.2
  957. Action action = (Action)e.getSource();
  958. action.removePropertyChangeListener(this);
  959. } else {
  960. if (e.getPropertyName().equals(Action.NAME)) {
  961. String text = (String) e.getNewValue();
  962. button.setText(text);
  963. button.repaint();
  964. } else if (e.getPropertyName().equals(Action.SHORT_DESCRIPTION)) {
  965. String text = (String) e.getNewValue();
  966. button.setToolTipText(text);
  967. } else if (propertyName.equals("enabled")) {
  968. Boolean enabledState = (Boolean) e.getNewValue();
  969. button.setEnabled(enabledState.booleanValue());
  970. button.repaint();
  971. } else if (e.getPropertyName().equals(Action.SMALL_ICON)) {
  972. Icon icon = (Icon) e.getNewValue();
  973. button.setIcon(icon);
  974. button.invalidate();
  975. button.repaint();
  976. } else if (e.getPropertyName().equals(Action.MNEMONIC_KEY)) {
  977. Integer mn = (Integer) e.getNewValue();
  978. button.setMnemonic(mn.intValue());
  979. button.invalidate();
  980. button.repaint();
  981. }
  982. }
  983. }
  984. }
  985. /**
  986. * Returns whether the border should be painted.
  987. * @return true if the border should be painted, false otherwise
  988. * @see #setBorderPainted
  989. */
  990. public boolean isBorderPainted() {
  991. return paintBorder;
  992. }
  993. /**
  994. * Sets whether the border should be painted.
  995. * @param b if true and border property is not <code>null</code>,
  996. * the border is painted.
  997. * @see #isBorderPainted
  998. * @beaninfo
  999. * bound: true
  1000. * attribute: visualUpdate true
  1001. * description: Whether the border should be painted.
  1002. */
  1003. public void setBorderPainted(boolean b) {
  1004. boolean oldValue = paintBorder;
  1005. paintBorder = b;
  1006. firePropertyChange(BORDER_PAINTED_CHANGED_PROPERTY, oldValue, paintBorder);
  1007. if (b != oldValue) {
  1008. revalidate();
  1009. repaint();
  1010. }
  1011. }
  1012. /**
  1013. * Paint the button's border if <code>BorderPainted</code>
  1014. * property is true.
  1015. * @param g the <code>Graphics</code> context in which to paint
  1016. *
  1017. * @see #paint
  1018. * @see #setBorder
  1019. */
  1020. protected void paintBorder(Graphics g) {
  1021. if (isBorderPainted()) {
  1022. super.paintBorder(g);
  1023. }
  1024. }
  1025. /**
  1026. * Returns whether focus should be painted.
  1027. * @return the <code>paintFocus</code> property
  1028. * @see #setFocusPainted
  1029. */
  1030. public boolean isFocusPainted() {
  1031. return paintFocus;
  1032. }
  1033. /**
  1034. * Sets whether focus should be painted.
  1035. * @param b if true, the focus state is painted
  1036. * @see #isFocusPainted
  1037. * @beaninfo
  1038. * bound: true
  1039. * attribute: visualUpdate true
  1040. * description: Whether focus should be painted
  1041. */
  1042. public void setFocusPainted(boolean b) {
  1043. boolean oldValue = paintFocus;
  1044. paintFocus = b;
  1045. firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY, oldValue, paintFocus);
  1046. if (b != oldValue && hasFocus()) {
  1047. revalidate();
  1048. repaint();
  1049. }
  1050. }
  1051. /**
  1052. * Checks whether the "content area" of the button should be filled.
  1053. * @return the <code>contentAreaFilled</code> property
  1054. * @see #setFocusPainted
  1055. */
  1056. public boolean isContentAreaFilled() {
  1057. return contentAreaFilled;
  1058. }
  1059. /**
  1060. * Sets whether the button should paint the content area
  1061. * or leave it transparent. If you wish to have a transparent
  1062. * button, for example and icon only button, then you should set
  1063. * this to false. Do not call <code>setOpaque(false)</code>.
  1064. * Whether the button follows the
  1065. * <code>RepaintManager</code>'s concept of opacity is L&F depandant.
  1066. * <p>
  1067. * This function may cause the component's opaque property to change.
  1068. * <p>
  1069. * The exact behavior of calling this function varies on a
  1070. * component-by-component and L&F-by-L&F basis.
  1071. *
  1072. * @param b if true, rollover effects should be painted
  1073. * @see #isContentAreaFilled
  1074. * @see #setOpaque
  1075. * @beaninfo
  1076. * bound: true
  1077. * attribute: visualUpdate true
  1078. * description: Whether the button should paint the content area
  1079. * or leave it transparent.
  1080. */
  1081. public void setContentAreaFilled(boolean b) {
  1082. boolean oldValue = contentAreaFilled;
  1083. contentAreaFilled = b;
  1084. firePropertyChange(CONTENT_AREA_FILLED_CHANGED_PROPERTY, oldValue, contentAreaFilled);
  1085. if (b != oldValue) {
  1086. repaint();
  1087. }
  1088. }
  1089. /**
  1090. * Checks whether rollover effects are enabled.
  1091. * @return the <code>rolloverEnabled</code> property
  1092. * @see #setFocusPainted
  1093. */
  1094. public boolean isRolloverEnabled() {
  1095. return rolloverEnabled;
  1096. }
  1097. /**
  1098. * Sets whether rollover effects should be enabled.
  1099. * @param b if true, rollover effects should be painted
  1100. * @see #isRolloverEnabled
  1101. * @beaninfo
  1102. * bound: true
  1103. * attribute: visualUpdate true
  1104. * description: Whether rollover effects should be enabled.
  1105. */
  1106. public void setRolloverEnabled(boolean b) {
  1107. boolean oldValue = rolloverEnabled;
  1108. rolloverEnabled = b;
  1109. firePropertyChange(ROLLOVER_ENABLED_CHANGED_PROPERTY, oldValue, rolloverEnabled);
  1110. if (b != oldValue) {
  1111. repaint();
  1112. }
  1113. }
  1114. /**
  1115. * Returns the keyboard mnemonic from the the current model.
  1116. * @return the keyboard mnemonic from the model
  1117. */
  1118. public int getMnemonic() {
  1119. return model.getMnemonic();
  1120. }
  1121. /**
  1122. * Sets the keyboard mnemonic on the current model.
  1123. *
  1124. * @param mnemonic the key code which represents the mnemonic
  1125. * @beaninfo
  1126. * bound: true
  1127. * attribute: visualUpdate true
  1128. * description: the keyboard character mnemonic
  1129. */
  1130. public void setMnemonic(int mnemonic) {
  1131. int oldValue = getMnemonic();
  1132. model.setMnemonic(mnemonic);
  1133. firePropertyChange(MNEMONIC_CHANGED_PROPERTY, oldValue, mnemonic);
  1134. if (mnemonic != oldValue) {
  1135. revalidate();
  1136. repaint();
  1137. }
  1138. }
  1139. /**
  1140. * Specifies the mnemonic value.
  1141. *
  1142. * @param mnemonic a char specifying the mnemonic value
  1143. * @beaninfo
  1144. * bound: true
  1145. * attribute: visualUpdate true
  1146. * description: the keyboard character mnemonic
  1147. */
  1148. public void setMnemonic(char mnemonic) {
  1149. int vk = (int) mnemonic;
  1150. if(vk >= 'a' && vk <='z')
  1151. vk -= ('a' - 'A');
  1152. setMnemonic(vk);
  1153. }
  1154. /**
  1155. * Identifies whether or not this component can receive the focus.
  1156. *
  1157. * @return true if this component can receive the focus
  1158. */
  1159. public boolean isFocusTraversable() {
  1160. return isEnabled();
  1161. }
  1162. /**
  1163. * Returns the model that this button represents.
  1164. * @return the <code>model</code> property
  1165. * @see #setModel
  1166. */
  1167. public ButtonModel getModel() {
  1168. return model;
  1169. }
  1170. /**
  1171. * Sets the model that this button represents.
  1172. * @param m the new <code>ButtonModel</code>
  1173. * @see #getModel
  1174. * @beaninfo
  1175. * bound: true
  1176. * description: Model that the Button uses.
  1177. */
  1178. public void setModel(ButtonModel newModel) {
  1179. ButtonModel oldModel = getModel();
  1180. if (oldModel != null) {
  1181. oldModel.removeChangeListener(changeListener);
  1182. oldModel.removeActionListener(actionListener);
  1183. changeListener = null;
  1184. actionListener = null;
  1185. }
  1186. model = newModel;
  1187. if (newModel != null) {
  1188. changeListener = createChangeListener();
  1189. actionListener = createActionListener();
  1190. itemListener = createItemListener();
  1191. newModel.addChangeListener(changeListener);
  1192. newModel.addActionListener(actionListener);
  1193. newModel.addItemListener(itemListener);
  1194. }
  1195. firePropertyChange(MODEL_CHANGED_PROPERTY, oldModel, newModel);
  1196. if (newModel != oldModel) {
  1197. revalidate();
  1198. repaint();
  1199. }
  1200. }
  1201. /**
  1202. * Returns the L&F object that renders this component.
  1203. * @return the ButtonUI object
  1204. * @see #setUI
  1205. */
  1206. public ButtonUI getUI() {
  1207. return (ButtonUI) ui;
  1208. }
  1209. /**
  1210. * Sets the L&F object that renders this component.
  1211. * @param ui the <code>ButtonUI</code> L&F object
  1212. * @see #getUI
  1213. */
  1214. public void setUI(ButtonUI ui) {
  1215. super.setUI(ui);
  1216. }
  1217. /**
  1218. * Notification from the <code>UIFactory</code> that the
  1219. * L&F has changed. Subtypes of <code>AbstractButton</code>
  1220. * should override this to update the UI. For
  1221. * example, <code>JButton</code> might do the following:
  1222. * <pre>
  1223. * setUI((ButtonUI)UIManager.getUI(
  1224. * "ButtonUI", "javax.swing.plaf.basic.BasicButtonUI", this));
  1225. * </pre>
  1226. */
  1227. public void updateUI() {
  1228. }
  1229. /**
  1230. * Adds a <code>ChangeListener</code> to the button.
  1231. * @param l the listener to be added
  1232. */
  1233. public void addChangeListener(ChangeListener l) {
  1234. listenerList.add(ChangeListener.class, l);
  1235. }
  1236. /**
  1237. * Removes a ChangeListener from the button.
  1238. * @param l the listener to be removed
  1239. */
  1240. public void removeChangeListener(ChangeListener l) {
  1241. listenerList.remove(ChangeListener.class, l);
  1242. }
  1243. /**
  1244. * Notifies all listeners that have registered interest for
  1245. * notification on this event type. The event instance
  1246. * is lazily created using the parameters passed into
  1247. * the fire method.
  1248. * @see EventListenerList
  1249. */
  1250. protected void fireStateChanged() {
  1251. // Guaranteed to return a non-null array
  1252. Object[] listeners = listenerList.getListenerList();
  1253. // Process the listeners last to first, notifying
  1254. // those that are interested in this event
  1255. for (int i = listeners.length-2; i>=0; i-=2) {
  1256. if (listeners[i]==ChangeListener.class) {
  1257. // Lazily create the event:
  1258. if (changeEvent == null)
  1259. changeEvent = new ChangeEvent(this);
  1260. ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  1261. }
  1262. }
  1263. }
  1264. /**
  1265. * Adds an <code>ActionListener</code> to the button.
  1266. * @param l the <code>ActionListener</code> to be added
  1267. */
  1268. public void addActionListener(ActionListener l) {
  1269. listenerList.add(ActionListener.class, l);
  1270. }
  1271. /**
  1272. * Removes an <code>ActionListener</code> from the button.
  1273. * If the listener is the currently set <code>Action</code>
  1274. * for the button, then the <code>Action</code>
  1275. * is set to <code>null</code>.
  1276. *
  1277. * @param l the listener to be removed
  1278. */
  1279. public void removeActionListener(ActionListener l) {
  1280. if ((l != null) && (getAction() == l)) {
  1281. setAction(null);
  1282. } else {
  1283. listenerList.remove(ActionListener.class, l);
  1284. }
  1285. }
  1286. /**
  1287. * Subclasses that want to handle <code>ChangeEvents</code> differently
  1288. * can override this to return another <code>ChangeListener</code>
  1289. * implementation.
  1290. *
  1291. * @return the new <code>ButtonChangeListener</code>
  1292. */
  1293. protected ChangeListener createChangeListener() {
  1294. return (ChangeListener) new ButtonChangeListener();
  1295. }
  1296. /**
  1297. * Extends <code>ChangeListener</code> to be serializable.
  1298. * <p>
  1299. * <strong>Warning:</strong>
  1300. * Serialized objects of this class will not be compatible with
  1301. * future Swing releases. The current serialization support is appropriate
  1302. * for short term storage or RMI between applications running the same
  1303. * version of Swing. A future release of Swing will provide support for
  1304. * long term persistence.
  1305. */
  1306. protected class ButtonChangeListener implements ChangeListener, Serializable {
  1307. ButtonChangeListener() {
  1308. }
  1309. public void stateChanged(ChangeEvent e) {
  1310. fireStateChanged();
  1311. repaint();
  1312. }
  1313. }
  1314. /**
  1315. * Notifies all listeners that have registered interest for
  1316. * notification on this event type. The event instance
  1317. * is lazily created using the parameters passed into
  1318. * the fire method.
  1319. *
  1320. * @param e the <code>ActionEvent</code> object
  1321. * @see EventListenerList
  1322. */
  1323. protected void fireActionPerformed(ActionEvent event) {
  1324. // Guaranteed to return a non-null array
  1325. Object[] listeners = listenerList.getListenerList();
  1326. ActionEvent e = null;
  1327. // Process the listeners last to first, notifying
  1328. // those that are interested in this event
  1329. for (int i = listeners.length-2; i>=0; i-=2) {
  1330. if (listeners[i]==ActionListener.class) {
  1331. // Lazily create the event:
  1332. if (e == null) {
  1333. String actionCommand = event.getActionCommand();
  1334. if(actionCommand == null) {
  1335. actionCommand = getActionCommand();
  1336. }
  1337. e = new ActionEvent(AbstractButton.this,
  1338. ActionEvent.ACTION_PERFORMED,
  1339. actionCommand,
  1340. event.getModifiers());
  1341. }
  1342. ((ActionListener)listeners[i+1]).actionPerformed(e);
  1343. }
  1344. }
  1345. }
  1346. /**
  1347. * Notifies all listeners that have registered interest for
  1348. * notification on this event type. The event instance
  1349. * is lazily created using the parameters passed into
  1350. * the fire method.
  1351. *
  1352. * @param event the <code>ItemEvent</code> object
  1353. * @see EventListenerList
  1354. */
  1355. protected void fireItemStateChanged(ItemEvent event) {
  1356. // Guaranteed to return a non-null array
  1357. Object[] listeners = listenerList.getListenerList();
  1358. ItemEvent e = null;
  1359. // Process the listeners last to first, notifying
  1360. // those that are interested in this event
  1361. for (int i = listeners.length-2; i>=0; i-=2) {
  1362. if (listeners[i]==ItemListener.class) {
  1363. // Lazily create the event:
  1364. if (e == null) {
  1365. e = new ItemEvent(AbstractButton.this,
  1366. ItemEvent.ITEM_STATE_CHANGED,
  1367. AbstractButton.this,
  1368. event.getStateChange());
  1369. }
  1370. ((ItemListener)listeners[i+1]).itemStateChanged(e);
  1371. }
  1372. }
  1373. if (accessibleContext != null) {
  1374. if (event.getStateChange() == ItemEvent.SELECTED) {
  1375. accessibleContext.firePropertyChange(
  1376. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  1377. null, AccessibleState.SELECTED);
  1378. accessibleContext.firePropertyChange(
  1379. AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
  1380. new Integer(0), new Integer(1));
  1381. } else {
  1382. accessibleContext.firePropertyChange(
  1383. AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  1384. AccessibleState.SELECTED, null);
  1385. accessibleContext.firePropertyChange(
  1386. AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
  1387. new Integer(1), new Integer(0));
  1388. }
  1389. }
  1390. }
  1391. private class ForwardActionEvents implements ActionListener, Serializable {
  1392. public void actionPerformed(ActionEvent event) {
  1393. fireActionPerformed(event);
  1394. }
  1395. }
  1396. protected ActionListener createActionListener() {
  1397. return new ForwardActionEvents();
  1398. }
  1399. private class ForwardItemEvents implements ItemListener, Serializable {
  1400. public void itemStateChanged(ItemEvent event) {
  1401. fireItemStateChanged(event);
  1402. }
  1403. }
  1404. protected ItemListener createItemListener() {
  1405. return new ForwardItemEvents();
  1406. }
  1407. /**
  1408. * Enables (or disables) the button.
  1409. * @param b true to enable the button, otherwise false
  1410. */
  1411. public void setEnabled(boolean b) {
  1412. if (!b && model.isRollover()) {
  1413. model.setRollover(false);
  1414. }
  1415. super.setEnabled(b);
  1416. model.setEnabled(b);
  1417. }
  1418. // *** Deprecated java.awt.Button APIs below *** //
  1419. /**
  1420. * Returns the label text.
  1421. *
  1422. * @return a <code>String</code> containing the label
  1423. * @deprecated - Replaced by <code>getText</code>
  1424. */
  1425. public String getLabel() {
  1426. return getText();
  1427. }
  1428. /**
  1429. * Sets the label text.
  1430. *
  1431. * @param label a <code>String</code> containing the text
  1432. * @deprecated - Replaced by <code>setText(text)</code>
  1433. * @beaninfo
  1434. * bound: true
  1435. * description: Replace by setText(text)
  1436. */
  1437. public void setLabel(String label) {
  1438. setText(label);
  1439. }
  1440. /**
  1441. * Adds an <code>ItemListener</code> to the <code>checkbox</code>.
  1442. * @param l the <code>ItemListener</code> to be added
  1443. */
  1444. public void addItemListener(ItemListener l) {
  1445. listenerList.add(ItemListener.class, l);
  1446. }
  1447. /**
  1448. * Removes an <code>ItemListener</code> from the button.
  1449. * @param l the <code>ItemListener</code> to be removed
  1450. */
  1451. public void removeItemListener(ItemListener l) {
  1452. listenerList.remove(ItemListener.class, l);
  1453. }
  1454. /**
  1455. * Returns an array (length 1) containing the label or
  1456. * <code>null</code> if the button is not selected.
  1457. *
  1458. * @return an array containing 1 Object: the text of the button,
  1459. * if the item is selected; otherwise <code>null</code>
  1460. */
  1461. public Object[] getSelectedObjects() {
  1462. if (isSelected() == false) {
  1463. return null;
  1464. }
  1465. Object[] selectedObjects = new Object[1];
  1466. selectedObjects[0] = getText();
  1467. return selectedObjects;
  1468. }
  1469. protected void init(String text, Icon icon) {
  1470. setLayout(new OverlayLayout(this));
  1471. if(text != null) {
  1472. setText(text);
  1473. }
  1474. if(icon != null) {
  1475. setIcon(icon);
  1476. }
  1477. // Set the UI
  1478. updateUI();
  1479. // Listen for Focus events
  1480. addFocusListener(
  1481. new FocusListener() {
  1482. public void focusGained(FocusEvent event) {
  1483. }
  1484. public void focusLost(FocusEvent event) {
  1485. model.setArmed(false);
  1486. // repaint focus is lost
  1487. if(isFocusPainted()) {
  1488. repaint();
  1489. }
  1490. }
  1491. }
  1492. );
  1493. setAlignmentX(LEFT_ALIGNMENT);
  1494. setAlignmentY(CENTER_ALIGNMENT);
  1495. }
  1496. /**
  1497. * This is overridden to return false if the current <code>Icon</code>'s
  1498. * <code>Image</code> is not equal to the
  1499. * passed in <code>Image</code> <code>img</code>.
  1500. *
  1501. * @param img the <code>Image</code> to be compared
  1502. * @param infoflags flags used to repaint the button when the image
  1503. * is updated and which determine how much is to be painted
  1504. * @param x the x coordinate
  1505. * @param y the y coordinate
  1506. * @param w the width
  1507. * @param h the height
  1508. * @see java.awt.image.ImageObserver
  1509. * @see java.awt.Component#imageUpdate(java.awt.Image, int, int, int, int, int)
  1510. */
  1511. public boolean imageUpdate(Image img, int infoflags,
  1512. int x, int y, int w, int h) {
  1513. if (!SwingUtilities.doesIconReferenceImage(getIcon(), img) &&
  1514. !SwingUtilities.doesIconReferenceImage(getPressedIcon(), img) &&
  1515. !SwingUtilities.doesIconReferenceImage(getDisabledIcon(), img) &&
  1516. !SwingUtilities.doesIconReferenceImage(getSelectedIcon(), img) &&
  1517. !SwingUtilities.doesIconReferenceImage(getDisabledSelectedIcon(),
  1518. img) &&
  1519. !SwingUtilities.doesIconReferenceImage(getRolloverIcon(), img) &&
  1520. !SwingUtilities.doesIconReferenceImage(getRolloverSelectedIcon(),
  1521. img)) {
  1522. // We don't know about this image, disable the notification so
  1523. // we don't keep repainting.
  1524. return false;
  1525. }
  1526. return super.imageUpdate(img, infoflags, x, y, w, h);
  1527. }
  1528. /**
  1529. * Returns a string representation of this <code>AbstractButton</code>.
  1530. * This method
  1531. * is intended to be used only for debugging purposes, and the
  1532. * content and format of the returned string may vary between
  1533. * implementations. The returned string may be empty but may not
  1534. * be <code>null</code>.
  1535. * <P>
  1536. * Overriding <code>paramString</code> to provide information about the
  1537. * specific new aspects of the JFC components.
  1538. *
  1539. * @return a string representation of this <code>AbstractButton</code>
  1540. */
  1541. protected String paramString() {
  1542. String defaultIconString = ((defaultIcon != null)
  1543. && (defaultIcon != this) ?
  1544. defaultIcon.toString() : "");
  1545. String pressedIconString = ((pressedIcon != null)
  1546. && (pressedIcon != this) ?
  1547. pressedIcon.toString() : "");
  1548. String disabledIconString = ((disabledIcon != null)
  1549. && (disabledIcon != this) ?
  1550. disabledIcon.toString() : "");
  1551. String selectedIconString = ((selectedIcon != null)
  1552. && (selectedIcon != this) ?
  1553. selectedIcon.toString() : "");
  1554. String disabledSelectedIconString = ((disabledSelectedIcon != null) &&
  1555. (disabledSelectedIcon != this) ?
  1556. disabledSelectedIcon.toString()
  1557. : "");
  1558. String rolloverIconString = ((rolloverIcon != null)
  1559. && (rolloverIcon != this) ?
  1560. rolloverIcon.toString() : "");
  1561. String rolloverSelectedIconString = ((rolloverSelectedIcon != null) &&
  1562. (rolloverSelectedIcon != this) ?
  1563. rolloverSelectedIcon.toString()
  1564. : "");
  1565. String paintBorderString = (paintBorder ? "true" : "false");
  1566. String paintFocusString = (paintFocus ? "true" : "false");
  1567. String rolloverEnabledString = (rolloverEnabled ? "true" : "false");
  1568. return super.paramString() +
  1569. ",defaultIcon=" + defaultIconString +
  1570. ",disabledIcon=" + disabledIconString +
  1571. ",disabledSelectedIcon=" + disabledSelectedIconString +
  1572. ",margin=" + margin +
  1573. ",paintBorder=" + paintBorderString +
  1574. ",paintFocus=" + paintFocusString +
  1575. ",pressedIcon=" + pressedIconString +
  1576. ",rolloverEnabled=" + rolloverEnabledString +
  1577. ",rolloverIcon=" + rolloverIconString +
  1578. ",rolloverSelectedIcon=" + rolloverSelectedIconString +
  1579. ",selectedIcon=" + selectedIconString +
  1580. ",text=" + text;
  1581. }
  1582. ///////////////////
  1583. // Accessibility support
  1584. ///////////////////
  1585. /**
  1586. * This class implements accessibility support for the
  1587. * <code>AbstractButton</code> class. It provides an implementation of the
  1588. * Java Accessibility API appropriate to button and menu item
  1589. * user-interface elements.
  1590. * <p>
  1591. * <strong>Warning:</strong>
  1592. * Serialized objects of this class will not be compatible with
  1593. * future Swing releases. The current serialization support is appropriate
  1594. * for short term storage or RMI between applications running the same
  1595. * version of Swing. A future release of Swing will provide support for
  1596. * long term persistence.
  1597. */
  1598. protected abstract class AccessibleAbstractButton
  1599. extends AccessibleJComponent implements AccessibleAction,
  1600. AccessibleValue, AccessibleText {
  1601. /**
  1602. * Returns the accessible name of this object.
  1603. *
  1604. * @return the localized name of the object -- can be
  1605. * <code>null</code> if this
  1606. * object does not have a name
  1607. */
  1608. public String getAccessibleName() {
  1609. if (accessibleName != null) {
  1610. return accessibleName;
  1611. } else {
  1612. if (AbstractButton.this.getText() == null) {
  1613. return super.getAccessibleName();
  1614. } else {
  1615. return AbstractButton.this.getText();
  1616. }
  1617. }
  1618. }
  1619. /**
  1620. * Get the AccessibleIcons associated with this object if one
  1621. * or more exist. Otherwise return null.
  1622. */
  1623. public AccessibleIcon [] getAccessibleIcon() {
  1624. if (AbstractButton.this.accessibleIcon == null) {
  1625. return null;
  1626. } else {
  1627. AccessibleIcon [] ac = new AccessibleIcon[1];
  1628. ac[0] = AbstractButton.this.accessibleIcon;
  1629. return ac;
  1630. }
  1631. }
  1632. /**
  1633. * Get the state set of this object.
  1634. *
  1635. * @return an instance of AccessibleState containing the current state
  1636. * of the object
  1637. * @see AccessibleState
  1638. */
  1639. public AccessibleStateSet getAccessibleStateSet() {
  1640. AccessibleStateSet states = super.getAccessibleStateSet();
  1641. if (getModel().isArmed()) {
  1642. states.add(AccessibleState.ARMED);
  1643. }
  1644. if (hasFocus()) {
  1645. states.add(AccessibleState.FOCUSED);
  1646. }
  1647. if (getModel().isPressed()) {
  1648. states.add(AccessibleState.PRESSED);
  1649. }
  1650. if (isSelected()) {
  1651. states.add(AccessibleState.CHECKED);
  1652. }
  1653. return states;
  1654. }
  1655. /**
  1656. * Get the AccessibleRelationSet associated with this object if one
  1657. * exists. Otherwise return null.
  1658. * @see AccessibleRelation
  1659. */
  1660. public AccessibleRelationSet getAccessibleRelationSet() {
  1661. // Check where the AccessibleContext's relation
  1662. // set already contains a MEMBER_OF relation.
  1663. AccessibleRelationSet relationSet
  1664. = super.getAccessibleRelationSet();
  1665. if (!relationSet.contains(AccessibleRelation.MEMBER_OF)) {
  1666. // get the members of the button group if one exists
  1667. ButtonModel model = getModel();
  1668. if (model != null && model instanceof DefaultButtonModel) {
  1669. ButtonGroup group = ((DefaultButtonModel)model).getGroup();
  1670. if (group != null) {
  1671. // set the target of the MEMBER_OF relation to be
  1672. // the members of the button group.
  1673. int len = group.getButtonCount();
  1674. Object [] target = new Object[len];
  1675. Enumeration elem = group.getElements();
  1676. for (int i = 0; i < len; i++) {
  1677. if (elem.hasMoreElements()) {
  1678. target[i] = elem.nextElement();
  1679. }
  1680. }
  1681. AccessibleRelation relation =
  1682. new AccessibleRelation(AccessibleRelation.MEMBER_OF);
  1683. relation.setTarget(target);
  1684. relationSet.add(relation);
  1685. }
  1686. }
  1687. }
  1688. return relationSet;
  1689. }
  1690. /**
  1691. * Get the AccessibleAction associated with this object. In the
  1692. * implementation of the Java Accessibility API for this class,
  1693. * return this object, which is responsible for implementing the
  1694. * AccessibleAction interface on behalf of itself.
  1695. *
  1696. * @return this object
  1697. */
  1698. public AccessibleAction getAccessibleAction() {
  1699. return this;
  1700. }
  1701. /**
  1702. * Get the AccessibleValue associated with this object. In the
  1703. * implementation of the Java Accessibility API for this class,
  1704. * return this object, which is responsible for implementing the
  1705. * AccessibleValue interface on behalf of itself.
  1706. *
  1707. * @return this object
  1708. */
  1709. public AccessibleValue getAccessibleValue() {
  1710. return this;
  1711. }
  1712. /**
  1713. * Returns the number of Actions available in this object. The
  1714. * default behavior of a button is to have one action - toggle
  1715. * the button.
  1716. *
  1717. * @return 1, the number of Actions in this object
  1718. */
  1719. public int getAccessibleActionCount() {
  1720. return 1;
  1721. }
  1722. /**
  1723. * Return a description of the specified action of the object.
  1724. *
  1725. * @param i zero-based index of the actions
  1726. */
  1727. public String getAccessibleActionDescription(int i) {
  1728. if (i == 0) {
  1729. return UIManager.getString("AbstractButton.clickText");
  1730. } else {
  1731. return null;
  1732. }
  1733. }
  1734. /**
  1735. * Perform the specified Action on the object
  1736. *
  1737. * @param i zero-based index of actions
  1738. * @return true if the the action was performed; else false.
  1739. */
  1740. public boolean doAccessibleAction(int i) {
  1741. if (i == 0) {
  1742. doClick();
  1743. return true;
  1744. } else {
  1745. return false;
  1746. }
  1747. }
  1748. /**
  1749. * Get the value of this object as a Number.
  1750. *
  1751. * @return An Integer of 0 if this isn't selected or an Integer of 1 if
  1752. * this is selected.
  1753. * @see AbstractButton#isSelected
  1754. */
  1755. public Number getCurrentAccessibleValue() {
  1756. if (isSelected()) {
  1757. return new Integer(1);
  1758. } else {
  1759. return new Integer(0);
  1760. }
  1761. }
  1762. /**
  1763. * Set the value of this object as a Number.
  1764. *
  1765. * @return True if the value was set.
  1766. */
  1767. public boolean setCurrentAccessibleValue(Number n) {
  1768. if (n instanceof Integer) {
  1769. int i = n.intValue();
  1770. if (i == 0) {
  1771. setSelected(false);
  1772. } else {
  1773. setSelected(true);
  1774. }
  1775. return true;
  1776. } else {
  1777. return false;
  1778. }
  1779. }
  1780. /**
  1781. * Get the minimum value of this object as a Number.
  1782. *
  1783. * @return an Integer of 0.
  1784. */
  1785. public Number getMinimumAccessibleValue() {
  1786. return new Integer(0);
  1787. }
  1788. /**
  1789. * Get the maximum value of this object as a Number.
  1790. *
  1791. * @return An Integer of 1.
  1792. */
  1793. public Number getMaximumAccessibleValue() {
  1794. return new Integer(1);
  1795. }
  1796. /* AccessibleText ---------- */
  1797. public AccessibleText getAccessibleText() {
  1798. View view = (View)AbstractButton.this.getClientProperty("html");
  1799. if (view != null) {
  1800. return this;
  1801. } else {
  1802. return null;
  1803. }
  1804. }
  1805. /**
  1806. * Given a point in local coordinates, return the zero-based index
  1807. * of the character under that Point. If the point is invalid,
  1808. * this method returns -1.
  1809. *
  1810. * Note: the AbstractButton must have a valid size (e.g. have
  1811. * been added to a parent container whose ancestor container
  1812. * is a valid top-level window) for this method to be able
  1813. * to return a meaningful value.
  1814. *
  1815. * @param p the Point in local coordinates
  1816. * @return the zero-based index of the character under Point p; if
  1817. * Point is invalid returns -1.
  1818. */
  1819. public int getIndexAtPoint(Point p) {
  1820. View view = (View) AbstractButton.this.getClientProperty("html");
  1821. if (view != null) {
  1822. Rectangle r = getTextRectangle();
  1823. if (r == null) {
  1824. return -1;
  1825. }
  1826. Rectangle2D.Float shape =
  1827. new Rectangle2D.Float(r.x, r.y, r.width, r.height);
  1828. Position.Bias bias[] = new Position.Bias[1];
  1829. return view.viewToModel(p.x, p.y, shape, bias);
  1830. } else {
  1831. return -1;
  1832. }
  1833. }
  1834. /**
  1835. * Determine the bounding box of the character at the given
  1836. * index into the string. The bounds are returned in local
  1837. * coordinates. If the index is invalid an empty rectangle is
  1838. * returned.
  1839. *
  1840. * Note: the AbstractButton must have a valid size (e.g. have
  1841. * been added to a parent container whose ancestor container
  1842. * is a valid top-level window) for this method to be able
  1843. * to return a meaningful value.
  1844. *
  1845. * @param i the index into the String
  1846. * @return the screen coordinates of the character's the bounding box,
  1847. * if index is invalid returns an empty rectangle.
  1848. */
  1849. public Rectangle getCharacterBounds(int i) {
  1850. View view = (View) AbstractButton.this.getClientProperty("html");
  1851. if (view != null) {
  1852. Rectangle r = getTextRectangle();
  1853. if (r == null) {
  1854. return null;
  1855. }
  1856. Rectangle2D.Float shape =
  1857. new Rectangle2D.Float(r.x, r.y, r.width, r.height);
  1858. try {
  1859. Shape charShape =
  1860. view.modelToView(i, shape, Position.Bias.Forward);
  1861. return charShape.getBounds();
  1862. } catch (BadLocationException e) {
  1863. return null;
  1864. }
  1865. } else {
  1866. return null;
  1867. }
  1868. }
  1869. /**
  1870. * Return the number of characters (valid indicies)
  1871. *
  1872. * @return the number of characters
  1873. */
  1874. public int getCharCount() {
  1875. View view = (View) AbstractButton.this.getClientProperty("html");
  1876. if (view != null) {
  1877. Document d = view.getDocument();
  1878. if (d instanceof StyledDocument) {
  1879. StyledDocument doc = (StyledDocument)d;
  1880. return doc.getLength();
  1881. }
  1882. }
  1883. return accessibleContext.getAccessibleName().length();
  1884. }
  1885. /**
  1886. * Return the zero-based offset of the caret.
  1887. *
  1888. * Note: That to the right of the caret will have the same index
  1889. * value as the offset (the caret is between two characters).
  1890. * @return the zero-based offset of the caret.
  1891. */
  1892. public int getCaretPosition() {
  1893. // There is no caret.
  1894. return -1;
  1895. }
  1896. /**
  1897. * Returns the String at a given index.
  1898. *
  1899. * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
  1900. * or AccessibleText.SENTENCE to retrieve
  1901. * @param index an index within the text >= 0
  1902. * @return the letter, word, or sentence,
  1903. * null for an invalid index or part
  1904. */
  1905. public String getAtIndex(int part, int index) {
  1906. if (index < 0 || index >= getCharCount()) {
  1907. return null;
  1908. }
  1909. switch (part) {
  1910. case AccessibleText.CHARACTER:
  1911. try {
  1912. return getText(index, 1);
  1913. } catch (BadLocationException e) {
  1914. return null;
  1915. }
  1916. case AccessibleText.WORD:
  1917. try {
  1918. String s = getText(0, getCharCount());
  1919. BreakIterator words = BreakIterator.getWordInstance();
  1920. words.setText(s);
  1921. int end = words.following(index);
  1922. return s.substring(words.previous(), end);
  1923. } catch (BadLocationException e) {
  1924. return null;
  1925. }
  1926. case AccessibleText.SENTENCE:
  1927. try {
  1928. String s = getText(0, getCharCount());
  1929. BreakIterator sentence =
  1930. BreakIterator.getSentenceInstance();
  1931. sentence.setText(s);
  1932. int end = sentence.following(index);
  1933. return s.substring(sentence.previous(), end);
  1934. } catch (BadLocationException e) {
  1935. return null;
  1936. }
  1937. default:
  1938. return null;
  1939. }
  1940. }
  1941. /**
  1942. * Returns the String after a given index.
  1943. *
  1944. * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
  1945. * or AccessibleText.SENTENCE to retrieve
  1946. * @param index an index within the text >= 0
  1947. * @return the letter, word, or sentence, null for an invalid
  1948. * index or part
  1949. */
  1950. public String getAfterIndex(int part, int index) {
  1951. if (index < 0 || index >= getCharCount()) {
  1952. return null;
  1953. }
  1954. switch (part) {
  1955. case AccessibleText.CHARACTER:
  1956. if (index+1 >= getCharCount()) {
  1957. return null;
  1958. }
  1959. try {
  1960. return getText(index+1, 1);
  1961. } catch (BadLocationException e) {
  1962. return null;
  1963. }
  1964. case AccessibleText.WORD:
  1965. try {
  1966. String s = getText(0, getCharCount());
  1967. BreakIterator words = BreakIterator.getWordInstance();
  1968. words.setText(s);
  1969. int start = words.following(index);
  1970. if (start == BreakIterator.DONE || start >= s.length()) {
  1971. return null;
  1972. }
  1973. int end = words.following(start);
  1974. if (end == BreakIterator.DONE || end >= s.length()) {
  1975. return null;
  1976. }
  1977. return s.substring(start, end);
  1978. } catch (BadLocationException e) {
  1979. return null;
  1980. }
  1981. case AccessibleText.SENTENCE:
  1982. try {
  1983. String s = getText(0, getCharCount());
  1984. BreakIterator sentence =
  1985. BreakIterator.getSentenceInstance();
  1986. sentence.setText(s);
  1987. int start = sentence.following(index);
  1988. if (start == BreakIterator.DONE || start >= s.length()) {
  1989. return null;
  1990. }
  1991. int end = sentence.following(start);
  1992. if (end == BreakIterator.DONE || end >= s.length()) {
  1993. return null;
  1994. }
  1995. return s.substring(start, end);
  1996. } catch (BadLocationException e) {
  1997. return null;
  1998. }
  1999. default:
  2000. return null;
  2001. }
  2002. }
  2003. /**
  2004. * Returns the String before a given index.
  2005. *
  2006. * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
  2007. * or AccessibleText.SENTENCE to retrieve
  2008. * @param index an index within the text >= 0
  2009. * @return the letter, word, or sentence, null for an invalid index
  2010. * or part
  2011. */
  2012. public String getBeforeIndex(int part, int index) {
  2013. if (index < 0 || index > getCharCount()-1) {
  2014. return null;
  2015. }
  2016. switch (part) {
  2017. case AccessibleText.CHARACTER:
  2018. if (index == 0) {
  2019. return null;
  2020. }
  2021. try {
  2022. return getText(index-1, 1);
  2023. } catch (BadLocationException e) {
  2024. return null;
  2025. }
  2026. case AccessibleText.WORD:
  2027. try {
  2028. String s = getText(0, getCharCount());
  2029. BreakIterator words = BreakIterator.getWordInstance();
  2030. words.setText(s);
  2031. int end = words.following(index);
  2032. end = words.previous();
  2033. int start = words.previous();
  2034. if (start == BreakIterator.DONE) {
  2035. return null;
  2036. }
  2037. return s.substring(start, end);
  2038. } catch (BadLocationException e) {
  2039. return null;
  2040. }
  2041. case AccessibleText.SENTENCE:
  2042. try {
  2043. String s = getText(0, getCharCount());
  2044. BreakIterator sentence =
  2045. BreakIterator.getSentenceInstance();
  2046. sentence.setText(s);
  2047. int end = sentence.following(index);
  2048. end = sentence.previous();
  2049. int start = sentence.previous();
  2050. if (start == BreakIterator.DONE) {
  2051. return null;
  2052. }
  2053. return s.substring(start, end);
  2054. } catch (BadLocationException e) {
  2055. return null;
  2056. }
  2057. default:
  2058. return null;
  2059. }
  2060. }
  2061. /**
  2062. * Return the AttributeSet for a given character at a given index
  2063. *
  2064. * @param i the zero-based index into the text
  2065. * @return the AttributeSet of the character
  2066. */
  2067. public AttributeSet getCharacterAttribute(int i) {
  2068. View view = (View) AbstractButton.this.getClientProperty("html");
  2069. if (view != null) {
  2070. Document d = view.getDocument();
  2071. if (d instanceof StyledDocument) {
  2072. StyledDocument doc = (StyledDocument)d;
  2073. Element elem = doc.getCharacterElement(i);
  2074. if (elem != null) {
  2075. return elem.getAttributes();
  2076. }
  2077. }
  2078. }
  2079. return null;
  2080. }
  2081. /**
  2082. * Returns the start offset within the selected text.
  2083. * If there is no selection, but there is
  2084. * a caret, the start and end offsets will be the same.
  2085. *
  2086. * @return the index into the text of the start of the selection
  2087. */
  2088. public int getSelectionStart() {
  2089. // Text cannot be selected.
  2090. return -1;
  2091. }
  2092. /**
  2093. * Returns the end offset within the selected text.
  2094. * If there is no selection, but there is
  2095. * a caret, the start and end offsets will be the same.
  2096. *
  2097. * @return the index into teh text of the end of the selection
  2098. */
  2099. public int getSelectionEnd() {
  2100. // Text cannot be selected.
  2101. return -1;
  2102. }
  2103. /**
  2104. * Returns the portion of the text that is selected.
  2105. *
  2106. * @return the String portion of the text that is selected
  2107. */
  2108. public String getSelectedText() {
  2109. // Text cannot be selected.
  2110. return null;
  2111. }
  2112. /*
  2113. * Returns the text substring starting at the specified
  2114. * offset with the specified length.
  2115. */
  2116. private String getText(int offset, int length)
  2117. throws BadLocationException {
  2118. View view = (View) AbstractButton.this.getClientProperty("html");
  2119. if (view != null) {
  2120. Document d = view.getDocument();
  2121. if (d instanceof StyledDocument) {
  2122. StyledDocument doc = (StyledDocument)d;
  2123. return doc.getText(offset, length);
  2124. }
  2125. }
  2126. return null;
  2127. }
  2128. /*
  2129. * Returns the bounding rectangle for the component text.
  2130. */
  2131. private Rectangle getTextRectangle() {
  2132. String text = AbstractButton.this.getText();
  2133. Icon icon = (AbstractButton.this.isEnabled()) ? AbstractButton.this.getIcon() : AbstractButton.this.getDisabledIcon();
  2134. if ((icon == null) && (text == null)) {
  2135. return null;
  2136. }
  2137. Rectangle paintIconR = new Rectangle();
  2138. Rectangle paintTextR = new Rectangle();
  2139. Rectangle paintViewR = new Rectangle();
  2140. Insets paintViewInsets = new Insets(0, 0, 0, 0);
  2141. paintViewInsets = AbstractButton.this.getInsets(paintViewInsets);
  2142. paintViewR.x = paintViewInsets.left;
  2143. paintViewR.y = paintViewInsets.top;
  2144. paintViewR.width = AbstractButton.this.getWidth() - (paintViewInsets.left + paintViewInsets.right);
  2145. paintViewR.height = AbstractButton.this.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);
  2146. Graphics g = AbstractButton.this.getGraphics();
  2147. if (g == null) {
  2148. return null;
  2149. }
  2150. String clippedText = SwingUtilities.layoutCompoundLabel(
  2151. (JComponent)AbstractButton.this,
  2152. g.getFontMetrics(),
  2153. text,
  2154. icon,
  2155. AbstractButton.this.getVerticalAlignment(),
  2156. AbstractButton.this.getHorizontalAlignment(),
  2157. AbstractButton.this.getVerticalTextPosition(),
  2158. AbstractButton.this.getHorizontalTextPosition(),
  2159. paintViewR,
  2160. paintIconR,
  2161. paintTextR,
  2162. 0);
  2163. return paintTextR;
  2164. }
  2165. }
  2166. }