1. /*
  2. * @(#)SynthRootPaneUI.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 com.sun.java.swing.plaf.gtk;
  8. import java.awt.*;
  9. import java.awt.event.ActionEvent;
  10. import java.beans.PropertyChangeEvent;
  11. import java.beans.PropertyChangeListener;
  12. import javax.swing.*;
  13. import javax.swing.plaf.*;
  14. /**
  15. * Basic implementation of RootPaneUI, there is one shared between all
  16. * JRootPane instances.
  17. *
  18. * @version 1.9, 01/23/03 (based on BasicRootPaneUI v 1.8)
  19. * @author Scott Violet
  20. */
  21. class SynthRootPaneUI extends RootPaneUI implements SynthUI,
  22. PropertyChangeListener, LazyActionMap.Loader {
  23. private SynthStyle style;
  24. public static ComponentUI createUI(JComponent c) {
  25. return new SynthRootPaneUI();
  26. }
  27. public void installUI(JComponent c) {
  28. installDefaults((JRootPane)c);
  29. installComponents((JRootPane)c);
  30. installListeners((JRootPane)c);
  31. installKeyboardActions((JRootPane)c);
  32. }
  33. public void uninstallUI(JComponent c) {
  34. uninstallDefaults((JRootPane)c);
  35. uninstallComponents((JRootPane)c);
  36. uninstallListeners((JRootPane)c);
  37. uninstallKeyboardActions((JRootPane)c);
  38. }
  39. protected void installDefaults(JRootPane c){
  40. fetchStyle(c);
  41. }
  42. protected void installComponents(JRootPane root) {
  43. }
  44. protected void installListeners(JRootPane root) {
  45. root.addPropertyChangeListener(this);
  46. }
  47. protected void installKeyboardActions(JRootPane root) {
  48. InputMap km = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, root);
  49. SwingUtilities.replaceUIInputMap(root, JComponent.WHEN_IN_FOCUSED_WINDOW,
  50. km);
  51. LazyActionMap.installLazyActionMap(root, this);
  52. updateDefaultButtonBindings(root);
  53. }
  54. protected void uninstallDefaults(JRootPane root) {
  55. SynthContext context = getContext(root, ENABLED);
  56. style.uninstallDefaults(context);
  57. context.dispose();
  58. style = null;
  59. }
  60. protected void uninstallComponents(JRootPane root) {
  61. }
  62. protected void uninstallListeners(JRootPane root) {
  63. root.removePropertyChangeListener(this);
  64. }
  65. protected void uninstallKeyboardActions(JRootPane root) {
  66. SwingUtilities.replaceUIInputMap(root, JComponent.
  67. WHEN_IN_FOCUSED_WINDOW, null);
  68. SwingUtilities.replaceUIActionMap(root, null);
  69. }
  70. public SynthContext getContext(JComponent c) {
  71. return getContext(c, getComponentState(c));
  72. }
  73. private SynthContext getContext(JComponent c, int state) {
  74. return SynthContext.getContext(SynthContext.class, c,
  75. SynthLookAndFeel.getRegion(c), style, state);
  76. }
  77. private Region getRegion(JComponent c) {
  78. return SynthLookAndFeel.getRegion(c);
  79. }
  80. private int getComponentState(JComponent c) {
  81. return SynthLookAndFeel.getComponentState(c);
  82. }
  83. private void fetchStyle(JComponent c) {
  84. SynthContext context = getContext(c, ENABLED);
  85. style = SynthLookAndFeel.updateStyle(context, this);
  86. context.dispose();
  87. }
  88. InputMap getInputMap(int condition, JComponent c) {
  89. if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
  90. return createInputMap(condition, c);
  91. }
  92. return null;
  93. }
  94. ComponentInputMap createInputMap(int condition, JComponent c) {
  95. return new RootPaneInputMap(c);
  96. }
  97. public void loadActionMap(JComponent c, ActionMap map) {
  98. map.put("press", new DefaultAction((JRootPane)c, true));
  99. map.put("release", new DefaultAction((JRootPane)c, false));
  100. }
  101. /**
  102. * Invoked when the default button property has changed. This reloads
  103. * the bindings from the defaults table with name
  104. * <code>RootPane.defaultButtonWindowKeyBindings</code>.
  105. */
  106. void updateDefaultButtonBindings(JRootPane root) {
  107. InputMap km = SwingUtilities.getUIInputMap(root, JComponent.
  108. WHEN_IN_FOCUSED_WINDOW);
  109. while (km != null && !(km instanceof RootPaneInputMap)) {
  110. km = km.getParent();
  111. }
  112. if (km != null) {
  113. km.clear();
  114. if (root.getDefaultButton() != null) {
  115. SynthContext context = getContext(root, ENABLED);
  116. Object[] bindings = (Object[])style.get(context,
  117. "RootPane.defaultButtonWindowKeyBindings");
  118. if (bindings != null) {
  119. LookAndFeel.loadKeyBindings(km, bindings);
  120. }
  121. context.dispose();
  122. }
  123. }
  124. }
  125. public void update(Graphics g, JComponent c) {
  126. SynthContext context = getContext(c);
  127. SynthLookAndFeel.update(context, g);
  128. paint(context, g);
  129. context.dispose();
  130. }
  131. public void paint(Graphics g, JComponent c) {
  132. SynthContext context = getContext(c);
  133. paint(context, g);
  134. context.dispose();
  135. }
  136. protected void paint(SynthContext context, Graphics g) {
  137. }
  138. /**
  139. * Invoked when a property changes on the root pane. If the event
  140. * indicates the <code>defaultButton</code> has changed, this will
  141. * reinstall the keyboard actions.
  142. */
  143. public void propertyChange(PropertyChangeEvent e) {
  144. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  145. fetchStyle((JRootPane)e.getSource());
  146. }
  147. if ("defaultButton".equals(e.getPropertyName())) {
  148. JRootPane rootpane = (JRootPane)e.getSource();
  149. updateDefaultButtonBindings(rootpane);
  150. if (rootpane.getClientProperty("temporaryDefaultButton") == null) {
  151. rootpane.putClientProperty("initialDefaultButton", e.getNewValue());
  152. }
  153. }
  154. }
  155. // This was transplanted from JRootPane.
  156. static class DefaultAction extends AbstractAction {
  157. JRootPane root;
  158. boolean press;
  159. DefaultAction(JRootPane root, boolean press) {
  160. this.root = root;
  161. this.press = press;
  162. }
  163. public void actionPerformed(ActionEvent e) {
  164. JButton owner = root.getDefaultButton();
  165. if (owner != null && SwingUtilities.getRootPane(owner) == root) {
  166. if (press) {
  167. owner.doClick(20);
  168. }
  169. }
  170. }
  171. public boolean isEnabled() {
  172. JButton owner = root.getDefaultButton();
  173. return (owner != null && owner.getModel().isEnabled());
  174. }
  175. }
  176. private static class RootPaneInputMap extends ComponentInputMapUIResource {
  177. public RootPaneInputMap(JComponent c) {
  178. super(c);
  179. }
  180. }
  181. }