home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / awt / contai~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  10.6 KB  |  408 lines

  1. /*
  2.  * @(#)Container.java    1.38 95/12/14 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 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. package java.awt;
  20.  
  21. import java.io.PrintStream;
  22. import java.awt.peer.ContainerPeer;
  23.  
  24. /**
  25.  * A generic Abstract Window Toolkit(AWT) container object is a component 
  26.  * that can contain other AWT components.
  27.  *
  28.  * @version     1.38, 12/14/95
  29.  * @author     Arthur van Hoff
  30.  * @author     Sami Shaio
  31.  */
  32. public abstract class Container extends Component {
  33.  
  34.     /**
  35.      * The number of components in this container.
  36.      */
  37.     int ncomponents;
  38.  
  39.     /** 
  40.      * The components in this container.
  41.      */
  42.     Component component[] = new Component[4];
  43.  
  44.     /** 
  45.      * Layout manager for this container.
  46.      */
  47.     LayoutManager layoutMgr;
  48.  
  49.     /**
  50.      * Constructs a new Container. Containers should not be subclassed or 
  51.      * instantiated directly.
  52.      */
  53.     Container() {
  54.     }
  55.  
  56.     /** 
  57.      * Returns the number of components in this panel.
  58.      * @see #getComponent
  59.      */
  60.     public int countComponents() {
  61.     return ncomponents;
  62.     }
  63.  
  64.     /** 
  65.      * Gets the nth component in this container.
  66.      * @param n the number of the component to get
  67.      * @exception ArrayIndexOutOfBoundsException If the nth value does not 
  68.      * exist.
  69.      */
  70.     public synchronized Component getComponent(int n) {
  71.     if ((n < 0) || (n >= ncomponents)) {
  72.         throw new ArrayIndexOutOfBoundsException("No such child: " + n);
  73.     }
  74.     return component[n];
  75.     }
  76.  
  77.     /**
  78.      * Gets all the components in this container.
  79.      */
  80.     public synchronized Component[] getComponents() {
  81.     Component list[] = new Component[ncomponents];
  82.     System.arraycopy(component, 0, list, 0, ncomponents);
  83.     return list;
  84.     }
  85.  
  86.     /**
  87.      * Returns the insets of the container. The insets indicate the size of
  88.      * the border of the container. A Frame, for example, will have a top inset
  89.      * that corresponds to the height of the Frame's title bar. 
  90.      * @see LayoutManager
  91.      */
  92.     public Insets insets() {
  93.     ContainerPeer peer = (ContainerPeer)this.peer;
  94.     return (peer != null) ? peer.insets() : new Insets(0, 0, 0, 0);
  95.     }
  96.  
  97.     /** 
  98.      * Adds the specified component to this container.
  99.      * @param comp the component to be added
  100.      */
  101.     public Component add(Component comp) {
  102.     return add(comp, -1);
  103.     }
  104.  
  105.     /** 
  106.      * Adds the specified component to this container at the given position.
  107.      * @param comp the component to be added 
  108.      * @param pos the position at which to insert the component. -1
  109.      * means insert at the end.
  110.      * @see #remove
  111.      */
  112.     public synchronized Component add(Component comp, int pos) {
  113.     if (pos > ncomponents || (pos < 0 && pos != -1)) {
  114.         throw new IllegalArgumentException("illegal component position");
  115.     }
  116.     // check to see that comp isn't one of this container's parents
  117.     if (comp instanceof Container) {
  118.         for (Container cn = this; cn != null; cn=cn.parent) {
  119.         if (cn == comp) {
  120.             throw new IllegalArgumentException("adding container's parent to itself");
  121.         }
  122.         }
  123.     }
  124.  
  125.     if (comp.parent != null) {
  126.         comp.parent.remove(comp);
  127.     }
  128.     if (ncomponents == component.length) {
  129.         Component newcomponents[] = new Component[ncomponents * 2];
  130.         System.arraycopy(component, 0, newcomponents, 0, ncomponents);
  131.         component = newcomponents;
  132.     }
  133.     if (pos == -1 || pos==ncomponents) {
  134.         component[ncomponents++] = comp;
  135.     } else {
  136.         System.arraycopy(component, pos, component, pos+1, ncomponents-pos);
  137.         component[pos] = comp;
  138.         ncomponents++;
  139.     }
  140.     comp.parent = this;
  141.     invalidate();
  142.     if (peer != null) {
  143.         comp.addNotify();
  144.     }
  145.     return comp;
  146.     }
  147.  
  148.     /**
  149.      * Adds the specified component to this container. The component
  150.      * is also added to the layout manager of this container using the
  151.      * name specified
  152. .
  153.      * @param name the component name
  154.      * @param comp the component to be added
  155.      * @see #remove
  156.      * @see LayoutManager
  157.      */
  158.     public synchronized Component add(String name, Component comp) {
  159.     Component c = add(comp);
  160.     LayoutManager layoutMgr = this.layoutMgr;
  161.     if (layoutMgr != null) {
  162.         layoutMgr.addLayoutComponent(name, comp);
  163.     }
  164.     return c;
  165.     }
  166.  
  167.     /** 
  168.      * Removes the specified component from this container.
  169.      * @param comp the component to be removed
  170.      * @see #add
  171.      */
  172.     public synchronized void remove(Component comp) {
  173.     if (comp.parent == this)  {
  174.         for (int i = 0 ; i < ncomponents ; i++) {
  175.         if (component[i] == comp) {
  176.             if (peer != null) {
  177.             comp.removeNotify();
  178.             }
  179.             if (layoutMgr != null) {
  180.             layoutMgr.removeLayoutComponent(comp);
  181.             }
  182.             comp.parent = null;
  183.             System.arraycopy(component, i + 1, component, i, ncomponents - i - 1);
  184.             component[--ncomponents] = null;
  185.             invalidate();
  186.             return;
  187.         }
  188.         }
  189.     }
  190.     }
  191.  
  192.     /** 
  193.      * Removes all the components from this container.
  194.      * @see #add
  195.      * @see #remove
  196.      */
  197.     public synchronized void removeAll() {
  198.     while (ncomponents > 0) {
  199.         Component comp = component[--ncomponents];
  200.         component[ncomponents] = null;
  201.  
  202.         if (peer != null) {
  203.         comp.removeNotify();
  204.         }
  205.         if (layoutMgr != null) {
  206.         layoutMgr.removeLayoutComponent(comp);
  207.         }
  208.         comp.parent = null;
  209.     }
  210.     invalidate();
  211.     }
  212.  
  213.     /** 
  214.      * Gets the layout manager for this container.  
  215.      * @see #layout
  216.      * @see #setLayout
  217.      */
  218.     public LayoutManager getLayout() {
  219.     return layoutMgr;
  220.     }
  221.  
  222.     /** 
  223.      * Sets the layout manager for this container.
  224.      * @param mgr the specified layout manager
  225.      * @see #layout
  226.      * @see #getLayout
  227.      */
  228.     public void setLayout(LayoutManager mgr) {
  229.     layoutMgr = mgr;
  230.     invalidate();
  231.     }
  232.  
  233.     /** 
  234.      * Does a layout on this Container. 
  235.      * @see #setLayout
  236.      */
  237.     public synchronized void layout() {
  238.     LayoutManager layoutMgr = this.layoutMgr;
  239.     if (layoutMgr != null) {
  240.         layoutMgr.layoutContainer(this);
  241.     }
  242.     }
  243.  
  244.     /** 
  245.      * Validates this Container and all of the components contained within it. 
  246.      * @see #validate
  247.      * @see Component#invalidate
  248.      */
  249.     public synchronized void validate() {
  250.     super.validate();
  251.     for (int i = 0 ; i < ncomponents ; i++) {
  252.         Component comp = component[i];
  253.         if (!comp.valid) {
  254.         comp.validate();
  255.         }
  256.     }
  257.     }
  258.  
  259.     /** 
  260.      * Returns the preferred size of this container.  
  261.      * @see #minimumSize
  262.      */
  263.     public synchronized Dimension preferredSize() {
  264.     LayoutManager layoutMgr = this.layoutMgr;
  265.     return (layoutMgr != null) ? layoutMgr.preferredLayoutSize(this) : super.preferredSize();
  266.     }
  267.  
  268.     /** 
  269.      * Returns the minimum size of this container.  
  270.      * @see #preferredSize
  271.      */
  272.     public synchronized Dimension minimumSize() {
  273.     LayoutManager layoutMgr = this.layoutMgr;
  274.     return (layoutMgr != null) ? layoutMgr.minimumLayoutSize(this) : super.minimumSize();
  275.     }
  276.  
  277.     /** 
  278.      * Paints the components in this container.
  279.      * @param g the specified Graphics window
  280.      * @see Component#paint
  281.      * @see Component#paintAll
  282.      */
  283.     public void paintComponents(Graphics g) {
  284.     for (int i = 0 ; i < ncomponents ; i++) {
  285.         Component comp = component[i];
  286.         if (comp != null) {
  287.         Graphics cg = g.create(comp.x, comp.y, comp.width, comp.height);
  288.         try {
  289.             comp.paintAll(cg);
  290.         } finally {
  291.             cg.dispose();
  292.         }
  293.         }
  294.     }
  295.     }
  296.  
  297.     /** 
  298.      * Prints the components in this container.
  299.      * @param g the specified Graphics window
  300.      * @see Component#print
  301.      * @see Component#printAll
  302.      */
  303.     public void printComponents(Graphics g) {
  304.     for (int i = 0 ; i < ncomponents ; i++) {
  305.         Component comp = component[i];
  306.         if (comp != null) {
  307.         Graphics cg = g.create(comp.x, comp.y, comp.width, comp.height);
  308.         try {
  309.             comp.printAll(cg);
  310.         } finally {
  311.             cg.dispose();
  312.         }
  313.         }
  314.     }
  315.     }
  316.  
  317.     /**
  318.      * Delivers an event. The appropriate component is located and
  319.      * the event is delivered to it.
  320.      * @param e the event
  321.      * @see Component#handleEvent
  322.      * @see Component#postEvent
  323.      */
  324.     public void deliverEvent(Event e) {
  325.     Component comp = locate(e.x, e.y);
  326.  
  327.     if ((comp != null) && (comp != this)) {
  328.         e.translate(-comp.x, -comp.y);
  329.         comp.deliverEvent(e);
  330.     } else {
  331.         postEvent(e);
  332.     }
  333.     }
  334.  
  335.     /**
  336.      * Locates the component that contains the x,y position.
  337.      * @param x the x coordinate
  338.      * @param y the y coordinate
  339.      * @return null if the component is not within the x and y
  340.      * coordinates; returns the component otherwise. 
  341.      * @see Component#inside 
  342.      */
  343.     public Component locate(int x, int y) {
  344.     if (!inside(x, y)) {
  345.         return null;
  346.     }
  347.     for (int i = 0 ; i < ncomponents ; i++) {
  348.         Component comp = component[i];
  349.         if ((comp != null) && comp.inside(x - comp.x, y - comp.y)) {
  350.         return comp;
  351.         }
  352.     }
  353.     return this;
  354.     }
  355.  
  356.     /** 
  357.      * Notifies the container to create a peer. It will also
  358.      * notify the components contained in this container.
  359.      * @see #removeNotify
  360.      */
  361.     public synchronized void addNotify() {
  362.     for (int i = 0 ; i < ncomponents ; i++) {
  363.         component[i].addNotify();
  364.     }
  365.     super.addNotify();
  366.     }
  367.  
  368.     /** 
  369.      * Notifies the container to remove its peer. It will
  370.      * also notify the components contained in this container.
  371.      * @see #addNotify
  372.      */
  373.     public synchronized void removeNotify() {
  374.     for (int i = 0 ; i < ncomponents ; i++) {
  375.         component[i].removeNotify();
  376.     }
  377.     super.removeNotify();
  378.     }
  379.  
  380.     /**
  381.      * Returns the parameter String of this Container.
  382.      */
  383.     protected String paramString() {
  384.     String str = super.paramString();
  385.     LayoutManager layoutMgr = this.layoutMgr;
  386.     if (layoutMgr != null) {
  387.         str += ",layout=" + layoutMgr.getClass().getName();
  388.     }
  389.     return str;
  390.     }
  391.  
  392.     /**
  393.      * Prints out a list, starting at the specified indention, to the specified
  394.      * out stream. 
  395.      * @param out the Stream name
  396.      * @param indent the start of the list
  397.      */
  398.     public void list(PrintStream out, int indent) {
  399.     super.list(out, indent);
  400.     for (int i = 0 ; i < ncomponents ; i++) {
  401.         Component comp = component[i];
  402.         if (comp != null) {
  403.         comp.list(out, indent+1);
  404.         }
  405.     }
  406.     }
  407. }
  408.