1 7 8 package javax.swing; 9 10 11 import javax.swing.event.*; 12 import java.awt.event.*; 13 14 import java.awt.Component ; 15 import java.awt.Container ; 16 import java.awt.Window ; 17 import java.beans.PropertyChangeListener ; 18 import java.beans.PropertyChangeEvent ; 19 20 import java.io.Serializable ; 21 22 23 27 28 class AncestorNotifier implements ComponentListener, PropertyChangeListener , Serializable 29 { 30 Component firstInvisibleAncestor; 31 EventListenerList listenerList = new EventListenerList(); 32 JComponent root; 33 34 AncestorNotifier(JComponent root) { 35 this.root = root; 36 addListeners(root, true); 37 } 38 39 void addAncestorListener(AncestorListener l) { 40 listenerList.add(AncestorListener.class, l); 41 } 42 43 void removeAncestorListener(AncestorListener l) { 44 listenerList.remove(AncestorListener.class, l); 45 } 46 47 AncestorListener[] getAncestorListeners() { 48 return (AncestorListener[])listenerList.getListeners(AncestorListener.class); 49 } 50 51 58 protected void fireAncestorAdded(JComponent source, int id, Container ancestor, Container ancestorParent) { 59 Object [] listeners = listenerList.getListenerList(); 61 for (int i = listeners.length-2; i>=0; i-=2) { 64 if (listeners[i]==AncestorListener.class) { 65 AncestorEvent ancestorEvent = 67 new AncestorEvent(source, id, ancestor, ancestorParent); 68 ((AncestorListener)listeners[i+1]).ancestorAdded(ancestorEvent); 69 } 70 } 71 } 72 73 80 protected void fireAncestorRemoved(JComponent source, int id, Container ancestor, Container ancestorParent) { 81 Object [] listeners = listenerList.getListenerList(); 83 for (int i = listeners.length-2; i>=0; i-=2) { 86 if (listeners[i]==AncestorListener.class) { 87 AncestorEvent ancestorEvent = 89 new AncestorEvent(source, id, ancestor, ancestorParent); 90 ((AncestorListener)listeners[i+1]).ancestorRemoved(ancestorEvent); 91 } 92 } 93 } 94 101 protected void fireAncestorMoved(JComponent source, int id, Container ancestor, Container ancestorParent) { 102 Object [] listeners = listenerList.getListenerList(); 104 for (int i = listeners.length-2; i>=0; i-=2) { 107 if (listeners[i]==AncestorListener.class) { 108 AncestorEvent ancestorEvent = 110 new AncestorEvent(source, id, ancestor, ancestorParent); 111 ((AncestorListener)listeners[i+1]).ancestorMoved(ancestorEvent); 112 } 113 } 114 } 115 116 void removeAllListeners() { 117 removeListeners(root); 118 } 119 120 void addListeners(Component ancestor, boolean addToFirst) { 121 Component a; 122 123 firstInvisibleAncestor = null; 124 for (a = ancestor; 125 firstInvisibleAncestor == null; 126 a = a.getParent()) { 127 if (addToFirst || a != ancestor) { 128 a.addComponentListener(this); 129 130 if (a instanceof JComponent ) { 131 JComponent jAncestor = (JComponent )a; 132 133 jAncestor.addPropertyChangeListener(this); 134 } 135 } 136 if (!a.isVisible() || a.getParent() == null || a instanceof Window ) { 137 firstInvisibleAncestor = a; 138 } 139 } 140 if (firstInvisibleAncestor instanceof Window && 141 firstInvisibleAncestor.isVisible()) { 142 firstInvisibleAncestor = null; 143 } 144 } 145 146 void removeListeners(Component ancestor) { 147 Component a; 148 for (a = ancestor; a != null; a = a.getParent()) { 149 a.removeComponentListener(this); 150 if (a instanceof JComponent ) { 151 JComponent jAncestor = (JComponent )a; 152 jAncestor.removePropertyChangeListener(this); 153 } 154 if (a == firstInvisibleAncestor || a instanceof Window ) { 155 break; 156 } 157 } 158 } 159 160 public void componentResized(ComponentEvent e) {} 161 162 public void componentMoved(ComponentEvent e) { 163 Component source = e.getComponent(); 164 165 fireAncestorMoved(root, AncestorEvent.ANCESTOR_MOVED, 166 (Container )source, source.getParent()); 167 } 168 169 public void componentShown(ComponentEvent e) { 170 Component ancestor = e.getComponent(); 171 172 if (ancestor == firstInvisibleAncestor) { 173 addListeners(ancestor, false); 174 if (firstInvisibleAncestor == null) { 175 fireAncestorAdded(root, AncestorEvent.ANCESTOR_ADDED, 176 (Container )ancestor, ancestor.getParent()); 177 } 178 } 179 } 180 181 public void componentHidden(ComponentEvent e) { 182 Component ancestor = e.getComponent(); 183 boolean needsNotify = firstInvisibleAncestor == null; 184 185 if ( !(ancestor instanceof Window ) ) { 186 removeListeners(ancestor.getParent()); 187 } 188 firstInvisibleAncestor = ancestor; 189 if (needsNotify) { 190 fireAncestorRemoved(root, AncestorEvent.ANCESTOR_REMOVED, 191 (Container )ancestor, ancestor.getParent()); 192 } 193 } 194 195 public void propertyChange(PropertyChangeEvent evt) { 196 String s = evt.getPropertyName(); 197 198 if (s!=null && (s.equals("parent") || s.equals("ancestor"))) { 199 JComponent component = (JComponent )evt.getSource(); 200 201 if (evt.getNewValue() != null) { 202 if (component == firstInvisibleAncestor) { 203 addListeners(component, false); 204 if (firstInvisibleAncestor == null) { 205 fireAncestorAdded(root, AncestorEvent.ANCESTOR_ADDED, 206 component, component.getParent()); 207 } 208 } 209 } else { 210 boolean needsNotify = firstInvisibleAncestor == null; 211 Container oldParent = (Container )evt.getOldValue(); 212 213 removeListeners(oldParent); 214 firstInvisibleAncestor = component; 215 if (needsNotify) { 216 fireAncestorRemoved(root, AncestorEvent.ANCESTOR_REMOVED, 217 component, oldParent); 218 } 219 } 220 } 221 } 222 } 223 | Popular Tags |