1 19 20 package org.netbeans.modules.xml.xam.ui.layout; 21 22 import java.awt.Component ; 23 import java.awt.Container ; 24 import java.awt.Cursor ; 25 import java.awt.Dimension ; 26 import java.awt.Frame ; 27 import java.awt.Graphics ; 28 import java.awt.Insets ; 29 import java.awt.Point ; 30 import java.awt.Rectangle ; 31 import java.awt.Window ; 32 import java.awt.event.MouseEvent ; 33 import javax.swing.JPanel ; 34 import javax.swing.JScrollPane ; 35 import javax.swing.JViewport ; 36 import javax.swing.SwingUtilities ; 37 38 110 public class JSplitterBar extends JPanel { 111 112 static final long serialVersionUID = 1L; 113 static final Cursor VERT_CURSOR = new Cursor (Cursor.N_RESIZE_CURSOR); 114 static final Cursor HORIZ_CURSOR = new Cursor (Cursor.E_RESIZE_CURSOR); 115 static final Cursor DEF_CURSOR = new Cursor (Cursor.DEFAULT_CURSOR); 116 private int orientation = SplitterLayout.HORIZONTAL; 117 118 private boolean alreadyDrawn = false; 119 private Rectangle originalBounds = null; 120 private Window wBar; 121 private boolean mouseInside = false; 122 123 126 public JSplitterBar(){ 127 addMouseMotionListener(new SplitterBarMouseMotionListener(this)); 128 addMouseListener(new SplitterBarMouseListener(this)); 129 } 130 131 private void checkOtherComponents(){ 132 Rectangle currBounds = getBounds(); Component comps[] = getParent().getComponents(); 135 136 if(currBounds.x<originalBounds.x){ int offset = originalBounds.x - currBounds.x; 140 for (int i = 0; i < comps.length; i++){ 142 if (comps[i] == this){ 143 assert i-1>-1:"There should be a column to the left of this splitter"; 145 if (i-1 > -1){ 146 Rectangle oldB = comps[i-1].getBounds(); 147 comps[i-1].setBounds(oldB.x, oldB.y, oldB.width-offset, oldB.height); 149 } 151 for (int k = i+1; k < comps.length; k++){ 154 Rectangle old = comps[k].getBounds(); 155 comps[k].setBounds(old.x-offset, old.y, old.width, old.height); 157 comps[k].setVisible(true); 159 160 } 161 } 162 } 163 } else if(currBounds.x>originalBounds.x){ int offset = currBounds.x - originalBounds.x; 167 Component left = null; 170 int thisIdx = -1; 171 for (int i = 0; i < comps.length; i++){ 172 if(comps[i] == this){ 173 thisIdx = i; 174 assert i>0:"There should be a column to the left of this splitter."; 175 if (i-1 > -1){ 176 left = comps[i-1]; 177 Rectangle old = left.getBounds(); 178 left.setBounds(old.x, old.y, old.width+offset, old.height); 179 } 181 } 182 } 183 184 if (thisIdx+1 < comps.length) { 186 for (int j = thisIdx+1; j < comps.length; j++){ 187 Rectangle old = comps[j].getBounds(); 188 comps[j].setBounds(old.x+offset, old.y, old.width, old.height); 189 } 191 } 192 } } 195 196 void mouseDrag(MouseEvent e){ 197 if (SplitterLayout.dragee == null) { 198 SplitterLayout.dragee = this; 199 } else if (SplitterLayout.dragee != this) { 200 return; 201 } 202 Container cp = getParent(); Container viewport = cp.getParent(); 205 Rectangle viewRect = null; 206 Rectangle columnViewRect = null; 207 Component columnView = null; 208 if (viewport instanceof JViewport ){ 210 JScrollPane scrollPane = (JScrollPane )((JViewport )viewport).getParent(); 211 viewRect = ((JViewport )viewport).getViewRect(); 214 columnView = ((JViewport )viewport).getParent().getParent(); 216 if (columnView != null) { 217 columnViewRect = columnView.getBounds(); 218 } else { 220 return; 222 } 223 } else { 224 return; 226 } 227 Component c = getParent(); 228 while(c.getParent()!=null){ 230 c = c.getParent(); 231 } 232 if(!alreadyDrawn && c instanceof Frame ){ 234 originalBounds = getBounds(); 235 wBar = new Window ((Frame )c); 236 wBar.setBackground(getBackground().darker()); 237 } 238 Rectangle parentRect = cp.getBounds(); Point l = getLocationOnScreen(); 243 Insets insets = cp.getInsets(); 244 parentRect.height -= insets.top+insets.bottom; 246 int x = l.x+e.getX(); 250 Point viewRectLocation = viewRect.getLocation(); 253 SwingUtilities.convertPointToScreen(viewRectLocation, viewport); 255 Point columnViewRectLocation = columnViewRect.getLocation(); 257 SwingUtilities.convertPointToScreen(columnViewRectLocation, columnView); 259 int y = viewRectLocation.y; 261 int rightBoundaryScreenPos = columnViewRectLocation.x + columnViewRect.width - this.getWidth(); 266 270 int leftBoundaryScreenPos = 0; 273 Component left = null; 274 Component [] comps = getParent().getComponents(); 275 for (Component comp:comps){ 276 if (comp == this){ 277 Dimension min = left.getMinimumSize(); 278 Point p = left.getLocationOnScreen(); 279 leftBoundaryScreenPos = Math.max(p.x+min.width,columnViewRectLocation.x); 284 break; 285 } 286 left = comp; 287 } 288 if (x<leftBoundaryScreenPos){ 290 x = leftBoundaryScreenPos; 291 } 293 else if(x>rightBoundaryScreenPos){ 294 x = rightBoundaryScreenPos; 295 } 297 wBar.setBounds(x, y, 298 3 , 299 columnViewRect.height); 300 if(!alreadyDrawn){ 302 wBar.setVisible(true); 303 alreadyDrawn = true; 304 } 305 } 306 307 void mouseEnter(MouseEvent e){ 308 if(SplitterLayout.dragee!=null) return; 309 setCursor(HORIZ_CURSOR); 310 mouseInside = true; 311 invalidate(); 312 validate(); 313 repaint(); 314 } 315 316 void mouseExit(MouseEvent e){ 317 if(SplitterLayout.dragee!=null) return; 318 setCursor(DEF_CURSOR); 319 mouseInside = false; 320 invalidate(); 321 validate(); 322 repaint(); 323 } 324 325 void mouseRelease(MouseEvent e){ 326 if(alreadyDrawn){ 327 if(SplitterLayout.dragee!=this) return; 328 SplitterLayout.dragee = null; 329 Point p = wBar.getLocationOnScreen(); 330 SwingUtilities.convertPointFromScreen(p, getParent()); 331 wBar.setVisible(false); 332 wBar.dispose(); 333 wBar = null; 334 alreadyDrawn = false; 335 Rectangle r = getBounds(); r.x += e.getX(); 337 setLocation(p.x, r.y); 340 setCursor(DEF_CURSOR); 341 342 346 checkOtherComponents(); 347 mouseInside = false; 348 invalidate(); 349 getParent().validate(); 350 SplitterLayout.dragee = null; 351 } 352 } 353 354 361 public void paint(Graphics g){ 362 super.paint(g); 363 g.setColor(getBackground()); 364 Component c[] = getComponents(); 369 if(c!=null && c.length>0) 370 for(int i = 0; i<c.length; i++){ 371 if(c[i] instanceof JSplitterSpace){ 372 Rectangle r = c[i].getBounds(); 374 g.fill3DRect(r.x+r.width/2-1, r.y+2, 3, r.y+r.height-5, true); 375 } 376 } else{ 377 Rectangle r = getBounds(); 378 g.fill3DRect(r.width/2-1, 2, 3, r.height-5, true); 379 } 380 } 381 382 383 384 387 public void update(Graphics g){ 388 paint(g); 389 } 390 } | Popular Tags |