home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Container.java < prev    next >
Text File  |  1996-05-03  |  11KB  |  428 lines

  1. /*
  2.  * @(#)Container.java    1.39 96/02/28 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.39, 28 Feb 1996
  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.     if (valid) {
  142.         invalidate();
  143.     }
  144.     if (peer != null) {
  145.         comp.addNotify();
  146.     }
  147.     return comp;
  148.     }
  149.  
  150.     /**
  151.      * Adds the specified component to this container. The component
  152.      * is also added to the layout manager of this container using the
  153.      * name specified
  154. .
  155.      * @param name the component name
  156.      * @param comp the component to be added
  157.      * @see #remove
  158.      * @see LayoutManager
  159.      */
  160.     public synchronized Component add(String name, Component comp) {
  161.     Component c = add(comp);
  162.     LayoutManager layoutMgr = this.layoutMgr;
  163.     if (layoutMgr != null) {
  164.         layoutMgr.addLayoutComponent(name, comp);
  165.     }
  166.     return c;
  167.     }
  168.  
  169.     /** 
  170.      * Removes the specified component from this container.
  171.      * @param comp the component to be removed
  172.      * @see #add
  173.      */
  174.     public synchronized void remove(Component comp) {
  175.     if (comp.parent == this)  {
  176.         for (int i = 0 ; i < ncomponents ; i++) {
  177.         if (component[i] == comp) {
  178.             if (peer != null) {
  179.             comp.removeNotify();
  180.             }
  181.             if (layoutMgr != null) {
  182.             layoutMgr.removeLayoutComponent(comp);
  183.             }
  184.             comp.parent = null;
  185.             System.arraycopy(component, i + 1, component, i, ncomponents - i - 1);
  186.             component[--ncomponents] = null;
  187.             if (valid) {
  188.             invalidate();
  189.             }
  190.             return;
  191.         }
  192.         }
  193.     }
  194.     }
  195.  
  196.     /** 
  197.      * Removes all the components from this container.
  198.      * @see #add
  199.      * @see #remove
  200.      */
  201.     public synchronized void removeAll() {
  202.     while (ncomponents > 0) {
  203.         Component comp = component[--ncomponents];
  204.         component[ncomponents] = null;
  205.  
  206.         if (peer != null) {
  207.         comp.removeNotify();
  208.         }
  209.         if (layoutMgr != null) {
  210.         layoutMgr.removeLayoutComponent(comp);
  211.         }
  212.         comp.parent = null;
  213.     }
  214.     if (valid) {
  215.         invalidate();
  216.     }
  217.     }
  218.  
  219.     /** 
  220.      * Gets the layout manager for this container.  
  221.      * @see #layout
  222.      * @see #setLayout
  223.      */
  224.     public LayoutManager getLayout() {
  225.     return layoutMgr;
  226.     }
  227.  
  228.     /** 
  229.      * Sets the layout manager for this container.
  230.      * @param mgr the specified layout manager
  231.      * @see #layout
  232.      * @see #getLayout
  233.      */
  234.     public void setLayout(LayoutManager mgr) {
  235.     layoutMgr = mgr;
  236.     if (valid) {
  237.         invalidate();
  238.     }
  239.     }
  240.  
  241.     /** 
  242.      * Does a layout on this Container. 
  243.      * @see #setLayout
  244.      */
  245.     public synchronized void layout() {
  246.     LayoutManager layoutMgr = this.layoutMgr;
  247.     if (layoutMgr != null) {
  248.         layoutMgr.layoutContainer(this);
  249.     }
  250.     }
  251.  
  252.     /** 
  253.      * Validates this Container and all of the components contained within it. 
  254.      * @see #validate
  255.      * @see Component#invalidate
  256.      */
  257.     public synchronized void validate() {
  258.     super.validate();
  259.     for (int i = 0 ; i < ncomponents ; i++) {
  260.         Component comp = component[i];
  261.         if (!comp.valid) {
  262.         comp.validate();
  263.         }
  264.     }
  265.     }
  266.  
  267.     /** 
  268.      * Returns the preferred size of this container.  
  269.      * @see #minimumSize
  270.      */
  271.     public synchronized Dimension preferredSize() {
  272.     LayoutManager layoutMgr = this.layoutMgr;
  273.     return (layoutMgr != null) ? layoutMgr.preferredLayoutSize(this) : super.preferredSize();
  274.     }
  275.  
  276.     /** 
  277.      * Returns the minimum size of this container.  
  278.      * @see #preferredSize
  279.      */
  280.     public synchronized Dimension minimumSize() {
  281.     LayoutManager layoutMgr = this.layoutMgr;
  282.     return (layoutMgr != null) ? layoutMgr.minimumLayoutSize(this) : super.minimumSize();
  283.     }
  284.  
  285.     /** 
  286.      * Paints the components in this container.
  287.      * @param g the specified Graphics window
  288.      * @see Component#paint
  289.      * @see Component#paintAll
  290.      */
  291.     public void paintComponents(Graphics g) {
  292.     for (int i = 0 ; i < ncomponents ; i++) {
  293.         Component comp = component[i];
  294.         if (comp != null) {
  295.         Graphics cg = g.create(comp.x, comp.y, comp.width, comp.height);
  296.         try {
  297.             comp.paintAll(cg);
  298.         } finally {
  299.             cg.dispose();
  300.         }
  301.         }
  302.     }
  303.     }
  304.  
  305.     /** 
  306.      * Prints the components in this container.
  307.      * @param g the specified Graphics window
  308.      * @see Component#print
  309.      * @see Component#printAll
  310.      */
  311.     public void printComponents(Graphics g) {
  312.     for (int i = 0 ; i < ncomponents ; i++) {
  313.         Component comp = component[i];
  314.         if (comp != null) {
  315.         Graphics cg = g.create(comp.x, comp.y, comp.width, comp.height);
  316.         try {
  317.             comp.printAll(cg);
  318.         } finally {
  319.             cg.dispose();
  320.         }
  321.         }
  322.     }
  323.     }
  324.  
  325.     /**
  326.      * Delivers an event. The appropriate component is located and
  327.      * the event is delivered to it.
  328.      * @param e the event
  329.      * @see Component#handleEvent
  330.      * @see Component#postEvent
  331.      */
  332.     public void deliverEvent(Event e) {
  333.     Component comp = locate(e.x, e.y);
  334.  
  335.     if ((comp != null) && (comp != this)) {
  336.         e.translate(-comp.x, -comp.y);
  337.         comp.deliverEvent(e);
  338.     } else {
  339.         postEvent(e);
  340.     }
  341.     }
  342.  
  343.     /**
  344.      * Locates the component that contains the x,y position.
  345.      * @param x the x coordinate
  346.      * @param y the y coordinate
  347.      * @return null if the component is not within the x and y
  348.      * coordinates; returns the component otherwise. 
  349.      * @see Component#inside 
  350.      */
  351.     public Component locate(int x, int y) {
  352.     if (!inside(x, y)) {
  353.         return null;
  354.     }
  355.     for (int i = ncomponents - 1 ; i >= 0 ; i--) {
  356.         Component comp = component[i];
  357.         if ((comp != null) && comp.inside(x - comp.x, y - comp.y)) {
  358.         return comp;
  359.         }
  360.     }
  361.     return this;
  362.     }
  363.  
  364.     /** 
  365.      * Notifies the container to create a peer. It will also
  366.      * notify the components contained in this container.
  367.      * @see #removeNotify
  368.      */
  369.     public synchronized void addNotify() {
  370.     for (int i = 0 ; i < ncomponents ; i++) {
  371.         component[i].addNotify();
  372.     }
  373.     super.addNotify();
  374.     }
  375.  
  376.     /** 
  377.      * Notifies the container to remove its peer. It will
  378.      * also notify the components contained in this container.
  379.      * @see #addNotify
  380.      */
  381.     public synchronized void removeNotify() {
  382.     for (int i = 0 ; i < ncomponents ; i++) {
  383.         component[i].removeNotify();
  384.     }
  385.     super.removeNotify();
  386.     }
  387.  
  388.     /**
  389.      * Returns the parameter String of this Container.
  390.      */
  391.     protected String paramString() {
  392.     String str = super.paramString();
  393.     LayoutManager layoutMgr = this.layoutMgr;
  394.     if (layoutMgr != null) {
  395.         str += ",layout=" + layoutMgr.getClass().getName();
  396.     }
  397.     return str;
  398.     }
  399.  
  400.     /**
  401.      * Prints out a list, starting at the specified indention, to the specified
  402.      * out stream. 
  403.      * @param out the Stream name
  404.      * @param indent the start of the list
  405.      */
  406.     public void list(PrintStream out, int indent) {
  407.     super.list(out, indent);
  408.     for (int i = 0 ; i < ncomponents ; i++) {
  409.         Component comp = component[i];
  410.         if (comp != null) {
  411.         comp.list(out, indent+1);
  412.         }
  413.     }
  414.     }
  415.  
  416.     void setFocusOwner(Component c) {
  417.     if (parent != null) {
  418.         parent.setFocusOwner(c);
  419.     }
  420.     }
  421.  
  422.     void nextFocus(Component base) {
  423.     if (parent != null) {
  424.         parent.nextFocus(base);
  425.     }
  426.     }
  427. }
  428.