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.Component; |
| 29 | import sun.awt.AppContext; |
| 30 | import sun.awt.SunToolkit; |
| 31 | |
| 32 | /** |
| 33 | * A low-level event which indicates that a Component has gained or lost the |
| 34 | * input focus. This low-level event is generated by a Component (such as a |
| 35 | * TextField). The event is passed to every <code>FocusListener</code> or |
| 36 | * <code>FocusAdapter</code> object which registered to receive such events |
| 37 | * using the Component's <code>addFocusListener</code> method. (<code> |
| 38 | * FocusAdapter</code> objects implement the <code>FocusListener</code> |
| 39 | * interface.) Each such listener object gets this <code>FocusEvent</code> when |
| 40 | * the event occurs. |
| 41 | * <p> |
| 42 | * There are two levels of focus events: permanent and temporary. Permanent |
| 43 | * focus change events occur when focus is directly moved from one Component to |
| 44 | * another, such as through a call to requestFocus() or as the user uses the |
| 45 | * TAB key to traverse Components. Temporary focus change events occur when |
| 46 | * focus is temporarily lost for a Component as the indirect result of another |
| 47 | * operation, such as Window deactivation or a Scrollbar drag. In this case, |
| 48 | * the original focus state will automatically be restored once that operation |
| 49 | * is finished, or, for the case of Window deactivation, when the Window is |
| 50 | * reactivated. Both permanent and temporary focus events are delivered using |
| 51 | * the FOCUS_GAINED and FOCUS_LOST event ids; the level may be distinguished in |
| 52 | * the event using the isTemporary() method. |
| 53 | * |
| 54 | * @see FocusAdapter |
| 55 | * @see FocusListener |
| 56 | * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/focuslistener.html">Tutorial: Writing a Focus Listener</a> |
| 57 | * |
| 58 | * @author Carl Quinn |
| 59 | * @author Amy Fowler |
| 60 | * @since 1.1 |
| 61 | */ |
| 62 | public class FocusEvent extends ComponentEvent { |
| 63 | |
| 64 | /** |
| 65 | * The first number in the range of ids used for focus events. |
| 66 | */ |
| 67 | public static final int FOCUS_FIRST = 1004; |
| 68 | |
| 69 | /** |
| 70 | * The last number in the range of ids used for focus events. |
| 71 | */ |
| 72 | public static final int FOCUS_LAST = 1005; |
| 73 | |
| 74 | /** |
| 75 | * This event indicates that the Component is now the focus owner. |
| 76 | */ |
| 77 | public static final int FOCUS_GAINED = FOCUS_FIRST; //Event.GOT_FOCUS |
| 78 | |
| 79 | /** |
| 80 | * This event indicates that the Component is no longer the focus owner. |
| 81 | */ |
| 82 | public static final int FOCUS_LOST = 1 + FOCUS_FIRST; //Event.LOST_FOCUS |
| 83 | |
| 84 | /** |
| 85 | * A focus event can have two different levels, permanent and temporary. |
| 86 | * It will be set to true if some operation takes away the focus |
| 87 | * temporarily and intends on getting it back once the event is completed. |
| 88 | * Otherwise it will be set to false. |
| 89 | * |
| 90 | * @serial |
| 91 | * @see #isTemporary |
| 92 | */ |
| 93 | boolean temporary; |
| 94 | |
| 95 | /** |
| 96 | * The other Component involved in this focus change. For a FOCUS_GAINED |
| 97 | * event, this is the Component that lost focus. For a FOCUS_LOST event, |
| 98 | * this is the Component that gained focus. If this focus change occurs |
| 99 | * with a native application, a Java application in a different VM, or with |
| 100 | * no other Component, then the opposite Component is null. |
| 101 | * |
| 102 | * @see #getOppositeComponent |
| 103 | * @since 1.4 |
| 104 | */ |
| 105 | transient Component opposite; |
| 106 | |
| 107 | /* |
| 108 | * JDK 1.1 serialVersionUID |
| 109 | */ |
| 110 | private static final long serialVersionUID = 523753786457416396L; |
| 111 | |
| 112 | /** |
| 113 | * Constructs a <code>FocusEvent</code> object with the |
| 114 | * specified temporary state and opposite <code>Component</code>. |
| 115 | * The opposite <code>Component</code> is the other |
| 116 | * <code>Component</code> involved in this focus change. |
| 117 | * For a <code>FOCUS_GAINED</code> event, this is the |
| 118 | * <code>Component</code> that lost focus. For a |
| 119 | * <code>FOCUS_LOST</code> event, this is the <code>Component</code> |
| 120 | * that gained focus. If this focus change occurs with a native |
| 121 | * application, with a Java application in a different VM, |
| 122 | * or with no other <code>Component</code>, then the opposite |
| 123 | * <code>Component</code> is <code>null</code>. |
| 124 | * <p>Note that passing in an invalid <code>id</code> results in |
| 125 | * unspecified behavior. This method throws an |
| 126 | * <code>IllegalArgumentException</code> if <code>source</code> |
| 127 | * is <code>null</code>. |
| 128 | * |
| 129 | * @param source the <code>Component</code> that originated the event |
| 130 | * @param id <code>FOCUS_GAINED</code> or <code>FOCUS_LOST</code> |
| 131 | * @param temporary <code>true</code> if the focus change is temporary; |
| 132 | * <code>false</code> otherwise |
| 133 | * @param opposite the other Component involved in the focus change, |
| 134 | * or <code>null</code> |
| 135 | * @throws IllegalArgumentException if <code>source</code> is null |
| 136 | * @since 1.4 |
| 137 | */ |
| 138 | public FocusEvent(Component source, int id, boolean temporary, |
| 139 | Component opposite) { |
| 140 | super(source, id); |
| 141 | this.temporary = temporary; |
| 142 | this.opposite = opposite; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Constructs a <code>FocusEvent</code> object and identifies |
| 147 | * whether or not the change is temporary. |
| 148 | * <p>Note that passing in an invalid <code>id</code> results in |
| 149 | * unspecified behavior. This method throws an |
| 150 | * <code>IllegalArgumentException</code> if <code>source</code> |
| 151 | * is <code>null</code>. |
| 152 | * |
| 153 | * @param source the <code>Component</code> that originated the event |
| 154 | * @param id an integer indicating the type of event |
| 155 | * @param temporary <code>true</code> if the focus change is temporary; |
| 156 | * <code>false</code> otherwise |
| 157 | * @throws IllegalArgumentException if <code>source</code> is null |
| 158 | */ |
| 159 | public FocusEvent(Component source, int id, boolean temporary) { |
| 160 | this(source, id, temporary, null); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Constructs a <code>FocusEvent</code> object and identifies it |
| 165 | * as a permanent change in focus. |
| 166 | * <p>Note that passing in an invalid <code>id</code> results in |
| 167 | * unspecified behavior. This method throws an |
| 168 | * <code>IllegalArgumentException</code> if <code>source</code> |
| 169 | * is <code>null</code>. |
| 170 | * |
| 171 | * @param source the <code>Component</code> that originated the event |
| 172 | * @param id an integer indicating the type of event |
| 173 | * @throws IllegalArgumentException if <code>source</code> is null |
| 174 | */ |
| 175 | public FocusEvent(Component source, int id) { |
| 176 | this(source, id, false); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Identifies the focus change event as temporary or permanent. |
| 181 | * |
| 182 | * @return <code>true</code> if the focus change is temporary; |
| 183 | * <code>false</code> otherwise |
| 184 | */ |
| 185 | public boolean isTemporary() { |
| 186 | return temporary; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Returns the other Component involved in this focus change. For a |
| 191 | * FOCUS_GAINED event, this is the Component that lost focus. For a |
| 192 | * FOCUS_LOST event, this is the Component that gained focus. If this |
| 193 | * focus change occurs with a native application, with a Java application |
| 194 | * in a different VM or context, or with no other Component, then null is |
| 195 | * returned. |
| 196 | * |
| 197 | * @return the other Component involved in the focus change, or null |
| 198 | * @since 1.4 |
| 199 | */ |
| 200 | public Component getOppositeComponent() { |
| 201 | if (opposite == null) { |
| 202 | return null; |
| 203 | } |
| 204 | |
| 205 | return (SunToolkit.targetToAppContext(opposite) == |
| 206 | AppContext.getAppContext()) |
| 207 | ? opposite |
| 208 | : null; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Returns a parameter string identifying this event. |
| 213 | * This method is useful for event-logging and for debugging. |
| 214 | * |
| 215 | * @return a string identifying the event and its attributes |
| 216 | */ |
| 217 | public String paramString() { |
| 218 | String typeStr; |
| 219 | switch(id) { |
| 220 | case FOCUS_GAINED: |
| 221 | typeStr = "FOCUS_GAINED"; |
| 222 | break; |
| 223 | case FOCUS_LOST: |
| 224 | typeStr = "FOCUS_LOST"; |
| 225 | break; |
| 226 | default: |
| 227 | typeStr = "unknown type"; |
| 228 | } |
| 229 | return typeStr + (temporary ? ",temporary" : ",permanent") + |
| 230 | ",opposite=" + getOppositeComponent(); |
| 231 | } |
| 232 | |
| 233 | } |