KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > swing > CustomSplitPanel


1 package org.antlr.works.swing;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7 /*
8
9 [The "BSD licence"]
10 Copyright (c) 2005-2006 Jean Bovet
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
15 are met:
16
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22 3. The name of the author may not be used to endorse or promote products
23 derived from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 */

37
38 public class CustomSplitPanel extends JPanel {
39
40     public static final int LEFT_INDEX = 0;
41     public static final int MIDDLE_INDEX = 1;
42     public static final int RIGHT_INDEX = 2;
43
44     public JSplitPane leftSplitPane;
45     public JSplitPane rightSplitPane;
46
47     public Component left, middle, right;
48     public Map JavaDoc<Component,Float JavaDoc> widths = new HashMap JavaDoc<Component, Float JavaDoc>();
49
50     public CustomSplitPanel() {
51         super(new BorderLayout());
52         leftSplitPane = createSplitPane();
53         rightSplitPane = createSplitPane();
54     }
55
56     public JSplitPane createSplitPane() {
57         JSplitPane pane = new JSplitPane();
58         pane.setBorder(null);
59         pane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
60         pane.setContinuousLayout(true);
61         pane.setOneTouchExpandable(true);
62         return pane;
63     }
64
65     public void setComponents(Component leftComponent, Component middleComponent, Component rightComponent) {
66         leftSplitPane.setLeftComponent(left = leftComponent);
67         leftSplitPane.setRightComponent(middle = middleComponent);
68         rightSplitPane.setLeftComponent(leftSplitPane);
69         rightSplitPane.setRightComponent(right = rightComponent);
70         add(rightSplitPane, BorderLayout.CENTER);
71         resize();
72     }
73
74     public void setComponentWidth(Component c, float width) {
75         widths.put(c, width);
76     }
77
78     public void resize() {
79         if(left != null && middle != null && right != null) {
80             setDividerLocationToComponentWidth(rightSplitPane, getWidth(left)+getWidth(middle));
81             setDividerLocationToComponentWidth(leftSplitPane, getWidth(left));
82         } else if(left != null && middle != null) {
83             setDividerLocationToComponentWidth(rightSplitPane, getWidth(left));
84         } else if(left != null && right != null) {
85             setDividerLocationToComponentWidth(rightSplitPane, getWidth(left));
86         } else if(middle != null && right != null) {
87             setDividerLocationToComponentWidth(rightSplitPane, getWidth(middle));
88         }
89
90         /* This is really ugly but if I don't do the resize later on again, it happens that if the right divider is
91         moved and the middle panel hidden and then showed again, the left split pane's divider will be screwed up. Maybe
92         a bug in my code but don't have time to investigate more. If someone finds the reason, let me know.
93         */

94         SwingUtilities.invokeLater(new Runnable JavaDoc() {
95             public void run() {
96                 if(left != null && middle != null && right != null) {
97                     setDividerLocationToComponentWidth(rightSplitPane, getWidth(left)+getWidth(middle));
98                     setDividerLocationToComponentWidth(leftSplitPane, getWidth(left));
99                 } else if(left != null && middle != null) {
100                     setDividerLocationToComponentWidth(rightSplitPane, getWidth(left));
101                 } else if(left != null && right != null) {
102                     setDividerLocationToComponentWidth(rightSplitPane, getWidth(left));
103                 } else if(middle != null && right != null) {
104                     setDividerLocationToComponentWidth(rightSplitPane, getWidth(middle));
105                 }
106             }
107         });
108
109     }
110
111     public void setDividerLocationToComponentWidth(JSplitPane splitPane, int width) {
112         splitPane.setDividerLocation(width);
113
114     }
115
116     public int getWidth(Component c) {
117         Float JavaDoc width = widths.get(c);
118         if(width != null)
119             return (int)width.floatValue();
120         else
121             return 0;
122     }
123
124     public void setComponent(Component c, int index) {
125         switch(index) {
126             case LEFT_INDEX: setLeftComponent(c); break;
127             case MIDDLE_INDEX: setMiddleComponent(c); break;
128             case RIGHT_INDEX: setRightComponent(c); break;
129         }
130     }
131
132     public Component getComponentAtIndex(int index) {
133         switch(index) {
134             case LEFT_INDEX: return left;
135             case MIDDLE_INDEX: return middle;
136             case RIGHT_INDEX: return right;
137         }
138         return null;
139     }
140
141     public void setLeftComponent(Component c) {
142         if(c == null) {
143             removeLeftComponent();
144             return;
145         }
146
147         if(middle != null && right != null) {
148             rightSplitPane.setLeftComponent(null);
149             leftSplitPane.setLeftComponent(c);
150             leftSplitPane.setRightComponent(middle);
151             rightSplitPane.setLeftComponent(leftSplitPane);
152         } else if(middle != null) {
153             remove(middle);
154             rightSplitPane.setLeftComponent(c);
155             rightSplitPane.setRightComponent(middle);
156             add(rightSplitPane);
157         } else if(right != null) {
158             remove(right);
159             rightSplitPane.setLeftComponent(c);
160             rightSplitPane.setRightComponent(right);
161             add(rightSplitPane);
162         } else if(left == null) {
163             add(c);
164         }
165         left = c;
166         resize();
167     }
168
169     public void removeLeftComponent() {
170         if(middle != null && right != null) {
171             leftSplitPane.setLeftComponent(null);
172             leftSplitPane.setRightComponent(null);
173             rightSplitPane.setLeftComponent(middle);
174         } else if(middle != null) {
175             rightSplitPane.setLeftComponent(null);
176             rightSplitPane.setRightComponent(null);
177             remove(rightSplitPane);
178             add(middle);
179         } else if(right != null) {
180             rightSplitPane.setLeftComponent(null);
181             rightSplitPane.setRightComponent(null);
182             remove(rightSplitPane);
183             add(right);
184         }
185         left = null;
186         resize();
187     }
188
189     public void setMiddleComponent(Component c) {
190         if(c == null) {
191             removeMiddleComponent();
192             return;
193         }
194
195         if(left != null && right != null) {
196             rightSplitPane.setLeftComponent(leftSplitPane);
197             leftSplitPane.setLeftComponent(left);
198             leftSplitPane.setRightComponent(c);
199         } else if(left != null) {
200             remove(left);
201             rightSplitPane.setLeftComponent(left);
202             rightSplitPane.setRightComponent(c);
203             add(rightSplitPane);
204         } else if(right != null) {
205             remove(right);
206             rightSplitPane.setLeftComponent(c);
207             rightSplitPane.setRightComponent(right);
208             add(rightSplitPane);
209         } else if(middle == null) {
210             add(c);
211         }
212         middle = c;
213         resize();
214     }
215
216     public void removeMiddleComponent() {
217         if(left != null && right != null) {
218             leftSplitPane.setLeftComponent(null);
219             leftSplitPane.setRightComponent(null);
220             rightSplitPane.setLeftComponent(left);
221         } else if(left != null) {
222             rightSplitPane.setLeftComponent(null);
223             rightSplitPane.setRightComponent(null);
224             remove(rightSplitPane);
225             add(left);
226         } else if(right != null) {
227             rightSplitPane.setLeftComponent(null);
228             rightSplitPane.setRightComponent(null);
229             remove(rightSplitPane);
230             add(right);
231         }
232         middle = null;
233         resize();
234     }
235
236     public void setRightComponent(Component c) {
237         if(c == null) {
238             removeRightComponent();
239             return;
240         }
241
242         if(left != null && middle != null) {
243             rightSplitPane.setLeftComponent(null);
244             rightSplitPane.setRightComponent(null);
245             leftSplitPane.setLeftComponent(left);
246             leftSplitPane.setRightComponent(middle);
247             rightSplitPane.setLeftComponent(leftSplitPane);
248             rightSplitPane.setRightComponent(c);
249         } else if(left != null) {
250             remove(left);
251             rightSplitPane.setLeftComponent(left);
252             rightSplitPane.setRightComponent(c);
253             add(rightSplitPane);
254         } else if(middle != null) {
255             remove(middle);
256             rightSplitPane.setLeftComponent(middle);
257             rightSplitPane.setRightComponent(c);
258             add(rightSplitPane);
259         } else if(right == null) {
260             add(c);
261         }
262         right = c;
263         resize();
264     }
265
266     public void removeRightComponent() {
267         if(left != null && middle != null) {
268             leftSplitPane.setLeftComponent(null);
269             leftSplitPane.setRightComponent(null);
270             rightSplitPane.setLeftComponent(left);
271             rightSplitPane.setRightComponent(middle);
272         } else if(left != null) {
273             rightSplitPane.setLeftComponent(null);
274             rightSplitPane.setRightComponent(null);
275             remove(rightSplitPane);
276             add(left);
277         } else if(middle != null) {
278             rightSplitPane.setLeftComponent(null);
279             rightSplitPane.setRightComponent(null);
280             remove(rightSplitPane);
281             add(middle);
282         }
283         right = null;
284         resize();
285     }
286
287 }
288
Popular Tags