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