1. /*
  2. * @(#)AncestorNotifier.java 1.13 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 javax.swing;
  11. import javax.swing.event.*;
  12. import java.awt.event.*;
  13. import java.awt.Component;
  14. import java.awt.Container;
  15. import java.awt.Window;
  16. import java.beans.PropertyChangeListener;
  17. import java.beans.PropertyChangeEvent;
  18. import java.io.Serializable;
  19. /**
  20. * @version 1.13 02/02/00
  21. * @author Dave Moore
  22. */
  23. class AncestorNotifier implements ComponentListener, PropertyChangeListener, Serializable
  24. {
  25. Component firstInvisibleAncestor;
  26. EventListenerList listenerList = new EventListenerList();
  27. JComponent root;
  28. AncestorNotifier(JComponent root) {
  29. this.root = root;
  30. addListeners(root, true);
  31. }
  32. void addAncestorListener(AncestorListener l) {
  33. listenerList.add(AncestorListener.class, l);
  34. }
  35. void removeAncestorListener(AncestorListener l) {
  36. listenerList.remove(AncestorListener.class, l);
  37. }
  38. /*
  39. * Notify all listeners that have registered interest for
  40. * notification on this event type. The event instance
  41. * is lazily created using the parameters passed into
  42. * the fire method.
  43. * @see EventListenerList
  44. */
  45. protected void fireAncestorAdded(JComponent source, int id, Container ancestor, Container ancestorParent) {
  46. // Guaranteed to return a non-null array
  47. Object[] listeners = listenerList.getListenerList();
  48. // Process the listeners last to first, notifying
  49. // those that are interested in this event
  50. for (int i = listeners.length-2; i>=0; i-=2) {
  51. if (listeners[i]==AncestorListener.class) {
  52. // Lazily create the event:
  53. AncestorEvent ancestorEvent =
  54. new AncestorEvent(source, id, ancestor, ancestorParent);
  55. ((AncestorListener)listeners[i+1]).ancestorAdded(ancestorEvent);
  56. }
  57. }
  58. }
  59. /*
  60. * Notify all listeners that have registered interest for
  61. * notification on this event type. The event instance
  62. * is lazily created using the parameters passed into
  63. * the fire method.
  64. * @see EventListenerList
  65. */
  66. protected void fireAncestorRemoved(JComponent source, int id, Container ancestor, Container ancestorParent) {
  67. // Guaranteed to return a non-null array
  68. Object[] listeners = listenerList.getListenerList();
  69. // Process the listeners last to first, notifying
  70. // those that are interested in this event
  71. for (int i = listeners.length-2; i>=0; i-=2) {
  72. if (listeners[i]==AncestorListener.class) {
  73. // Lazily create the event:
  74. AncestorEvent ancestorEvent =
  75. new AncestorEvent(source, id, ancestor, ancestorParent);
  76. ((AncestorListener)listeners[i+1]).ancestorRemoved(ancestorEvent);
  77. }
  78. }
  79. }
  80. /*
  81. * Notify all listeners that have registered interest for
  82. * notification on this event type. The event instance
  83. * is lazily created using the parameters passed into
  84. * the fire method.
  85. * @see EventListenerList
  86. */
  87. protected void fireAncestorMoved(JComponent source, int id, Container ancestor, Container ancestorParent) {
  88. // Guaranteed to return a non-null array
  89. Object[] listeners = listenerList.getListenerList();
  90. // Process the listeners last to first, notifying
  91. // those that are interested in this event
  92. for (int i = listeners.length-2; i>=0; i-=2) {
  93. if (listeners[i]==AncestorListener.class) {
  94. // Lazily create the event:
  95. AncestorEvent ancestorEvent =
  96. new AncestorEvent(source, id, ancestor, ancestorParent);
  97. ((AncestorListener)listeners[i+1]).ancestorMoved(ancestorEvent);
  98. }
  99. }
  100. }
  101. void removeAllListeners() {
  102. removeListeners(root);
  103. }
  104. void addListeners(Component ancestor, boolean addToFirst) {
  105. Component a;
  106. firstInvisibleAncestor = null;
  107. for (a = ancestor;
  108. firstInvisibleAncestor == null;
  109. a = a.getParent()) {
  110. if (addToFirst || a != ancestor) {
  111. a.addComponentListener(this);
  112. if (a instanceof JComponent) {
  113. JComponent jAncestor = (JComponent)a;
  114. jAncestor.addPropertyChangeListener(this);
  115. }
  116. }
  117. if (!a.isVisible() || a.getParent() == null) {
  118. firstInvisibleAncestor = a;
  119. }
  120. }
  121. if (firstInvisibleAncestor instanceof Window) {
  122. firstInvisibleAncestor = null;
  123. }
  124. }
  125. void removeListeners(Component ancestor) {
  126. Component a;
  127. for (a = ancestor; a != null; a = a.getParent()) {
  128. a.removeComponentListener(this);
  129. if (a instanceof JComponent) {
  130. JComponent jAncestor = (JComponent)a;
  131. jAncestor.removePropertyChangeListener(this);
  132. }
  133. if (a == firstInvisibleAncestor) {
  134. break;
  135. }
  136. }
  137. }
  138. public void componentResized(ComponentEvent e) {}
  139. public void componentMoved(ComponentEvent e) {
  140. Component source = e.getComponent();
  141. fireAncestorMoved(root, AncestorEvent.ANCESTOR_MOVED,
  142. (Container)source, source.getParent());
  143. }
  144. public void componentShown(ComponentEvent e) {
  145. Component ancestor = e.getComponent();
  146. if (ancestor == firstInvisibleAncestor) {
  147. addListeners(ancestor, false);
  148. if (firstInvisibleAncestor == null) {
  149. fireAncestorAdded(root, AncestorEvent.ANCESTOR_ADDED,
  150. (Container)ancestor, ancestor.getParent());
  151. }
  152. }
  153. }
  154. public void componentHidden(ComponentEvent e) {
  155. Component ancestor = e.getComponent();
  156. boolean needsNotify = firstInvisibleAncestor == null;
  157. removeListeners(ancestor.getParent());
  158. firstInvisibleAncestor = ancestor;
  159. if (needsNotify) {
  160. fireAncestorRemoved(root, AncestorEvent.ANCESTOR_REMOVED,
  161. (Container)ancestor, ancestor.getParent());
  162. }
  163. }
  164. public void propertyChange(PropertyChangeEvent evt) {
  165. String s = evt.getPropertyName();
  166. if (s!=null && (s.equals("parent") || s.equals("ancestor"))) {
  167. JComponent component = (JComponent)evt.getSource();
  168. if (evt.getNewValue() != null) {
  169. if (component == firstInvisibleAncestor) {
  170. addListeners(component, false);
  171. if (firstInvisibleAncestor == null) {
  172. fireAncestorAdded(root, AncestorEvent.ANCESTOR_ADDED,
  173. component, component.getParent());
  174. }
  175. }
  176. } else {
  177. boolean needsNotify = firstInvisibleAncestor == null;
  178. Container oldParent = (Container)evt.getOldValue();
  179. removeListeners(oldParent);
  180. firstInvisibleAncestor = component;
  181. if (needsNotify) {
  182. fireAncestorRemoved(root, AncestorEvent.ANCESTOR_REMOVED,
  183. component, oldParent);
  184. }
  185. }
  186. }
  187. }
  188. }