1 7 8 package com.sun.java.swing.plaf.motif; 9 10 import java.awt.*; 11 import java.awt.event.*; 12 import javax.swing.JSplitPane ; 13 import javax.swing.UIManager ; 14 import javax.swing.plaf.basic.BasicSplitPaneUI ; 15 import javax.swing.plaf.basic.BasicSplitPaneDivider ; 16 17 18 31 public class MotifSplitPaneDivider extends BasicSplitPaneDivider 32 { 33 37 private static final Cursor defaultCursor = 38 Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); 39 40 41 public static final int minimumThumbSize = 6; 42 public static final int defaultDividerSize = 18; 43 44 protected static final int pad = 6; 45 46 private int hThumbOffset = 30; 47 private int vThumbOffset = 40; 48 protected int hThumbWidth = 12; 49 protected int hThumbHeight = 18; 50 protected int vThumbWidth = 18; 51 protected int vThumbHeight = 12; 52 53 protected Color highlightColor; 54 protected Color shadowColor; 55 protected Color focusedColor; 56 57 60 public MotifSplitPaneDivider(BasicSplitPaneUI ui) { 61 super(ui); 62 highlightColor = UIManager.getColor("SplitPane.highlight"); 63 shadowColor = UIManager.getColor("SplitPane.shadow"); 64 focusedColor = UIManager.getColor("SplitPane.activeThumb"); 65 setDividerSize(hThumbWidth + pad); 66 } 67 68 72 public void setDividerSize(int newSize) { 73 Insets insets = getInsets(); 74 int borderSize = 0; 75 if (getBasicSplitPaneUI().getOrientation() == 76 JSplitPane.HORIZONTAL_SPLIT) { 77 if (insets != null) { 78 borderSize = insets.left + insets.right; 79 } 80 } 81 else if (insets != null) { 82 borderSize = insets.top + insets.bottom; 83 } 84 if (newSize < pad + minimumThumbSize + borderSize) { 85 setDividerSize(pad + minimumThumbSize + borderSize); 86 } else { 87 vThumbHeight = hThumbWidth = newSize - pad - borderSize; 88 super.setDividerSize(newSize); 89 } 90 } 91 92 95 public void paint(Graphics g) { 98 Color bgColor = getBackground(); 99 Dimension size = getSize(); 100 101 g.setColor(getBackground()); 103 g.fillRect(0, 0, size.width, size.height); 104 105 if(getBasicSplitPaneUI().getOrientation() == 106 JSplitPane.HORIZONTAL_SPLIT) { 107 int center = size.width/2; 108 int x = center - hThumbWidth/2; 109 int y = hThumbOffset; 110 111 g.setColor(shadowColor); 113 g.drawLine(center-1, 0, center-1, size.height); 114 115 g.setColor(highlightColor); 116 g.drawLine(center, 0, center, size.height); 117 118 g.setColor((splitPane.hasFocus()) ? focusedColor : 120 getBackground()); 121 g.fillRect(x+1, y+1, hThumbWidth-2, hThumbHeight-1); 122 123 g.setColor(highlightColor); 124 g.drawLine(x, y, x+hThumbWidth-1, y); g.drawLine(x, y+1, x, y+hThumbHeight-1); 127 g.setColor(shadowColor); 128 g.drawLine(x+1, y+hThumbHeight-1, 129 x+hThumbWidth-1, y+hThumbHeight-1); g.drawLine(x+hThumbWidth-1, y+1, 131 x+hThumbWidth-1, y+hThumbHeight-2); 133 } else { 134 int center = size.height/2; 135 int x = size.width - vThumbOffset; 136 int y = size.height/2 - vThumbHeight/2; 137 138 g.setColor(shadowColor); 140 g.drawLine(0, center-1, size.width, center-1); 141 142 g.setColor(highlightColor); 143 g.drawLine(0, center, size.width, center); 144 145 g.setColor((splitPane.hasFocus()) ? focusedColor : 147 getBackground()); 148 g.fillRect(x+1, y+1, vThumbWidth-1, vThumbHeight-1); 149 150 g.setColor(highlightColor); 151 g.drawLine(x, y, x+vThumbWidth, y); g.drawLine(x, y+1, x, y+vThumbHeight); 154 g.setColor(shadowColor); 155 g.drawLine(x+1, y+vThumbHeight, 156 x+vThumbWidth, y+vThumbHeight); g.drawLine(x+vThumbWidth, y+1, 158 x+vThumbWidth, y+vThumbHeight-1); } 160 super.paint(g); 161 162 } 163 164 167 public Dimension getMinimumSize() { 168 return getPreferredSize(); 169 } 170 171 175 public void setBasicSplitPaneUI(BasicSplitPaneUI newUI) { 176 if (splitPane != null) { 177 splitPane.removePropertyChangeListener(this); 178 if (mouseHandler != null) { 179 splitPane.removeMouseListener(mouseHandler); 180 splitPane.removeMouseMotionListener(mouseHandler); 181 removeMouseListener(mouseHandler); 182 removeMouseMotionListener(mouseHandler); 183 mouseHandler = null; 184 } 185 } 186 splitPaneUI = newUI; 187 if (newUI != null) { 188 splitPane = newUI.getSplitPane(); 189 if (splitPane != null) { 190 if (mouseHandler == null) mouseHandler=new MotifMouseHandler(); 191 splitPane.addMouseListener(mouseHandler); 192 splitPane.addMouseMotionListener(mouseHandler); 193 addMouseListener(mouseHandler); 194 addMouseMotionListener(mouseHandler); 195 splitPane.addPropertyChangeListener(this); 196 if (splitPane.isOneTouchExpandable()) { 197 oneTouchExpandableChanged(); 198 } 199 } 200 } 201 else { 202 splitPane = null; 203 } 204 } 205 206 210 private boolean isInThumb(int x, int y) { 211 Dimension size = getSize(); 212 int thumbX; 213 int thumbY; 214 int thumbWidth; 215 int thumbHeight; 216 217 if (getBasicSplitPaneUI().getOrientation() == 218 JSplitPane.HORIZONTAL_SPLIT) { 219 int center = size.width/2; 220 thumbX = center - hThumbWidth/2; 221 thumbY = hThumbOffset; 222 thumbWidth = hThumbWidth; 223 thumbHeight = hThumbHeight; 224 } 225 else { 226 int center = size.height/2; 227 thumbX = size.width - vThumbOffset; 228 thumbY = size.height/2 - vThumbHeight/2; 229 thumbWidth = vThumbWidth; 230 thumbHeight = vThumbHeight; 231 } 232 return (x >= thumbX && x < (thumbX + thumbWidth) && 233 y >= thumbY && y < (thumbY + thumbHeight)); 234 } 235 236 241 private DragController getDragger() { 242 return dragger; 243 } 244 245 private JSplitPane getSplitPane() { 246 return splitPane; 247 } 248 249 250 255 private class MotifMouseHandler extends MouseHandler { 256 public void mousePressed(MouseEvent e) { 257 if (e.getSource() == MotifSplitPaneDivider.this && 259 getDragger() == null && getSplitPane().isEnabled() && 260 isInThumb(e.getX(), e.getY())) { 261 super.mousePressed(e); 262 } 263 } 264 265 public void mouseMoved(MouseEvent e) { 266 if (getDragger() != null) { 267 return; 268 } 269 if (!isInThumb(e.getX(), e.getY())) { 270 if (getCursor() != defaultCursor) { 271 setCursor(defaultCursor); 272 } 273 return; 274 } 275 super.mouseMoved(e); 276 } 277 } 278 } 279 | Popular Tags |