KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JSplitPane


1 /*
2  * SwingWT Copyright(c)2003-2004, R. Rawson-Tetley
3  *
4  * For more information on distributing and using this program, please see the
5  * accompanying "COPYING" file.
6  *
7  * Contact me by electronic mail: bobintetley@users.sourceforge.net
8  *
9  * $Log: JSplitPane.java,v $
10  * Revision 1.16 2004/05/06 19:36:26 laurentmartelli
11  * Removed debug trace
12  *
13  * Revision 1.15 2004/05/06 18:43:40 laurentmartelli
14  * Fixed setDividerLocation(int) bug
15  *
16  * Revision 1.14 2004/05/05 12:43:21 bobintetley
17  * Patches/new files from Laurent Martell
18  *
19  * Revision 1.13 2004/04/28 08:38:12 bobintetley
20  * Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
21  *
22  * Revision 1.12 2004/04/27 06:37:44 bobintetley
23  * Corrected hierarchy with JComponent descending Container
24  *
25  * Revision 1.11 2004/04/06 10:50:37 bobintetley
26  * Thread safety work
27  *
28  * Revision 1.10 2004/03/26 14:52:46 bobintetley
29  * Support for setDividerLocation() and thread safety
30  *
31  * Revision 1.9 2004/02/13 22:55:29 djspiewak
32  * Improved the JToolBar class and added two new methods
33  * Revision 1.8 2004/01/26 08:11:00 bobintetley Many
34  * bugfixes and addition of SwingSet
35  *
36  * Revision 1.7 2003/12/15 18:29:57 bobintetley Changed setParent() method to
37  * setSwingWTParent() to avoid conflicts with applications
38  *
39  * Revision 1.6 2003/12/14 09:13:38 bobintetley Added CVS log to source headers
40  *
41  */

42
43 package swingwtx.swing;
44
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.custom.SashForm;
47
48 import swingwt.awt.Dimension;
49
50 /**
51  * JSplitPane kindly contributed by Jack Park. Couple of changes here and there
52  * by me to fit existing standards and things.
53  *
54  * 29/11/2003 - Thanks to Diane Trout for skeletons of additional methods
55  * required.
56  *
57  * @author Jack Park
58  * @author Diane Trout
59  * @author Robin Rawson-Tetley
60  */

