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

  1. package java.awt;
  2.  
  3. import java.awt.peer.ContainerPeer;
  4. import java.io.PrintStream;
  5.  
  6. public abstract class Container extends Component {
  7.    int ncomponents;
  8.    Component[] component = new Component[4];
  9.    LayoutManager layoutMgr;
  10.  
  11.    Container() {
  12.    }
  13.  
  14.    public int countComponents() {
  15.       return this.ncomponents;
  16.    }
  17.  
  18.    public synchronized Component getComponent(int n) {
  19.       if (n >= 0 && n < this.ncomponents) {
  20.          return this.component[n];
  21.       } else {
  22.          throw new ArrayIndexOutOfBoundsException("No such child: " + n);
  23.       }
  24.    }
  25.  
  26.    public synchronized Component[] getComponents() {
  27.       Component[] list = new Component[this.ncomponents];
  28.       System.arraycopy(this.component, 0, list, 0, this.ncomponents);
  29.       return list;
  30.    }
  31.  
  32.    public Insets insets() {
  33.       ContainerPeer peer = (ContainerPeer)super.peer;
  34.       return peer != null ? peer.insets() : new Insets(0, 0, 0, 0);
  35.    }
  36.  
  37.    public Component add(Component comp) {
  38.       return this.add(comp, -1);
  39.    }
  40.  
  41.    public synchronized Component add(Component comp, int pos) {
  42.       if (pos <= this.ncomponents && (pos >= 0 || pos == -1)) {
  43.          if (comp instanceof Container) {
  44.             for(Container cn = this; cn != null; cn = cn.parent) {
  45.                if (cn == comp) {
  46.                   throw new IllegalArgumentException("adding container's parent to itself");
  47.                }
  48.             }
  49.          }
  50.  
  51.          if (comp.parent != null) {
  52.             comp.parent.remove(comp);
  53.          }
  54.  
  55.          if (this.ncomponents == this.component.length) {
  56.             Component[] newcomponents = new Component[this.ncomponents * 2];
  57.             System.arraycopy(this.component, 0, newcomponents, 0, this.ncomponents);
  58.             this.component = newcomponents;
  59.          }
  60.  
  61.          if (pos != -1 && pos != this.ncomponents) {
  62.             System.arraycopy(this.component, pos, this.component, pos + 1, this.ncomponents - pos);
  63.             this.component[pos] = comp;
  64.             ++this.ncomponents;
  65.          } else {
  66.             this.component[this.ncomponents++] = comp;
  67.          }
  68.  
  69.          comp.parent = this;
  70.          ((Component)this).invalidate();
  71.          if (super.peer != null) {
  72.             comp.addNotify();
  73.          }
  74.  
  75.          return comp;
  76.       } else {
  77.          throw new IllegalArgumentException("illegal component position");
  78.       }
  79.    }
  80.  
  81.    public synchronized Component add(String name, Component comp) {
  82.       Component c = this.add(comp);
  83.       LayoutManager layoutMgr = this.layoutMgr;
  84.       if (layoutMgr != null) {
  85.          layoutMgr.addLayoutComponent(name, comp);
  86.       }
  87.  
  88.       return c;
  89.    }
  90.  
  91.    public synchronized void remove(Component comp) {
  92.       if (comp.parent == this) {
  93.          for(int i = 0; i < this.ncomponents; ++i) {
  94.             if (this.component[i] == comp) {
  95.                if (super.peer != null) {
  96.                   comp.removeNotify();
  97.                }
  98.  
  99.                if (this.layoutMgr != null) {
  100.                   this.layoutMgr.removeLayoutComponent(comp);
  101.                }
  102.  
  103.                comp.parent = null;
  104.                System.arraycopy(this.component, i + 1, this.component, i, this.ncomponents - i - 1);
  105.                this.component[--this.ncomponents] = null;
  106.                ((Component)this).invalidate();
  107.                return;
  108.             }
  109.          }
  110.       }
  111.  
  112.    }
  113.  
  114.    public synchronized void removeAll() {
  115.       Component comp;
  116.       for(; this.ncomponents > 0; comp.parent = null) {
  117.          comp = this.component[--this.ncomponents];
  118.          this.component[this.ncomponents] = null;
  119.          if (super.peer != null) {
  120.             comp.removeNotify();
  121.          }
  122.  
  123.          if (this.layoutMgr != null) {
  124.             this.layoutMgr.removeLayoutComponent(comp);
  125.          }
  126.       }
  127.  
  128.       ((Component)this).invalidate();
  129.    }
  130.  
  131.    public LayoutManager getLayout() {
  132.       return this.layoutMgr;
  133.    }
  134.  
  135.    public void setLayout(LayoutManager mgr) {
  136.       this.layoutMgr = mgr;
  137.       ((Component)this).invalidate();
  138.    }
  139.  
  140.    public synchronized void layout() {
  141.       LayoutManager layoutMgr = this.layoutMgr;
  142.       if (layoutMgr != null) {
  143.          layoutMgr.layoutContainer(this);
  144.       }
  145.  
  146.    }
  147.  
  148.    public synchronized void validate() {
  149.       super.validate();
  150.  
  151.       for(int i = 0; i < this.ncomponents; ++i) {
  152.          Component comp = this.component[i];
  153.          if (!comp.valid) {
  154.             comp.validate();
  155.          }
  156.       }
  157.  
  158.    }
  159.  
  160.    public synchronized Dimension preferredSize() {
  161.       LayoutManager layoutMgr = this.layoutMgr;
  162.       return layoutMgr != null ? layoutMgr.preferredLayoutSize(this) : super.preferredSize();
  163.    }
  164.  
  165.    public synchronized Dimension minimumSize() {
  166.       LayoutManager layoutMgr = this.layoutMgr;
  167.       return layoutMgr != null ? layoutMgr.minimumLayoutSize(this) : super.minimumSize();
  168.    }
  169.  
  170.    public void paintComponents(Graphics g) {
  171.       for(int i = 0; i < this.ncomponents; ++i) {
  172.          Component comp = this.component[i];
  173.          if (comp != null) {
  174.             Graphics cg = g.create(comp.x, comp.y, comp.width, comp.height);
  175.  
  176.             try {
  177.                comp.paintAll(cg);
  178.             } finally {
  179.                cg.dispose();
  180.             }
  181.          }
  182.       }
  183.  
  184.    }
  185.  
  186.    public void printComponents(Graphics g) {
  187.       for(int i = 0; i < this.ncomponents; ++i) {
  188.          Component comp = this.component[i];
  189.          if (comp != null) {
  190.             Graphics cg = g.create(comp.x, comp.y, comp.width, comp.height);
  191.  
  192.             try {
  193.                comp.printAll(cg);
  194.             } finally {
  195.                cg.dispose();
  196.             }
  197.          }
  198.       }
  199.  
  200.    }
  201.  
  202.    public void deliverEvent(Event e) {
  203.       Component comp = this.locate(e.x, e.y);
  204.       if (comp != null && comp != this) {
  205.          e.translate(-comp.x, -comp.y);
  206.          comp.deliverEvent(e);
  207.       } else {
  208.          ((Component)this).postEvent(e);
  209.       }
  210.    }
  211.  
  212.    public Component locate(int x, int y) {
  213.       if (!((Component)this).inside(x, y)) {
  214.          return null;
  215.       } else {
  216.          for(int i = 0; i < this.ncomponents; ++i) {
  217.             Component comp = this.component[i];
  218.             if (comp != null && comp.inside(x - comp.x, y - comp.y)) {
  219.                return comp;
  220.             }
  221.          }
  222.  
  223.          return this;
  224.       }
  225.    }
  226.  
  227.    public synchronized void addNotify() {
  228.       for(int i = 0; i < this.ncomponents; ++i) {
  229.          this.component[i].addNotify();
  230.       }
  231.  
  232.       super.addNotify();
  233.    }
  234.  
  235.    public synchronized void removeNotify() {
  236.       for(int i = 0; i < this.ncomponents; ++i) {
  237.          this.component[i].removeNotify();
  238.       }
  239.  
  240.       super.removeNotify();
  241.    }
  242.  
  243.    protected String paramString() {
  244.       String str = super.paramString();
  245.       LayoutManager layoutMgr = this.layoutMgr;
  246.       if (layoutMgr != null) {
  247.          str = str + ",layout=" + layoutMgr.getClass().getName();
  248.       }
  249.  
  250.       return str;
  251.    }
  252.  
  253.    public void list(PrintStream out, int indent) {
  254.       super.list(out, indent);
  255.  
  256.       for(int i = 0; i < this.ncomponents; ++i) {
  257.          Component comp = this.component[i];
  258.          if (comp != null) {
  259.             comp.list(out, indent + 1);
  260.          }
  261.       }
  262.  
  263.    }
  264. }
  265.