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