KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > BorderLayout


1 /*
2  * @(#)BorderLayout.java 1.56 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt;
9
10 import java.util.Hashtable JavaDoc;
11
12 /**
13  * A border layout lays out a container, arranging and resizing
14  * its components to fit in five regions:
15  * north, south, east, west, and center.
16  * Each region may contain no more than one component, and
17  * is identified by a corresponding constant:
18  * <code>NORTH</code>, <code>SOUTH</code>, <code>EAST</code>,
19  * <code>WEST</code>, and <code>CENTER</code>. When adding a
20  * component to a container with a border layout, use one of these
21  * five constants, for example:
22  * <pre>
23  * Panel p = new Panel();
24  * p.setLayout(new BorderLayout());
25  * p.add(new Button("Okay"), BorderLayout.SOUTH);
26  * </pre>
27  * As a convenience, <code>BorderLayout</code> interprets the
28  * absence of a string specification the same as the constant
29  * <code>CENTER</code>:
30  * <pre>
31  * Panel p2 = new Panel();
32  * p2.setLayout(new BorderLayout());
33  * p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER);
34  * </pre>
35  * <p>
36  * In addition, <code>BorderLayout</code> supports the relative
37  * positioning constants, <code>PAGE_START</code>, <code>PAGE_END</code>,
38  * <code>LINE_START</code>, and <code>LINE_END</code>.
39  * In a container whose <code>ComponentOrientation</code> is set to
40  * <code>ComponentOrientation.LEFT_TO_RIGHT</code>, these constants map to
41  * <code>NORTH</code>, <code>SOUTH</code>, <code>WEST</code>, and
42  * <code>EAST</code>, respectively.
43  * <p>
44  * For compatibility with previous releases, <code>BorderLayout</code>
45  * also includes the relative positioning constants <code>BEFORE_FIRST_LINE</code>,
46  * <code>AFTER_LAST_LINE</code>, <code>BEFORE_LINE_BEGINS</code> and
47  * <code>AFTER_LINE_ENDS</code>. These are equivalent to
48  * <code>PAGE_START</code>, <code>PAGE_END</code>, <code>LINE_START</code>
49  * and <code>LINE_END</code> respectively. For
50  * consistency with the relative positioning constants used by other
51  * components, the latter constants are preferred.
52  * <p>
53  * Mixing both absolute and relative positioning constants can lead to
54  * unpredicable results. If
55  * you use both types, the relative constants will take precedence.
56  * For example, if you add components using both the <code>NORTH</code>
57  * and <code>PAGE_START</code> constants in a container whose
58  * orientation is <code>LEFT_TO_RIGHT</code>, only the
59  * <code>PAGE_START</code> will be layed out.
60  * <p>
61  * NOTE: Currently (in the Java 2 platform v1.2),
62  * <code>BorderLayout</code> does not support vertical
63  * orientations. The <code>isVertical</code> setting on the container's
64  * <code>ComponentOrientation</code> is not respected.
65  * <p>
66  * The components are laid out according to their
67  * preferred sizes and the constraints of the container's size.
68  * The <code>NORTH</code> and <code>SOUTH</code> components may
69  * be stretched horizontally; the <code>EAST</code> and
70  * <code>WEST</code> components may be stretched vertically;
71  * the <code>CENTER</code> component may stretch both horizontally
72  * and vertically to fill any space left over.
73  * <p>
74  * Here is an example of five buttons in an applet laid out using
75  * the <code>BorderLayout</code> layout manager:
76  * <p>
77  * <img SRC="doc-files/BorderLayout-1.gif"
78  * alt="Diagram of an applet demonstrating BorderLayout.
79  * Each section of the BorderLayout contains a Button corresponding to its position in the layout, one of:
80  * North, West, Center, East, or South."
81  * ALIGN=center HSPACE=10 VSPACE=7>
82  * <p>
83  * The code for this applet is as follows:
84  * <p>
85  * <hr><blockquote><pre>
86  * import java.awt.*;
87  * import java.applet.Applet;
88  *
89  * public class buttonDir extends Applet {
90  * public void init() {
91  * setLayout(new BorderLayout());
92  * add(new Button("North"), BorderLayout.NORTH);
93  * add(new Button("South"), BorderLayout.SOUTH);
94  * add(new Button("East"), BorderLayout.EAST);
95  * add(new Button("West"), BorderLayout.WEST);
96  * add(new Button("Center"), BorderLayout.CENTER);
97  * }
98  * }
99  * </pre></blockquote><hr>
100  * <p>
101  * @version 1.56, 05/18/04
102  * @author Arthur van Hoff
103  * @see java.awt.Container#add(String, Component)
104  * @see java.awt.ComponentOrientation
105  * @since JDK1.0
106  */

