1 /*
   2  * Copyright (c) 1996, 2008, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.event;
  27 
  28 import java.awt.AWTEvent;
  29 import java.awt.Component;
  30 import java.awt.Rectangle;
  31 import javax.tools.annotation.GenerateNativeHeader;
  32 
  33 /**
  34  * A low-level event which indicates that a component moved, changed
  35  * size, or changed visibility (also, the root class for the other
  36  * component-level events).
  37  * <P>
  38  * Component events are provided for notification purposes ONLY;
  39  * The AWT will automatically handle component moves and resizes
  40  * internally so that GUI layout works properly regardless of
  41  * whether a program is receiving these events or not.
  42  * <P>
  43  * In addition to serving as the base class for other component-related
  44  * events (InputEvent, FocusEvent, WindowEvent, ContainerEvent),
  45  * this class defines the events that indicate changes in
  46  * a component's size, position, or visibility.
  47  * <P>
  48  * This low-level event is generated by a component object (such as a
  49  * List) when the component is moved, resized, rendered invisible, or made
  50  * visible again. The event is passed to every <code>ComponentListener</code>
  51  * or <code>ComponentAdapter</code> object which registered to receive such
  52  * events using the component's <code>addComponentListener</code> method.
  53  * (<code>ComponentAdapter</code> objects implement the
  54  * <code>ComponentListener</code> interface.) Each such listener object
  55  * gets this <code>ComponentEvent</code> when the event occurs.
  56  * <p>
  57  * An unspecified behavior will be caused if the {@code id} parameter
  58  * of any particular {@code ComponentEvent} instance is not
  59  * in the range from {@code COMPONENT_FIRST} to {@code COMPONENT_LAST}.
  60  *
  61  * @see ComponentAdapter
  62  * @see ComponentListener
  63  * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/componentlistener.html">Tutorial: Writing a Component Listener</a>
  64  *
  65  * @author Carl Quinn
  66  * @since 1.1
  67  */
  68 /* No native methods here, but the constants are needed in the supporting JNI code */
  69 @GenerateNativeHeader
  70 public class ComponentEvent extends AWTEvent {
  71 
  72     /**
  73      * The first number in the range of ids used for component events.
  74      */
  75     public static final int COMPONENT_FIRST             = 100;
  76 
  77     /**
  78      * The last number in the range of ids used for component events.
  79      */
  80     public static final int COMPONENT_LAST              = 103;
  81 
  82    /**
  83      * This event indicates that the component's position changed.
  84      */
  85     public static final int COMPONENT_MOVED     = COMPONENT_FIRST;
  86 
  87     /**
  88      * This event indicates that the component's size changed.
  89      */
  90     public static final int COMPONENT_RESIZED   = 1 + COMPONENT_FIRST;
  91 
  92     /**
  93      * This event indicates that the component was made visible.
  94      */
  95     public static final int COMPONENT_SHOWN     = 2 + COMPONENT_FIRST;
  96 
  97     /**
  98      * This event indicates that the component was rendered invisible.
  99      */
 100     public static final int COMPONENT_HIDDEN    = 3 + COMPONENT_FIRST;
 101 
 102     /*
 103      * JDK 1.1 serialVersionUID
 104      */
 105     private static final long serialVersionUID = 8101406823902992965L;
 106 
 107     /**
 108      * Constructs a <code>ComponentEvent</code> object.
 109      * <p> This method throws an
 110      * <code>IllegalArgumentException</code> if <code>source</code>
 111      * is <code>null</code>.
 112      *
 113      * @param source The <code>Component</code> that originated the event
 114      * @param id     An integer indicating the type of event.
 115      *                     For information on allowable values, see
 116      *                     the class description for {@link ComponentEvent}
 117      * @throws IllegalArgumentException if <code>source</code> is null
 118      * @see #getComponent()
 119      * @see #getID()
 120      */
 121     public ComponentEvent(Component source, int id) {
 122         super(source, id);
 123     }
 124 
 125     /**
 126      * Returns the originator of the event.
 127      *
 128      * @return the <code>Component</code> object that originated
 129      * the event, or <code>null</code> if the object is not a
 130      * <code>Component</code>.
 131      */
 132     public Component getComponent() {
 133         return (source instanceof Component) ? (Component)source : null;
 134     }
 135 
 136     /**
 137      * Returns a parameter string identifying this event.
 138      * This method is useful for event-logging and for debugging.
 139      *
 140      * @return a string identifying the event and its attributes
 141      */
 142     public String paramString() {
 143         String typeStr;
 144         Rectangle b = (source !=null
 145                        ? ((Component)source).getBounds()
 146                        : null);
 147 
 148         switch(id) {
 149           case COMPONENT_SHOWN:
 150               typeStr = "COMPONENT_SHOWN";
 151               break;
 152           case COMPONENT_HIDDEN:
 153               typeStr = "COMPONENT_HIDDEN";
 154               break;
 155           case COMPONENT_MOVED:
 156               typeStr = "COMPONENT_MOVED ("+
 157                          b.x+","+b.y+" "+b.width+"x"+b.height+")";
 158               break;
 159           case COMPONENT_RESIZED:
 160               typeStr = "COMPONENT_RESIZED ("+
 161                          b.x+","+b.y+" "+b.width+"x"+b.height+")";
 162               break;
 163           default:
 164               typeStr = "unknown type";
 165         }
 166         return typeStr;
 167     }
 168 }