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

  1. package java.awt;
  2.  
  3. import java.util.Enumeration;
  4. import java.util.Hashtable;
  5.  
  6. public class CardLayout implements LayoutManager {
  7.    Hashtable tab;
  8.    int hgap;
  9.    int vgap;
  10.  
  11.    public CardLayout() {
  12.       this(0, 0);
  13.    }
  14.  
  15.    public CardLayout(int hgap, int vgap) {
  16.       this.tab = new Hashtable();
  17.       this.hgap = hgap;
  18.       this.vgap = vgap;
  19.    }
  20.  
  21.    public void addLayoutComponent(String name, Component comp) {
  22.       if (this.tab.size() > 0) {
  23.          comp.hide();
  24.       }
  25.  
  26.       this.tab.put(name, comp);
  27.    }
  28.  
  29.    public void removeLayoutComponent(Component comp) {
  30.       Enumeration e = this.tab.keys();
  31.  
  32.       while(e.hasMoreElements()) {
  33.          String key = (String)e.nextElement();
  34.          if (this.tab.get(key) == comp) {
  35.             this.tab.remove(key);
  36.             return;
  37.          }
  38.       }
  39.  
  40.    }
  41.  
  42.    public Dimension preferredLayoutSize(Container parent) {
  43.       Insets insets = parent.insets();
  44.       int ncomponents = parent.countComponents();
  45.       int w = 0;
  46.       int h = 0;
  47.  
  48.       for(int i = 0; i < ncomponents; ++i) {
  49.          Component comp = parent.getComponent(i);
  50.          Dimension d = comp.preferredSize();
  51.          if (d.width > w) {
  52.             w = d.width;
  53.          }
  54.  
  55.          if (d.height > h) {
  56.             h = d.height;
  57.          }
  58.       }
  59.  
  60.       return new Dimension(insets.left + insets.right + w + this.hgap * 2, insets.top + insets.bottom + h + this.vgap * 2);
  61.    }
  62.  
  63.    public Dimension minimumLayoutSize(Container parent) {
  64.       Insets insets = parent.insets();
  65.       int ncomponents = parent.countComponents();
  66.       int w = 0;
  67.       int h = 0;
  68.  
  69.       for(int i = 0; i < ncomponents; ++i) {
  70.          Component comp = parent.getComponent(i);
  71.          Dimension d = comp.minimumSize();
  72.          if (d.width > w) {
  73.             w = d.width;
  74.          }
  75.  
  76.          if (d.height > h) {
  77.             h = d.height;
  78.          }
  79.       }
  80.  
  81.       return new Dimension(insets.left + insets.right + w + this.hgap * 2, insets.top + insets.bottom + h + this.vgap * 2);
  82.    }
  83.  
  84.    public void layoutContainer(Container parent) {
  85.       synchronized(parent){}
  86.  
  87.       try {
  88.          Insets insets = parent.insets();
  89.          int ncomponents = parent.countComponents();
  90.  
  91.          for(int i = 0; i < ncomponents; ++i) {
  92.             Component comp = parent.getComponent(i);
  93.             if (comp.visible) {
  94.                comp.reshape(this.hgap + insets.left, this.vgap + insets.top, parent.width - (this.hgap * 2 + insets.left + insets.right), parent.height - (this.vgap * 2 + insets.top + insets.bottom));
  95.             }
  96.          }
  97.       } catch (Throwable var9) {
  98.          throw var9;
  99.       }
  100.  
  101.    }
  102.  
  103.    void checkLayout(Container parent) {
  104.       if (parent.getLayout() != this) {
  105.          throw new IllegalArgumentException("wrong parent for CardLayout");
  106.       }
  107.    }
  108.  
  109.    public void first(Container parent) {
  110.       synchronized(parent){}
  111.  
  112.       try {
  113.          this.checkLayout(parent);
  114.          int ncomponents = parent.countComponents();
  115.  
  116.          for(int i = 0; i < ncomponents; ++i) {
  117.             Component comp = parent.getComponent(i);
  118.             if (comp.visible) {
  119.                comp.hide();
  120.                comp = parent.getComponent(0);
  121.                comp.show();
  122.                parent.validate();
  123.                return;
  124.             }
  125.          }
  126.  
  127.       } catch (Throwable var8) {
  128.          throw var8;
  129.       }
  130.    }
  131.  
  132.    public void next(Container parent) {
  133.       synchronized(parent){}
  134.  
  135.       try {
  136.          this.checkLayout(parent);
  137.          int ncomponents = parent.countComponents();
  138.  
  139.          for(int i = 0; i < ncomponents; ++i) {
  140.             Component comp = parent.getComponent(i);
  141.             if (comp.visible) {
  142.                comp.hide();
  143.                comp = parent.getComponent(i + 1 < ncomponents ? i + 1 : 0);
  144.                comp.show();
  145.                parent.validate();
  146.                return;
  147.             }
  148.          }
  149.  
  150.       } catch (Throwable var8) {
  151.          throw var8;
  152.       }
  153.    }
  154.  
  155.    public void previous(Container parent) {
  156.       synchronized(parent){}
  157.  
  158.       try {
  159.          this.checkLayout(parent);
  160.          int ncomponents = parent.countComponents();
  161.  
  162.          for(int i = 0; i < ncomponents; ++i) {
  163.             Component comp = parent.getComponent(i);
  164.             if (comp.visible) {
  165.                comp.hide();
  166.                comp = parent.getComponent(i > 0 ? i - 1 : ncomponents - 1);
  167.                comp.show();
  168.                parent.validate();
  169.                return;
  170.             }
  171.          }
  172.  
  173.       } catch (Throwable var8) {
  174.          throw var8;
  175.       }
  176.    }
  177.  
  178.    public void last(Container parent) {
  179.       synchronized(parent){}
  180.  
  181.       try {
  182.          this.checkLayout(parent);
  183.          int ncomponents = parent.countComponents();
  184.  
  185.          for(int i = 0; i < ncomponents; ++i) {
  186.             Component comp = parent.getComponent(i);
  187.             if (comp.visible) {
  188.                comp.hide();
  189.                comp = parent.getComponent(ncomponents - 1);
  190.                comp.show();
  191.                parent.validate();
  192.                return;
  193.             }
  194.          }
  195.  
  196.       } catch (Throwable var8) {
  197.          throw var8;
  198.       }
  199.    }
  200.  
  201.    public void show(Container parent, String name) {
  202.       synchronized(parent){}
  203.  
  204.       try {
  205.          this.checkLayout(parent);
  206.          Component next = (Component)this.tab.get(name);
  207.          if (next != null && !next.visible) {
  208.             int ncomponents = parent.countComponents();
  209.  
  210.             for(int i = 0; i < ncomponents; ++i) {
  211.                Component comp = parent.getComponent(i);
  212.                if (comp.visible) {
  213.                   comp.hide();
  214.                   break;
  215.                }
  216.             }
  217.  
  218.             next.show();
  219.             parent.validate();
  220.          }
  221.       } catch (Throwable var10) {
  222.          throw var10;
  223.       }
  224.  
  225.    }
  226.  
  227.    public String toString() {
  228.       return this.getClass().getName() + "[hgap=" + this.hgap + ",vgap=" + this.vgap + "]";
  229.    }
  230. }
  231.