jdk/src/share/classes/java/awt/event/ComponentEvent.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.AWTEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A low-level event which indicates that a component moved, changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * size, or changed visibility (also, the root class for the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * component-level events).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Component events are provided for notification purposes ONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The AWT will automatically handle component moves and resizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * internally so that GUI layout works properly regardless of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * whether a program is receiving these events or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * In addition to serving as the base class for other component-related
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * events (InputEvent, FocusEvent, WindowEvent, ContainerEvent),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * this class defines the events that indicate changes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * a component's size, position, or visibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This low-level event is generated by a component object (such as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * List) when the component is moved, resized, rendered invisible, or made
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * visible again. The event is passed to every <code>ComponentListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * or <code>ComponentAdapter</code> object which registered to receive such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * events using the component's <code>addComponentListener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * (<code>ComponentAdapter</code> objects implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>ComponentListener</code> interface.) Each such listener object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * gets this <code>ComponentEvent</code> when the event occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see ComponentAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see ComponentListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/componentlistener.html">Tutorial: Writing a Component Listener</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Carl Quinn
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class ComponentEvent extends AWTEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The first number in the range of ids used for component events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static final int COMPONENT_FIRST             = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * The last number in the range of ids used for component events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static final int COMPONENT_LAST              = 103;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * This event indicates that the component's position changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static final int COMPONENT_MOVED     = COMPONENT_FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * This event indicates that the component's size changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static final int COMPONENT_RESIZED   = 1 + COMPONENT_FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * This event indicates that the component was made visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static final int COMPONENT_SHOWN     = 2 + COMPONENT_FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * This event indicates that the component was rendered invisible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static final int COMPONENT_HIDDEN    = 3 + COMPONENT_FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final long serialVersionUID = 8101406823902992965L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Constructs a <code>ComponentEvent</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <p>Note that passing in an invalid <code>id</code> results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * unspecified behavior. This method throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <code>IllegalArgumentException</code> if <code>source</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param source the <code>Component</code> that originated the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param id     an integer indicating the type of event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @throws IllegalArgumentException if <code>source</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public ComponentEvent(Component source, int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        super(source, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Returns the originator of the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return the <code>Component</code> object that originated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * the event, or <code>null</code> if the object is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public Component getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return (source instanceof Component) ? (Component)source : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Returns a parameter string identifying this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * This method is useful for event-logging and for debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @return a string identifying the event and its attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        String typeStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Rectangle b = (source !=null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                       ? ((Component)source).getBounds()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                       : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        switch(id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
          case COMPONENT_SHOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
              typeStr = "COMPONENT_SHOWN";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
          case COMPONENT_HIDDEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
              typeStr = "COMPONENT_HIDDEN";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
          case COMPONENT_MOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
              typeStr = "COMPONENT_MOVED ("+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                         b.x+","+b.y+" "+b.width+"x"+b.height+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
          case COMPONENT_RESIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
              typeStr = "COMPONENT_RESIZED ("+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                         b.x+","+b.y+" "+b.width+"x"+b.height+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
              typeStr = "unknown type";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return typeStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}