1. /*
  2. * @(#)AncestorNotifier.java 1.16 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 javax.swing;
  8. import javax.swing.event.*;
  9. import java.awt.event.*;
  10. import java.awt.Component;
  11. import java.awt.Container;
  12. import java.awt.Window;
  13. import java.beans.PropertyChangeListener;
  14. import java.beans.PropertyChangeEvent;
  15. import java.io.Serializable;
  16. /**
  17. * @version 1.16 01/23/03
  18. * @author Dave Moore
  19. */
  20. class AncestorNotifier implements ComponentListener, PropertyChangeListener, Serializable
  21. {
  22. Component firstInvisibleAncestor;
  23. EventListenerList listenerList = new EventListenerList();
  24. JComponent root;
  25. AncestorNotifier(JComponent root) {
  26. this.root = root;
  27. addListeners(root, true);
  28. }
  29. void addAncestorListener(AncestorListener l) {
  30. listenerList.add(AncestorListener.class, l);
  31. }
  32. void removeAncestorListener(AncestorListener l) {
  33. listenerList.remove(AncestorListener.class, l);
  34. }
  35. AncestorListener[] getAncestorListeners() {
  36. return (AncestorListener[])listenerList.getListeners(AncestorListener.class);
  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. }