1. /*
  2. * @(#)ComponentEvent.java 1.21 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.AWTEvent;
  12. import java.awt.Event;
  13. import java.awt.Component;
  14. import java.awt.Rectangle;
  15. /**
  16. * A low-level event which indicates that a component moved, changed
  17. * size, or changed visibility (also, the root class for the other
  18. * component-level events).
  19. * <P>
  20. * Component events are provided for notification purposes ONLY;
  21. * The AWT will automatically handle component moves and resizes
  22. * internally so that GUI layout works properly regardless of
  23. * whether a program is receiving these events or not.
  24. * <P>
  25. * In addition to serving as the base class for other component-related
  26. * events (InputEvent, FocusEvent, WindowEvent, ContainerEvent),
  27. * this class defines the events that indicate changes in
  28. * a component's size, position, or visibility.
  29. * <P>
  30. * This low-level event is generated by a component object (such as a
  31. * List) when the component is moved, resized, rendered invisible, or made
  32. * visible again. The event is passed to every <code>ComponentListener</code>
  33. * or <code>ComponentAdapter</code> object which registered to receive such
  34. * events using the component's <code>addComponentListener</code> method.
  35. * (<code>ComponentAdapter</code> objects implement the
  36. * <code>ComponentListener</code> interface.) Each such listener object
  37. * gets this <code>ComponentEvent</code> when the event occurs.
  38. *
  39. * @see ComponentAdapter
  40. * @see ComponentListener
  41. * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/componentlistener.html">Tutorial: Writing a Component Listener</a>
  42. * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  43. *
  44. * @author Carl Quinn
  45. * @version 1.21 02/02/00
  46. * @since 1.1
  47. */
  48. public class ComponentEvent extends AWTEvent {
  49. /**
  50. * The first number in the range of ids used for component events.
  51. */
  52. public static final int COMPONENT_FIRST = 100;
  53. /**
  54. * The last number in the range of ids used for component events.
  55. */
  56. public static final int COMPONENT_LAST = 103;
  57. /**
  58. * This event indicates that the component's position changed.
  59. */
  60. public static final int COMPONENT_MOVED = COMPONENT_FIRST;
  61. /**
  62. * This event indicates that the component's size changed.
  63. */
  64. public static final int COMPONENT_RESIZED = 1 + COMPONENT_FIRST;
  65. /**
  66. * This event indicates that the component was made visible.
  67. */
  68. public static final int COMPONENT_SHOWN = 2 + COMPONENT_FIRST;
  69. /**
  70. * This event indicates that the component was rendered invisible.
  71. */
  72. public static final int COMPONENT_HIDDEN = 3 + COMPONENT_FIRST;
  73. /*
  74. * JDK 1.1 serialVersionUID
  75. */
  76. private static final long serialVersionUID = 8101406823902992965L;
  77. /**
  78. * Constructs a ComponentEvent object.
  79. *
  80. * @param source the Component object that originated the event
  81. * @param id an integer indicating the type of event
  82. */
  83. public ComponentEvent(Component source, int id) {
  84. super(source, id);
  85. }
  86. /**
  87. * Returns the originator of the event.
  88. *
  89. * @return the Component object that originated the event
  90. */
  91. public Component getComponent() {
  92. // return (source instanceof Component) ? (Component)source : null;
  93. return (Component)source; // cast should always be OK, type was checked in constructor
  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. }