1. /*
  2. * @(#)BasicButtonListener.java 1.46 00/02/02
  3. *
  4. * Copyright 1997-2000 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.plaf.basic;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import java.beans.*;
  14. import javax.swing.*;
  15. import javax.swing.event.*;
  16. import javax.swing.plaf.ActionMapUIResource;
  17. import javax.swing.plaf.ButtonUI;
  18. import javax.swing.plaf.ComponentInputMapUIResource;
  19. /**
  20. * Button Listener
  21. *
  22. * @version 1.46 02/02/00
  23. * @author Jeff Dinkins
  24. * @author Arnaud Weber (keyboard UI support)
  25. */
  26. public class BasicButtonListener implements MouseListener, MouseMotionListener,
  27. FocusListener, ChangeListener, PropertyChangeListener
  28. {
  29. /** Set to true when the WindowInputMap is installed. */
  30. private boolean createdWindowInputMap;
  31. public BasicButtonListener(AbstractButton b) {
  32. }
  33. public void propertyChange(PropertyChangeEvent e) {
  34. String prop = e.getPropertyName();
  35. if(prop.equals(AbstractButton.MNEMONIC_CHANGED_PROPERTY)) {
  36. updateMnemonicBinding((AbstractButton)e.getSource());
  37. }
  38. if(prop.equals(AbstractButton.CONTENT_AREA_FILLED_CHANGED_PROPERTY)) {
  39. checkOpacity((AbstractButton) e.getSource() );
  40. }
  41. if(prop.equals(AbstractButton.TEXT_CHANGED_PROPERTY)) {
  42. AbstractButton b = (AbstractButton) e.getSource();
  43. BasicHTML.updateRenderer(b, b.getText());
  44. }
  45. }
  46. protected void checkOpacity(AbstractButton b) {
  47. b.setOpaque( b.isContentAreaFilled() );
  48. }
  49. /**
  50. * Register default key actions: pressing space to "click" a
  51. * button and registring the keyboard mnemonic (if any).
  52. */
  53. public void installKeyboardActions(JComponent c) {
  54. AbstractButton b = (AbstractButton)c;
  55. // Update the mnemonic binding.
  56. updateMnemonicBinding(b);
  57. // Reset the ActionMap.
  58. ActionMap map = getActionMap(b);
  59. SwingUtilities.replaceUIActionMap(c, map);
  60. InputMap km = getInputMap(JComponent.WHEN_FOCUSED, c);
  61. SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, km);
  62. }
  63. /**
  64. * Unregister's default key actions
  65. */
  66. public void uninstallKeyboardActions(JComponent c) {
  67. if (createdWindowInputMap) {
  68. SwingUtilities.replaceUIInputMap(c, JComponent.
  69. WHEN_IN_FOCUSED_WINDOW, null);
  70. createdWindowInputMap = false;
  71. }
  72. SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null);
  73. SwingUtilities.replaceUIActionMap(c, null);
  74. }
  75. /**
  76. * Returns the ActionMap to use for <code>b</code>. Called as part of
  77. * <code>installKeyboardActions</code>.
  78. */
  79. ActionMap getActionMap(AbstractButton b) {
  80. return createActionMap(b);
  81. }
  82. /**
  83. * Returns the InputMap for condition <code>condition</code>. Called as
  84. * part of <code>installKeyboardActions</code>.
  85. */
  86. InputMap getInputMap(int condition, JComponent c) {
  87. if (condition == JComponent.WHEN_FOCUSED) {
  88. ButtonUI ui = ((AbstractButton)c).getUI();
  89. if (ui != null && (ui instanceof BasicButtonUI)) {
  90. return (InputMap)UIManager.get(((BasicButtonUI)ui).
  91. getPropertyPrefix() +"focusInputMap");
  92. }
  93. }
  94. return null;
  95. }
  96. /**
  97. * Creates and returns the ActionMap to use for the button.
  98. */
  99. ActionMap createActionMap(AbstractButton c) {
  100. ActionMap retValue = new javax.swing.plaf.ActionMapUIResource();
  101. retValue.put("pressed", new PressedAction((AbstractButton)c));
  102. retValue.put("released", new ReleasedAction((AbstractButton)c));
  103. return retValue;
  104. }
  105. /**
  106. * Resets the binding for the mnemonic in the WHEN_IN_FOCUSED_WINDOW
  107. * UI InputMap.
  108. */
  109. void updateMnemonicBinding(AbstractButton b) {
  110. int m = b.getMnemonic();
  111. if(m != 0) {
  112. InputMap map;
  113. if (!createdWindowInputMap) {
  114. map = new ComponentInputMapUIResource(b);
  115. SwingUtilities.replaceUIInputMap(b,
  116. JComponent.WHEN_IN_FOCUSED_WINDOW, map);
  117. createdWindowInputMap = true;
  118. }
  119. else {
  120. map = SwingUtilities.getUIInputMap(b, JComponent.
  121. WHEN_IN_FOCUSED_WINDOW);
  122. }
  123. if (map != null) {
  124. map.clear();
  125. map.put(KeyStroke.getKeyStroke(m, ActionEvent.ALT_MASK, false),
  126. "pressed");
  127. map.put(KeyStroke.getKeyStroke(m, ActionEvent.ALT_MASK, true),
  128. "released");
  129. map.put(KeyStroke.getKeyStroke(m, 0, true), "released");
  130. }
  131. }
  132. else if (createdWindowInputMap) {
  133. InputMap map = SwingUtilities.getUIInputMap(b, JComponent.
  134. WHEN_IN_FOCUSED_WINDOW);
  135. if (map != null) {
  136. map.clear();
  137. }
  138. }
  139. }
  140. public void stateChanged(ChangeEvent e) {
  141. AbstractButton b = (AbstractButton) e.getSource();
  142. b.repaint();
  143. }
  144. public void focusGained(FocusEvent e) {
  145. AbstractButton b = (AbstractButton) e.getSource();
  146. if (b instanceof JButton && ((JButton)b).isDefaultCapable()) {
  147. JRootPane root = b.getRootPane();
  148. if (root != null) {
  149. root.setDefaultButton((JButton)b);
  150. }
  151. }
  152. b.repaint();
  153. }
  154. public void focusLost(FocusEvent e) {
  155. AbstractButton b = (AbstractButton) e.getSource();
  156. b.repaint();
  157. }
  158. public void mouseMoved(MouseEvent e) {
  159. };
  160. public void mouseDragged(MouseEvent e) {
  161. };
  162. public void mouseClicked(MouseEvent e) {
  163. };
  164. public void mousePressed(MouseEvent e) {
  165. if (SwingUtilities.isLeftMouseButton(e) ) {
  166. AbstractButton b = (AbstractButton) e.getSource();
  167. if(b.contains(e.getX(), e.getY())) {
  168. ButtonModel model = b.getModel();
  169. if (!model.isEnabled()) {
  170. // Disabled buttons ignore all input...
  171. return;
  172. }
  173. if (!model.isArmed()) {
  174. // button not armed, should be
  175. model.setArmed(true);
  176. }
  177. model.setPressed(true);
  178. if(!b.hasFocus()) {
  179. b.requestFocus();
  180. }
  181. }
  182. }
  183. };
  184. public void mouseReleased(MouseEvent e) {
  185. AbstractButton b = (AbstractButton) e.getSource();
  186. ButtonModel model = b.getModel();
  187. model.setPressed(false);
  188. };
  189. public void mouseEntered(MouseEvent e) {
  190. AbstractButton b = (AbstractButton) e.getSource();
  191. if(b.contains(e.getX(), e.getY())) {
  192. ButtonModel model = b.getModel();
  193. if(b.isRolloverEnabled()) {
  194. model.setRollover(true);
  195. }
  196. model.setArmed(true);
  197. }
  198. };
  199. public void mouseExited(MouseEvent e) {
  200. AbstractButton b = (AbstractButton) e.getSource();
  201. if(!b.contains(e.getX(), e.getY())) {
  202. ButtonModel model = b.getModel();
  203. if(b.isRolloverEnabled()) {
  204. model.setRollover(false);
  205. }
  206. model.setArmed(false);
  207. }
  208. };
  209. static class PressedAction extends AbstractAction {
  210. AbstractButton b = null;
  211. PressedAction(AbstractButton b) {
  212. this.b = b;
  213. }
  214. public void actionPerformed(ActionEvent e) {
  215. ButtonModel model = b.getModel();
  216. model.setArmed(true);
  217. model.setPressed(true);
  218. if(!b.hasFocus()) {
  219. b.requestFocus();
  220. }
  221. }
  222. public boolean isEnabled() {
  223. if(!b.getModel().isEnabled()) {
  224. return false;
  225. } else {
  226. return true;
  227. }
  228. }
  229. }
  230. static class ReleasedAction extends AbstractAction {
  231. AbstractButton b = null;
  232. ReleasedAction(AbstractButton b) {
  233. this.b = b;
  234. }
  235. public void actionPerformed(ActionEvent e) {
  236. ButtonModel model = b.getModel();
  237. model.setPressed(false);
  238. model.setArmed(false);
  239. }
  240. public boolean isEnabled() {
  241. if(!b.getModel().isEnabled()) {
  242. return false;
  243. } else {
  244. return true;
  245. }
  246. }
  247. }
  248. }