1. /*
  2. * @(#)ContainerEvent.java 1.15 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.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. * @author Tim Prinzing
  35. * @author Amy Fowler
  36. * @version 1.15 01/23/03
  37. * @since 1.1
  38. */
  39. public class ContainerEvent extends ComponentEvent {
  40. /**
  41. * The first number in the range of ids used for container events.
  42. */
  43. public static final int CONTAINER_FIRST = 300;
  44. /**
  45. * The last number in the range of ids used for container events.
  46. */
  47. public static final int CONTAINER_LAST = 301;
  48. /**
  49. * This event indicates that a component was added to the container.
  50. */
  51. public static final int COMPONENT_ADDED = CONTAINER_FIRST;
  52. /**
  53. * This event indicates that a component was removed from the container.
  54. */
  55. public static final int COMPONENT_REMOVED = 1 + CONTAINER_FIRST;
  56. /**
  57. * The non-null component that is being added or
  58. * removed from the Container.
  59. *
  60. * @serial
  61. * @see #getChild()
  62. */
  63. Component child;
  64. /*
  65. * JDK 1.1 serialVersionUID
  66. */
  67. private static final long serialVersionUID = -4114942250539772041L;
  68. /**
  69. * Constructs a <code>ContainerEvent</code> object.
  70. * <p>Note that passing in an invalid <code>id</code> results in
  71. * unspecified behavior.
  72. *
  73. * @param source the <code>Component</code> object (container)
  74. * that originated the event
  75. * @param id an integer indicating the type of event
  76. * @param child the component that was added or removed
  77. */
  78. public ContainerEvent(Component source, int id, Component child) {
  79. super(source, id);
  80. this.child = child;
  81. }
  82. /**
  83. * Returns the originator of the event.
  84. *
  85. * @return the Container object that originated the event
  86. */
  87. public Container getContainer() {
  88. return (Container)source; // cast should always be OK, type was checked in constructor
  89. }
  90. /**
  91. * Returns the component that was affected by the event.
  92. *
  93. * @return the Component object that was added or removed
  94. */
  95. public Component getChild() {
  96. return child;
  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. switch(id) {
  107. case COMPONENT_ADDED:
  108. typeStr = "COMPONENT_ADDED";
  109. break;
  110. case COMPONENT_REMOVED:
  111. typeStr = "COMPONENT_REMOVED";
  112. break;
  113. default:
  114. typeStr = "unknown type";
  115. }
  116. return typeStr + ",child="+child.getName();
  117. }
  118. }