blob: f927312ba0bc5fde617de0bc6ced8aaa2bcbb1ba [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package java.awt.event;
27
28import java.awt.Container;
29import java.awt.Component;
30
31/**
32 * A low-level event which indicates that a container's contents
33 * changed because a component was added or removed.
34 * <P>
35 * Container events are provided for notification purposes ONLY;
36 * The AWT will automatically handle changes to the containers
37 * contents internally so that the program works properly regardless of
38 * whether the program is receiving these events or not.
39 * <P>
40 * This low-level event is generated by a container object (such as a
41 * Panel) when a component is added to it or removed from it.
42 * The event is passed to every <code>ContainerListener</code>
43 * or <code>ContainerAdapter</code> object which registered to receive such
44 * events using the component's <code>addContainerListener</code> method.
45 * (<code>ContainerAdapter</code> objects implement the
46 * <code>ContainerListener</code> interface.) Each such listener object
47 * gets this <code>ContainerEvent</code> when the event occurs.
48 *
49 * @see ContainerAdapter
50 * @see ContainerListener
51 * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/containerlistener.html">Tutorial: Writing a Container Listener</a>
52 *
53 * @author Tim Prinzing
54 * @author Amy Fowler
55 * @since 1.1
56 */
57public class ContainerEvent extends ComponentEvent {
58
59 /**
60 * The first number in the range of ids used for container events.
61 */
62 public static final int CONTAINER_FIRST = 300;
63
64 /**
65 * The last number in the range of ids used for container events.
66 */
67 public static final int CONTAINER_LAST = 301;
68
69 /**
70 * This event indicates that a component was added to the container.
71 */
72 public static final int COMPONENT_ADDED = CONTAINER_FIRST;
73
74 /**
75 * This event indicates that a component was removed from the container.
76 */
77 public static final int COMPONENT_REMOVED = 1 + CONTAINER_FIRST;
78
79 /**
80 * The non-null component that is being added or
81 * removed from the Container.
82 *
83 * @serial
84 * @see #getChild()
85 */
86 Component child;
87
88 /*
89 * JDK 1.1 serialVersionUID
90 */
91 private static final long serialVersionUID = -4114942250539772041L;
92
93 /**
94 * Constructs a <code>ContainerEvent</code> object.
95 * <p>Note that passing in an invalid <code>id</code> results in
96 * unspecified behavior. This method throws an
97 * <code>IllegalArgumentException</code> if <code>source</code>
98 * is <code>null</code>.
99 *
100 * @param source the <code>Component</code> object (container)
101 * that originated the event
102 * @param id an integer indicating the type of event
103 * @param child the component that was added or removed
104 * @throws IllegalArgumentException if <code>source</code> is null
105 */
106 public ContainerEvent(Component source, int id, Component child) {
107 super(source, id);
108 this.child = child;
109 }
110
111 /**
112 * Returns the originator of the event.
113 *
114 * @return the <code>Container</code> object that originated
115 * the event, or <code>null</code> if the object is not a
116 * <code>Container</code>.
117 */
118 public Container getContainer() {
119 return (source instanceof Container) ? (Container)source : null;
120 }
121
122 /**
123 * Returns the component that was affected by the event.
124 *
125 * @return the Component object that was added or removed
126 */
127 public Component getChild() {
128 return child;
129 }
130
131 /**
132 * Returns a parameter string identifying this event.
133 * This method is useful for event-logging and for debugging.
134 *
135 * @return a string identifying the event and its attributes
136 */
137 public String paramString() {
138 String typeStr;
139 switch(id) {
140 case COMPONENT_ADDED:
141 typeStr = "COMPONENT_ADDED";
142 break;
143 case COMPONENT_REMOVED:
144 typeStr = "COMPONENT_REMOVED";
145 break;
146 default:
147 typeStr = "unknown type";
148 }
149 return typeStr + ",child="+child.getName();
150 }
151}