home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / BorderLayout.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  14.8 KB  |  456 lines

  1. /*
  2.  * @(#)BorderLayout.java    1.33 98/03/18
  3.  *
  4.  * Copyright 1995-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. import java.util.Hashtable;
  18.  
  19. /**
  20.  * A border layout lays out a container, arranging and resizing
  21.  * its components to fit in five regions:
  22.  * north, south, east, west, and center.
  23.  * Each region is identified by a corresponding constant:
  24.  * <code>NORTH</code>, <code>SOUTH</code>, <code>EAST</code>, 
  25.  * <code>WEST</code>, and <code>CENTER</code>.  When adding a 
  26.  * component to a container with a border layout, use one of these
  27.  * five constants, for example:
  28.  * <pre>
  29.  *    Panel p = new Panel();
  30.  *    p.setLayout(new BorderLayout());
  31.  *    p.add(new Button("Okay"), BorderLayout.SOUTH);
  32.  * </pre>
  33.  * As a convenience, BorderLayout interprets the absence of a string
  34.  * specification the same as the constant <code>CENTER</code>:
  35.  * <pre>
  36.  *    Panel p2 = new Panel();
  37.  *    p2.setLayout(new BorderLayout());
  38.  *    p2.add(new TextArea());  // Same as p.add(new TextArea(), BorderLayout.CENTER);
  39.  * </pre>
  40.  * <p>
  41.  * The components are laid out according to their 
  42.  * preferred sizes and the constraints of the container's size. 
  43.  * The <code>NORTH</code> and <code>SOUTH</code> components may 
  44.  * be stretched horizontally; the <code>EAST</code> and 
  45.  * <code>WEST</code> components may be stretched vertically; 
  46.  * the <code>CENTER</code> component may stretch both horizontally 
  47.  * and vertically to fill any space left over. 
  48.  * <p>
  49.  * Here is an example of five buttons in an applet laid out using 
  50.  * the <code>BorderLayout</code> layout manager:
  51.  * <p>
  52.  * <img src="images-awt/BorderLayout-1.gif"
  53.  * ALIGN=center HSPACE=10 VSPACE=7>
  54.  * <p>
  55.  * The code for this applet is as follows: 
  56.  * <p>
  57.  * <hr><blockquote><pre>
  58.  * import java.awt.*;
  59.  * import java.applet.Applet;
  60.  * 
  61.  * public class buttonDir extends Applet {
  62.  *   public void init() {
  63.  *     setLayout(new BorderLayout());
  64.  *     add(new Button("North"), BorderLayout.NORTH);
  65.  *     add(new Button("South"), BorderLayout.SOUTH);
  66.  *     add(new Button("East"), BorderLayout.EAST);
  67.  *     add(new Button("West"), BorderLayout.WEST);
  68.  *     add(new Button("Center"), BorderLayout.CENTER);
  69.  *   }
  70.  * }
  71.  * </pre></blockquote><hr>
  72.  * <p>
  73.  * @version     1.27 02/11/97
  74.  * @author     Arthur van Hoff
  75.  * @see         java.awt.Container.add(String, Component)
  76.  * @since       JDK1.0
  77.  */
  78. public class BorderLayout implements LayoutManager2,
  79.                      java.io.Serializable {
  80.     int hgap;
  81.     int vgap;
  82.  
  83.     Component north;
  84.     Component west;
  85.     Component east;
  86.     Component south;
  87.     Component center;
  88.  
  89.     /**
  90.      * The north layout constraint (top of container).
  91.      */
  92.     public static final String NORTH  = "North";
  93.  
  94.     /**
  95.      * The south layout constraint (bottom of container).
  96.      */
  97.     public static final String SOUTH  = "South";
  98.  
  99.     /**
  100.      * The east layout constraint (right side of container).
  101.      */
  102.     public static final String EAST   = "East";
  103.  
  104.     /**
  105.      * The west layout constraint (left side of container).
  106.      */
  107.     public static final String WEST   = "West";
  108.  
  109.     /**
  110.      * The center layout constraint (middle of container).
  111.      */
  112.     public static final String CENTER = "Center";
  113.  
  114.     /*
  115.      * JDK 1.1 serialVersionUID 
  116.      */
  117.      private static final long serialVersionUID = -8658291919501921765L;
  118.  
  119.     /**
  120.      * Constructs a new border layout with  
  121.      * no gaps between components.
  122.      */
  123.     public BorderLayout() {
  124.     this(0, 0);
  125.     }
  126.  
  127.     /**
  128.      * Constructs a border layout with the specified gaps 
  129.      * between components.
  130.      * The horizontal gap is specified by <code>hgap</code> 
  131.      * and the vertical gap is specified by <code>vgap</code>.
  132.      * @param   hgap   the horizontal gap.
  133.      * @param   vgap   the vertical gap.    
  134.      */    
  135.     public BorderLayout(int hgap, int vgap) {
  136.     this.hgap = hgap;
  137.     this.vgap = vgap;
  138.     }
  139.  
  140.     /**
  141.      * Returns the horizontal gap between components.
  142.      * @since   JDK1.1
  143.      */
  144.     public int getHgap() {
  145.     return hgap;
  146.     }
  147.     
  148.     /**
  149.      * Sets the horizontal gap between components.
  150.      * @param hgap the horizontal gap between components
  151.      * @since   JDK1.1
  152.      */
  153.     public void setHgap(int hgap) {
  154.     this.hgap = hgap;
  155.     }
  156.     
  157.     /**
  158.      * Returns the vertical gap between components.
  159.      * @since   JDK1.1
  160.      */
  161.     public int getVgap() {
  162.     return vgap;
  163.     }
  164.     
  165.     /**
  166.      * Sets the vertical gap between components.
  167.      * @param vgap the vertical gap between components
  168.      * @since   JDK1.1
  169.      */
  170.     public void setVgap(int vgap) {
  171.     this.vgap = vgap;
  172.     }
  173.  
  174.     /**
  175.      * Adds the specified component to the layout, using the specified
  176.      * constraint object.  For border layouts, the constraint must be
  177.      * one of the following constants:  <code>NORTH</code>,
  178.      * <code>SOUTH</code>, <code>EAST</code>,
  179.      * <code>WEST</code>, or <code>CENTER</code>.  
  180.      * <p>
  181.      * Most applications do not call this method directly. This method 
  182.      * is called when a component is added to a container using the 
  183.      * <code>Container.add</code> method with the same argument types.
  184.      * @param   comp         the component to be added.
  185.      * @param   constraints  an object that specifies how and where 
  186.      *                       the component is added to the layout.
  187.      * @see     java.awt.Container#add(java.awt.Component, java.lang.Object)
  188.      * @exception   IllegalArgumentException  if the constraint object is not
  189.      *                 a string, or if it not one of the five specified 
  190.      *              constants.
  191.      * @since   JDK1.1
  192.      */
  193.     public void addLayoutComponent(Component comp, Object constraints) {
  194.     if ((constraints == null) || (constraints instanceof String)) {
  195.         addLayoutComponent((String)constraints, comp);
  196.     } else {
  197.         throw new IllegalArgumentException("cannot add to layout: constraint must be a string (or null)");
  198.     }
  199.     }
  200.  
  201.     /**
  202.      * @deprecated  replaced by <code>addLayoutComponent(Component, Object)</code>.
  203.      */
  204.     public void addLayoutComponent(String name, Component comp) {
  205.  
  206.     /* Special case:  treat null the same as "Center". */
  207.     if (name == null) {
  208.         name = "Center";
  209.     }
  210.  
  211.     /* Assign the component to one of the known regions of the layout.
  212.      */
  213.     if ("Center".equals(name)) {
  214.         center = comp;
  215.     } else if ("North".equals(name)) {
  216.         north = comp;
  217.     } else if ("South".equals(name)) {
  218.         south = comp;
  219.     } else if ("East".equals(name)) {
  220.         east = comp;
  221.     } else if ("West".equals(name)) {
  222.         west = comp;
  223.     } else {
  224.         throw new IllegalArgumentException("cannot add to layout: unknown constraint: " + name);
  225.     }
  226.     }
  227.  
  228.     /**
  229.      * Removes the specified component from this border layout. This 
  230.      * method is called when a container calls its <code>remove</code> or 
  231.      * <code>removeAll</code> methods. Most applications do not call this 
  232.      * method directly. 
  233.      * @param   comp   the component to be removed.
  234.      * @see     java.awt.Container#remove(java.awt.Component)
  235.      * @see     java.awt.Container#removeAll()
  236.      */
  237.     public void removeLayoutComponent(Component comp) {
  238.     if (comp == center) {
  239.         center = null;
  240.     } else if (comp == north) {
  241.         north = null;
  242.     } else if (comp == south) {
  243.         south = null;
  244.     } else if (comp == east) {
  245.         east = null;
  246.     } else if (comp == west) {
  247.         west = null;
  248.     }
  249.     }
  250.  
  251.     /**
  252.      * Determines the minimum size of the <code>target</code> container 
  253.      * using this layout manager. 
  254.      * <p>
  255.      * This method is called when a container calls its 
  256.      * <code>getMinimumSize</code> method. Most applications do not call 
  257.      * this method directly. 
  258.      * @param   target   the container in which to do the layout.
  259.      * @return  the minimum dimensions needed to lay out the subcomponents 
  260.      *          of the specified container.
  261.      * @see     java.awt.Container  
  262.      * @see     java.awt.BorderLayout#preferredLayoutSize
  263.      * @see     java.awt.Container#getMinimumSize()
  264.      */
  265.     public Dimension minimumLayoutSize(Container target) {
  266.     Dimension dim = new Dimension(0, 0);
  267.  
  268.     if ((east != null) && east.visible) {
  269.         Dimension d = east.getMinimumSize();
  270.         dim.width += d.width + hgap;
  271.         dim.height = Math.max(d.height, dim.height);
  272.     }
  273.     if ((west != null) && west.visible) {
  274.         Dimension d = west.getMinimumSize();
  275.         dim.width += d.width + hgap;
  276.         dim.height = Math.max(d.height, dim.height);
  277.     }
  278.     if ((center != null) && center.visible) {
  279.         Dimension d = center.getMinimumSize();
  280.         dim.width += d.width;
  281.         dim.height = Math.max(d.height, dim.height);
  282.     }
  283.     if ((north != null) && north.visible) {
  284.         Dimension d = north.getMinimumSize();
  285.         dim.width = Math.max(d.width, dim.width);
  286.         dim.height += d.height + vgap;
  287.     }
  288.     if ((south != null) && south.visible) {
  289.         Dimension d = south.getMinimumSize();
  290.         dim.width = Math.max(d.width, dim.width);
  291.         dim.height += d.height + vgap;
  292.     }
  293.  
  294.     Insets insets = target.getInsets();
  295.     dim.width += insets.left + insets.right;
  296.     dim.height += insets.top + insets.bottom;
  297.  
  298.     return dim;
  299.     }
  300.     
  301.     /**
  302.      * Determines the preferred size of the <code>target</code> 
  303.      * container using this layout manager, based on the components
  304.      * in the container. 
  305.      * <p>
  306.      * Most applications do not call this method directly. This method
  307.      * is called when a container calls its <code>getPreferredSize</code> 
  308.      * method.
  309.      * @param   target   the container in which to do the layout.
  310.      * @return  the preferred dimensions to lay out the subcomponents 
  311.      *          of the specified container.
  312.      * @see     java.awt.Container  
  313.      * @see     java.awt.BorderLayout#minimumLayoutSize  
  314.      * @see     java.awt.Container#getPreferredSize()
  315.      */
  316.     public Dimension preferredLayoutSize(Container target) {
  317.     Dimension dim = new Dimension(0, 0);
  318.  
  319.     if ((east != null) && east.visible) {
  320.         Dimension d = east.getPreferredSize();
  321.         dim.width += d.width + hgap;
  322.         dim.height = Math.max(d.height, dim.height);
  323.     }
  324.     if ((west != null) && west.visible) {
  325.         Dimension d = west.getPreferredSize();
  326.         dim.width += d.width + hgap;
  327.         dim.height = Math.max(d.height, dim.height);
  328.     }
  329.     if ((center != null) && center.visible) {
  330.         Dimension d = center.getPreferredSize();
  331.         dim.width += d.width;
  332.         dim.height = Math.max(d.height, dim.height);
  333.     }
  334.     if ((north != null) && north.visible) {
  335.         Dimension d = north.getPreferredSize();
  336.         dim.width = Math.max(d.width, dim.width);
  337.         dim.height += d.height + vgap;
  338.     }
  339.     if ((south != null) && south.visible) {
  340.         Dimension d = south.getPreferredSize();
  341.         dim.width = Math.max(d.width, dim.width);
  342.         dim.height += d.height + vgap;
  343.     }
  344.  
  345.     Insets insets = target.getInsets();
  346.     dim.width += insets.left + insets.right;
  347.     dim.height += insets.top + insets.bottom;
  348.  
  349.     return dim;
  350.     }
  351.  
  352.     /**
  353.      * Returns the maximum dimensions for this layout given the components
  354.      * in the specified target container.
  355.      * @param target the component which needs to be laid out
  356.      * @see Container
  357.      * @see #minimumLayoutSize
  358.      * @see #preferredLayoutSize
  359.      */
  360.     public Dimension maximumLayoutSize(Container target) {
  361.     return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  362.     }
  363.  
  364.     /**
  365.      * Returns the alignment along the x axis.  This specifies how
  366.      * the component would like to be aligned relative to other 
  367.      * components.  The value should be a number between 0 and 1
  368.      * where 0 represents alignment along the origin, 1 is aligned
  369.      * the furthest away from the origin, 0.5 is centered, etc.
  370.      */
  371.     public float getLayoutAlignmentX(Container parent) {
  372.     return 0.5f;
  373.     }
  374.  
  375.     /**
  376.      * Returns the alignment along the y axis.  This specifies how
  377.      * the component would like to be aligned relative to other 
  378.      * components.  The value should be a number between 0 and 1
  379.      * where 0 represents alignment along the origin, 1 is aligned
  380.      * the furthest away from the origin, 0.5 is centered, etc.
  381.      */
  382.     public float getLayoutAlignmentY(Container parent) {
  383.     return 0.5f;
  384.     }
  385.  
  386.     /**
  387.      * Invalidates the layout, indicating that if the layout manager
  388.      * has cached information it should be discarded.
  389.      */
  390.     public void invalidateLayout(Container target) {
  391.     }
  392.                       
  393.     /**
  394.      * Lays out the container argument using this border layout. 
  395.      * <p>
  396.      * This method actually reshapes the components in the specified
  397.      * container in order to satisfy the constraints of this 
  398.      * <code>BorderLayout</code> object. The <code>NORTH</code> 
  399.      * and <code>SOUTH</code> components, if any, are placed at 
  400.      * the top and bottom of the container, respectively. The 
  401.      * <code>WEST</code> and <code>EAST</code> components are 
  402.      * then placed on the left and right, respectively. Finally, 
  403.      * the <code>CENTER</code> object is placed in any remaining 
  404.      * space in the middle. 
  405.      * <p>
  406.      * Most applications do not call this method directly. This method 
  407.      * is called when a container calls its <code>doLayout</code> method. 
  408.      * @param   target   the container in which to do the layout.
  409.      * @see     java.awt.Container  
  410.      * @see     java.awt.Container#doLayout()
  411.      */
  412.     public void layoutContainer(Container target) {
  413.     Insets insets = target.getInsets();
  414.     int top = insets.top;
  415.     int bottom = target.height - insets.bottom;
  416.     int left = insets.left;
  417.     int right = target.width - insets.right;
  418.  
  419.     if ((north != null) && north.visible) {
  420.         north.setSize(right - left, north.height);
  421.         Dimension d = north.getPreferredSize();
  422.         north.setBounds(left, top, right - left, d.height);
  423.         top += d.height + vgap;
  424.     }
  425.     if ((south != null) && south.visible) {
  426.         south.setSize(right - left, south.height);
  427.         Dimension d = south.getPreferredSize();
  428.         south.setBounds(left, bottom - d.height, right - left, d.height);
  429.         bottom -= d.height + vgap;
  430.     }
  431.     if ((east != null) && east.visible) {
  432.         east.setSize(east.width, bottom - top);
  433.         Dimension d = east.getPreferredSize();
  434.         east.setBounds(right - d.width, top, d.width, bottom - top);
  435.         right -= d.width + hgap;
  436.     }
  437.     if ((west != null) && west.visible) {
  438.         west.setSize(west.width, bottom - top);
  439.         Dimension d = west.getPreferredSize();
  440.         west.setBounds(left, top, d.width, bottom - top);
  441.         left += d.width + hgap;
  442.     }
  443.     if ((center != null) && center.visible) {
  444.         center.setBounds(left, top, right - left, bottom - top);
  445.     }
  446.     }
  447.     
  448.     /**
  449.      * Returns a string representation of the state of this border layout.
  450.      * @return    a string representation of this border layout.
  451.      */
  452.     public String toString() {
  453.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
  454.     }
  455. }
  456.