001 /*
002 * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.event;
027
028 import java.awt.AWTEvent;
029 import java.awt.Component;
030 import java.awt.Rectangle;
031
032 /**
033 * A low-level event which indicates that a component moved, changed
034 * size, or changed visibility (also, the root class for the other
035 * component-level events).
036 * <P>
037 * Component events are provided for notification purposes ONLY;
038 * The AWT will automatically handle component moves and resizes
039 * internally so that GUI layout works properly regardless of
040 * whether a program is receiving these events or not.
041 * <P>
042 * In addition to serving as the base class for other component-related
043 * events (InputEvent, FocusEvent, WindowEvent, ContainerEvent),
044 * this class defines the events that indicate changes in
045 * a component's size, position, or visibility.
046 * <P>
047 * This low-level event is generated by a component object (such as a
048 * List) when the component is moved, resized, rendered invisible, or made
049 * visible again. The event is passed to every <code>ComponentListener</code>
050 * or <code>ComponentAdapter</code> object which registered to receive such
051 * events using the component's <code>addComponentListener</code> method.
052 * (<code>ComponentAdapter</code> objects implement the
053 * <code>ComponentListener</code> interface.) Each such listener object
054 * gets this <code>ComponentEvent</code> when the event occurs.
055 *
056 * @see ComponentAdapter
057 * @see ComponentListener
058 * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/componentlistener.html">Tutorial: Writing a Component Listener</a>
059 *
060 * @author Carl Quinn
061 * @version 1.37 06/05/07
062 * @since 1.1
063 */
064 public class ComponentEvent extends AWTEvent {
065
066 /**
067 * The first number in the range of ids used for component events.
068 */
069 public static final int COMPONENT_FIRST = 100;
070
071 /**
072 * The last number in the range of ids used for component events.
073 */
074 public static final int COMPONENT_LAST = 103;
075
076 /**
077 * This event indicates that the component's position changed.
078 */
079 public static final int COMPONENT_MOVED = COMPONENT_FIRST;
080
081 /**
082 * This event indicates that the component's size changed.
083 */
084 public static final int COMPONENT_RESIZED = 1 + COMPONENT_FIRST;
085
086 /**
087 * This event indicates that the component was made visible.
088 */
089 public static final int COMPONENT_SHOWN = 2 + COMPONENT_FIRST;
090
091 /**
092 * This event indicates that the component was rendered invisible.
093 */
094 public static final int COMPONENT_HIDDEN = 3 + COMPONENT_FIRST;
095
096 /*
097 * JDK 1.1 serialVersionUID
098 */
099 private static final long serialVersionUID = 8101406823902992965L;
100
101 /**
102 * Constructs a <code>ComponentEvent</code> object.
103 * <p>Note that passing in an invalid <code>id</code> results in
104 * unspecified behavior. This method throws an
105 * <code>IllegalArgumentException</code> if <code>source</code>
106 * is <code>null</code>.
107 *
108 * @param source the <code>Component</code> that originated the event
109 * @param id an integer indicating the type of event
110 * @throws IllegalArgumentException if <code>source</code> is null
111 */
112 public ComponentEvent(Component source, int id) {
113 super (source, id);
114 }
115
116 /**
117 * Returns the originator of the event.
118 *
119 * @return the <code>Component</code> object that originated
120 * the event, or <code>null</code> if the object is not a
121 * <code>Component</code>.
122 */
123 public Component getComponent() {
124 return (source instanceof Component) ? (Component) source
125 : null;
126 }
127
128 /**
129 * Returns a parameter string identifying this event.
130 * This method is useful for event-logging and for debugging.
131 *
132 * @return a string identifying the event and its attributes
133 */
134 public String paramString() {
135 String typeStr;
136 Rectangle b = (source != null ? ((Component) source)
137 .getBounds() : null);
138
139 switch (id) {
140 case COMPONENT_SHOWN:
141 typeStr = "COMPONENT_SHOWN";
142 break;
143 case COMPONENT_HIDDEN:
144 typeStr = "COMPONENT_HIDDEN";
145 break;
146 case COMPONENT_MOVED:
147 typeStr = "COMPONENT_MOVED (" + b.x + "," + b.y + " "
148 + b.width + "x" + b.height + ")";
149 break;
150 case COMPONENT_RESIZED:
151 typeStr = "COMPONENT_RESIZED (" + b.x + "," + b.y + " "
152 + b.width + "x" + b.height + ")";
153 break;
154 default:
155 typeStr = "unknown type";
156 }
157 return typeStr;
158 }
159 }
|