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