1 /* 2 * Copyright (c) 1996, 2013, 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 java.lang.annotation.Native; 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="https://docs.oracle.com/javase/tutorial/uiswing/events/componentlistener.html">Tutorial: Writing a Component Listener</a> 64 * 65 * @author Carl Quinn 66 * @since 1.1 67 */ 68 public class ComponentEvent extends AWTEvent { 69 70 /** 71 * The first number in the range of ids used for component events. 72 */ 73 public static final int COMPONENT_FIRST = 100; 74 75 /** 76 * The last number in the range of ids used for component events. 77 */ 78 public static final int COMPONENT_LAST = 103; 79 80 /** 81 * This event indicates that the component's position changed. 82 */ 83 @Native public static final int COMPONENT_MOVED = COMPONENT_FIRST; 84 85 /** 86 * This event indicates that the component's size changed. 87 */ 88 @Native public static final int COMPONENT_RESIZED = 1 + COMPONENT_FIRST; 89 90 /** 91 * This event indicates that the component was made visible. 92 */ 93 @Native public static final int COMPONENT_SHOWN = 2 + COMPONENT_FIRST; 94 95 /** 96 * This event indicates that the component was rendered invisible. 97 */ 98 @Native public static final int COMPONENT_HIDDEN = 3 + COMPONENT_FIRST; 99 100 /* 101 * JDK 1.1 serialVersionUID 102 */ 103 private static final long serialVersionUID = 8101406823902992965L; 104 105 /** 106 * Constructs a <code>ComponentEvent</code> object. 107 * <p> This method throws an 108 * <code>IllegalArgumentException</code> if <code>source</code> 109 * is <code>null</code>. 110 * 111 * @param source The <code>Component</code> that originated the event 112 * @param id An integer indicating the type of event. 113 * For information on allowable values, see 114 * the class description for {@link ComponentEvent} 115 * @throws IllegalArgumentException if <code>source</code> is null 116 * @see #getComponent() 117 * @see #getID() 118 */ 119 public ComponentEvent(Component source, int id) { 120 super(source, id); 121 } 122 123 /** 124 * Returns the originator of the event. 125 * 126 * @return the <code>Component</code> object that originated 127 * the event, or <code>null</code> if the object is not a 128 * <code>Component</code>. 129 */ 130 public Component getComponent() { 131 return (source instanceof Component) ? (Component)source : null; 132 } 133 134 /** 135 * Returns a parameter string identifying this event. 136 * This method is useful for event-logging and for debugging. 137 * 138 * @return a string identifying the event and its attributes 139 */ 140 public String paramString() { 141 String typeStr; 142 Rectangle b = (source !=null 143 ? ((Component)source).getBounds() 144 : null); 145 146 switch(id) { 147 case COMPONENT_SHOWN: 148 typeStr = "COMPONENT_SHOWN"; 149 break; 150 case COMPONENT_HIDDEN: 151 typeStr = "COMPONENT_HIDDEN"; 152 break; 153 case COMPONENT_MOVED: 154 typeStr = "COMPONENT_MOVED ("+ 155 b.x+","+b.y+" "+b.width+"x"+b.height+")"; 156 break; 157 case COMPONENT_RESIZED: 158 typeStr = "COMPONENT_RESIZED ("+ 159 b.x+","+b.y+" "+b.width+"x"+b.height+")"; 160 break; 161 default: 162 typeStr = "unknown type"; 163 } 164 return typeStr; 165 } 166 }