61 public class JSplitPane extends JComponent {
62     
63     public final static int VERTICAL_SPLIT = 0;
64     public final static int HORIZONTAL_SPLIT = 1;
65     
66     protected int pOrientation = HORIZONTAL_SPLIT;
67     protected double pResizeWeight = 0;
68     
69     protected swingwt.awt.Component leftComponent = null;
70     protected swingwt.awt.Component rightComponent = null;
71     
72     private SashForm ppeer = null;
73     
74     public JSplitPane() {
75     }
76     public JSplitPane(int newOrientation) {
77         this(newOrientation, false);
78     }
79     public JSplitPane(int newOrientation, boolean newContinuousLayout) {
80         this(newOrientation, newContinuousLayout, null, null);
81     }
82     public JSplitPane(
83     int newOrientation,
84     swingwt.awt.Component newLeftComponent,
85     swingwt.awt.Component newRightComponent) {
86         this(newOrientation, false, newLeftComponent, newRightComponent);
87     }
88     
89     public JSplitPane(
90     int newOrientation,
91     boolean newContinuousLayout,
92     swingwt.awt.Component newLeftComponent,
93     swingwt.awt.Component newRightComponent) {
94         
95         pOrientation = newOrientation;
96         if (pOrientation != HORIZONTAL_SPLIT && pOrientation != VERTICAL_SPLIT)
97             throw new IllegalArgumentException JavaDoc("Invalid orientation.");
98         if (newLeftComponent != null)
99             setLeftComponent(newLeftComponent);
100         if (newRightComponent != null)
101             setRightComponent(newRightComponent);
102         
103     }
104     
105     public void setLeftComponent(swingwt.awt.Component comp) {
106         leftComponent = comp;
107         if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
108             addLeft();
109         }
110     }
111     
112     public void setRightComponent(swingwt.awt.Component comp) {
113         rightComponent = comp;
114         if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
115             addRight();
116         }
117     }
118     
119     void addLeft() {
120         final JSplitPane me = this;
121         SwingUtilities.invokeSync(new Runnable JavaDoc() {
122             public void run() {
123                 try {
124                     leftComponent.setSwingWTParent(me);
125                 } catch (Exception JavaDoc e) {
126                     e.printStackTrace();
127                 }
128             }
129         });
130     }
131     
132     void addRight() {
133         final JSplitPane me = this;
134         SwingUtilities.invokeSync(new Runnable JavaDoc() {
135             public void run() {
136                 try {
137                     rightComponent.setSwingWTParent(me);
138                 } catch (Exception JavaDoc e) {
139                     e.printStackTrace();
140                 }
141             }
142         });
143     }
144     
145     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
146         descendantHasPeer = true;
147         ppeer = new SashForm(parent.getComposite(), SWT.BORDER);
148         peer = ppeer;
149         composite = ppeer;
150         this.parent = parent;
151         ppeer.setOrientation( this.pOrientation == VERTICAL_SPLIT ? SWT.VERTICAL : SWT.HORIZONTAL );
152         if (leftComponent != null) {
153             addLeft();
154             if (rightComponent != null)
155                 addRight();
156             // No point adding right if we couldn't add left, since
157
// SWT identifies left/right, top/bottom from parental order.
158
}
159         if (pResizeWeight != 0)
160             setResizeWeight(pResizeWeight);
161     }
162     
163     public swingwt.awt.Component getLeftComponent() {
164         return leftComponent;
165     }
166     public void setTopComponent(swingwt.awt.Component comp) {
167         setLeftComponent(comp);
168     }
169     public swingwt.awt.Component getTopComponent() {
170         return leftComponent;
171     }
172     public swingwt.awt.Component getRightComponent() {
173         return rightComponent;
174     }
175     public void setBottomComponent(swingwt.awt.Component comp) {
176         setRightComponent(comp);
177     }
178     public swingwt.awt.Component getBottomComponent() {
179         return rightComponent;
180     }
181     public int getOrientation() {
182         return pOrientation;
183     }
184     public void setContinuousLayout(boolean b) {
185     }
186     public int getDividerSize() {
187         return 2;
188     }
189     public void setDividerSize(int newSize) {
190     }
191     public void setOrientation(final int newOrientation) {
192         if (pOrientation != HORIZONTAL_SPLIT && pOrientation != VERTICAL_SPLIT)
193             throw new IllegalArgumentException JavaDoc("Invalid orientation.");
194         pOrientation = newOrientation;
195         SwingUtilities.invokeSync(new Runnable JavaDoc() {
196             public void run() {
197                 if (SwingWTUtils.isSWTControlAvailable(ppeer))
198                     ppeer.setOrientation(newOrientation == VERTICAL_SPLIT ? SWT.VERTICAL : SWT.HORIZONTAL);
199             }
200         });
201     }
202     public void setResizeWeight(final double value) {
203         pResizeWeight = value;
204         SwingUtilities.invokeSync(new Runnable JavaDoc() {
205             public void run() {
206                 if (SwingWTUtils.isSWTControlAvailable(ppeer) && leftComponent != null && rightComponent != null) {
207                     int leftWeight = (int) (value * 10);
208                     int rightWeight = 10 - leftWeight;
209                     ppeer.setWeights(new int[] { leftWeight, rightWeight });
210                 }
211             }
212         });
213     }
214     public double getResizeWeight() {
215         return pResizeWeight;
216     }
217     
218     /**
219      * Enable one touch expand collapse of the splitter
220      */

221     public void setOneTouchExpandable(boolean newValue) {
222     }
223     
224     /**
225      * reset splitter to sizes based on child window sizes
226      */

227     public void resetToPreferredSizes() {
228         if (SwingWTUtils.isSWTControlAvailable(ppeer) && leftComponent != null && rightComponent != null) {
229             setPreferredSize(
230             new Dimension(
231             leftComponent.getPreferredSize().width
232             + rightComponent.getPreferredSize().width,
233             leftComponent.getPreferredSize().height
234             + rightComponent.getPreferredSize().height));
235         }
236     }
237     
238     /**
239      * Set the current location of the splitter proportional
240      * to the width as a percentage.
241      */

242     public void setDividerLocation(double proportionalLocation) {
243         if (SwingWTUtils.isSWTControlAvailable(ppeer) && leftComponent != null) {
244             setResizeWeight(proportionalLocation);
245         }
246     }
247     
248     public void setDividerLocation(int location) {
249         if (SwingWTUtils.isSWTControlAvailable(ppeer) && leftComponent != null) {
250             setResizeWeight( ((double)location) / ((double)getWidth()) );
251         }
252     }
253     
254 }
255
Popular Tags