001/* ContainerEvent.java -- components added/removed from a container
002   Copyright (C) 1999, 2002, 2005  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.awt.event;
040
041import java.awt.Component;
042import java.awt.Container;
043
044/**
045 * This event is generated when a component is added or removed from a
046 * container.  Applications do not ordinarily need to handle these events
047 * since the AWT system handles them internally.
048 *
049 * @author Aaron M. Renn (arenn@urbanophile.com)
050 * @see ContainerAdapter
051 * @see ContainerListener
052 * @since 1.1
053 * @status updated to 1.4
054 */
055public class ContainerEvent extends ComponentEvent
056{
057  /**
058   * Compatible with JDK 1.1+.
059   */
060  private static final long serialVersionUID = -4114942250539772041L;
061
062  /** This is the first id in the id range used by this class. */
063  public static final int CONTAINER_FIRST = 300;
064
065  /** This is the last id in the id range used by this class. */
066  public static final int CONTAINER_LAST = 301;
067
068  /** This id indicates a component was added to the container. */
069  public static final int COMPONENT_ADDED = 300;
070
071  /** This id indicates a component was removed from the container. */
072  public static final int COMPONENT_REMOVED = 301;
073
074  /**
075   * The non-null child component that was added or removed.
076   *
077   * @serial the child component that changed
078   */
079  private final Component child;
080
081  /**
082   * Initializes a new instance of <code>ContainerEvent</code> with the
083   * specified source and id.  Additionally, the affected child component
084   * is also passed as a parameter. Note that an invalid id leads to
085   * unspecified results.
086   *
087   * @param source the source container of the event
088   * @param id the event id
089   * @param child the child component affected by this event
090   * @throws IllegalArgumentException if source is null
091   */
092  public ContainerEvent(Component source, int id, Component child)
093  {
094    super(source, id);
095    this.child = child;
096  }
097
098  /**
099   * Returns the source of this event as a <code>Container</code>.
100   *
101   * @return the source of the event
102   * @throws ClassCastException if the source is changed to a non-Container
103   */
104  public Container getContainer()
105  {
106    return (Container) source;
107  }
108
109  /**
110   * This method returns the child object that was added or removed from
111   * the container.
112   *
113   * @return the child object added or removed
114   */
115  public Component getChild()
116  {
117    return child;
118  }
119
120  /**
121   * This method returns a string identifying this event. It is formatted as:
122   * <code>(getID() == COMPONENT_ADDED ? "COMPONENT_ADDED"
123   * : "COMPONENT_REMOVED") + ",child=" + getChild().getName()</code>.
124   *
125   * @return a string identifying this event
126   */
127  public String paramString()
128  {
129    // Unlike Sun, we don't throw NullPointerException if child is illegally
130    // null.
131    return (id == COMPONENT_ADDED ? "COMPONENT_ADDED,child="
132            : id == COMPONENT_REMOVED ? "COMPONENT_REMOVED,child="
133            : "unknown type,child=") + (child == null ? "" : child.getName());
134  }
135} // class ContainerEvent