1 6 7 package org.netbeans.swing.gridsplit; 8 9 import java.awt.Color ; 10 import java.awt.Component ; 11 import java.awt.Container ; 12 import java.awt.Cursor ; 13 import java.awt.Dimension ; 14 import java.awt.Graphics ; 15 import java.awt.Insets ; 16 import java.awt.LayoutManager ; 17 import java.awt.Point ; 18 import java.awt.Rectangle ; 19 import java.awt.event.MouseEvent ; 20 import java.awt.event.MouseListener ; 21 import java.awt.event.MouseMotionListener ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import javax.swing.JComponent ; 25 import javax.swing.JPanel ; 26 import javax.swing.SwingUtilities ; 27 28 29 33 public class GridSplitPane extends JPanel 34 implements GridSplitModelListener, MouseMotionListener , MouseListener { 35 36 private GridSplitDivider draggingDivider; 37 private GridSplitModel model; 38 private ArrayList dividers = new ArrayList (); 39 private boolean dividersAreOutdated = true; 40 43 protected int beginDragDividerLocation; 44 private int lastDragLocation; 45 46 47 GridSplitPane( GridSplitModel model ) { 48 setLayout( new GridSplitLayout() ); 49 this.model = model; 50 model.addChangeListener( this ); 51 addMouseMotionListener( this ); 52 addMouseListener( this ); 53 54 addAll( model.getRootCell() ); 55 } 56 57 private void addAll( GridSplitCell cell ) { 58 if( cell.isHidden() ) 59 return; 60 61 if( null != cell.getComponent() ) 62 add( cell.getComponent() ); 63 64 for( int i=0; i<cell.count(); i++ ) { 65 addAll( cell.cellAt( i ) ); 66 } 67 } 68 69 private void removeAll( GridSplitCell cell ) { 70 if( null != cell.getComponent() ) 71 remove( cell.getComponent() ); 72 73 for( int i=0; i<cell.count(); i++ ) { 74 removeAll( cell.cellAt( i ) ); 75 } 76 } 77 78 public int getDividerSize() { 79 return 5; 80 } 81 82 public GridSplitModel getModel() { 83 return model; 84 } 85 86 public Dimension getMinimumSize() { 87 return model.getRootCell().getMinimumSize( getDividerSize() ); 88 } 89 90 public void cellAdded( GridSplitCell cell ) { 91 addAll( cell ); 92 dividersAreOutdated = true; 93 revalidate(); 94 } 95 96 public void cellRemoved( GridSplitCell cell ) { 97 removeAll( cell ); 98 dividersAreOutdated = true; 99 revalidate(); 100 } 101 102 public void cellModified( GridSplitCell oldCell, GridSplitCell newCell ) { 103 if( newCell.isHidden() != oldCell.isHidden() ) { 104 if( newCell.isHidden() ) { 105 removeAll( newCell ); 106 } else { 107 addAll( newCell ); 108 } 109 dividersAreOutdated = true; 110 revalidate(); 111 } 112 } 113 114 public void mouseMoved( MouseEvent e ) { 115 switchCursor( e ); 116 e.consume(); 117 } 118 119 public void mouseDragged( MouseEvent e ) { 120 if( null == draggingDivider ) 121 return; 122 123 draggingDivider.dragTo( e.getPoint() ); 124 e.consume(); 125 } 126 127 public void mouseReleased(MouseEvent e) { 128 if( null == draggingDivider ) 129 return; 130 131 final Point p = new Point ( e.getPoint() ); 132 SwingUtilities.invokeLater( new Runnable () { 133 public void run() { 134 draggingDivider.finishDraggingTo( p ); 135 draggingDivider = null; 136 } 137 }); 138 switchCursor( e ); 139 e.consume(); 140 } 141 142 public void mousePressed(MouseEvent e) { 143 GridSplitDivider divider = dividerAtPoint( e.getPoint() ); 144 if( null == divider ) 145 return; 146 147 if( divider.isOnCollapseButton( e.getPoint() ) ) { 148 e.consume(); 149 return; 150 } 151 152 draggingDivider = divider; 153 divider.startDragging( e.getPoint() ); 154 e.consume(); 155 } 156 157 public void mouseExited(MouseEvent e) { 158 if( null == draggingDivider ) { 159 setCursor( Cursor.getDefaultCursor() ); 160 } 161 e.consume(); 162 } 163 164 public void mouseEntered(MouseEvent e) { 165 } 166 167 public void mouseClicked(MouseEvent e) { 168 GridSplitDivider divider = dividerAtPoint( e.getPoint() ); 169 if( null == divider ) 170 return; 171 172 if( divider.collapse( e.getPoint() ) ) { 173 dividersAreOutdated = true; 174 e.consume(); 175 return; 176 } 177 } 178 179 180 private void switchCursor( MouseEvent e ) { 181 GridSplitDivider divider = dividerAtPoint( e.getPoint() ); 182 if( null == divider || divider.isOnCollapseButton( e.getPoint() ) ) { 183 setCursor( Cursor.getDefaultCursor() ); 184 } else { 185 if( divider.isHorizontal() ) { 186 setCursor( Cursor.getPredefinedCursor( Cursor.E_RESIZE_CURSOR ) ); 187 } else { 188 setCursor( Cursor.getPredefinedCursor( Cursor.N_RESIZE_CURSOR ) ); 189 } 190 } 191 } 192 193 private GridSplitDivider dividerAtPoint( Point p ) { 194 for( Iterator i=dividers.iterator(); i.hasNext(); ) { 195 GridSplitDivider d = (GridSplitDivider)i.next(); 196 if( d.containsPoint( p ) ) 197 return d; 198 } 199 return null; 200 } 201 202 public void paint( Graphics g ) { 203 super.paint(g); 204 for( Iterator i=dividers.iterator(); i.hasNext(); ) { 205 GridSplitDivider divider = (GridSplitDivider)i.next(); 206 divider.paint( g ); 207 } 208 } 209 210 protected class GridSplitLayout implements LayoutManager { 211 212 public void layoutContainer( Container c ) { 213 if( c != GridSplitPane.this ) 214 return; 215 216 GridSplitCell root = model.getRootCell(); 217 218 root.resize( getWidth(), getHeight(), getDividerSize() ); 219 root.setLocation( 0, 0, getDividerSize() ); 220 221 if( dividersAreOutdated ) { 222 dividers.clear(); 223 createDividers( root ); 224 dividersAreOutdated = false; 225 } 226 reshapeDividers(); 227 } 228 229 private void createDividers( GridSplitCell cell ) { 230 if( !cell.isSplit() ) 231 return; 232 233 ArrayList visibleChildren = cell.getVisibleCells(); 234 for( int i=0; i<visibleChildren.size(); i++ ) { 235 GridSplitCell first = (GridSplitCell)visibleChildren.get( i ); 236 if( i < visibleChildren.size()-1 ) { 237 GridSplitCell second = (GridSplitCell)visibleChildren.get( i+1 ); 238 239 GridSplitDivider divider = new GridSplitDivider( GridSplitPane.this, getDividerSize(), first, second ); 240 dividers.add( divider ); 241 } 242 createDividers( first ); 243 } 244 } 245 246 private void reshapeDividers() { 247 for( Iterator i=dividers.iterator(); i.hasNext(); ) { 248 GridSplitDivider divider = (GridSplitDivider)i.next(); 249 divider.reshape(); 250 } 251 } 252 253 public Dimension minimumLayoutSize(Container container) { 254 return container.getSize(); 255 } 256 257 public Dimension preferredLayoutSize(Container container) { 258 return container.getSize(); 259 } 260 261 public void removeLayoutComponent(Component c) {} 262 263 public void addLayoutComponent(String string, Component c) {} 264 } } 266 | Popular Tags |