1. /*
  2. * @(#)ContainerEvent.java 1.9 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.Container;
  10. import java.awt.Component;
  11. /**
  12. * A low-level event which indicates that a container's contents
  13. * changed because a component was added or removed.
  14. * <P>
  15. * Container events are provided for notification purposes ONLY;
  16. * The AWT will automatically handle changes to the containers
  17. * contents internally so that the program works properly regardless of
  18. * whether the program is receiving these events or not.
  19. * <P>
  20. * This low-level event is generated by a container object (such as a
  21. * Panel) when a component is added to it or removed from it.
  22. * The event is passed to every <code>ContainerListener</code>
  23. * or <code>ContainerAdapter</code> object which registered to receive such
  24. * events using the component's <code>addContainerListener</code> method.
  25. * (<code>ContainerAdapter</code> objects implement the
  26. * <code>ContainerListener</code> interface.) Each such listener object
  27. * gets this <code>ContainerEvent</code> when the event occurs.
  28. *
  29. * @see ContainerAdapter
  30. * @see ContainerListener
  31. * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/containerlistener.html">Tutorial: Writing a Container Listener</a>
  32. * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  33. *
  34. * @version 1.9 11/29/01
  35. * @author Tim Prinzing
  36. * @author Amy Fowler
  37. */
  38. public class ContainerEvent extends ComponentEvent {
  39. /**
  40. * The first number in the range of ids used for container events.
  41. */
  42. public static final int CONTAINER_FIRST = 300;
  43. /**
  44. * The last number in the range of ids used for container events.
  45. */
  46. public static final int CONTAINER_LAST = 301;
  47. /**
  48. * This event indicates that a component was added to the container.
  49. */
  50. public static final int COMPONENT_ADDED = CONTAINER_FIRST;
  51. /**
  52. * This event indicates that a component was removed from the container.
  53. */
  54. public static final int COMPONENT_REMOVED = 1 + CONTAINER_FIRST;
  55. /**
  56. * The non-null component that is being added or
  57. * removed from the Container.
  58. *
  59. * @serial
  60. * @see getChild
  61. */
  62. Component child;
  63. /*
  64. * JDK 1.1 serialVersionUID
  65. */
  66. private static final long serialVersionUID = -4114942250539772041L;
  67. /**
  68. * Constructs a ContainerEvent object.
  69. *
  70. * @param source the Component object (container) that originated the event
  71. * @param id an integer indicating the type of event
  72. * @param child the component that was added or removed
  73. */
  74. public ContainerEvent(Component source, int id, Component child) {
  75. super(source, id);
  76. this.child = child;
  77. }
  78. /**
  79. * Returns the originator of the event.
  80. *
  81. * @return the Container object that originated the event
  82. */
  83. public Container getContainer() {
  84. return (Container)source; // cast should always be OK, type was checked in constructor
  85. }
  86. /**
  87. * Returns the component that was affected by the event.
  88. *
  89. * @return the Component object that was added or removed
  90. */
  91. public Component getChild() {
  92. return child;
  93. }
  94. /**
  95. * Returns a parameter string identifying this event.
  96. * This method is useful for event-logging and for debugging.
  97. *
  98. * @return a string identifying the event and its attributes
  99. */
  100. public String paramString() {
  101. String typeStr;
  102. switch(id) {
  103. case COMPONENT_ADDED:
  104. typeStr = "COMPONENT_ADDED";
  105. break;
  106. case COMPONENT_REMOVED:
  107. typeStr = "COMPONENT_REMOVED";
  108. break;
  109. default:
  110. typeStr = "unknown type";
  111. }
  112. return typeStr + ",child="+child.getName();
  113. }
  114. }