home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / BorderLayout.java < prev    next >
Text File  |  1996-05-03  |  7KB  |  236 lines

  1. /*
  2.  * @(#)BorderLayout.java    1.17 95/09/08 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt;
  21.  
  22. import java.util.Hashtable;
  23.  
  24. /**
  25.  * A TNT style border bag layout. It will layout a container
  26.  * using members named "North", "South", "East", "West" and
  27.  * "Center".
  28.  *
  29.  * The "North", "South", "East" and "West" components get layed out
  30.  * according to their preferred sizes and the constraints of the
  31.  * container's size. The "Center" component will get any space left
  32.  * over. 
  33.  * 
  34.  * @version     1.17 08 Sep 1995
  35.  * @author     Arthur van Hoff
  36.  */
  37. public class BorderLayout implements LayoutManager {
  38.     int hgap;
  39.     int vgap;
  40.  
  41.     Component north;
  42.     Component west;
  43.     Component east;
  44.     Component south;
  45.     Component center;
  46.  
  47.     /**
  48.      * Constructs a new BorderLayout.
  49.      */
  50.     public BorderLayout() {
  51.     }
  52.  
  53.     /**
  54.      * Constructs a BorderLayout with the specified gaps.
  55.      * @param hgap the horizontal gap
  56.      * @param vgap the vertical gap
  57.      */
  58.     public BorderLayout(int hgap, int vgap) {
  59.     this.hgap = hgap;
  60.     this.vgap = vgap;
  61.     }
  62.  
  63.     /**
  64.      * Adds the specified named component to the layout.
  65.      * @param name the String name
  66.      * @param comp the component to be added
  67.      */
  68.     public void addLayoutComponent(String name, Component comp) {
  69.     if ("Center".equals(name)) {
  70.         center = comp;
  71.     } else if ("North".equals(name)) {
  72.         north = comp;
  73.     } else if ("South".equals(name)) {
  74.         south = comp;
  75.     } else if ("East".equals(name)) {
  76.         east = comp;
  77.     } else if ("West".equals(name)) {
  78.         west = comp;
  79.     }
  80.     }
  81.  
  82.     /**
  83.      * Removes the specified component from the layout.
  84.      * @param comp the component to be removed
  85.      */
  86.     public void removeLayoutComponent(Component comp) {
  87.     if (comp == center) {
  88.         center = null;
  89.     } else if (comp == north) {
  90.         north = null;
  91.     } else if (comp == south) {
  92.         south = null;
  93.     } else if (comp == east) {
  94.         east = null;
  95.     } else if (comp == west) {
  96.         west = null;
  97.     }
  98.     }
  99.  
  100.     /**
  101.      * Returns the minimum dimensions needed to layout the components
  102.      * contained in the specified target container. 
  103.      * @param target the Container on which to do the layout
  104.      * @see Container
  105.      * @see #preferredLayoutSize
  106.      */
  107.     public Dimension minimumLayoutSize(Container target) {
  108.     Dimension dim = new Dimension(0, 0);
  109.  
  110.     if ((east != null) && east.visible) {
  111.         Dimension d = east.minimumSize();
  112.         dim.width += d.width + hgap;
  113.         dim.height = Math.max(d.height, dim.height);
  114.     }
  115.     if ((west != null) && west.visible) {
  116.         Dimension d = west.minimumSize();
  117.         dim.width += d.width + hgap;
  118.         dim.height = Math.max(d.height, dim.height);
  119.     }
  120.     if ((center != null) && center.visible) {
  121.         Dimension d = center.minimumSize();
  122.         dim.width += d.width;
  123.         dim.height = Math.max(d.height, dim.height);
  124.     }
  125.     if ((north != null) && north.visible) {
  126.         Dimension d = north.minimumSize();
  127.         dim.width = Math.max(d.width, dim.width);
  128.         dim.height += d.height + vgap;
  129.     }
  130.     if ((south != null) && south.visible) {
  131.         Dimension d = south.minimumSize();
  132.         dim.width = Math.max(d.width, dim.width);
  133.         dim.height += d.height + vgap;
  134.     }
  135.  
  136.     Insets insets = target.insets();
  137.     dim.width += insets.left + insets.right;
  138.     dim.height += insets.top + insets.bottom;
  139.  
  140.     return dim;
  141.     }
  142.     
  143.     /**
  144.      * Returns the preferred dimensions for this layout given the components
  145.      * in the specified target container.
  146.      * @param target the component which needs to be laid out
  147.      * @see Container
  148.      * @see #minimumLayoutSize
  149.      */
  150.     public Dimension preferredLayoutSize(Container target) {
  151.     Dimension dim = new Dimension(0, 0);
  152.  
  153.     if ((east != null) && east.visible) {
  154.         Dimension d = east.preferredSize();
  155.         dim.width += d.width + hgap;
  156.         dim.height = Math.max(d.height, dim.height);
  157.     }
  158.     if ((west != null) && west.visible) {
  159.         Dimension d = west.preferredSize();
  160.         dim.width += d.width + hgap;
  161.         dim.height = Math.max(d.height, dim.height);
  162.     }
  163.     if ((center != null) && center.visible) {
  164.         Dimension d = center.preferredSize();
  165.         dim.width += d.width;
  166.         dim.height = Math.max(d.height, dim.height);
  167.     }
  168.     if ((north != null) && north.visible) {
  169.         Dimension d = north.preferredSize();
  170.         dim.width = Math.max(d.width, dim.width);
  171.         dim.height += d.height + vgap;
  172.     }
  173.     if ((south != null) && south.visible) {
  174.         Dimension d = south.preferredSize();
  175.         dim.width = Math.max(d.width, dim.width);
  176.         dim.height += d.height + vgap;
  177.     }
  178.  
  179.     Insets insets = target.insets();
  180.     dim.width += insets.left + insets.right;
  181.     dim.height += insets.top + insets.bottom;
  182.  
  183.     return dim;
  184.     }
  185.  
  186.     /**
  187.      * Lays out the specified container. This method will actually reshape the
  188.      * components in the specified target container in order to satisfy the 
  189.      * constraints of the BorderLayout object. 
  190.      * @param target the component being laid out
  191.      * @see Container
  192.      */
  193.     public void layoutContainer(Container target) {
  194.     Insets insets = target.insets();
  195.     int top = insets.top;
  196.     int bottom = target.height - insets.bottom;
  197.     int left = insets.left;
  198.     int right = target.width - insets.right;
  199.  
  200.     if ((north != null) && north.visible) {
  201.         north.resize(right - left, north.height);
  202.         Dimension d = north.preferredSize();
  203.         north.reshape(left, top, right - left, d.height);
  204.         top += d.height + vgap;
  205.     }
  206.     if ((south != null) && south.visible) {
  207.         south.resize(right - left, south.height);
  208.         Dimension d = south.preferredSize();
  209.         south.reshape(left, bottom - d.height, right - left, d.height);
  210.         bottom -= d.height + vgap;
  211.     }
  212.     if ((east != null) && east.visible) {
  213.         east.resize(east.width, bottom - top);
  214.         Dimension d = east.preferredSize();
  215.         east.reshape(right - d.width, top, d.width, bottom - top);
  216.         right -= d.width + hgap;
  217.     }
  218.     if ((west != null) && west.visible) {
  219.         west.resize(west.width, bottom - top);
  220.         Dimension d = west.preferredSize();
  221.         west.reshape(left, top, d.width, bottom - top);
  222.         left += d.width + hgap;
  223.     }
  224.     if ((center != null) && center.visible) {
  225.         center.reshape(left, top, right - left, bottom - top);
  226.     }
  227.     }
  228.     
  229.     /**
  230.      * Returns the String representation of this BorderLayout's values.
  231.      */
  232.     public String toString() {
  233.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
  234.     }
  235. }
  236.