1. /*
  2. * @(#)ComponentEvent.java 1.19 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.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. * @version 1.19 11/29/01
  42. * @author Carl Quinn
  43. */
  44. public class ComponentEvent extends AWTEvent {
  45. /**
  46. * The first number in the range of ids used for component events.
  47. */
  48. public static final int COMPONENT_FIRST = 100;
  49. /**
  50. * The last number in the range of ids used for component events.
  51. */
  52. public static final int COMPONENT_LAST = 103;
  53. /**
  54. * This event indicates that the component's position changed.
  55. */
  56. public static final int COMPONENT_MOVED = COMPONENT_FIRST;
  57. /**
  58. * This event indicates that the component's size changed.
  59. */
  60. public static final int COMPONENT_RESIZED = 1 + COMPONENT_FIRST;
  61. /**
  62. * This event indicates that the component was made visible.
  63. */
  64. public static final int COMPONENT_SHOWN = 2 + COMPONENT_FIRST;
  65. /**
  66. * This event indicates that the component was rendered invisible.
  67. */
  68. public static final int COMPONENT_HIDDEN = 3 + COMPONENT_FIRST;
  69. /*
  70. * JDK 1.1 serialVersionUID
  71. */
  72. private static final long serialVersionUID = 8101406823902992965L;
  73. /**
  74. * Constructs a ComponentEvent object.
  75. *
  76. * @param source the Component object that originated the event
  77. * @param id an integer indicating the type of event
  78. */
  79. public ComponentEvent(Component source, int id) {
  80. super(source, id);
  81. }
  82. /**
  83. * Returns the originator of the event.
  84. *
  85. * @return the Component object that originated the event
  86. */
  87. public Component getComponent() {
  88. // return (source instanceof Component) ? (Component)source : null;
  89. return (Component)source; // cast should always be OK, type was checked in constructor
  90. }
  91. /**
  92. * Returns a parameter string identifying this event.
  93. * This method is useful for event-logging and for debugging.
  94. *
  95. * @return a string identifying the event and its attributes
  96. */
  97. public String paramString() {
  98. String typeStr;
  99. Rectangle b = (source !=null
  100. ? ((Component)source).getBounds()
  101. : null);
  102. switch(id) {
  103. case COMPONENT_SHOWN:
  104. typeStr = "COMPONENT_SHOWN";
  105. break;
  106. case COMPONENT_HIDDEN:
  107. typeStr = "COMPONENT_HIDDEN";
  108. break;
  109. case COMPONENT_MOVED:
  110. typeStr = "COMPONENT_MOVED ("+
  111. b.x+","+b.y+" "+b.width+"x"+b.height+")";
  112. break;
  113. case COMPONENT_RESIZED:
  114. typeStr = "COMPONENT_RESIZED ("+
  115. b.x+","+b.y+" "+b.width+"x"+b.height+")";
  116. break;
  117. default:
  118. typeStr = "unknown type";
  119. }
  120. return typeStr;
  121. }
  122. }