1. /*
  2. * @(#)ComponentEvent.java 1.27 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.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.27 12/19/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. This method throws an
  78. * <code>IllegalArgumentException</code> if <code>source</code>
  79. * is <code>null</code>.
  80. *
  81. * @param source the <code>Component</code> that originated the event
  82. * @param id an integer indicating the type of event
  83. * @throws IllegalArgumentException if <code>source</code> is null
  84. */
  85. public ComponentEvent(Component source, int id) {
  86. super(source, id);
  87. }
  88. /**
  89. * Returns the originator of the event.
  90. *
  91. * @return the <code>Component</code> object that originated
  92. * the event, or <code>null</code> if the object is not a
  93. * <code>Component</code>.
  94. */
  95. public Component getComponent() {
  96. return (source instanceof Component) ? (Component)source : null;
  97. }
  98. /**
  99. * Returns a parameter string identifying this event.
  100. * This method is useful for event-logging and for debugging.
  101. *
  102. * @return a string identifying the event and its attributes
  103. */
  104. public String paramString() {
  105. String typeStr;
  106. Rectangle b = (source !=null
  107. ? ((Component)source).getBounds()
  108. : null);
  109. switch(id) {
  110. case COMPONENT_SHOWN:
  111. typeStr = "COMPONENT_SHOWN";
  112. break;
  113. case COMPONENT_HIDDEN:
  114. typeStr = "COMPONENT_HIDDEN";
  115. break;
  116. case COMPONENT_MOVED:
  117. typeStr = "COMPONENT_MOVED ("+
  118. b.x+","+b.y+" "+b.width+"x"+b.height+")";
  119. break;
  120. case COMPONENT_RESIZED:
  121. typeStr = "COMPONENT_RESIZED ("+
  122. b.x+","+b.y+" "+b.width+"x"+b.height+")";
  123. break;
  124. default:
  125. typeStr = "unknown type";
  126. }
  127. return typeStr;
  128. }
  129. }