Merge from mainline.
[official-gcc.git] / libjava / classpath / java / awt / BorderLayout.java
blob50061ec677142cb5bca03b85dca5e439e4ae6151
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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. */
39 package java.awt;
42 /**
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
52 /**
53 * Constant indicating the top of the container
55 public static final String NORTH = "North";
57 /**
58 * Constant indicating the bottom of the container
60 public static final String SOUTH = "South";
62 /**
63 * Constant indicating the right side of the container
65 public static final String EAST = "East";
67 /**
68 * Constant indicating the left side of the container
70 public static final String WEST = "West";
72 /**
73 * Constant indicating the center of the container
75 public static final String CENTER = "Center";
78 /**
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.
87 * @since 1.2
89 public static final String BEFORE_FIRST_LINE = "First";
91 /**
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.
100 * @since 1.2
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.
113 * @since 1.2
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.
126 * @since 1.2
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}.
136 * @since 1.4
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}.
146 * @since 1.4
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}.
156 * @since 1.4
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}.
166 * @since 1.4
168 public static final String LINE_END = AFTER_LINE_ENDS;
172 * Serialization constant.
174 private static final long serialVersionUID = -8658291919501921765L;
178 * @serial
180 private Component north;
183 * @serial
185 private Component south;
188 * @serial
190 private Component east;
193 * @serial
195 private Component west;
198 * @serial
200 private Component center;
203 * @serial
205 private Component firstLine;
208 * @serial
210 private Component lastLine;
213 * @serial
215 private Component firstItem;
218 * @serial
220 private Component lastItem;
223 * @serial The horizontal gap between components
225 private int hgap;
228 * @serial The vertical gap between components
230 private int vgap;
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()
245 this(0,0);
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)
257 this.hgap = hgap;
258 this.vgap = vgap;
262 * Returns the horitzontal gap value.
264 * @return The horitzontal gap value.
266 public int getHgap()
268 return(hgap);
272 * Sets the horizontal gap to the specified value.
274 * @param hgap The new horizontal gap.
276 public void setHgap(int hgap)
278 this.hgap = hgap;
282 * Returns the vertical gap value.
284 * @return The vertical gap value.
286 public int getVgap()
288 return(vgap);
292 * Sets the vertical gap to the specified value.
294 * @param vgap The new vertical gap value.
296 public void setVgap(int vgap)
298 this.vgap = 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))
337 center = component;
338 else if (str.equals(NORTH))
339 north = component;
340 else if (str.equals(SOUTH))
341 south = component;
342 else if (str.equals(EAST))
343 east = component;
344 else if (str.equals(WEST))
345 west = component;
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;
354 else
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)
366 north = null;
367 if (south == component)
368 south = null;
369 if (east == component)
370 east = null;
371 if (west == component)
372 west = null;
373 if (center == component)
374 center = null;
375 if (firstItem == component)
376 firstItem = null;
377 if (lastItem == component)
378 lastItem = null;
379 if (firstLine == component)
380 firstLine = null;
381 if (lastLine == component)
382 lastLine = null;
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)
433 return 0.5F;
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)
448 return 0.5F;
452 * Instructs this object to discard any layout information it might
453 * have cached.
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
464 * object.
466 * @param target The container to lay out.
468 public void layoutContainer(Container target)
470 synchronized (target.getTreeLock())
472 Insets i = target.getInsets();
473 int top = i.top;
474 int bottom = target.height - i.bottom;
475 int left = i.left;
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)
490 my_south = lastLine;
491 if (firstItem != null)
493 if (left_to_right)
494 my_west = firstItem;
495 else
496 my_east = firstItem;
498 if (lastItem != null)
500 if (left_to_right)
501 my_east = lastItem;
502 else
503 my_west = lastItem;
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;
520 if (my_east != null)
522 Dimension e = calcCompSize(my_east, PREF);
523 my_east.setBounds(right - e.width, top, e.width, bottom - top);
524 right -= e.width + hgap;
527 if (my_west != null)
529 Dimension w = calcCompSize(my_west, PREF);
530 my_west.setBounds(left, top, w.width, bottom - top);
531 left += w.width + hgap;
534 if (center != null)
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);
553 if (what == MIN)
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
562 * layout.
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
579 // does JDK 1.3.
580 if (firstLine != null)
581 my_north = firstLine;
582 if (lastLine != null)
583 my_south = lastLine;
584 if (firstItem != null)
586 if (left_to_right)
587 my_west = firstItem;
588 else
589 my_east = firstItem;
591 if (lastItem != null)
593 if (left_to_right)
594 my_east = lastItem;
595 else
596 my_west = lastItem;
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)
611 width = ndim.width;
612 if (sdim.width > width)
613 width = sdim.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;
628 else
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
642 * recognized
643 * @since 1.5
645 public Component getLayoutComponent(Object constraints)
647 if (constraints == CENTER)
648 return center;
649 if (constraints == NORTH)
650 return north;
651 if (constraints == EAST)
652 return east;
653 if (constraints == SOUTH)
654 return south;
655 if (constraints == WEST)
656 return west;
657 if (constraints == PAGE_START)
658 return firstLine;
659 if (constraints == PAGE_END)
660 return lastLine;
661 if (constraints == LINE_START)
662 return firstItem;
663 if (constraints == LINE_END)
664 return lastItem;
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)
685 return center;
686 // Note that we don't support vertical layouts.
687 if (constraints == NORTH)
688 return north;
689 if (constraints == SOUTH)
690 return 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
713 * in this class.
714 * @param c the component
715 * @return the constraint, or null
716 * @since 1.5
718 public Object getConstraints(Component c)
720 if (c == null)
721 return null;
722 if (c == center)
723 return CENTER;
724 if (c == north)
725 return NORTH;
726 if (c == east)
727 return EAST;
728 if (c == south)
729 return SOUTH;
730 if (c == west)
731 return WEST;
732 if (c == firstLine)
733 return PAGE_START;
734 if (c == lastLine)
735 return PAGE_END;
736 if (c == firstItem)
737 return LINE_START;
738 if (c == lastItem)
739 return LINE_END;
740 return null;