1. /*
  2. * @(#)BasicRootPaneUI.java 1.3 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.event.ActionEvent;
  12. import java.beans.PropertyChangeEvent;
  13. import java.beans.PropertyChangeListener;
  14. import javax.swing.*;
  15. import javax.swing.plaf.*;
  16. /**
  17. * Basic implementation of RootPaneUI, there is one shared between all
  18. * JRootPane instances.
  19. *
  20. * @version 1.3 02/02/00
  21. * @author Scott Violet
  22. */
  23. public class BasicRootPaneUI extends RootPaneUI implements
  24. PropertyChangeListener {
  25. private static RootPaneUI rootPaneUI = new BasicRootPaneUI();
  26. public static ComponentUI createUI(JComponent c) {
  27. return rootPaneUI;
  28. }
  29. public void installUI(JComponent c) {
  30. installDefaults((JRootPane)c);
  31. installComponents((JRootPane)c);
  32. installListeners((JRootPane)c);
  33. installKeyboardActions((JRootPane)c);
  34. }
  35. public void uninstallUI(JComponent c) {
  36. uninstallDefaults((JRootPane)c);
  37. uninstallComponents((JRootPane)c);
  38. uninstallListeners((JRootPane)c);
  39. uninstallKeyboardActions((JRootPane)c);
  40. }
  41. protected void installDefaults(JRootPane c){
  42. }
  43. protected void installComponents(JRootPane root) {
  44. }
  45. protected void installListeners(JRootPane root) {
  46. root.addPropertyChangeListener(this);
  47. }
  48. protected void installKeyboardActions(JRootPane root) {
  49. InputMap km = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, root);
  50. SwingUtilities.replaceUIInputMap(root, JComponent.WHEN_IN_FOCUSED_WINDOW,
  51. km);
  52. ActionMap am = getActionMap(root);
  53. SwingUtilities.replaceUIActionMap(root, am);
  54. updateDefaultButtonBindings(root);
  55. }
  56. protected void uninstallDefaults(JRootPane root) {
  57. }
  58. protected void uninstallComponents(JRootPane root) {
  59. }
  60. protected void uninstallListeners(JRootPane root) {
  61. root.removePropertyChangeListener(this);
  62. }
  63. protected void uninstallKeyboardActions(JRootPane root) {
  64. SwingUtilities.replaceUIInputMap(root, JComponent.
  65. WHEN_IN_FOCUSED_WINDOW, null);
  66. SwingUtilities.replaceUIActionMap(root, null);
  67. }
  68. InputMap getInputMap(int condition, JComponent c) {
  69. if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
  70. return createInputMap(condition, c);
  71. }
  72. return null;
  73. }
  74. ActionMap getActionMap(JComponent c) {
  75. return createActionMap(c);
  76. }
  77. ComponentInputMap createInputMap(int condition, JComponent c) {
  78. return new ComponentInputMapUIResource(c);
  79. }
  80. ActionMap createActionMap(JComponent c) {
  81. ActionMap map = new ActionMapUIResource();
  82. map.put("press", new DefaultAction((JRootPane)c, true));
  83. map.put("release", new DefaultAction((JRootPane)c, false));
  84. return map;
  85. }
  86. /**
  87. * Invoked when the default button property has changed. This reloads
  88. * the bindings from the defaults table with name
  89. * <code>RootPane.defaultButtonWindowKeyBindings</code>.
  90. */
  91. void updateDefaultButtonBindings(JRootPane root) {
  92. InputMap km = SwingUtilities.getUIInputMap(root, JComponent.
  93. WHEN_IN_FOCUSED_WINDOW);
  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. updateDefaultButtonBindings((JRootPane)e.getSource());
  113. }
  114. }
  115. // This was transplanted from JRootPane.
  116. static class DefaultAction extends AbstractAction {
  117. JRootPane root;
  118. boolean press;
  119. DefaultAction(JRootPane root, boolean press) {
  120. this.root = root;
  121. this.press = press;
  122. }
  123. public void actionPerformed(ActionEvent e) {
  124. JButton owner = root.getDefaultButton();
  125. if (owner != null && SwingUtilities.getRootPane(owner) == root) {
  126. ButtonModel model = owner.getModel();
  127. if (press) {
  128. model.setArmed(true);
  129. model.setPressed(true);
  130. } else {
  131. model.setPressed(false);
  132. }
  133. }
  134. }
  135. public boolean isEnabled() {
  136. JButton owner = root.getDefaultButton();
  137. return (owner != null && owner.getModel().isEnabled());
  138. }
  139. }
  140. }