KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > SplitView


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.core.windows.view;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.core.windows.Constants;
29 import org.netbeans.core.windows.Debug;
30 import org.netbeans.core.windows.view.ui.MultiSplitPane;
31
32 import javax.swing.*;
33 import java.awt.*;
34 import java.beans.PropertyChangeEvent JavaDoc;
35 import java.beans.PropertyChangeListener JavaDoc;
36
37
38 /**
39  * Class which represents model of split element for GUI hierarchy.
40  *
41  * @author Peter Zavadsky
42  */

43 public class SplitView extends ViewElement {
44     
45     private int orientation;
46     
47     private ArrayList JavaDoc<Double JavaDoc> splitWeights;
48     
49     private ArrayList JavaDoc<ViewElement> children;
50     
51     private MultiSplitPane splitPane;
52     
53     private boolean isDirty = false;
54     
55     public SplitView(Controller controller, double resizeWeight,
56                         int orientation, List JavaDoc<Double JavaDoc> splitWeights, List JavaDoc<ViewElement> children) {
57         super(controller, resizeWeight);
58         
59         this.orientation = orientation;
60         this.splitWeights = new ArrayList JavaDoc<Double JavaDoc>( splitWeights );
61         this.children = new ArrayList JavaDoc<ViewElement>( children );
62     }
63
64     public void setOrientation( int newOrientation ) {
65         this.orientation = newOrientation;
66     }
67     
68     public void setSplitWeights( List JavaDoc<Double JavaDoc> newSplitWeights ) {
69         splitWeights.clear();
70         splitWeights.addAll( newSplitWeights );
71     }
72     
73     public int getOrientation() {
74         return orientation;
75     }
76     
77     public List JavaDoc<ViewElement> getChildren() {
78         return new ArrayList JavaDoc<ViewElement>( children );
79     }
80     
81     public Component getComponent() {
82         return getSplitPane();
83     }
84     
85     public void remove( ViewElement view ) {
86         int index = children.indexOf( view );
87         if( index >= 0 ) {
88             children.remove( index );
89             splitWeights.remove( index );
90             if( null != splitPane ) {
91                 splitPane.removeViewElementAt( index );
92             }
93             isDirty = true; //force invalidation of splitpane
94
}
95     }
96     
97     public void setChildren( List JavaDoc<ViewElement> newChildren ) {
98         children.clear();
99         children.addAll( newChildren );
100         
101         assert children.size() == splitWeights.size();
102         
103         isDirty = true; //force invalidation of splitpane
104

105         if( null != splitPane ) {
106             updateSplitPane();
107         }
108     }
109     
110     public boolean updateAWTHierarchy(Dimension availableSpace) {
111         boolean res = false;
112         
113         if( !availableSpace.equals( getSplitPane().getSize() ) || isDirty ) {
114             isDirty = false;
115             getSplitPane().setSize( availableSpace );
116             getSplitPane().invalidate();
117             res = true;
118         }
119         for( Iterator JavaDoc i=children.iterator(); i.hasNext(); ) {
120             ViewElement child = (ViewElement)i.next();
121             res |= child.updateAWTHierarchy( child.getComponent().getSize() );
122         }
123         
124         return res;
125     }
126     
127     private MultiSplitPane getSplitPane() {
128         if(splitPane == null) {
129             splitPane = new MultiSplitPane();
130             updateSplitPane();
131
132             
133             splitPane.setDividerSize(orientation == JSplitPane.VERTICAL_SPLIT
134                 ? Constants.DIVIDER_SIZE_VERTICAL
135                 : Constants.DIVIDER_SIZE_HORIZONTAL);
136             
137             splitPane.setBorder(BorderFactory.createEmptyBorder());
138             
139             splitPane.addPropertyChangeListener("splitPositions", // NOI18N
140
new PropertyChangeListener JavaDoc() {
141                     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
142                         ArrayList JavaDoc<Double JavaDoc> weights = new ArrayList JavaDoc<Double JavaDoc>( children.size() );
143                         ArrayList JavaDoc<ViewElement> views = new ArrayList JavaDoc<ViewElement>( children.size() );
144                         splitPane.calculateSplitWeights( views, weights );
145                         ViewElement[] arrViews = new ViewElement[views.size()];
146                         double[] arrWeights = new double[views.size()];
147                         for( int i=0; i<views.size(); i++ ) {
148                             arrViews[i] = views.get( i );
149                             arrWeights[i] = weights.get( i ).doubleValue();
150                         }
151                         getController().userMovedSplit( SplitView.this, arrViews, arrWeights );
152                     }
153                 });
154         }
155         
156         return splitPane;
157     }
158
159     public int getDividerSize() {
160         return getSplitPane().getDividerSize();
161     }
162     
163     public String JavaDoc toString() {
164         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
165         buffer.append( super.toString() );
166         buffer.append( "[" ); // NOI18N
167
for( int i=0; i<children.size(); i++ ) {
168             ViewElement child = (ViewElement)children.get( i );
169             buffer.append( (i+1) );
170             buffer.append( '=' );
171             if( child instanceof SplitView ) {
172                 buffer.append( child.getClass() );
173                 buffer.append( '@' ); // NOI18N
174
buffer.append( Integer.toHexString(child.hashCode()) );
175             } else {
176                 buffer.append( child.toString() );
177             }
178             if( i < children.size()-1 )
179                 buffer.append( ", " ); // NOI18N
180
}
181         buffer.append( "]" ); // NOI18N
182

183         return buffer.toString();
184     }
185     
186     private void updateSplitPane() {
187         ViewElement[] arrViews = new ViewElement[children.size()];
188         double[] arrSplitWeights = new double[children.size()];
189         for( int i=0; i<children.size(); i++ ) {
190             ViewElement view = (ViewElement)children.get( i );
191             
192             arrViews[i] = view;
193             arrSplitWeights[i] = ((Double JavaDoc)splitWeights.get(i)).doubleValue();
194         }
195         splitPane.setChildren( orientation, arrViews, arrSplitWeights );
196     }
197
198     private static void debugLog(String JavaDoc message) {
199         Debug.log(SplitView.class, message);
200     }
201 }
202
203
Popular Tags