1. /*
  2. * @(#)AncestorNotifier.java 1.19 03/12/19
  3. *
  4. * Copyright 2004 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.19 12/19/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 || a instanceof Window) {
  118. firstInvisibleAncestor = a;
  119. }
  120. }
  121. if (firstInvisibleAncestor instanceof Window &&
  122. firstInvisibleAncestor.isVisible()) {
  123. firstInvisibleAncestor = null;
  124. }
  125. }
  126. void removeListeners(Component ancestor) {
  127. Component a;
  128. for (a = ancestor; a != null; a = a.getParent()) {
  129. a.removeComponentListener(this);
  130. if (a instanceof JComponent) {
  131. JComponent jAncestor = (JComponent)a;
  132. jAncestor.removePropertyChangeListener(this);
  133. }
  134. if (a == firstInvisibleAncestor || a instanceof Window) {
  135. break;
  136. }
  137. }
  138. }
  139. public void componentResized(ComponentEvent e) {}
  140. public void componentMoved(ComponentEvent e) {
  141. Component source = e.getComponent();
  142. fireAncestorMoved(root, AncestorEvent.ANCESTOR_MOVED,
  143. (Container)source, source.getParent());
  144. }
  145. public void componentShown(ComponentEvent e) {
  146. Component ancestor = e.getComponent();
  147. if (ancestor == firstInvisibleAncestor) {
  148. addListeners(ancestor, false);
  149. if (firstInvisibleAncestor == null) {
  150. fireAncestorAdded(root, AncestorEvent.ANCESTOR_ADDED,
  151. (Container)ancestor, ancestor.getParent());
  152. }
  153. }
  154. }
  155. public void componentHidden(ComponentEvent e) {
  156. Component ancestor = e.getComponent();
  157. boolean needsNotify = firstInvisibleAncestor == null;
  158. if ( !(ancestor instanceof Window) ) {
  159. removeListeners(ancestor.getParent());
  160. }
  161. firstInvisibleAncestor = ancestor;
  162. if (needsNotify) {
  163. fireAncestorRemoved(root, AncestorEvent.ANCESTOR_REMOVED,
  164. (Container)ancestor, ancestor.getParent());
  165. }
  166. }
  167. public void propertyChange(PropertyChangeEvent evt) {
  168. String s = evt.getPropertyName();
  169. if (s!=null && (s.equals("parent") || s.equals("ancestor"))) {
  170. JComponent component = (JComponent)evt.getSource();
  171. if (evt.getNewValue() != null) {
  172. if (component == firstInvisibleAncestor) {
  173. addListeners(component, false);
  174. if (firstInvisibleAncestor == null) {
  175. fireAncestorAdded(root, AncestorEvent.ANCESTOR_ADDED,
  176. component, component.getParent());
  177. }
  178. }
  179. } else {
  180. boolean needsNotify = firstInvisibleAncestor == null;
  181. Container oldParent = (Container)evt.getOldValue();
  182. removeListeners(oldParent);
  183. firstInvisibleAncestor = component;
  184. if (needsNotify) {
  185. fireAncestorRemoved(root, AncestorEvent.ANCESTOR_REMOVED,
  186. component, oldParent);
  187. }
  188. }
  189. }
  190. }
  191. }