KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > BorderLayout


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4  
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7  
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9  
10    $Log: BorderLayout.java,v $
11    Revision 1.13 2004/05/03 20:53:42 dannaab
12    small tweak to layoutmanager instance objects to make easier to read
13
14    Revision 1.12 2004/04/23 00:52:31 dannaab
15    Handle borders in a Swing-like way. Implement EmptyBorder & TitledBorder
16
17    Revision 1.11 2004/04/21 10:43:57 bobintetley
18    Code cleanup and native build script fix
19
20    Revision 1.10 2004/04/20 19:05:38 bobintetley
21    Code cleanup/refactoring
22
23    Revision 1.9 2004/04/20 16:35:45 bobintetley
24    Code cleanup
25
26    Revision 1.8 2004/03/22 15:10:21 bobintetley
27    JRootPane and JLayeredPane implementation
28  
29    Revision 1.7 2004/01/06 08:28:02 bobintetley
30    Header fixes
31  
32  
33  */

34
35 package swingwt.awt;
36
37
38
39
40 public class BorderLayout implements LayoutManager2 {
41
42     private int hgap;
43     private int vgap;
44     private Component north;
45     private Component west;
46     private Component east;
47     private Component south;
48     private Component center;
49     private Component firstLine;
50     private Component lastLine;
51     private Component firstItem;
52     private Component lastItem;
53     
54     public static final String JavaDoc NORTH = "North";
55     public static final String JavaDoc SOUTH = "South";
56     public static final String JavaDoc EAST = "East";
57     public static final String JavaDoc WEST = "West";
58     public static final String JavaDoc CENTER = "Center";
59     public static final String JavaDoc BEFORE_FIRST_LINE = "First";
60     public static final String JavaDoc AFTER_LAST_LINE = "Last";
61     public static final String JavaDoc BEFORE_LINE_BEGINS = "Before";
62     public static final String JavaDoc AFTER_LINE_ENDS = "After";
63     public static final String JavaDoc PAGE_START = BEFORE_FIRST_LINE;
64     public static final String JavaDoc PAGE_END = AFTER_LAST_LINE;
65     public static final String JavaDoc LINE_START = BEFORE_LINE_BEGINS;
66     public static final String JavaDoc LINE_END = AFTER_LINE_ENDS;
67
68     public BorderLayout() {
69         this(0, 0);
70     }
71     
72     public BorderLayout(int hgap, int vgap) {
73         this.hgap = hgap;
74         this.vgap = vgap;
75     }
76     
77     public int getHgap() {
78         return hgap;
79     }
80     
81     public void setHgap(int hgap) {
82         this.hgap = hgap;
83     }
84     
85     public int getVgap() {
86         return vgap;
87     }
88     
89     public void setVgap(int vgap) {
90         this.vgap = vgap;
91     }
92     
93     public void addLayoutComponent(Component comp, Object JavaDoc constraints) {
94     if ((constraints == null) || (constraints instanceof String JavaDoc)) {
95         addLayoutComponent((String JavaDoc) constraints, comp);
96     } else {
97         throw new IllegalArgumentException JavaDoc("constraint must be a string");
98     }
99     }
100     
101     public void addLayoutComponent(String JavaDoc name, Component comp) {
102         
103             if (name == null) {
104                 name = "Center";
105             }
106             if ("Center".equals(name)) {
107                 center = comp;
108             }
109             else if ("North".equals(name)) {
110                 north = comp;
111             }
112             else if ("South".equals(name)) {
113                 south = comp;
114             }
115             else if ("East".equals(name)) {
116                 east = comp;
117             }
118             else if ("West".equals(name)) {
119                 west = comp;
120             }
121             else if (BEFORE_FIRST_LINE.equals(name)) {
122                 firstLine = comp;
123             }
124             else if (AFTER_LAST_LINE.equals(name)) {
125                 lastLine = comp;
126             }
127             else if (BEFORE_LINE_BEGINS.equals(name)) {
128                 firstItem = comp;
129             }
130             else if (AFTER_LINE_ENDS.equals(name)) {
131                 lastItem = comp;
132             }
133             else {
134                 throw new IllegalArgumentException JavaDoc(
135                 "unknown constraint: " + name);
136             }
137         
138     }
139     
140     public void removeLayoutComponent(Component comp) {
141         
142             if (comp == center) {
143                 center = null;
144             }
145             else if (comp == north) {
146                 north = null;
147             }
148             else if (comp == south) {
149                 south = null;
150             }
151             else if (comp == east) {
152                 east = null;
153             }
154             else if (comp == west) {
155                 west = null;
156             }
157             if (comp == firstLine) {
158                 firstLine = null;
159             }
160             else if (comp == lastLine) {
161                 lastLine = null;
162             }
163             else if (comp == firstItem) {
164                 firstItem = null;
165             }
166             else if (comp == lastItem) {
167                 lastItem = null;
168             }
169         
170     }
171     
172     public Dimension minimumLayoutSize(Container target) {
173
174             Dimension dim = new Dimension(0, 0);
175             
176             boolean ltr = target.getComponentOrientation().isLeftToRight();
177             Component c = null;
178             
179             if ((c = getChild(EAST, ltr)) != null) {
180                 Dimension d = c.getMinimumSize();
181                 dim.width += d.width + hgap;
182                 dim.height = Math.max(d.height, dim.height);
183             }
184             if ((c = getChild(WEST, ltr)) != null) {
185                 Dimension d = c.getMinimumSize();
186                 dim.width += d.width + hgap;
187                 dim.height = Math.max(d.height, dim.height);
188             }
189             if ((c = getChild(CENTER, ltr)) != null) {
190                 Dimension d = c.getMinimumSize();
191                 dim.width += d.width;
192                 dim.height = Math.max(d.height, dim.height);
193             }
194             if ((c = getChild(NORTH, ltr)) != null) {
195                 Dimension d = c.getMinimumSize();
196                 dim.width = Math.max(d.width, dim.width);
197                 dim.height += d.height + vgap;
198             }
199             if ((c = getChild(SOUTH, ltr)) != null) {
200                 Dimension d = c.getMinimumSize();
201                 dim.width = Math.max(d.width, dim.width);
202                 dim.height += d.height + vgap;
203             }
204             
205             Insets insets = target.getInsets();
206             dim.width += insets.left + insets.right;
207             dim.height += insets.top + insets.bottom;
208             
209             return dim;
210         
211     }
212     
213     public Dimension preferredLayoutSize(Container target) {
214         
215             Dimension dim = new Dimension(0, 0);
216             
217             boolean ltr = target.getComponentOrientation().isLeftToRight();
218             Component c = null;
219             
220             if ((c = getChild(EAST, ltr)) != null) {
221                 Dimension d = c.getPreferredSize();
222                 dim.width += d.width + hgap;
223                 dim.height = Math.max(d.height, dim.height);
224             }
225             if ((c = getChild(WEST, ltr)) != null) {
226                 Dimension d = c.getPreferredSize();
227                 dim.width += d.width + hgap;
228                 dim.height = Math.max(d.height, dim.height);
229             }
230             if ((c = getChild(CENTER, ltr)) != null) {
231                 Dimension d = c.getPreferredSize();
232                 dim.width += d.width;
233                 dim.height = Math.max(d.height, dim.height);
234             }
235             if ((c = getChild(NORTH, ltr)) != null) {
236                 Dimension d = c.getPreferredSize();
237                 dim.width = Math.max(d.width, dim.width);
238                 dim.height += d.height + vgap;
239             }
240             if ((c = getChild(SOUTH, ltr)) != null) {
241                 Dimension d = c.getPreferredSize();
242                 dim.width = Math.max(d.width, dim.width);
243                 dim.height += d.height + vgap;
244             }
245             
246             Insets insets = target.getInsets();
247             dim.width += insets.left + insets.right;
248             dim.height += insets.top + insets.bottom;
249             
250             return dim;
251         
252     }
253     
254     public Dimension maximumLayoutSize(Container target) {
255         return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
256     }
257     
258     public float getLayoutAlignmentX(Container parent) {
259         return (float) 0.5;
260     }
261     
262     public float getLayoutAlignmentY(Container parent) {
263         return (float) 0.5;
264     }
265     
266     public void invalidateLayout(Container target) {
267     }
268     
269     public void layoutContainer(Container target) {
270         Insets insets = target.getInsets();
271         int top = insets.top;
272         int bottom = target.getHeight() - insets.bottom;
273         int left = insets.left;
274         int right = target.getWidth() - insets.right;
275
276         boolean ltr = target.getComponentOrientation().isLeftToRight();
277         Component c = null;
278
279         if ((c = getChild(NORTH, ltr)) != null) {
280             c.setSize(right - left, c.getHeight());
281             Dimension d = c.getPreferredSize();
282             c.setBounds(left, top, right - left, d.height);
283             top += d.height + vgap;
284         }
285         if ((c = getChild(SOUTH, ltr)) != null) {
286             c.setSize(right - left, c.getHeight());
287             Dimension d = c.getPreferredSize();
288             c.setBounds(left, bottom - d.height, right - left, d.height);
289             bottom -= d.height + vgap;
290         }
291         if ((c = getChild(EAST, ltr)) != null) {
292             c.setSize(c.getWidth(), bottom - top);
293             Dimension d = c.getPreferredSize();
294             c.setBounds(right - d.width, top, d.width, bottom - top);
295             right -= d.width + hgap;
296         }
297         if ((c = getChild(WEST, ltr)) != null) {
298             c.setSize(c.getWidth(), bottom - top);
299             Dimension d = c.getPreferredSize();
300             c.setBounds(left, top, d.width, bottom - top);
301             left += d.width + hgap;
302         }
303         if ((c = getChild(CENTER, ltr)) != null) {
304             c.setBounds(left, top, right - left, bottom - top);
305         }
306     }
307     
308     private Component getChild(String JavaDoc key, boolean ltr) {
309         Component result = null;
310         
311         if (key == NORTH) {
312             result = (firstLine != null) ? firstLine : north;
313         } else if (key == SOUTH) {
314             result = (lastLine != null) ? lastLine : south;
315         } else if (key == WEST) {
316             result = ltr ? firstItem : lastItem;
317             if (result == null) {
318                 result = west;
319             }
320         } else if (key == EAST) {
321             result = ltr ? lastItem : firstItem;
322             if (result == null) {
323                 result = east;
324             }
325         } else if (key == CENTER) {
326             result = center;
327         }
328         if (result != null && !result.isVisible()) {
329             result = null;
330         }
331         return result;
332     }
333     
334 }
335
Popular Tags