KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > gridsplit > GridSplitPane


1 /*
2  * NwaySplitPane.java
3  *
4  * Created on 22 November 2004, 13:05
5  */

6
7 package org.netbeans.swing.gridsplit;
8
9 import java.awt.Color JavaDoc;
10 import java.awt.Component JavaDoc;
11 import java.awt.Container JavaDoc;
12 import java.awt.Cursor JavaDoc;
13 import java.awt.Dimension JavaDoc;
14 import java.awt.Graphics JavaDoc;
15 import java.awt.Insets JavaDoc;
16 import java.awt.LayoutManager JavaDoc;
17 import java.awt.Point JavaDoc;
18 import java.awt.Rectangle JavaDoc;
19 import java.awt.event.MouseEvent JavaDoc;
20 import java.awt.event.MouseListener JavaDoc;
21 import java.awt.event.MouseMotionListener JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import javax.swing.JComponent JavaDoc;
25 import javax.swing.JPanel JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27
28
29 /**
30  *
31  * @author aubrecht
32  */

33 public class GridSplitPane extends JPanel JavaDoc
34                 implements GridSplitModelListener, MouseMotionListener JavaDoc, MouseListener JavaDoc {
35     
36     private GridSplitDivider draggingDivider;
37     private GridSplitModel model;
38     private ArrayList JavaDoc dividers = new ArrayList JavaDoc();
39     private boolean dividersAreOutdated = true;
40     /**
41      * Location of the divider when the dragging session began.
42      */

43     protected int beginDragDividerLocation;
44     private int lastDragLocation;
45     
46     /** Creates a new instance of NwaySplitPane */
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 JavaDoc 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 JavaDoc e ) {
115         switchCursor( e );
116         e.consume();
117     }
118
119     public void mouseDragged( MouseEvent JavaDoc e ) {
120         if( null == draggingDivider )
121             return;
122         
123         draggingDivider.dragTo( e.getPoint() );
124         e.consume();
125     }
126
127     public void mouseReleased(MouseEvent JavaDoc e) {
128         if( null == draggingDivider )
129             return;
130         
131         final Point JavaDoc p = new Point JavaDoc( e.getPoint() );
132         SwingUtilities.invokeLater( new Runnable JavaDoc() {
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 JavaDoc 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 JavaDoc e) {
158         if( null == draggingDivider ) {
159             setCursor( Cursor.getDefaultCursor() );
160         }
161         e.consume();
162     }
163
164     public void mouseEntered(MouseEvent JavaDoc e) {
165     }
166
167     public void mouseClicked(MouseEvent JavaDoc 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 JavaDoc 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 JavaDoc p ) {
194         for( Iterator JavaDoc 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 JavaDoc g ) {
203         super.paint(g);
204         for( Iterator JavaDoc i=dividers.iterator(); i.hasNext(); ) {
205             GridSplitDivider divider = (GridSplitDivider)i.next();
206             divider.paint( g );
207         }
208     }
209     
210     protected class GridSplitLayout implements LayoutManager JavaDoc {
211         
212         public void layoutContainer( Container JavaDoc 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 JavaDoc 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 JavaDoc i=dividers.iterator(); i.hasNext(); ) {
248                 GridSplitDivider divider = (GridSplitDivider)i.next();
249                 divider.reshape();
250             }
251         }
252
253         public Dimension JavaDoc minimumLayoutSize(Container JavaDoc container) {
254             return container.getSize();
255         }
256
257         public Dimension JavaDoc preferredLayoutSize(Container JavaDoc container) {
258             return container.getSize();
259         }
260
261         public void removeLayoutComponent(Component JavaDoc c) {}
262
263         public void addLayoutComponent(String JavaDoc string, Component JavaDoc c) {}
264     } // End of class BasicSplitPaneDivider.DividerLayout
265
}
266
Popular Tags