1 /* BorderLayout.java -- A layout manager class
2 Copyright (C) 1999, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
43 * This class implements a layout manager that positions components
44 * in certain sectors of the parent container.
46 * @author Aaron M. Renn (arenn@urbanophile.com)
47 * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
49 public class BorderLayout
implements LayoutManager2
, java
.io
.Serializable
53 * Constant indicating the top of the container
55 public static final String NORTH
= "North";
58 * Constant indicating the bottom of the container
60 public static final String SOUTH
= "South";
63 * Constant indicating the right side of the container
65 public static final String EAST
= "East";
68 * Constant indicating the left side of the container
70 public static final String WEST
= "West";
73 * Constant indicating the center of the container
75 public static final String CENTER
= "Center";
79 * The constant indicating the position before the first line of the
80 * layout. The exact position depends on the writing system: For a
81 * top-to-bottom orientation, it is the same as {@link #NORTH}, for
82 * a bottom-to-top orientation, it is the same as {@link #SOUTH}.
84 * <p>This constant is an older name for {@link #PAGE_START} which
85 * has exactly the same value.
89 public static final String BEFORE_FIRST_LINE
= "First";
92 * The constant indicating the position after the last line of the
93 * layout. The exact position depends on the writing system: For a
94 * top-to-bottom orientation, it is the same as {@link #SOUTH}, for
95 * a bottom-to-top orientation, it is the same as {@link #NORTH}.
97 * <p>This constant is an older name for {@link #PAGE_END} which
98 * has exactly the same value.
102 public static final String AFTER_LAST_LINE
= "Last";
105 * The constant indicating the position before the first item of the
106 * layout. The exact position depends on the writing system: For a
107 * left-to-right orientation, it is the same as {@link #WEST}, for
108 * a right-to-left orientation, it is the same as {@link #EAST}.
110 * <p>This constant is an older name for {@link #LINE_START} which
111 * has exactly the same value.
115 public static final String BEFORE_LINE_BEGINS
= "Before";
118 * The constant indicating the position after the last item of the
119 * layout. The exact position depends on the writing system: For a
120 * left-to-right orientation, it is the same as {@link #EAST}, for
121 * a right-to-left orientation, it is the same as {@link #WEST}.
123 * <p>This constant is an older name for {@link #LINE_END} which
124 * has exactly the same value.
128 public static final String AFTER_LINE_ENDS
= "After";
131 * The constant indicating the position before the first line of the
132 * layout. The exact position depends on the writing system: For a
133 * top-to-bottom orientation, it is the same as {@link #NORTH}, for
134 * a bottom-to-top orientation, it is the same as {@link #SOUTH}.
138 public static final String PAGE_START
= BEFORE_FIRST_LINE
;
141 * The constant indicating the position after the last line of the
142 * layout. The exact position depends on the writing system: For a
143 * top-to-bottom orientation, it is the same as {@link #SOUTH}, for
144 * a bottom-to-top orientation, it is the same as {@link #NORTH}.
148 public static final String PAGE_END
= AFTER_LAST_LINE
;
151 * The constant indicating the position before the first item of the
152 * layout. The exact position depends on the writing system: For a
153 * left-to-right orientation, it is the same as {@link #WEST}, for
154 * a right-to-left orientation, it is the same as {@link #EAST}.
158 public static final String LINE_START
= BEFORE_LINE_BEGINS
;
161 * The constant indicating the position after the last item of the
162 * layout. The exact position depends on the writing system: For a
163 * left-to-right orientation, it is the same as {@link #EAST}, for
164 * a right-to-left orientation, it is the same as {@link #WEST}.
168 public static final String LINE_END
= AFTER_LINE_ENDS
;
172 * Serialization constant.
174 private static final long serialVersionUID
= -8658291919501921765L;
180 private Component north
;
185 private Component south
;
190 private Component east
;
195 private Component west
;
200 private Component center
;
205 private Component firstLine
;
210 private Component lastLine
;
215 private Component firstItem
;
220 private Component lastItem
;
223 * @serial The horizontal gap between components
228 * @serial The vertical gap between components
233 // Some constants for use with calcSize().
234 private static final int MIN
= 0;
235 private static final int MAX
= 1;
236 private static final int PREF
= 2;
240 * Initializes a new instance of <code>BorderLayout</code> with no
241 * horiztonal or vertical gaps between components.
243 public BorderLayout()
249 * Initializes a new instance of <code>BorderLayout</code> with the
250 * specified horiztonal and vertical gaps between components.
252 * @param hgap The horizontal gap between components.
253 * @param vgap The vertical gap between components.
255 public BorderLayout(int hgap
, int vgap
)
262 * Returns the horitzontal gap value.
264 * @return The horitzontal gap value.
272 * Sets the horizontal gap to the specified value.
274 * @param hgap The new horizontal gap.
276 public void setHgap(int hgap
)
282 * Returns the vertical gap value.
284 * @return The vertical gap value.
292 * Sets the vertical gap to the specified value.
294 * @param vgap The new vertical gap value.
296 public void setVgap(int vgap
)
302 * Adds a component to the layout in the specified constraint position,
303 * which must be one of the string constants defined in this class.
305 * @param component The component to add.
306 * @param constraints The constraint string.
308 * @exception IllegalArgumentException If the constraint object is not
309 * a string, or is not one of the specified constants in this class.
311 public void addLayoutComponent(Component component
, Object constraints
)
313 if (constraints
!= null && ! (constraints
instanceof String
))
314 throw new IllegalArgumentException("Constraint must be a string");
316 addLayoutComponent((String
) constraints
, component
);
320 * Adds a component to the layout in the specified constraint position,
321 * which must be one of the string constants defined in this class.
323 * @param constraints The constraint string.
324 * @param component The component to add.
326 * @exception IllegalArgumentException If the constraint object is not
327 * one of the specified constants in this class.
329 * @deprecated This method is deprecated in favor of
330 * <code>addLayoutComponent(Component, Object)</code>.
332 public void addLayoutComponent(String constraints
, Component component
)
334 String str
= constraints
;
336 if (str
== null || str
.equals(CENTER
))
338 else if (str
.equals(NORTH
))
340 else if (str
.equals(SOUTH
))
342 else if (str
.equals(EAST
))
344 else if (str
.equals(WEST
))
346 else if (str
.equals(BEFORE_FIRST_LINE
))
347 firstLine
= component
;
348 else if (str
.equals(AFTER_LAST_LINE
))
349 lastLine
= component
;
350 else if (str
.equals(BEFORE_LINE_BEGINS
))
351 firstItem
= component
;
352 else if (str
.equals(AFTER_LINE_ENDS
))
353 lastItem
= component
;
355 throw new IllegalArgumentException("Constraint value not valid: " + str
);
359 * Removes the specified component from the layout.
361 * @param component The component to remove from the layout.
363 public void removeLayoutComponent(Component component
)
365 if (north
== component
)
367 if (south
== component
)
369 if (east
== component
)
371 if (west
== component
)
373 if (center
== component
)
375 if (firstItem
== component
)
377 if (lastItem
== component
)
379 if (firstLine
== component
)
381 if (lastLine
== component
)
386 * Returns the minimum size of the specified container using this layout.
388 * @param target The container to calculate the minimum size for.
390 * @return The minimum size of the container
392 public Dimension
minimumLayoutSize(Container target
)
394 return calcSize(target
, MIN
);
398 * Returns the preferred size of the specified container using this layout.
400 * @param target The container to calculate the preferred size for.
402 * @return The preferred size of the container
404 public Dimension
preferredLayoutSize(Container target
)
406 return calcSize(target
, PREF
);
410 * Returns the maximum size of the specified container using this layout.
412 * @param target The container to calculate the maximum size for.
414 * @return The maximum size of the container
416 public Dimension
maximumLayoutSize(Container target
)
418 return new Dimension (Integer
.MAX_VALUE
, Integer
.MAX_VALUE
);
422 * Returns the X axis alignment, which is a <code>float</code> indicating
423 * where along the X axis this container wishs to position its layout.
424 * 0 indicates align to the left, 1 indicates align to the right, and 0.5
425 * indicates align to the center.
427 * @param parent The parent container.
429 * @return The X alignment value.
431 public float getLayoutAlignmentX(Container parent
)
437 * Returns the Y axis alignment, which is a <code>float</code> indicating
438 * where along the Y axis this container wishs to position its layout.
439 * 0 indicates align to the top, 1 indicates align to the bottom, and 0.5
440 * indicates align to the center.
442 * @param parent The parent container.
444 * @return The Y alignment value.
446 public float getLayoutAlignmentY(Container parent
)
452 * Instructs this object to discard any layout information it might
455 * @param parent The parent container.
457 public void invalidateLayout(Container parent
)
459 // Nothing to do here.
463 * Lays out the specified container according to the constraints in this
466 * @param target The container to lay out.
468 public void layoutContainer(Container target
)
470 synchronized (target
.getTreeLock())
472 Insets i
= target
.getInsets();
474 int bottom
= target
.height
- i
.bottom
;
476 int right
= target
.width
- i
.right
;
478 boolean left_to_right
= target
.getComponentOrientation().isLeftToRight();
480 Component my_north
= north
;
481 Component my_east
= east
;
482 Component my_south
= south
;
483 Component my_west
= west
;
485 // Note that we currently don't handle vertical layouts.
486 // Neither does JDK 1.3.
487 if (firstLine
!= null)
488 my_north
= firstLine
;
489 if (lastLine
!= null)
491 if (firstItem
!= null)
498 if (lastItem
!= null)
506 if (my_north
!= null)
508 Dimension n
= calcCompSize(my_north
, PREF
);
509 my_north
.setBounds(left
, top
, right
- left
, n
.height
);
510 top
+= n
.height
+ vgap
;
513 if (my_south
!= null)
515 Dimension s
= calcCompSize(my_south
, PREF
);
516 my_south
.setBounds(left
, bottom
- s
.height
, right
- left
, s
.height
);
517 bottom
-= s
.height
+ vgap
;
522 Dimension e
= calcCompSize(my_east
, PREF
);
523 my_east
.setBounds(right
- e
.width
, top
, e
.width
, bottom
- top
);
524 right
-= e
.width
+ hgap
;
529 Dimension w
= calcCompSize(my_west
, PREF
);
530 my_west
.setBounds(left
, top
, w
.width
, bottom
- top
);
531 left
+= w
.width
+ hgap
;
535 center
.setBounds(left
, top
, right
- left
, bottom
- top
);
540 * Returns a string representation of this layout manager.
542 * @return A string representation of this object.
544 public String
toString()
546 return getClass().getName() + "[hgap=" + hgap
+ ",vgap=" + vgap
+ "]";
549 private Dimension
calcCompSize(Component comp
, int what
)
551 if (comp
== null || ! comp
.isVisible())
552 return new Dimension(0, 0);
554 return comp
.getMinimumSize();
555 else if (what
== MAX
)
556 return comp
.getMaximumSize();
557 return comp
.getPreferredSize();
561 * This is a helper function used to compute the various sizes for this
564 private Dimension
calcSize(Container target
, int what
)
566 synchronized (target
.getTreeLock())
568 Insets ins
= target
.getInsets();
570 ComponentOrientation orient
= target
.getComponentOrientation ();
571 boolean left_to_right
= orient
.isLeftToRight ();
573 Component my_north
= north
;
574 Component my_east
= east
;
575 Component my_south
= south
;
576 Component my_west
= west
;
578 // Note that we currently don't handle vertical layouts. Neither
580 if (firstLine
!= null)
581 my_north
= firstLine
;
582 if (lastLine
!= null)
584 if (firstItem
!= null)
591 if (lastItem
!= null)
599 Dimension ndim
= calcCompSize(my_north
, what
);
600 Dimension sdim
= calcCompSize(my_south
, what
);
601 Dimension edim
= calcCompSize(my_east
, what
);
602 Dimension wdim
= calcCompSize(my_west
, what
);
603 Dimension cdim
= calcCompSize(center
, what
);
605 int width
= edim
.width
+ cdim
.width
+ wdim
.width
+ (hgap
* 2);
606 // Check for overflow.
607 if (width
< edim
.width
|| width
< cdim
.width
|| width
< cdim
.width
)
608 width
= Integer
.MAX_VALUE
;
610 if (ndim
.width
> width
)
612 if (sdim
.width
> width
)
615 width
+= (ins
.left
+ ins
.right
);
617 int height
= edim
.height
;
618 if (cdim
.height
> height
)
619 height
= cdim
.height
;
620 if (wdim
.height
> height
)
621 height
= wdim
.height
;
623 int addedHeight
= height
+ (ndim
.height
+ sdim
.height
+ (vgap
* 2)
624 + ins
.top
+ ins
.bottom
);
625 // Check for overflow.
626 if (addedHeight
< height
)
627 height
= Integer
.MAX_VALUE
;
629 height
= addedHeight
;
631 return(new Dimension(width
, height
));
636 * Return the component at the indicated location, or null if no component
637 * is at that location. The constraints argument must be one of the
638 * location constants specified by this class.
639 * @param constraints the location
640 * @return the component at that location, or null
641 * @throws IllegalArgumentException if the constraints argument is not
645 public Component
getLayoutComponent(Object constraints
)
647 if (constraints
== CENTER
)
649 if (constraints
== NORTH
)
651 if (constraints
== EAST
)
653 if (constraints
== SOUTH
)
655 if (constraints
== WEST
)
657 if (constraints
== PAGE_START
)
659 if (constraints
== PAGE_END
)
661 if (constraints
== LINE_START
)
663 if (constraints
== LINE_END
)
665 throw new IllegalArgumentException("constraint " + constraints
666 + " is not recognized");
670 * Return the component at the specified location, which must be one
671 * of the absolute constants such as CENTER or SOUTH. The container's
672 * orientation is used to map this location to the correct corresponding
673 * component, so for instance in a right-to-left container, a request
674 * for the EAST component could return the LINE_END component. This will
675 * return null if no component is available at the given location.
676 * @param container the container whose orientation is used
677 * @param constraints the absolute location of the component
678 * @return the component at the location, or null
679 * @throws IllegalArgumentException if the constraint is not recognized
681 public Component
getLayoutComponent(Container container
, Object constraints
)
683 ComponentOrientation orient
= container
.getComponentOrientation();
684 if (constraints
== CENTER
)
686 // Note that we don't support vertical layouts.
687 if (constraints
== NORTH
)
689 if (constraints
== SOUTH
)
691 if (constraints
== WEST
)
693 // Note that relative layout takes precedence.
694 if (orient
.isLeftToRight())
695 return firstItem
== null ? west
: firstItem
;
696 return lastItem
== null ? west
: lastItem
;
698 if (constraints
== EAST
)
700 // Note that relative layout takes precedence.
701 if (orient
.isLeftToRight())
702 return lastItem
== null ? east
: lastItem
;
703 return firstItem
== null ? east
: firstItem
;
705 throw new IllegalArgumentException("constraint " + constraints
706 + " is not recognized");
710 * Return the constraint corresponding to a component in this layout.
711 * If the component is null, or is not in this layout, returns null.
712 * Otherwise, this will return one of the constraint constants defined
714 * @param c the component
715 * @return the constraint, or null
718 public Object
getConstraints(Component c
)