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