107 public class BorderLayout implements LayoutManager2 JavaDoc,
108                      java.io.Serializable JavaDoc {
109     /**
110      * Constructs a border layout with the horizontal gaps
111      * between components.
112      * The horizontal gap is specified by <code>hgap</code>.
113      *
114      * @see #getHgap()
115      * @see #setHgap(int)
116      *
117      * @serial
118      */

119     int hgap;
120     
121     /**
122      * Constructs a border layout with the vertical gaps
123      * between components.
124      * The vertical gap is specified by <code>vgap</code>.
125      *
126      * @see #getVgap()
127      * @see #setVgap(int)
128      * @serial
129      */

130     int vgap;
131
132     /**
133      * Constant to specify components location to be the
134      * north portion of the border layout.
135      * @serial
136      * @see #getChild(String, boolean)
137      * @see #addLayoutComponent
138      * @see #getLayoutAlignmentX
139      * @see #getLayoutAlignmentY
140      * @see #removeLayoutComponent
141      */

142     Component JavaDoc north;
143      /**
144      * Constant to specify components location to be the
145      * west portion of the border layout.
146      * @serial
147      * @see #getChild(String, boolean)
148      * @see #addLayoutComponent
149      * @see #getLayoutAlignmentX
150      * @see #getLayoutAlignmentY
151      * @see #removeLayoutComponent
152      */

153     Component JavaDoc west;
154     /**
155      * Constant to specify components location to be the
156      * east portion of the border layout.
157      * @serial
158      * @see #getChild(String, boolean)
159      * @see #addLayoutComponent
160      * @see #getLayoutAlignmentX
161      * @see #getLayoutAlignmentY
162      * @see #removeLayoutComponent
163      */

164     Component JavaDoc east;
165     /**
166      * Constant to specify components location to be the
167      * south portion of the border layout.
168      * @serial
169      * @see #getChild(String, boolean)
170      * @see #addLayoutComponent
171      * @see #getLayoutAlignmentX
172      * @see #getLayoutAlignmentY
173      * @see #removeLayoutComponent
174      */

175     Component JavaDoc south;
176     /**
177      * Constant to specify components location to be the
178      * center portion of the border layout.
179      * @serial
180      * @see #getChild(String, boolean)
181      * @see #addLayoutComponent
182      * @see #getLayoutAlignmentX
183      * @see #getLayoutAlignmentY
184      * @see #removeLayoutComponent
185      */

186     Component JavaDoc center;
187     
188     /**
189      *
190      * A relative positioning constant, that can be used instead of
191      * north, south, east, west or center.
192      * mixing the two types of constants can lead to unpredicable results. If
193      * you use both types, the relative constants will take precedence.
194      * For example, if you add components using both the <code>NORTH</code>
195      * and <code>BEFORE_FIRST_LINE</code> constants in a container whose
196      * orientation is <code>LEFT_TO_RIGHT</code>, only the
197      * <code>BEFORE_FIRST_LINE</code> will be layed out.
198      * This will be the same for lastLine, firstItem, lastItem.
199      * @serial
200      */

201     Component JavaDoc firstLine;
202      /**
203      * A relative positioning constant, that can be used instead of
204      * north, south, east, west or center.
205      * Please read Description for firstLine.
206      * @serial
207      */

208     Component JavaDoc lastLine;
209      /**
210      * A relative positioning constant, that can be used instead of
211      * north, south, east, west or center.
212      * Please read Description for firstLine.
213      * @serial
214      */

215     Component JavaDoc firstItem;
216     /**
217      * A relative positioning constant, that can be used instead of
218      * north, south, east, west or center.
219      * Please read Description for firstLine.
220      * @serial
221      */

222     Component JavaDoc lastItem;
223
224     /**
225      * The north layout constraint (top of container).
226      */

227     public static final String JavaDoc NORTH = "North";
228
229     /**
230      * The south layout constraint (bottom of container).
231      */

232     public static final String JavaDoc SOUTH = "South";
233
234     /**
235      * The east layout constraint (right side of container).
236      */

237     public static final String JavaDoc EAST = "East";
238
239     /**
240      * The west layout constraint (left side of container).
241      */

242     public static final String JavaDoc WEST = "West";
243
244     /**
245      * The center layout constraint (middle of container).
246      */

247     public static final String JavaDoc CENTER = "Center";
248
249     /**
250      * Synonym for PAGE_START. Exists for compatibility with previous
251      * versions. PAGE_START is preferred.
252      *
253      * @see #PAGE_START
254      * @since 1.2
255      */

256     public static final String JavaDoc BEFORE_FIRST_LINE = "First";
257
258     /**
259      * Synonym for PAGE_END. Exists for compatibility with previous
260      * versions. PAGE_END is preferred.
261      *
262      * @see #PAGE_END
263      * @since 1.2
264      */

265     public static final String JavaDoc AFTER_LAST_LINE = "Last";
266
267     /**
268      * Synonym for LINE_START. Exists for compatibility with previous
269      * versions. LINE_START is preferred.
270      *
271      * @see #LINE_START
272      * @since 1.2
273      */

274     public static final String JavaDoc BEFORE_LINE_BEGINS = "Before";
275
276     /**
277      * Synonym for LINE_END. Exists for compatibility with previous
278      * versions. LINE_END is preferred.
279      *
280      * @see #LINE_END
281      * @since 1.2
282      */

283     public static final String JavaDoc AFTER_LINE_ENDS = "After";
284
285     /**
286      * The component comes before the first line of the layout's content.
287      * For Western, left-to-right and top-to-bottom orientations, this is
288      * equivalent to NORTH.
289      *
290      * @see java.awt.Component#getComponentOrientation
291      * @since 1.4
292      */

293     public static final String JavaDoc PAGE_START = BEFORE_FIRST_LINE;
294
295     /**
296      * The component comes after the last line of the layout's content.
297      * For Western, left-to-right and top-to-bottom orientations, this is
298      * equivalent to SOUTH.
299      *
300      * @see java.awt.Component#getComponentOrientation
301      * @since 1.4
302      */

303     public static final String JavaDoc PAGE_END = AFTER_LAST_LINE;
304
305     /**
306      * The component goes at the beginning of the line direction for the
307      * layout. For Western, left-to-right and top-to-bottom orientations,
308      * this is equivalent to WEST.
309      *
310      * @see java.awt.Component#getComponentOrientation
311      * @since 1.4
312      */

313     public static final String JavaDoc LINE_START = BEFORE_LINE_BEGINS;
314
315     /**
316      * The component goes at the end of the line direction for the
317      * layout. For Western, left-to-right and top-to-bottom orientations,
318      * this is equivalent to EAST.
319      *
320      * @see java.awt.Component#getComponentOrientation
321      * @since 1.4
322      */

323     public static final String JavaDoc LINE_END = AFTER_LINE_ENDS;
324
325     /*
326      * JDK 1.1 serialVersionUID
327      */

328      private static final long serialVersionUID = -8658291919501921765L;
329
330     /**
331      * Constructs a new border layout with
332      * no gaps between components.
333      */

334     public BorderLayout() {
335     this(0, 0);
336     }
337
338     /**
339      * Constructs a border layout with the specified gaps
340      * between components.
341      * The horizontal gap is specified by <code>hgap</code>
342      * and the vertical gap is specified by <code>vgap</code>.
343      * @param hgap the horizontal gap.
344      * @param vgap the vertical gap.
345      */

346     public BorderLayout(int hgap, int vgap) {
347     this.hgap = hgap;
348     this.vgap = vgap;
349     }
350
351     /**
352      * Returns the horizontal gap between components.
353      * @since JDK1.1
354      */

355     public int getHgap() {
356     return hgap;
357     }
358
359     /**
360      * Sets the horizontal gap between components.
361      * @param hgap the horizontal gap between components
362      * @since JDK1.1
363      */

364     public void setHgap(int hgap) {
365     this.hgap = hgap;
366     }
367
368     /**
369      * Returns the vertical gap between components.
370      * @since JDK1.1
371      */

372     public int getVgap() {
373     return vgap;
374     }
375
376     /**
377      * Sets the vertical gap between components.
378      * @param vgap the vertical gap between components
379      * @since JDK1.1
380      */

381     public void setVgap(int vgap) {
382     this.vgap = vgap;
383     }
384
385     /**
386      * Adds the specified component to the layout, using the specified
387      * constraint object. For border layouts, the constraint must be
388      * one of the following constants: <code>NORTH</code>,
389      * <code>SOUTH</code>, <code>EAST</code>,
390      * <code>WEST</code>, or <code>CENTER</code>.
391      * <p>
392      * Most applications do not call this method directly. This method
393      * is called when a component is added to a container using the
394      * <code>Container.add</code> method with the same argument types.
395      * @param comp the component to be added.
396      * @param constraints an object that specifies how and where
397      * the component is added to the layout.
398      * @see java.awt.Container#add(java.awt.Component, java.lang.Object)
399      * @exception IllegalArgumentException if the constraint object is not
400      * a string, or if it not one of the five specified
401      * constants.
402      * @since JDK1.1
403      */

404     public void addLayoutComponent(Component JavaDoc comp, Object JavaDoc constraints) {
405       synchronized (comp.getTreeLock()) {
406     if ((constraints == null) || (constraints instanceof String JavaDoc)) {
407         addLayoutComponent((String JavaDoc)constraints, comp);
408     } else {
409         throw new IllegalArgumentException JavaDoc("cannot add to layout: constraint must be a string (or null)");
410     }
411       }
412     }
413
414     /**
415      * @deprecated replaced by <code>addLayoutComponent(Component, Object)</code>.
416      */

417     @Deprecated JavaDoc
418     public void addLayoutComponent(String JavaDoc name, Component JavaDoc comp) {
419       synchronized (comp.getTreeLock()) {
420     /* Special case: treat null the same as "Center". */
421     if (name == null) {
422         name = "Center";
423     }
424
425     /* Assign the component to one of the known regions of the layout.
426      */

427     if ("Center".equals(name)) {
428         center = comp;
429     } else if ("North".equals(name)) {
430         north = comp;
431     } else if ("South".equals(name)) {
432         south = comp;
433     } else if ("East".equals(name)) {
434         east = comp;
435     } else if ("West".equals(name)) {
436         west = comp;
437     } else if (BEFORE_FIRST_LINE.equals(name)) {
438         firstLine = comp;
439     } else if (AFTER_LAST_LINE.equals(name)) {
440         lastLine = comp;
441     } else if (BEFORE_LINE_BEGINS.equals(name)) {
442         firstItem = comp;
443     } else if (AFTER_LINE_ENDS.equals(name)) {
444         lastItem = comp;
445     } else {
446         throw new IllegalArgumentException JavaDoc("cannot add to layout: unknown constraint: " + name);
447     }
448       }
449     }
450
451     /**
452      * Removes the specified component from this border layout. This
453      * method is called when a container calls its <code>remove</code> or
454      * <code>removeAll</code> methods. Most applications do not call this
455      * method directly.
456      * @param comp the component to be removed.
457      * @see java.awt.Container#remove(java.awt.Component)
458      * @see java.awt.Container#removeAll()
459      */

460     public void removeLayoutComponent(Component JavaDoc comp) {
461       synchronized (comp.getTreeLock()) {
462     if (comp == center) {
463         center = null;
464     } else if (comp == north) {
465         north = null;
466     } else if (comp == south) {
467         south = null;
468     } else if (comp == east) {
469         east = null;
470     } else if (comp == west) {
471         west = null;
472     }
473     if (comp == firstLine) {
474         firstLine = null;
475     } else if (comp == lastLine) {
476         lastLine = null;
477     } else if (comp == firstItem) {
478         firstItem = null;
479     } else if (comp == lastItem) {
480         lastItem = null;
481     }
482       }
483     }
484
485     /**
486      * Gets the component that was added using the given constraint
487      *
488      * @param constraints the desired constraint, one of <code>CENTER</code>,
489      * <code>NORTH</code>, <code>SOUTH</code>,
490      * <code>WEST</code>, <code>EAST</code>,
491      * <code>PAGE_START</code>, <code>PAGE_END</code>,
492      * <code>LINE_START</code>, <code>LINE_END</code>
493      * @return the component at the given location, or </code>null</code> if
494      * the location is empty
495      * @exception IllegalArgumentException if the constraint object is
496      * not one of the nine specified constants
497      * @see #addLayoutComponent(java.awt.Component, java.lang.Object)
498      * @since 1.5
499      */

500     public Component JavaDoc getLayoutComponent(Object JavaDoc constraints) {
501     if (CENTER.equals(constraints)) {
502         return center;
503     } else if (NORTH.equals(constraints)) {
504         return north;
505     } else if (SOUTH.equals(constraints)) {
506         return south;
507     } else if (WEST.equals(constraints)) {
508         return west;
509     } else if (EAST.equals(constraints)) {
510         return east;
511     } else if (PAGE_START.equals(constraints)) {
512         return firstLine;
513     } else if (PAGE_END.equals(constraints)) {
514         return lastLine;
515     } else if (LINE_START.equals(constraints)) {
516         return firstItem;
517     } else if (LINE_END.equals(constraints)) {
518         return lastItem;
519     } else {
520         throw new IllegalArgumentException JavaDoc("cannot get component: unknown constraint: " + constraints);
521     }
522     }
523
524
525     /**
526      * Gets the component that corresponds to the given constraint location
527      * based on the target Container's component orientation
528      *
529      * @param constraints the desired absolute position, one of <code>CENTER</code>,
530      * one of <code>NORTH</code>, <code>SOUTH</code>,
531      * <code>EAST</code>, <code>WEST</code>
532      * @param target the <code>Container</code> using this <code>BorderLayout</code>
533      * @return the component at the given location, or </code>null</code> if
534      * the location is empty
535      * @exception IllegalArgumentException if the constraint object is
536      * not one of the five specified constants
537      * @exception NullPointerException if the target parameter is null
538      * @see #addLayoutComponent(java.awt.Component, java.lang.Object)
539      * @since 1.5
540      */

541     public Component JavaDoc getLayoutComponent(Container JavaDoc target, Object JavaDoc constraints) {
542     boolean ltr = target.getComponentOrientation().isLeftToRight();
543         Component JavaDoc result = null;
544
545         if (NORTH.equals(constraints)) {
546             result = (firstLine != null) ? firstLine : north;
547         } else if (SOUTH.equals(constraints)) {
548             result = (lastLine != null) ? lastLine : south;
549         } else if (WEST.equals(constraints)) {
550             result = ltr ? firstItem : lastItem;
551             if (result == null) {
552         result = west;
553             }
554         } else if (EAST.equals(constraints)) {
555             result = ltr ? lastItem : firstItem;
556             if (result == null) {
557         result = east;
558             }
559         } else if (CENTER.equals(constraints)) {
560             result = center;
561     } else {
562         throw new IllegalArgumentException JavaDoc("cannot get component: invalid constraint: " + constraints);
563         }
564
565         return result;
566     }
567
568
569     /**
570      * Gets the constraints for the specified component
571      *
572      * @param comp the component to be queried
573      * @return the constraint for the specified component,
574      * or null if component is null or is not present
575      * in this layout
576      * @see #addLayoutComponent(java.awt.Component, java.lang.Object)
577      * @since 1.5
578      */

579     public Object JavaDoc getConstraints(Component JavaDoc comp) {
580     if (comp == center) {
581         return CENTER;
582     } else if (comp == north) {
583         return NORTH;
584     } else if (comp == south) {
585         return SOUTH;
586     } else if (comp == west) {
587         return WEST;
588     } else if (comp == east) {
589         return EAST;
590     } else if (comp == firstLine) {
591         return PAGE_START;
592     } else if (comp == lastLine) {
593         return PAGE_END;
594     } else if (comp == firstItem) {
595         return LINE_START;
596     } else if (comp == lastItem) {
597         return LINE_END;
598     }
599     return null;
600     }
601
602     /**
603      * Determines the minimum size of the <code>target</code> container
604      * using this layout manager.
605      * <p>
606      * This method is called when a container calls its
607      * <code>getMinimumSize</code> method. Most applications do not call
608      * this method directly.
609      * @param target the container in which to do the layout.
610      * @return the minimum dimensions needed to lay out the subcomponents
611      * of the specified container.
612      * @see java.awt.Container
613      * @see java.awt.BorderLayout#preferredLayoutSize
614      * @see java.awt.Container#getMinimumSize()
615      */

616     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc target) {
617       synchronized (target.getTreeLock()) {
618     Dimension JavaDoc dim = new Dimension JavaDoc(0, 0);
619
620         boolean ltr = target.getComponentOrientation().isLeftToRight();
621         Component JavaDoc c = null;
622
623     if ((c=getChild(EAST,ltr)) != null) {
624         Dimension JavaDoc d = c.getMinimumSize();
625         dim.width += d.width + hgap;
626         dim.height = Math.max(d.height, dim.height);
627     }
628     if ((c=getChild(WEST,ltr)) != null) {
629         Dimension JavaDoc d = c.getMinimumSize();
630         dim.width += d.width + hgap;
631         dim.height = Math.max(d.height, dim.height);
632     }
633     if ((c=getChild(CENTER,ltr)) != null) {
634         Dimension JavaDoc d = c.getMinimumSize();
635         dim.width += d.width;
636         dim.height = Math.max(d.height, dim.height);
637     }
638     if ((c=getChild(NORTH,ltr)) != null) {
639         Dimension JavaDoc d = c.getMinimumSize();
640         dim.width = Math.max(d.width, dim.width);
641         dim.height += d.height + vgap;
642     }
643     if ((c=getChild(SOUTH,ltr)) != null) {
644         Dimension JavaDoc d = c.getMinimumSize();
645         dim.width = Math.max(d.width, dim.width);
646         dim.height += d.height + vgap;
647     }
648
649     Insets JavaDoc insets = target.getInsets();
650     dim.width += insets.left + insets.right;
651     dim.height += insets.top + insets.bottom;
652
653     return dim;
654       }
655     }
656
657     /**
658      * Determines the preferred size of the <code>target</code>
659      * container using this layout manager, based on the components
660      * in the container.
661      * <p>
662      * Most applications do not call this method directly. This method
663      * is called when a container calls its <code>getPreferredSize</code>
664      * method.
665      * @param target the container in which to do the layout.
666      * @return the preferred dimensions to lay out the subcomponents
667      * of the specified container.
668      * @see java.awt.Container
669      * @see java.awt.BorderLayout#minimumLayoutSize
670      * @see java.awt.Container#getPreferredSize()
671      */

