1. /*
  2. * @(#)MotifSplitPaneDivider.java 1.17 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 com.sun.java.swing.plaf.motif;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.JSplitPane;
  11. import javax.swing.UIManager;
  12. import javax.swing.plaf.basic.BasicSplitPaneUI;
  13. import javax.swing.plaf.basic.BasicSplitPaneDivider;
  14. /**
  15. * Divider used for Motif split pane.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is appropriate
  20. * for short term storage or RMI between applications running the same
  21. * version of Swing. A future release of Swing will provide support for
  22. * long term persistence.
  23. *
  24. * @version 1.17 12/19/03
  25. * @author Jeff Dinkins
  26. */
  27. public class MotifSplitPaneDivider extends BasicSplitPaneDivider
  28. {
  29. /**
  30. * Default cursor, supers is package private, so we have to have one
  31. * too.
  32. */
  33. private static final Cursor defaultCursor =
  34. Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
  35. public static final int minimumThumbSize = 6;
  36. public static final int defaultDividerSize = 18;
  37. protected static final int pad = 6;
  38. private int hThumbOffset = 30;
  39. private int vThumbOffset = 40;
  40. protected int hThumbWidth = 12;
  41. protected int hThumbHeight = 18;
  42. protected int vThumbWidth = 18;
  43. protected int vThumbHeight = 12;
  44. protected Color highlightColor;
  45. protected Color shadowColor;
  46. protected Color focusedColor;
  47. /**
  48. * Creates a new Motif SplitPaneDivider
  49. */
  50. public MotifSplitPaneDivider(BasicSplitPaneUI ui) {
  51. super(ui);
  52. highlightColor = UIManager.getColor("SplitPane.highlight");
  53. shadowColor = UIManager.getColor("SplitPane.shadow");
  54. focusedColor = UIManager.getColor("SplitPane.activeThumb");
  55. setDividerSize(hThumbWidth + pad);
  56. }
  57. /**
  58. * overrides to hardcode the size of the divider
  59. * PENDING(jeff) - rewrite JSplitPane so that this ins't needed
  60. */
  61. public void setDividerSize(int newSize) {
  62. Insets insets = getInsets();
  63. int borderSize = 0;
  64. if (getBasicSplitPaneUI().getOrientation() ==
  65. JSplitPane.HORIZONTAL_SPLIT) {
  66. if (insets != null) {
  67. borderSize = insets.left + insets.right;
  68. }
  69. }
  70. else if (insets != null) {
  71. borderSize = insets.top + insets.bottom;
  72. }
  73. if (newSize < pad + minimumThumbSize + borderSize) {
  74. setDividerSize(pad + minimumThumbSize + borderSize);
  75. } else {
  76. vThumbHeight = hThumbWidth = newSize - pad - borderSize;
  77. super.setDividerSize(newSize);
  78. }
  79. }
  80. /**
  81. * Paints the divider.
  82. */
  83. // PENDING(jeff) - the thumb's location and size is currently hard coded.
  84. // It should be dynamic.
  85. public void paint(Graphics g) {
  86. Color bgColor = getBackground();
  87. Dimension size = getSize();
  88. // fill
  89. g.setColor(getBackground());
  90. g.fillRect(0, 0, size.width, size.height);
  91. if(getBasicSplitPaneUI().getOrientation() ==
  92. JSplitPane.HORIZONTAL_SPLIT) {
  93. int center = size.width2;
  94. int x = center - hThumbWidth2;
  95. int y = hThumbOffset;
  96. // split line
  97. g.setColor(shadowColor);
  98. g.drawLine(center-1, 0, center-1, size.height);
  99. g.setColor(highlightColor);
  100. g.drawLine(center, 0, center, size.height);
  101. // draw thumb
  102. g.setColor((splitPane.hasFocus()) ? focusedColor :
  103. getBackground());
  104. g.fillRect(x+1, y+1, hThumbWidth-2, hThumbHeight-1);
  105. g.setColor(highlightColor);
  106. g.drawLine(x, y, x+hThumbWidth-1, y); // top
  107. g.drawLine(x, y+1, x, y+hThumbHeight-1); // left
  108. g.setColor(shadowColor);
  109. g.drawLine(x+1, y+hThumbHeight-1,
  110. x+hThumbWidth-1, y+hThumbHeight-1); // bottom
  111. g.drawLine(x+hThumbWidth-1, y+1,
  112. x+hThumbWidth-1, y+hThumbHeight-2); // right
  113. } else {
  114. int center = size.height2;
  115. int x = size.width - vThumbOffset;
  116. int y = size.height2 - vThumbHeight2;
  117. // split line
  118. g.setColor(shadowColor);
  119. g.drawLine(0, center-1, size.width, center-1);
  120. g.setColor(highlightColor);
  121. g.drawLine(0, center, size.width, center);
  122. // draw thumb
  123. g.setColor((splitPane.hasFocus()) ? focusedColor :
  124. getBackground());
  125. g.fillRect(x+1, y+1, vThumbWidth-1, vThumbHeight-1);
  126. g.setColor(highlightColor);
  127. g.drawLine(x, y, x+vThumbWidth, y); // top
  128. g.drawLine(x, y+1, x, y+vThumbHeight); // left
  129. g.setColor(shadowColor);
  130. g.drawLine(x+1, y+vThumbHeight,
  131. x+vThumbWidth, y+vThumbHeight); // bottom
  132. g.drawLine(x+vThumbWidth, y+1,
  133. x+vThumbWidth, y+vThumbHeight-1); // right
  134. }
  135. super.paint(g);
  136. }
  137. /**
  138. * The minimums size is the same as the preferredSize
  139. */
  140. public Dimension getMinimumSize() {
  141. return getPreferredSize();
  142. }
  143. /**
  144. * Sets the SplitPaneUI that is using the receiver. This is completely
  145. * overriden from super to create a different MouseHandler.
  146. */
  147. public void setBasicSplitPaneUI(BasicSplitPaneUI newUI) {
  148. if (splitPane != null) {
  149. splitPane.removePropertyChangeListener(this);
  150. if (mouseHandler != null) {
  151. splitPane.removeMouseListener(mouseHandler);
  152. splitPane.removeMouseMotionListener(mouseHandler);
  153. removeMouseListener(mouseHandler);
  154. removeMouseMotionListener(mouseHandler);
  155. mouseHandler = null;
  156. }
  157. }
  158. splitPaneUI = newUI;
  159. if (newUI != null) {
  160. splitPane = newUI.getSplitPane();
  161. if (splitPane != null) {
  162. if (mouseHandler == null) mouseHandler=new MotifMouseHandler();
  163. splitPane.addMouseListener(mouseHandler);
  164. splitPane.addMouseMotionListener(mouseHandler);
  165. addMouseListener(mouseHandler);
  166. addMouseMotionListener(mouseHandler);
  167. splitPane.addPropertyChangeListener(this);
  168. if (splitPane.isOneTouchExpandable()) {
  169. oneTouchExpandableChanged();
  170. }
  171. }
  172. }
  173. else {
  174. splitPane = null;
  175. }
  176. }
  177. /**
  178. * Returns true if the point at <code>x</code>, <code>y</code>
  179. * is inside the thumb.
  180. */
  181. private boolean isInThumb(int x, int y) {
  182. Dimension size = getSize();
  183. int thumbX;
  184. int thumbY;
  185. int thumbWidth;
  186. int thumbHeight;
  187. if (getBasicSplitPaneUI().getOrientation() ==
  188. JSplitPane.HORIZONTAL_SPLIT) {
  189. int center = size.width2;
  190. thumbX = center - hThumbWidth2;
  191. thumbY = hThumbOffset;
  192. thumbWidth = hThumbWidth;
  193. thumbHeight = hThumbHeight;
  194. }
  195. else {
  196. int center = size.height2;
  197. thumbX = size.width - vThumbOffset;
  198. thumbY = size.height2 - vThumbHeight2;
  199. thumbWidth = vThumbWidth;
  200. thumbHeight = vThumbHeight;
  201. }
  202. return (x >= thumbX && x < (thumbX + thumbWidth) &&
  203. y >= thumbY && y < (thumbY + thumbHeight));
  204. }
  205. //
  206. // Two methods are exposed so that MotifMouseHandler can see the
  207. // superclass protected ivars
  208. //
  209. private DragController getDragger() {
  210. return dragger;
  211. }
  212. private JSplitPane getSplitPane() {
  213. return splitPane;
  214. }
  215. /**
  216. * MouseHandler is subclassed to only pass off to super if the mouse
  217. * is in the thumb. Motif only allows dragging when the thumb is clicked
  218. * in.
  219. */
  220. private class MotifMouseHandler extends MouseHandler {
  221. public void mousePressed(MouseEvent e) {
  222. // Constrain the mouse pressed to the thumb.
  223. if (e.getSource() == MotifSplitPaneDivider.this &&
  224. getDragger() == null && getSplitPane().isEnabled() &&
  225. isInThumb(e.getX(), e.getY())) {
  226. super.mousePressed(e);
  227. }
  228. }
  229. public void mouseMoved(MouseEvent e) {
  230. if (getDragger() != null) {
  231. return;
  232. }
  233. if (!isInThumb(e.getX(), e.getY())) {
  234. if (getCursor() != defaultCursor) {
  235. setCursor(defaultCursor);
  236. }
  237. return;
  238. }
  239. super.mouseMoved(e);
  240. }
  241. }
  242. }