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