672     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc target) {
673       synchronized (target.getTreeLock()) {
674     Dimension JavaDoc dim = new Dimension JavaDoc(0, 0);
675
676         boolean ltr = target.getComponentOrientation().isLeftToRight();
677         Component JavaDoc c = null;
678
679     if ((c=getChild(EAST,ltr)) != null) {
680         Dimension JavaDoc d = c.getPreferredSize();
681         dim.width += d.width + hgap;
682         dim.height = Math.max(d.height, dim.height);
683     }
684     if ((c=getChild(WEST,ltr)) != null) {
685         Dimension JavaDoc d = c.getPreferredSize();
686         dim.width += d.width + hgap;
687         dim.height = Math.max(d.height, dim.height);
688     }
689     if ((c=getChild(CENTER,ltr)) != null) {
690         Dimension JavaDoc d = c.getPreferredSize();
691         dim.width += d.width;
692         dim.height = Math.max(d.height, dim.height);
693     }
694     if ((c=getChild(NORTH,ltr)) != null) {
695         Dimension JavaDoc d = c.getPreferredSize();
696         dim.width = Math.max(d.width, dim.width);
697         dim.height += d.height + vgap;
698     }
699     if ((c=getChild(SOUTH,ltr)) != null) {
700         Dimension JavaDoc d = c.getPreferredSize();
701         dim.width = Math.max(d.width, dim.width);
702         dim.height += d.height + vgap;
703     }
704
705     Insets JavaDoc insets = target.getInsets();
706     dim.width += insets.left + insets.right;
707     dim.height += insets.top + insets.bottom;
708
709     return dim;
710       }
711     }
712
713     /**
714      * Returns the maximum dimensions for this layout given the components
715      * in the specified target container.
716      * @param target the component which needs to be laid out
717      * @see Container
718      * @see #minimumLayoutSize
719      * @see #preferredLayoutSize
720      */

