home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / BorderLayout.java < prev    next >
Text File  |  1997-10-01  |  16KB  |  468 lines

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