1. /*
  2. * @(#)ContainerEvent.java 1.11 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.Container;
  13. import java.awt.Component;
  14. /**
  15. * A low-level event which indicates that a container's contents
  16. * changed because a component was added or removed.
  17. * <P>
  18. * Container events are provided for notification purposes ONLY;
  19. * The AWT will automatically handle changes to the containers
  20. * contents internally so that the program works properly regardless of
  21. * whether the program is receiving these events or not.
  22. * <P>
  23. * This low-level event is generated by a container object (such as a
  24. * Panel) when a component is added to it or removed from it.
  25. * The event is passed to every <code>ContainerListener</code>
  26. * or <code>ContainerAdapter</code> object which registered to receive such
  27. * events using the component's <code>addContainerListener</code> method.
  28. * (<code>ContainerAdapter</code> objects implement the
  29. * <code>ContainerListener</code> interface.) Each such listener object
  30. * gets this <code>ContainerEvent</code> when the event occurs.
  31. *
  32. * @see ContainerAdapter
  33. * @see ContainerListener
  34. * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/containerlistener.html">Tutorial: Writing a Container Listener</a>
  35. * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  36. *
  37. * @author Tim Prinzing
  38. * @author Amy Fowler
  39. * @version 1.11 02/02/00
  40. * @since 1.1
  41. */
  42. public class ContainerEvent extends ComponentEvent {
  43. /**
  44. * The first number in the range of ids used for container events.
  45. */
  46. public static final int CONTAINER_FIRST = 300;
  47. /**
  48. * The last number in the range of ids used for container events.
  49. */
  50. public static final int CONTAINER_LAST = 301;
  51. /**
  52. * This event indicates that a component was added to the container.
  53. */
  54. public static final int COMPONENT_ADDED = CONTAINER_FIRST;
  55. /**
  56. * This event indicates that a component was removed from the container.
  57. */
  58. public static final int COMPONENT_REMOVED = 1 + CONTAINER_FIRST;
  59. /**
  60. * The non-null component that is being added or
  61. * removed from the Container.
  62. *
  63. * @serial
  64. * @see getChild
  65. */
  66. Component child;
  67. /*
  68. * JDK 1.1 serialVersionUID
  69. */
  70. private static final long serialVersionUID = -4114942250539772041L;
  71. /**
  72. * Constructs a ContainerEvent object.
  73. *
  74. * @param source the Component object (container) 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. }