721     public Dimension JavaDoc maximumLayoutSize(Container JavaDoc target) {
722     return new Dimension JavaDoc(Integer.MAX_VALUE, Integer.MAX_VALUE);
723     }
724
725     /**
726      * Returns the alignment along the x axis. This specifies how
727      * the component would like to be aligned relative to other
728      * components. The value should be a number between 0 and 1
729      * where 0 represents alignment along the origin, 1 is aligned
730      * the furthest away from the origin, 0.5 is centered, etc.
731      */

732     public float getLayoutAlignmentX(Container JavaDoc parent) {
733     return 0.5f;
734     }
735
736     /**
737      * Returns the alignment along the y axis. This specifies how
738      * the component would like to be aligned relative to other
739      * components. The value should be a number between 0 and 1
740      * where 0 represents alignment along the origin, 1 is aligned
741      * the furthest away from the origin, 0.5 is centered, etc.
742      */

743     public float getLayoutAlignmentY(Container JavaDoc parent) {
744     return 0.5f;
745     }
746
747     /**
748      * Invalidates the layout, indicating that if the layout manager
749      * has cached information it should be discarded.
750      */

751     public void invalidateLayout(Container JavaDoc target) {
752     }
753
754     /**
755      * Lays out the container argument using this border layout.
756      * <p>
757      * This method actually reshapes the components in the specified
758      * container in order to satisfy the constraints of this
759      * <code>BorderLayout</code> object. The <code>NORTH</code>
760      * and <code>SOUTH</code> components, if any, are placed at
761      * the top and bottom of the container, respectively. The
762      * <code>WEST</code> and <code>EAST</code> components are
763      * then placed on the left and right, respectively. Finally,
764      * the <code>CENTER</code> object is placed in any remaining
765      * space in the middle.
766      * <p>
767      * Most applications do not call this method directly. This method
768      * is called when a container calls its <code>doLayout</code> method.
769      * @param target the container in which to do the layout.
770      * @see java.awt.Container
771      * @see java.awt.Container#doLayout()
772      */

