1. /*
  2. * @(#)HierarchyEvent.java 1.5 00/02/02
  3. *
  4. * Copyright 1997-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.Component;
  13. import java.awt.Container;
  14. /**
  15. * An event which indicates a change to the Component hierarchy to which
  16. * a Component belongs.
  17. * <ul>
  18. * <li>Hierarchy Change Events (HierarchyListener)
  19. * <ul>
  20. * <li> addition of an ancestor
  21. * <li> removal of an ancestor
  22. * <li> hierarchy made displayable
  23. * <li> hierarchy made undisplayable
  24. * <li> hierarchy shown on the screen (both visible and displayable)
  25. * <li> hierarchy hidden on the screen (either invisible or undisplayable)
  26. * </ul>
  27. * <li>Ancestor Reshape Events (HierarchyBoundsListener)
  28. * <ul>
  29. * <li> an ancestor was resized
  30. * <li> an ancestor was moved
  31. * </ul>
  32. * </ul>
  33. * <p>
  34. * Hierarchy events are provided for notification purposes ONLY;
  35. * The AWT will automatically handle changes to the hierarchy internally so
  36. * that GUI layout and displayability works properly regardless of whether a
  37. * program is receiving these events or not.
  38. * <p>
  39. * This event is generated by a Container object (such as a Panel) when the
  40. * Container is added, removed, moved, or resized, and passed down the
  41. * hierarchy. It is also generated by a Component object when that object's
  42. * <code>addNotify</code>, <code>removeNotify</code>, <code>show</code>, or
  43. * <code>hide</code> method is called. ANCESTOR_MOVED and ANCESTOR_RESIZED
  44. * events are dispatched to every <code>HierarchyBoundsListener</code> or
  45. * <code>HierarchyBoundsAdapter</code> object which registered to receive
  46. * such events using the Component's <code>addHierarchyBoundsListener</code>
  47. * method. (<code>HierarchyBoundsAdapter</code> objects implement the <code>
  48. * HierarchyBoundsListener</code> interface.) HIERARCHY_CHANGED events are
  49. * dispatched to every <code>HierarchyListener</code> object which registered
  50. * to receive such events using the Component's <code>addHierarchyListener
  51. * </code> method. Each such listener object gets this <code>HierarchyEvent
  52. * </code> when the event occurs.
  53. *
  54. * @author David Mendenhall
  55. * @version 1.5, 02/02/00
  56. * @see HierarchyListener
  57. * @see HierarchyBoundsAdapter
  58. * @see HierarchyBoundsListener
  59. * @since 1.3
  60. */
  61. public class HierarchyEvent extends AWTEvent {
  62. /**
  63. * Marks the first integer id for the range of hierarchy event ids.
  64. */
  65. public static final int HIERARCHY_FIRST = 1400; // 1300 used by sun.awt.windows.ModalityEvent
  66. /**
  67. * A modification was made to the entire hierarchy tree.
  68. */
  69. public static final int HIERARCHY_CHANGED = HIERARCHY_FIRST;
  70. /**
  71. * An ancestor-Container was moved.
  72. */
  73. public static final int ANCESTOR_MOVED = 1 + HIERARCHY_FIRST;
  74. /**
  75. * An ancestor-Container was resized.
  76. */
  77. public static final int ANCESTOR_RESIZED = 2 + HIERARCHY_FIRST;
  78. /**
  79. * Marks the last integer id for the range of ancestor event ids.
  80. */
  81. public static final int HIERARCHY_LAST = ANCESTOR_RESIZED;
  82. /**
  83. * The hierarchy changed through a reparent operation.
  84. */
  85. public static final int PARENT_CHANGED = 0x1;
  86. /**
  87. * The displayability of the hierarchy changed. To discern the
  88. * current displayability of the hierarchy, call
  89. * Component.isDisplayable(). Displayability changes occur
  90. * in response to explicit or implicit calls to
  91. * Component.addNotify() and Component.removeNotify().
  92. *
  93. * @see java.awt.Component#isDisplayable()
  94. * @see java.awt.Component#addNotify()
  95. * @see java.awt.Component#removeNotify()
  96. */
  97. public static final int DISPLAYABILITY_CHANGED = 0x2;
  98. /**
  99. * The showing state of the hierarchy changed. To discern the
  100. * current showing state of the hierarchy, call
  101. * Component.isShowing(). Showing state changes occur
  102. * when either the displayability or visibility of the
  103. * hierarchy occurs. Displayability changes occur
  104. * in response to explicit or implicit calls to
  105. * Component.addNotify() and Component.removeNotify().
  106. * Visibility changes occur in response to explicit
  107. * or implicit calls to Component.show() and
  108. * Component.hide().
  109. *
  110. * @see java.awt.Component#isShowing()
  111. * @see java.awt.Component#addNotify()
  112. * @see java.awt.Component#removeNotify()
  113. * @see java.awt.Component#show()
  114. * @see java.awt.Component#hide()
  115. */
  116. public static final int SHOWING_CHANGED = 0x4;
  117. Component changed;
  118. Container changedParent;
  119. long changeFlags;
  120. /**
  121. * Constructs an HierarchyEvent object to identify a change in the
  122. * Component hierarchy.
  123. *
  124. * @param source the Component object that originated the event
  125. * @param id an integer indicating the type of event
  126. * @param changed the Component at the top of the hierarchy
  127. * which was changed
  128. * @param changedParent the parent of <code>changed</code>. This
  129. * may be the parent before or after the
  130. * change, depending on the type of change.
  131. */
  132. public HierarchyEvent(Component source, int id, Component changed,
  133. Container changedParent) {
  134. super(source, id);
  135. this.changed = changed;
  136. this.changedParent = changedParent;
  137. }
  138. /**
  139. * Constructs an HierarchyEvent object to identify a change in the
  140. * Component hierarchy.
  141. *
  142. * @param source the Component object that originated the event
  143. * @param id an integer indicating the type of event
  144. * @param changed the Component at the top of the hierarchy
  145. * which was changed
  146. * @param changedParent the parent of <code>changed</code>. This
  147. * may be the parent before or after the
  148. * change, depending on the type of change.
  149. * @param changeFlags a bitmask which indicates the type(s) of
  150. * HIERARCHY_CHANGED events represented in
  151. * this event object
  152. */
  153. public HierarchyEvent(Component source, int id, Component changed,
  154. Container changedParent, long changeFlags) {
  155. super(source, id);
  156. this.changed = changed;
  157. this.changedParent = changedParent;
  158. this.changeFlags = changeFlags;
  159. }
  160. /**
  161. * Returns the originator of the event.
  162. *
  163. * @return the Component object that originated the event
  164. */
  165. public Component getComponent() {
  166. return (Component)getSource();
  167. }
  168. /**
  169. * Returns the Component at the top of the hierarchy which was
  170. * changed.
  171. *
  172. * @return the changed Component
  173. */
  174. public Component getChanged() {
  175. return changed;
  176. }
  177. /**
  178. * Returns the parent of the Component returned by <code>
  179. * getChanged()</code>. For a HIERARCHY_CHANGED event where the
  180. * change was of type PARENT_CHANGED via a call to <code>
  181. * Container.add</code>, the parent returned is the parent
  182. * after the add operation. For a HIERARCHY_CHANGED event where
  183. * the change was of type PARENT_CHANGED via a call to <code>
  184. * Container.remove</code>, the parent returned is the parent
  185. * before the remove operation. For all other events and types,
  186. * the parent returned is the parent during the operation.
  187. *
  188. * @return the parent of the changed Component
  189. */
  190. public Container getChangedParent() {
  191. return changedParent;
  192. }
  193. /**
  194. * Returns a bitmask which indicates the type(s) of
  195. * HIERARCHY_CHANGED events represented in this event object.
  196. * The bits have been bitwise-ored together.
  197. *
  198. * @return the bitmask, or 0 if this is not an HIERARCHY_CHANGED
  199. * event
  200. */
  201. public long getChangeFlags() {
  202. return changeFlags;
  203. }
  204. /**
  205. * Returns a parameter string identifying this event.
  206. * This method is useful for event-logging and for debugging.
  207. *
  208. * @return a string identifying the event and its attributes
  209. */
  210. public String paramString() {
  211. String typeStr;
  212. switch(id) {
  213. case ANCESTOR_MOVED:
  214. typeStr = "ANCESTOR_MOVED ("+changed+","+changedParent+")";
  215. break;
  216. case ANCESTOR_RESIZED:
  217. typeStr = "ANCESTOR_RESIZED ("+changed+","+changedParent+")";
  218. break;
  219. case HIERARCHY_CHANGED: {
  220. typeStr = "HIERARCHY_CHANGED (";
  221. boolean first = true;
  222. if ((changeFlags & PARENT_CHANGED) != 0) {
  223. first = false;
  224. typeStr += "PARENT_CHANGED";
  225. }
  226. if ((changeFlags & DISPLAYABILITY_CHANGED) != 0) {
  227. if (first) {
  228. first = false;
  229. } else {
  230. typeStr += ",";
  231. }
  232. typeStr += "DISPLAYABILITY_CHANGED";
  233. }
  234. if ((changeFlags & SHOWING_CHANGED) != 0) {
  235. if (first) {
  236. first = false;
  237. } else {
  238. typeStr += ",";
  239. }
  240. typeStr += "SHOWING_CHANGED";
  241. }
  242. if (!first) {
  243. typeStr += ",";
  244. }
  245. typeStr += changed + "," + changedParent + ")";
  246. break;
  247. }
  248. default:
  249. typeStr = "unknown type";
  250. }
  251. return typeStr;
  252. }
  253. }