1. /*
  2. * @(#)SynthSplitPaneUI.java 1.15 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.synth;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.beans.*;
  11. import java.util.*;
  12. import javax.swing.*;
  13. import javax.swing.event.*;
  14. import javax.swing.plaf.*;
  15. import javax.swing.plaf.basic.*;
  16. import sun.swing.plaf.synth.SynthUI;
  17. /**
  18. * Synth's SplitPaneUI.
  19. *
  20. * @version 1.15, 12/19/03
  21. * @author Scott Violet
  22. */
  23. class SynthSplitPaneUI extends BasicSplitPaneUI implements
  24. PropertyChangeListener, SynthUI {
  25. /**
  26. * Keys to use for forward focus traversal when the JComponent is
  27. * managing focus.
  28. */
  29. private static Set managingFocusForwardTraversalKeys;
  30. /**
  31. * Keys to use for backward focus traversal when the JComponent is
  32. * managing focus.
  33. */
  34. private static Set managingFocusBackwardTraversalKeys;
  35. /**
  36. * Style for the JSplitPane.
  37. */
  38. private SynthStyle style;
  39. /**
  40. * Style for the divider.
  41. */
  42. private SynthStyle dividerStyle;
  43. /**
  44. * Creates a new SynthSplitPaneUI instance
  45. */
  46. public static ComponentUI createUI(JComponent x) {
  47. return new SynthSplitPaneUI();
  48. }
  49. /**
  50. * Installs the UI defaults.
  51. */
  52. protected void installDefaults() {
  53. updateStyle(splitPane);
  54. setOrientation(splitPane.getOrientation());
  55. setContinuousLayout(splitPane.isContinuousLayout());
  56. resetLayoutManager();
  57. /* Install the nonContinuousLayoutDivider here to avoid having to
  58. add/remove everything later. */
  59. if(nonContinuousLayoutDivider == null) {
  60. setNonContinuousLayoutDivider(
  61. createDefaultNonContinuousLayoutDivider(),
  62. true);
  63. } else {
  64. setNonContinuousLayoutDivider(nonContinuousLayoutDivider, true);
  65. }
  66. // focus forward traversal key
  67. if (managingFocusForwardTraversalKeys==null) {
  68. managingFocusForwardTraversalKeys = new TreeSet();
  69. managingFocusForwardTraversalKeys.add(
  70. KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
  71. }
  72. splitPane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  73. managingFocusForwardTraversalKeys);
  74. // focus backward traversal key
  75. if (managingFocusBackwardTraversalKeys==null) {
  76. managingFocusBackwardTraversalKeys = new TreeSet();
  77. managingFocusBackwardTraversalKeys.add(
  78. KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
  79. }
  80. splitPane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
  81. managingFocusBackwardTraversalKeys);
  82. }
  83. private void updateStyle(JSplitPane splitPane) {
  84. SynthContext context = getContext(splitPane, Region.SPLIT_PANE_DIVIDER,
  85. ENABLED);
  86. SynthStyle oldDividerStyle = dividerStyle;
  87. dividerStyle = SynthLookAndFeel.updateStyle(context, this);
  88. context.dispose();
  89. context = getContext(splitPane, ENABLED);
  90. SynthStyle oldStyle = style;
  91. style = SynthLookAndFeel.updateStyle(context, this);
  92. if (style != oldStyle) {
  93. Object value = style.get(context, "SplitPane.size");
  94. if (value == null) {
  95. value = new Integer(6);
  96. }
  97. LookAndFeel.installProperty(splitPane, "dividerSize", value);
  98. value = style.get(context, "SplitPane.oneTouchExpandable");
  99. if (value != null) {
  100. LookAndFeel.installProperty(splitPane, "oneTouchExpandable", value);
  101. }
  102. if (divider != null) {
  103. splitPane.remove(divider);
  104. divider.setDividerSize(splitPane.getDividerSize());
  105. }
  106. if (oldStyle != null) {
  107. uninstallKeyboardActions();
  108. installKeyboardActions();
  109. }
  110. }
  111. if (style != oldStyle || dividerStyle != oldDividerStyle) {
  112. // Only way to force BasicSplitPaneDivider to reread the
  113. // necessary properties.
  114. if (divider != null) {
  115. splitPane.remove(divider);
  116. }
  117. divider = createDefaultDivider();
  118. divider.setBasicSplitPaneUI(this);
  119. splitPane.add(divider, JSplitPane.DIVIDER);
  120. }
  121. context.dispose();
  122. }
  123. /**
  124. * Installs the event listeners for the UI.
  125. */
  126. protected void installListeners() {
  127. super.installListeners();
  128. splitPane.addPropertyChangeListener(this);
  129. }
  130. /**
  131. * Uninstalls the UI defaults.
  132. */
  133. protected void uninstallDefaults() {
  134. SynthContext context = getContext(splitPane, ENABLED);
  135. style.uninstallDefaults(context);
  136. context.dispose();
  137. style = null;
  138. context = getContext(splitPane, Region.SPLIT_PANE_DIVIDER, ENABLED);
  139. dividerStyle.uninstallDefaults(context);
  140. context.dispose();
  141. dividerStyle = null;
  142. super.uninstallDefaults();
  143. }
  144. /**
  145. * Uninstalls the event listeners for the UI.
  146. */
  147. protected void uninstallListeners() {
  148. super.uninstallListeners();
  149. splitPane.removePropertyChangeListener(this);
  150. }
  151. public SynthContext getContext(JComponent c) {
  152. return getContext(c, getComponentState(c));
  153. }
  154. private SynthContext getContext(JComponent c, int state) {
  155. return SynthContext.getContext(SynthContext.class, c,
  156. SynthLookAndFeel.getRegion(c), style, state);
  157. }
  158. private Region getRegion(JComponent c) {
  159. return SynthLookAndFeel.getRegion(c);
  160. }
  161. private int getComponentState(JComponent c) {
  162. return SynthLookAndFeel.getComponentState(c);
  163. }
  164. SynthContext getContext(JComponent c, Region region) {
  165. return getContext(c, region, getComponentState(c, region));
  166. }
  167. private SynthContext getContext(JComponent c, Region region, int state) {
  168. if (region == Region.SPLIT_PANE_DIVIDER) {
  169. return SynthContext.getContext(SynthContext.class, c, region,
  170. dividerStyle, state);
  171. }
  172. return SynthContext.getContext(SynthContext.class, c, region,
  173. style, state);
  174. }
  175. private int getComponentState(JComponent c, Region subregion) {
  176. int state = SynthLookAndFeel.getComponentState(c);
  177. if (divider.isMouseOver()) {
  178. state |= MOUSE_OVER;
  179. }
  180. return state;
  181. }
  182. public void propertyChange(PropertyChangeEvent e) {
  183. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  184. updateStyle((JSplitPane)e.getSource());
  185. }
  186. }
  187. /**
  188. * Creates the default divider.
  189. */
  190. public BasicSplitPaneDivider createDefaultDivider() {
  191. SynthSplitPaneDivider divider = new SynthSplitPaneDivider(this);
  192. divider.setDividerSize(splitPane.getDividerSize());
  193. return divider;
  194. }
  195. protected Component createDefaultNonContinuousLayoutDivider() {
  196. return new Canvas() {
  197. public void paint(Graphics g) {
  198. paintDragDivider(g, 0, 0, getWidth(), getHeight());
  199. }
  200. };
  201. }
  202. public void update(Graphics g, JComponent c) {
  203. SynthContext context = getContext(c);
  204. SynthLookAndFeel.update(context, g);
  205. context.getPainter().paintSplitPaneBackground(context,
  206. g, 0, 0, c.getWidth(), c.getHeight());
  207. paint(context, g);
  208. context.dispose();
  209. }
  210. public void paint(Graphics g, JComponent c) {
  211. SynthContext context = getContext(c);
  212. paint(context, g);
  213. context.dispose();
  214. }
  215. protected void paint(SynthContext context, Graphics g) {
  216. // This is done to update package private variables in
  217. // BasicSplitPaneUI
  218. super.paint(g, splitPane);
  219. }
  220. public void paintBorder(SynthContext context, Graphics g, int x,
  221. int y, int w, int h) {
  222. context.getPainter().paintSplitPaneBorder(context, g, x, y, w, h);
  223. }
  224. private void paintDragDivider(Graphics g, int x, int y, int w, int h) {
  225. SynthContext context = getContext(splitPane,Region.SPLIT_PANE_DIVIDER);
  226. context.setComponentState(((context.getComponentState() | MOUSE_OVER) ^
  227. MOUSE_OVER) | PRESSED);
  228. Shape oldClip = g.getClip();
  229. g.clipRect(x, y, w, h);
  230. context.getPainter().paintSplitPaneDragDivider(context, g, x, y, w, h,
  231. splitPane.getOrientation());
  232. g.setClip(oldClip);
  233. context.dispose();
  234. }
  235. public void finishedPaintingChildren(JSplitPane jc, Graphics g) {
  236. if(jc == splitPane && getLastDragLocation() != -1 &&
  237. !isContinuousLayout() && !draggingHW) {
  238. if(jc.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
  239. paintDragDivider(g, getLastDragLocation(), 0, dividerSize - 1,
  240. splitPane.getHeight() - 1);
  241. } else {
  242. paintDragDivider(g, 0, getLastDragLocation(),
  243. splitPane.getWidth() - 1, dividerSize - 1);
  244. }
  245. }
  246. }
  247. }