1. /*
  2. * @(#)BasicRootPaneUI.java 1.9 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.basic;
  8. import java.awt.event.ActionEvent;
  9. import java.beans.PropertyChangeEvent;
  10. import java.beans.PropertyChangeListener;
  11. import javax.swing.*;
  12. import javax.swing.plaf.*;
  13. /**
  14. * Basic implementation of RootPaneUI, there is one shared between all
  15. * JRootPane instances.
  16. *
  17. * @version 1.9 01/23/03
  18. * @author Scott Violet
  19. */
  20. public class BasicRootPaneUI extends RootPaneUI implements
  21. PropertyChangeListener {
  22. private static RootPaneUI rootPaneUI = new BasicRootPaneUI();
  23. public static ComponentUI createUI(JComponent c) {
  24. return rootPaneUI;
  25. }
  26. public void installUI(JComponent c) {
  27. installDefaults((JRootPane)c);
  28. installComponents((JRootPane)c);
  29. installListeners((JRootPane)c);
  30. installKeyboardActions((JRootPane)c);
  31. }
  32. public void uninstallUI(JComponent c) {
  33. uninstallDefaults((JRootPane)c);
  34. uninstallComponents((JRootPane)c);
  35. uninstallListeners((JRootPane)c);
  36. uninstallKeyboardActions((JRootPane)c);
  37. }
  38. protected void installDefaults(JRootPane c){
  39. }
  40. protected void installComponents(JRootPane root) {
  41. }
  42. protected void installListeners(JRootPane root) {
  43. root.addPropertyChangeListener(this);
  44. }
  45. protected void installKeyboardActions(JRootPane root) {
  46. InputMap km = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, root);
  47. SwingUtilities.replaceUIInputMap(root, JComponent.WHEN_IN_FOCUSED_WINDOW,
  48. km);
  49. ActionMap am = getActionMap(root);
  50. SwingUtilities.replaceUIActionMap(root, am);
  51. updateDefaultButtonBindings(root);
  52. }
  53. protected void uninstallDefaults(JRootPane root) {
  54. }
  55. protected void uninstallComponents(JRootPane root) {
  56. }
  57. protected void uninstallListeners(JRootPane root) {
  58. root.removePropertyChangeListener(this);
  59. }
  60. protected void uninstallKeyboardActions(JRootPane root) {
  61. SwingUtilities.replaceUIInputMap(root, JComponent.
  62. WHEN_IN_FOCUSED_WINDOW, null);
  63. SwingUtilities.replaceUIActionMap(root, null);
  64. }
  65. InputMap getInputMap(int condition, JComponent c) {
  66. if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
  67. return createInputMap(condition, c);
  68. }
  69. return null;
  70. }
  71. ActionMap getActionMap(JComponent c) {
  72. return createActionMap(c);
  73. }
  74. ComponentInputMap createInputMap(int condition, JComponent c) {
  75. return new RootPaneInputMap(c);
  76. }
  77. ActionMap createActionMap(JComponent c) {
  78. ActionMap map = new ActionMapUIResource();
  79. map.put("press", new DefaultAction((JRootPane)c, true));
  80. map.put("release", new DefaultAction((JRootPane)c, false));
  81. return map;
  82. }
  83. /**
  84. * Invoked when the default button property has changed. This reloads
  85. * the bindings from the defaults table with name
  86. * <code>RootPane.defaultButtonWindowKeyBindings</code>.
  87. */
  88. void updateDefaultButtonBindings(JRootPane root) {
  89. InputMap km = SwingUtilities.getUIInputMap(root, JComponent.
  90. WHEN_IN_FOCUSED_WINDOW);
  91. while (km != null && !(km instanceof RootPaneInputMap)) {
  92. km = km.getParent();
  93. }
  94. if (km != null) {
  95. km.clear();
  96. if (root.getDefaultButton() != null) {
  97. Object[] bindings = (Object[])UIManager.get
  98. ("RootPane.defaultButtonWindowKeyBindings");
  99. if (bindings != null) {
  100. LookAndFeel.loadKeyBindings(km, bindings);
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * Invoked when a property changes on the root pane. If the event
  107. * indicates the <code>defaultButton</code> has changed, this will
  108. * reinstall the keyboard actions.
  109. */
  110. public void propertyChange(PropertyChangeEvent e) {
  111. if(e.getPropertyName().equals("defaultButton")) {
  112. JRootPane rootpane = (JRootPane)e.getSource();
  113. updateDefaultButtonBindings(rootpane);
  114. if (rootpane.getClientProperty("temporaryDefaultButton") == null) {
  115. rootpane.putClientProperty("initialDefaultButton", e.getNewValue());
  116. }
  117. }
  118. }
  119. // This was transplanted from JRootPane.
  120. static class DefaultAction extends AbstractAction {
  121. JRootPane root;
  122. boolean press;
  123. DefaultAction(JRootPane root, boolean press) {
  124. this.root = root;
  125. this.press = press;
  126. }
  127. public void actionPerformed(ActionEvent e) {
  128. JButton owner = root.getDefaultButton();
  129. if (owner != null && SwingUtilities.getRootPane(owner) == root) {
  130. if (press) {
  131. owner.doClick(20);
  132. }
  133. }
  134. }
  135. public boolean isEnabled() {
  136. JButton owner = root.getDefaultButton();
  137. return (owner != null && owner.getModel().isEnabled());
  138. }
  139. }
  140. private static class RootPaneInputMap extends ComponentInputMapUIResource {
  141. public RootPaneInputMap(JComponent c) {
  142. super(c);
  143. }
  144. }
  145. }