KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > window > swing > ExtendedSplitPane


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.window.swing;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.event.ComponentAdapter JavaDoc;
23 import java.awt.event.ComponentEvent JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26
27 import javax.swing.JSplitPane JavaDoc;
28
29 /**
30
31  * @author Matthew Large
32  *
33  */

34 public class ExtendedSplitPane extends JSplitPane JavaDoc {
35
36     private double m_pos = 0.5;
37
38     private boolean reentrant = false;
39
40     /**
41      *
42      */

43     public ExtendedSplitPane() {
44         super();
45         setup();
46     }
47
48     /**
49      * @param newOrientation
50      */

51     public ExtendedSplitPane(int newOrientation) {
52         super(newOrientation);
53         setup();
54     }
55
56     /**
57      * @param newOrientation
58      * @param newContinuousLayout
59      */

60     public ExtendedSplitPane(int newOrientation, boolean newContinuousLayout) {
61         super(newOrientation, newContinuousLayout);
62         setup();
63     }
64
65     /**
66      * @param newOrientation
67      * @param newLeftComponent
68      * @param newRightComponent
69      */

70     public ExtendedSplitPane(
71         int newOrientation,
72         Component JavaDoc newLeftComponent,
73         Component JavaDoc newRightComponent) {
74         super(newOrientation, newLeftComponent, newRightComponent);
75         setup();
76     }
77
78     /**
79      * @param newOrientation
80      * @param newContinuousLayout
81      * @param newLeftComponent
82      * @param newRightComponent
83      */

84     public ExtendedSplitPane(
85         int newOrientation,
86         boolean newContinuousLayout,
87         Component JavaDoc newLeftComponent,
88         Component JavaDoc newRightComponent) {
89         super(
90             newOrientation,
91             newContinuousLayout,
92             newLeftComponent,
93             newRightComponent);
94         setup();
95     }
96
97     private void setup() {
98         this.setOneTouchExpandable(true);
99         this.setContinuousLayout(true);
100         DividerListener l = new DividerListener();
101         this.addPropertyChangeListener(l);
102         this.addComponentListener(l);
103     }
104
105     public void animateTo(double newLoc) {
106         if(newLoc!=this.m_pos) {
107             new MoveThread(this.m_pos, newLoc).run();
108         }
109     }
110
111     public double getProportionalDividerLocation() {
112         return this.m_pos;
113     }
114
115     public void setDividerLocation(double loc) {
116         this.m_pos = loc;
117         super.setDividerLocation(loc);
118     }
119
120     public void setExtDividerLocation(int loc) {
121         super.setDividerLocation(loc);
122     }
123
124     public class DividerListener
125         extends ComponentAdapter JavaDoc
126         implements PropertyChangeListener JavaDoc {
127         double percentage = 1.0;
128         boolean ignore = false;
129
130         public void propertyChange(PropertyChangeEvent JavaDoc event) {
131             String JavaDoc prop = event.getPropertyName();
132             JSplitPane JavaDoc split = (JSplitPane JavaDoc) event.getSource();
133             if (prop.equals("lastDividerLocation")) {
134                 if (ignore) {
135                     ignore = false;
136                 } else {
137                     if (getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
138                         int location = getDividerLocation();
139                         double newPercentage =
140                             ((double) location) / split.getWidth();
141                         if (percentage - newPercentage > 0.05
142                             || newPercentage - percentage > 0.05) {
143                             percentage = newPercentage;
144                         }
145                     } else {
146                         int location = getDividerLocation();
147                         double newPercentage =
148                             ((double) location) / split.getHeight();
149                         if (percentage - newPercentage > 0.05
150                             || newPercentage - percentage > 0.05) {
151                             percentage = newPercentage;
152                         }
153                     }
154                     Component JavaDoc[] comps = getComponents();
155                     for(int i=0; i<comps.length;i++) {
156                         comps[i].validate();
157                     }
158                 }
159             }
160             m_pos = percentage;
161         }
162
163         public void componentResized(ComponentEvent JavaDoc event) {
164             if (percentage > 0 && percentage < 1) {
165                 ignore = true;
166                 setDividerLocation(percentage);
167                 repaint();
168             }
169         }
170     }
171
172     class MoveThread extends Thread JavaDoc {
173
174         private double m_currentLoc;
175         private double m_newLoc;
176         private int m_nCount = 5;
177         private double m_diff;
178
179         MoveThread(double currentLoc, double newLoc) {
180             this.m_currentLoc = currentLoc;
181             this.m_newLoc = newLoc;
182             if (m_currentLoc > m_newLoc) {
183                 m_diff = (m_currentLoc - m_newLoc) / m_nCount;
184             } else if (newLoc > m_currentLoc) {
185                 m_diff = (m_newLoc - m_currentLoc) / m_nCount;
186             } else {
187                 m_diff = 0;
188             }
189         }
190
191         public void run() {
192             for (int n = 0; n < m_nCount; n++) {
193                 if (m_currentLoc > m_newLoc) {
194                     m_currentLoc = m_currentLoc + m_diff;
195                 } else {
196                     m_currentLoc = m_currentLoc - m_diff;
197                 }
198                 setDividerLocation(m_currentLoc);
199                 try {
200                     paintAll(getGraphics());
201                     Thread.sleep(20L);
202                 } catch (Exception JavaDoc exception) {
203                 }
204             }
205         }
206     }
207
208     /* (non-Javadoc)
209      * @see javax.swing.JSplitPane#setBottomComponent(java.awt.Component)
210      */

211     public void setBottomComponent(Component JavaDoc arg0) {
212         super.setBottomComponent(arg0);
213         super.setDividerLocation(this.m_pos);
214     }
215
216     /* (non-Javadoc)
217      * @see javax.swing.JSplitPane#setLeftComponent(java.awt.Component)
218      */

219     public void setLeftComponent(Component JavaDoc arg0) {
220         super.setLeftComponent(arg0);
221         if(this.m_pos>=0.0 && this.m_pos<=1.0) {
222             super.setDividerLocation(this.m_pos);
223         }
224     }
225
226     /* (non-Javadoc)
227      * @see javax.swing.JSplitPane#setRightComponent(java.awt.Component)
228      */

229     public void setRightComponent(Component JavaDoc arg0) {
230         super.setRightComponent(arg0);
231         super.setDividerLocation(this.m_pos);
232     }
233
234     /* (non-Javadoc)
235      * @see javax.swing.JSplitPane#setTopComponent(java.awt.Component)
236      */

237     public void setTopComponent(Component JavaDoc arg0) {
238         super.setTopComponent(arg0);
239         super.setDividerLocation(this.m_pos);
240     }
241
242 }
243
Popular Tags