1. /*
  2. * @(#)FocusEvent.java 1.22 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt.event;
  11. import java.awt.Component;
  12. import java.awt.Event;
  13. /**
  14. * A low-level event which indicates that a component has gained
  15. * or lost the keyboard focus.
  16. * This low-level event is generated by a component (such as a text field).
  17. * The event is passed to every <code>FocusListener</code>
  18. * or <code>FocusAdapter</code> object which registered to receive such
  19. * events using the component's <code>addFocusListener</code> method.
  20. * (<code>FocusAdapter</code> objects implement the
  21. * <code>FocusListener</code> interface.) Each such listener object
  22. * gets this <code>FocusEvent</code> when the event occurs.
  23. * <P>
  24. * There are two levels of focus change events: permanent and temporary.
  25. * Permanent focus change events occur when focus is directly moved
  26. * from one component to another, such as through calls to requestFocus()
  27. * or as the user uses the Tab key to traverse components.
  28. * Temporary focus change events occur when focus is temporarily
  29. * gained or lost for a component as the indirect result of another
  30. * operation, such as window deactivation or a scrollbar drag. In this
  31. * case, the original focus state will automatically be restored once
  32. * that operation is finished, or, for the case of window deactivation,
  33. * when the window is reactivated. Both permanent and temporary focus
  34. * events are delivered using the FOCUS_GAINED and FOCUS_LOST event ids;
  35. * the levels may be distinguished in the event using the isTemporary()
  36. * method.
  37. *
  38. * @see FocusAdapter
  39. * @see FocusListener
  40. * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/focuslistener.html">Tutorial: Writing a Focus Listener</a>
  41. * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  42. *
  43. * @author Carl Quinn
  44. * @author Amy Fowler
  45. * @version 1.22 02/02/00
  46. * @since 1.1
  47. */
  48. public class FocusEvent extends ComponentEvent {
  49. /**
  50. * The first number in the range of ids used for focus events.
  51. */
  52. public static final int FOCUS_FIRST = 1004;
  53. /**
  54. * The last number in the range of ids used for focus events.
  55. */
  56. public static final int FOCUS_LAST = 1005;
  57. /**
  58. * This event indicates that the component gained the keyboard focus.
  59. */
  60. public static final int FOCUS_GAINED = FOCUS_FIRST; //Event.GOT_FOCUS
  61. /**
  62. * This event indicates that the component lost the keyboard focus.
  63. */
  64. public static final int FOCUS_LOST = 1 + FOCUS_FIRST; //Event.LOST_FOCUS
  65. /**
  66. * A focus event can have two different levels,
  67. * permanent and temporary. It will be set to true if some
  68. * operation takes away the focus temporarily and
  69. * intends on getting it back once the event is completed.
  70. * Otherwise it will be set to false.
  71. *
  72. * @serial
  73. * @see isTemporary()
  74. */
  75. boolean temporary = false;
  76. /*
  77. * JDK 1.1 serialVersionUID
  78. */
  79. private static final long serialVersionUID = 523753786457416396L;
  80. /**
  81. * Constructs a FocusEvent object and identifies whether or not the
  82. * change is temporary.
  83. *
  84. * @param source the Component that originated the event
  85. * @param id an integer indicating the type of event
  86. * @param temporary a boolean, true if the focus change is temporary
  87. */
  88. public FocusEvent(Component source, int id, boolean temporary) {
  89. super(source, id);
  90. this.temporary = temporary;
  91. }
  92. /**
  93. * Constructs a FocusEvent object and identifies it as a permanent
  94. * change in focus.
  95. *
  96. * @param source the Component that originated the event
  97. * @param id an integer indicating the type of event
  98. */
  99. public FocusEvent(Component source, int id) {
  100. this(source, id, false);
  101. }
  102. /**
  103. * Identifies the focus change event as temporary or permanent.
  104. *
  105. * @return a boolean value, true if the focus change is temporary
  106. */
  107. public boolean isTemporary() {
  108. return temporary;
  109. }
  110. /**
  111. * Returns a parameter string identifying this event.
  112. * This method is useful for event-logging and for debugging.
  113. *
  114. * @return a string identifying the event and its attributes
  115. */
  116. public String paramString() {
  117. String typeStr;
  118. switch(id) {
  119. case FOCUS_GAINED:
  120. typeStr = "FOCUS_GAINED";
  121. break;
  122. case FOCUS_LOST:
  123. typeStr = "FOCUS_LOST";
  124. break;
  125. default:
  126. typeStr = "unknown type";
  127. }
  128. return typeStr + (temporary? ",temporary" : ",permanent");
  129. }
  130. }