1. /*
  2. * @(#)FocusEvent.java 1.28 03/01/23
  3. *
  4. * Copyright 2003 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.28 01/23/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.
  100. *
  101. * @param source the <code>Component</code> that originated the event
  102. * @param id <code>FOCUS_GAINED</code> or <code>FOCUS_LOST</code>
  103. * @param temporary <code>true</code> if the focus change is temporary;
  104. * <code>false</code> otherwise
  105. * @param opposite the other Component involved in the focus change,
  106. * or <code>null</code>
  107. */
  108. public FocusEvent(Component source, int id, boolean temporary,
  109. Component opposite) {
  110. super(source, id);
  111. this.temporary = temporary;
  112. this.opposite = opposite;
  113. }
  114. /**
  115. * Constructs a <code>FocusEvent</code> object and identifies
  116. * whether or not the change is temporary.
  117. * <p>Note that passing in an invalid <code>id</code> results in
  118. * unspecified behavior.
  119. *
  120. * @param source the <code>Component</code> that originated the event
  121. * @param id an integer indicating the type of event
  122. * @param temporary <code>true</code> if the focus change is temporary;
  123. * <code>false</code> otherwise
  124. */
  125. public FocusEvent(Component source, int id, boolean temporary) {
  126. this(source, id, temporary, null);
  127. }
  128. /**
  129. * Constructs a <code>FocusEvent</code> object and identifies it
  130. * as a permanent change in focus.
  131. * <p>Note that passing in an invalid <code>id</code> results in
  132. * unspecified behavior.
  133. *
  134. * @param source the <code>Component</code> that originated the event
  135. * @param id an integer indicating the type of event
  136. */
  137. public FocusEvent(Component source, int id) {
  138. this(source, id, false);
  139. }
  140. /**
  141. * Identifies the focus change event as temporary or permanent.
  142. *
  143. * @return <code>true</code> if the focus change is temporary;
  144. * <code>false</code> otherwise
  145. */
  146. public boolean isTemporary() {
  147. return temporary;
  148. }
  149. /**
  150. * Returns the other Component involved in this focus change. For a
  151. * FOCUS_GAINED event, this is the Component that lost focus. For a
  152. * FOCUS_LOST event, this is the Component that gained focus. If this
  153. * focus change occurs with a native application, with a Java application
  154. * in a different VM or context, or with no other Component, then null is
  155. * returned.
  156. *
  157. * @return the other Component involved in the focus change, or null
  158. * @since 1.4
  159. */
  160. public Component getOppositeComponent() {
  161. if (opposite == null) {
  162. return null;
  163. }
  164. return (SunToolkit.targetToAppContext(opposite) ==
  165. AppContext.getAppContext())
  166. ? opposite
  167. : null;
  168. }
  169. /**
  170. * Returns a parameter string identifying this event.
  171. * This method is useful for event-logging and for debugging.
  172. *
  173. * @return a string identifying the event and its attributes
  174. */
  175. public String paramString() {
  176. String typeStr;
  177. switch(id) {
  178. case FOCUS_GAINED:
  179. typeStr = "FOCUS_GAINED";
  180. break;
  181. case FOCUS_LOST:
  182. typeStr = "FOCUS_LOST";
  183. break;
  184. default:
  185. typeStr = "unknown type";
  186. }
  187. return typeStr + (temporary ? ",temporary" : ",permanent") +
  188. ",opposite=" + getOppositeComponent();
  189. }
  190. }