home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / awt / BorderLayout.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  4.1 KB  |  174 lines

  1. package java.awt;
  2.  
  3. public class BorderLayout implements LayoutManager {
  4.    int hgap;
  5.    int vgap;
  6.    Component north;
  7.    Component west;
  8.    Component east;
  9.    Component south;
  10.    Component center;
  11.  
  12.    public BorderLayout() {
  13.    }
  14.  
  15.    public BorderLayout(int hgap, int vgap) {
  16.       this.hgap = hgap;
  17.       this.vgap = vgap;
  18.    }
  19.  
  20.    public void addLayoutComponent(String name, Component comp) {
  21.       if ("Center".equals(name)) {
  22.          this.center = comp;
  23.       } else if ("North".equals(name)) {
  24.          this.north = comp;
  25.       } else if ("South".equals(name)) {
  26.          this.south = comp;
  27.       } else if ("East".equals(name)) {
  28.          this.east = comp;
  29.       } else {
  30.          if ("West".equals(name)) {
  31.             this.west = comp;
  32.          }
  33.  
  34.       }
  35.    }
  36.  
  37.    public void removeLayoutComponent(Component comp) {
  38.       if (comp == this.center) {
  39.          this.center = null;
  40.       } else if (comp == this.north) {
  41.          this.north = null;
  42.       } else if (comp == this.south) {
  43.          this.south = null;
  44.       } else if (comp == this.east) {
  45.          this.east = null;
  46.       } else {
  47.          if (comp == this.west) {
  48.             this.west = null;
  49.          }
  50.  
  51.       }
  52.    }
  53.  
  54.    public Dimension minimumLayoutSize(Container target) {
  55.       Dimension dim = new Dimension(0, 0);
  56.       if (this.east != null && this.east.visible) {
  57.          Dimension d = this.east.minimumSize();
  58.          dim.width += d.width + this.hgap;
  59.          dim.height = Math.max(d.height, dim.height);
  60.       }
  61.  
  62.       if (this.west != null && this.west.visible) {
  63.          Dimension d = this.west.minimumSize();
  64.          dim.width += d.width + this.hgap;
  65.          dim.height = Math.max(d.height, dim.height);
  66.       }
  67.  
  68.       if (this.center != null && this.center.visible) {
  69.          Dimension d = this.center.minimumSize();
  70.          dim.width += d.width;
  71.          dim.height = Math.max(d.height, dim.height);
  72.       }
  73.  
  74.       if (this.north != null && this.north.visible) {
  75.          Dimension d = this.north.minimumSize();
  76.          dim.width = Math.max(d.width, dim.width);
  77.          dim.height += d.height + this.vgap;
  78.       }
  79.  
  80.       if (this.south != null && this.south.visible) {
  81.          Dimension d = this.south.minimumSize();
  82.          dim.width = Math.max(d.width, dim.width);
  83.          dim.height += d.height + this.vgap;
  84.       }
  85.  
  86.       Insets insets = target.insets();
  87.       dim.width += insets.left + insets.right;
  88.       dim.height += insets.top + insets.bottom;
  89.       return dim;
  90.    }
  91.  
  92.    public Dimension preferredLayoutSize(Container target) {
  93.       Dimension dim = new Dimension(0, 0);
  94.       if (this.east != null && this.east.visible) {
  95.          Dimension d = this.east.preferredSize();
  96.          dim.width += d.width + this.hgap;
  97.          dim.height = Math.max(d.height, dim.height);
  98.       }
  99.  
  100.       if (this.west != null && this.west.visible) {
  101.          Dimension d = this.west.preferredSize();
  102.          dim.width += d.width + this.hgap;
  103.          dim.height = Math.max(d.height, dim.height);
  104.       }
  105.  
  106.       if (this.center != null && this.center.visible) {
  107.          Dimension d = this.center.preferredSize();
  108.          dim.width += d.width;
  109.          dim.height = Math.max(d.height, dim.height);
  110.       }
  111.  
  112.       if (this.north != null && this.north.visible) {
  113.          Dimension d = this.north.preferredSize();
  114.          dim.width = Math.max(d.width, dim.width);
  115.          dim.height += d.height + this.vgap;
  116.       }
  117.  
  118.       if (this.south != null && this.south.visible) {
  119.          Dimension d = this.south.preferredSize();
  120.          dim.width = Math.max(d.width, dim.width);
  121.          dim.height += d.height + this.vgap;
  122.       }
  123.  
  124.       Insets insets = target.insets();
  125.       dim.width += insets.left + insets.right;
  126.       dim.height += insets.top + insets.bottom;
  127.       return dim;
  128.    }
  129.  
  130.    public void layoutContainer(Container target) {
  131.       Insets insets = target.insets();
  132.       int top = insets.top;
  133.       int bottom = target.height - insets.bottom;
  134.       int left = insets.left;
  135.       int right = target.width - insets.right;
  136.       if (this.north != null && this.north.visible) {
  137.          this.north.resize(right - left, this.north.height);
  138.          Dimension d = this.north.preferredSize();
  139.          this.north.reshape(left, top, right - left, d.height);
  140.          top += d.height + this.vgap;
  141.       }
  142.  
  143.       if (this.south != null && this.south.visible) {
  144.          this.south.resize(right - left, this.south.height);
  145.          Dimension d = this.south.preferredSize();
  146.          this.south.reshape(left, bottom - d.height, right - left, d.height);
  147.          bottom -= d.height + this.vgap;
  148.       }
  149.  
  150.       if (this.east != null && this.east.visible) {
  151.          this.east.resize(this.east.width, bottom - top);
  152.          Dimension d = this.east.preferredSize();
  153.          this.east.reshape(right - d.width, top, d.width, bottom - top);
  154.          right -= d.width + this.hgap;
  155.       }
  156.  
  157.       if (this.west != null && this.west.visible) {
  158.          this.west.resize(this.west.width, bottom - top);
  159.          Dimension d = this.west.preferredSize();
  160.          this.west.reshape(left, top, d.width, bottom - top);
  161.          left += d.width + this.hgap;
  162.       }
  163.  
  164.       if (this.center != null && this.center.visible) {
  165.          this.center.reshape(left, top, right - left, bottom - top);
  166.       }
  167.  
  168.    }
  169.  
  170.    public String toString() {
  171.       return this.getClass().getName() + "[hgap=" + this.hgap + ",vgap=" + this.vgap + "]";
  172.    }
  173. }
  174.