1. /*
  2. * @(#)ComponentEvent.java 1.25 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.AWTEvent;
  9. import java.awt.Event;
  10. import java.awt.Component;
  11. import java.awt.Rectangle;
  12. /**
  13. * A low-level event which indicates that a component moved, changed
  14. * size, or changed visibility (also, the root class for the other
  15. * component-level events).
  16. * <P>
  17. * Component events are provided for notification purposes ONLY;
  18. * The AWT will automatically handle component moves and resizes
  19. * internally so that GUI layout works properly regardless of
  20. * whether a program is receiving these events or not.
  21. * <P>
  22. * In addition to serving as the base class for other component-related
  23. * events (InputEvent, FocusEvent, WindowEvent, ContainerEvent),
  24. * this class defines the events that indicate changes in
  25. * a component's size, position, or visibility.
  26. * <P>
  27. * This low-level event is generated by a component object (such as a
  28. * List) when the component is moved, resized, rendered invisible, or made
  29. * visible again. The event is passed to every <code>ComponentListener</code>
  30. * or <code>ComponentAdapter</code> object which registered to receive such
  31. * events using the component's <code>addComponentListener</code> method.
  32. * (<code>ComponentAdapter</code> objects implement the
  33. * <code>ComponentListener</code> interface.) Each such listener object
  34. * gets this <code>ComponentEvent</code> when the event occurs.
  35. *
  36. * @see ComponentAdapter
  37. * @see ComponentListener
  38. * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/componentlistener.html">Tutorial: Writing a Component Listener</a>
  39. * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  40. *
  41. * @author Carl Quinn
  42. * @version 1.25 01/23/03
  43. * @since 1.1
  44. */
  45. public class ComponentEvent extends AWTEvent {
  46. /**
  47. * The first number in the range of ids used for component events.
  48. */
  49. public static final int COMPONENT_FIRST = 100;
  50. /**
  51. * The last number in the range of ids used for component events.
  52. */
  53. public static final int COMPONENT_LAST = 103;
  54. /**
  55. * This event indicates that the component's position changed.
  56. */
  57. public static final int COMPONENT_MOVED = COMPONENT_FIRST;
  58. /**
  59. * This event indicates that the component's size changed.
  60. */
  61. public static final int COMPONENT_RESIZED = 1 + COMPONENT_FIRST;
  62. /**
  63. * This event indicates that the component was made visible.
  64. */
  65. public static final int COMPONENT_SHOWN = 2 + COMPONENT_FIRST;
  66. /**
  67. * This event indicates that the component was rendered invisible.
  68. */
  69. public static final int COMPONENT_HIDDEN = 3 + COMPONENT_FIRST;
  70. /*
  71. * JDK 1.1 serialVersionUID
  72. */
  73. private static final long serialVersionUID = 8101406823902992965L;
  74. /**
  75. * Constructs a <code>ComponentEvent</code> object.
  76. * <p>Note that passing in an invalid <code>id</code> results in
  77. * unspecified behavior.
  78. *
  79. * @param source the <code>Component</code> that originated the event
  80. * @param id an integer indicating the type of event
  81. */
  82. public ComponentEvent(Component source, int id) {
  83. super(source, id);
  84. }
  85. /**
  86. * Returns the originator of the event.
  87. *
  88. * @return the <code>Component</code> object that originated
  89. * the event, or <code>null</code> if the object is not a
  90. * <code>Component</code>.
  91. */
  92. public Component getComponent() {
  93. return (source instanceof Component) ? (Component)source : null;
  94. }
  95. /**
  96. * Returns a parameter string identifying this event.
  97. * This method is useful for event-logging and for debugging.
  98. *
  99. * @return a string identifying the event and its attributes
  100. */
  101. public String paramString() {
  102. String typeStr;
  103. Rectangle b = (source !=null
  104. ? ((Component)source).getBounds()
  105. : null);
  106. switch(id) {
  107. case COMPONENT_SHOWN:
  108. typeStr = "COMPONENT_SHOWN";
  109. break;
  110. case COMPONENT_HIDDEN:
  111. typeStr = "COMPONENT_HIDDEN";
  112. break;
  113. case COMPONENT_MOVED:
  114. typeStr = "COMPONENT_MOVED ("+
  115. b.x+","+b.y+" "+b.width+"x"+b.height+")";
  116. break;
  117. case COMPONENT_RESIZED:
  118. typeStr = "COMPONENT_RESIZED ("+
  119. b.x+","+b.y+" "+b.width+"x"+b.height+")";
  120. break;
  121. default:
  122. typeStr = "unknown type";
  123. }
  124. return typeStr;
  125. }
  126. }