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