001 /* ContainerEvent.java -- components added/removed from a container
002 Copyright (C) 1999, 2002, 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.awt.event;
040
041 import java.awt.Component;
042 import 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 */
055 public 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