home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / containe.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  9.6 KB  |  377 lines

  1. /*
  2.  * @(#)Container.java    1.30 95/08/17 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.30, 08/17/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 with 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.      * @see #remove
  101.      */
  102.     public synchronized Component add(Component comp) {
  103.     if (comp.parent != null) {
  104.         comp.parent.remove(comp);
  105.     }
  106.     if (ncomponents == component.length) {
  107.         Component newcomponents[] = new Component[ncomponents * 2];
  108.         System.arraycopy(component, 0, newcomponents, 0, ncomponents);
  109.         component = newcomponents;
  110.     }
  111.     component[ncomponents++] = comp;
  112.     comp.parent = this;
  113.     invalidate();
  114.     if (peer != null) {
  115.         comp.addNotify();
  116.     }
  117.     return comp;
  118.     }
  119.  
  120.     /**
  121.      * Adds the specified component to this container. The component is
  122.      * also added to the layout manager of this container using the name.
  123.      * @param name the component name
  124.      * @param comp the component to be added
  125.      * @see #remove
  126.      * @see LayoutManager
  127.      */
  128.     public synchronized Component add(String name, Component comp) {
  129.     LayoutManager layoutMgr = this.layoutMgr;
  130.     if (layoutMgr != null) {
  131.         layoutMgr.addLayoutComponent(name, comp);
  132.     }
  133.     return add(comp);
  134.     }
  135.  
  136.     /** 
  137.      * Removes the specified component from this container.
  138.      * @param comp the component to be removed
  139.      * @see #add
  140.      */
  141.     public synchronized void remove(Component comp) {
  142.     if (comp.parent == this)  {
  143.         for (int i = 0 ; i < ncomponents ; i++) {
  144.         if (component[i] == comp) {
  145.             if (peer != null) {
  146.             comp.removeNotify();
  147.             }
  148.             if (layoutMgr != null) {
  149.             layoutMgr.removeLayoutComponent(comp);
  150.             }
  151.             comp.parent = null;
  152.             System.arraycopy(component, i + 1, component, i, ncomponents - i - 1);
  153.             component[--ncomponents] = null;
  154.             invalidate();
  155.             return;
  156.         }
  157.         }
  158.     }
  159.     }
  160.  
  161.     /** 
  162.      * Removes all the components from this container.
  163.      * @see #add
  164.      * @see #remove
  165.      */
  166.     public synchronized void removeAll() {
  167.     while (ncomponents > 0) {
  168.         Component comp = component[--ncomponents];
  169.         component[ncomponents] = null;
  170.  
  171.         if (peer != null) {
  172.         comp.removeNotify();
  173.         }
  174.         if (layoutMgr != null) {
  175.         layoutMgr.removeLayoutComponent(comp);
  176.         }
  177.         comp.parent = null;
  178.     }
  179.     invalidate();
  180.     }
  181.  
  182.     /** 
  183.      * Gets the layout manager for this container.  
  184.      * @see #layout
  185.      * @see #setLayout
  186.      */
  187.     public LayoutManager getLayout() {
  188.     return layoutMgr;
  189.     }
  190.  
  191.     /** 
  192.      * Sets the layout manager for this container.
  193.      * @param mgr the specified layout manager
  194.      * @see #layout
  195.      * @see #getLayout
  196.      */
  197.     public void setLayout(LayoutManager mgr) {
  198.     layoutMgr = mgr;
  199.     invalidate();
  200.     }
  201.  
  202.     /** 
  203.      * Does a layout on this Container. 
  204.      * @see #setLayout
  205.      */
  206.     public synchronized void layout() {
  207.     LayoutManager layoutMgr = this.layoutMgr;
  208.     if (layoutMgr != null) {
  209.         layoutMgr.layoutContainer(this);
  210.     }
  211.     }
  212.  
  213.     /** 
  214.      * Validates this Container and all of the components contained within it. 
  215.      * @see #validate
  216.      * @see #invalidate
  217.      */
  218.     public synchronized void validate() {
  219.     super.validate();
  220.     for (int i = 0 ; i < ncomponents ; i++) {
  221.         Component comp = component[i];
  222.         if (!comp.valid) {
  223.         comp.validate();
  224.         }
  225.     }
  226.     }
  227.  
  228.     /** 
  229.      * Returns the preferred size of this container.  
  230.      * @see #minimumSize
  231.      */
  232.     public synchronized Dimension preferredSize() {
  233.     LayoutManager layoutMgr = this.layoutMgr;
  234.     return (layoutMgr != null) ? layoutMgr.preferredLayoutSize(this) : super.preferredSize();
  235.     }
  236.  
  237.     /** 
  238.      * Returns the minimum size of this container.  
  239.      * @see #preferredSize
  240.      */
  241.     public synchronized Dimension minimumSize() {
  242.     LayoutManager layoutMgr = this.layoutMgr;
  243.     return (layoutMgr != null) ? layoutMgr.minimumLayoutSize(this) : super.minimumSize();
  244.     }
  245.  
  246.     /** 
  247.      * Paints the components in this container.
  248.      * @param g the specified Graphics window
  249.      * @see #paint
  250.      * @see #paintAll
  251.      */
  252.     public void paintComponents(Graphics g) {
  253.     for (int i = 0 ; i < ncomponents ; i++) {
  254.         Component comp = component[i];
  255.         if (comp != null) {
  256.         Graphics cg = g.create(comp.x, comp.y, comp.width, comp.height);
  257.         try {
  258.             comp.paintAll(cg);
  259.         } finally {
  260.             cg.dispose();
  261.         }
  262.         }
  263.     }
  264.     }
  265.  
  266.     /** 
  267.      * Prints the components in this container.
  268.      * @param g the specified Graphics window
  269.      * @see #print
  270.      * @see #printAll
  271.      */
  272.     public void printComponents(Graphics g) {
  273.     for (int i = 0 ; i < ncomponents ; i++) {
  274.         Component comp = component[i];
  275.         if (comp != null) {
  276.         Graphics cg = g.create(comp.x, comp.y, comp.width, comp.height);
  277.         try {
  278.             comp.printAll(cg);
  279.         } finally {
  280.             cg.dispose();
  281.         }
  282.         }
  283.     }
  284.     }
  285.  
  286.     /**
  287.      * Delivers an event. The appropriate component is located and
  288.      * the event is delivered to it.
  289.      * @param e the event
  290.      * @see #handleEvent
  291.      * @see #postEvent
  292.      */
  293.     public void deliverEvent(Event e) {
  294.     Component comp = locate(e.x, e.y);
  295.  
  296.     if ((comp != null) && (comp != this)) {
  297.         e.translate(-comp.x, -comp.y);
  298.         comp.deliverEvent(e);
  299.     } else {
  300.         postEvent(e);
  301.     }
  302.     }
  303.  
  304.     /**
  305.      * Locates the component that contains the x,y position.
  306.      * @param x the x coordinate
  307.      * @param y the y coordinate
  308.      * @return null if the component is not within the x and y
  309.      * coordinates; returns the component otherwise. 
  310.      * @see #inside 
  311.      */
  312.     public Component locate(int x, int y) {
  313.     if (!inside(x, y)) {
  314.         return null;
  315.     }
  316.     for (int i = 0 ; i < ncomponents ; i++) {
  317.         Component comp = component[i];
  318.         if ((comp != null) && comp.inside(x - comp.x, y - comp.y)) {
  319.         return comp;
  320.         }
  321.     }
  322.     return this;
  323.     }
  324.  
  325.     /** 
  326.      * Notifies the container to create a peer. It will also
  327.      * notify the components contained in this container.
  328.      * @see #removeNotify
  329.      */
  330.     public synchronized void addNotify() {
  331.     for (int i = 0 ; i < ncomponents ; i++) {
  332.         component[i].addNotify();
  333.     }
  334.     super.addNotify();
  335.     }
  336.  
  337.     /** 
  338.      * Notifies the container to remove its peer. It will
  339.      * also notify the components contained in this container.
  340.      * @see #addNotify
  341.      */
  342.     public synchronized void removeNotify() {
  343.     for (int i = 0 ; i < ncomponents ; i++) {
  344.         component[i].removeNotify();
  345.     }
  346.     super.removeNotify();
  347.     }
  348.  
  349.     /**
  350.      * Returns the parameter String of this Container.
  351.      */
  352.     protected String paramString() {
  353.     String str = super.paramString();
  354.     LayoutManager layoutMgr = this.layoutMgr;
  355.     if (layoutMgr != null) {
  356.         str += ",layout=" + layoutMgr.getClass().getName();
  357.     }
  358.     return str;
  359.     }
  360.  
  361.     /**
  362.      * Prints out a list, starting at the specified indention, to the specified
  363.      * out stream. 
  364.      * @param out the Stream name
  365.      * @param indent the start of the list
  366.      */
  367.     public void list(PrintStream out, int indent) {
  368.     super.list(out, indent);
  369.     for (int i = 0 ; i < ncomponents ; i++) {
  370.         Component comp = component[i];
  371.         if (comp != null) {
  372.         comp.list(out, indent+1);
  373.         }
  374.     }
  375.     }
  376. }
  377.