773     public void layoutContainer(Container JavaDoc target) {
774       synchronized (target.getTreeLock()) {
775     Insets JavaDoc insets = target.getInsets();
776     int top = insets.top;
777     int bottom = target.height - insets.bottom;
778     int left = insets.left;
779     int right = target.width - insets.right;
780
781         boolean ltr = target.getComponentOrientation().isLeftToRight();
782         Component JavaDoc c = null;
783
784     if ((c=getChild(NORTH,ltr)) != null) {
785         c.setSize(right - left, c.height);
786         Dimension JavaDoc d = c.getPreferredSize();
787         c.setBounds(left, top, right - left, d.height);
788         top += d.height + vgap;
789     }
790     if ((c=getChild(SOUTH,ltr)) != null) {
791         c.setSize(right - left, c.height);
792         Dimension JavaDoc d = c.getPreferredSize();
793         c.setBounds(left, bottom - d.height, right - left, d.height);
794         bottom -= d.height + vgap;
795     }
796     if ((c=getChild(EAST,ltr)) != null) {
797         c.setSize(c.width, bottom - top);
798         Dimension JavaDoc d = c.getPreferredSize();
799         c.setBounds(right - d.width, top, d.width, bottom - top);
800         right -= d.width + hgap;
801     }
802     if ((c=getChild(WEST,ltr)) != null) {
803         c.setSize(c.width, bottom - top);
804         Dimension JavaDoc d = c.getPreferredSize();
805         c.setBounds(left, top, d.width, bottom - top);
806         left += d.width + hgap;
807     }
808     if ((c=getChild(CENTER,ltr)) != null) {
809         c.setBounds(left, top, right - left, bottom - top);
810     }
811       }
812     }
813
814     /**
815      * Get the component that corresponds to the given constraint location
816      *
817      * @param key The desired absolute position,
818      * either NORTH, SOUTH, EAST, or WEST.
819      * @param ltr Is the component line direction left-to-right?
820      */

821     private Component JavaDoc getChild(String JavaDoc key, boolean ltr) {
822         Component JavaDoc result = null;
823
824         if (key == NORTH) {
825             result = (firstLine != null) ? firstLine : north;
826         }
827         else if (key == SOUTH) {
828             result = (lastLine != null) ? lastLine : south;
829         }
830         else if (key == WEST) {
831             result = ltr ? firstItem : lastItem;
832             if (result == null) {
833                 result = west;
834             }
835         }
836         else if (key == EAST) {
837             result = ltr ? lastItem : firstItem;
838             if (result == null) {
839                 result = east;
840             }
841         }
842         else if (key == CENTER) {
843             result = center;
844         }
845         if (result != null && !result.visible) {
846             result = null;
847         }
848         return result;
849     }
850
851     /**
852      * Returns a string representation of the state of this border layout.
853      * @return a string representation of this border layout.
854      */

855     public String JavaDoc toString() {
856     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
857     }
858 }
859
Popular Tags