KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > AncestorNotifier


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
8 package javax.swing;
9
10
11 import javax.swing.event.*;
12 import java.awt.event.*;
13
14 import java.awt.Component JavaDoc;
15 import java.awt.Container JavaDoc;
16 import java.awt.Window JavaDoc;
17 import java.beans.PropertyChangeListener JavaDoc;
18 import java.beans.PropertyChangeEvent JavaDoc;
19
20 import java.io.Serializable JavaDoc;
21
22
23 /**
24  * @version 1.19 12/19/03
25  * @author Dave Moore
26  */

27
28 class AncestorNotifier implements ComponentListener, PropertyChangeListener JavaDoc, Serializable JavaDoc
29 {
30     Component JavaDoc firstInvisibleAncestor;
31     EventListenerList listenerList = new EventListenerList();
32     JComponent JavaDoc root;
33
34     AncestorNotifier(JComponent JavaDoc 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     /*
52      * Notify all listeners that have registered interest for
53      * notification on this event type. The event instance
54      * is lazily created using the parameters passed into
55      * the fire method.
56      * @see EventListenerList
57      */

58     protected void fireAncestorAdded(JComponent JavaDoc source, int id, Container JavaDoc ancestor, Container JavaDoc ancestorParent) {
59     // Guaranteed to return a non-null array
60
Object JavaDoc[] listeners = listenerList.getListenerList();
61     // Process the listeners last to first, notifying
62
// those that are interested in this event
63
for (int i = listeners.length-2; i>=0; i-=2) {
64         if (listeners[i]==AncestorListener.class) {
65         // Lazily create the event:
66
AncestorEvent ancestorEvent =
67             new AncestorEvent(source, id, ancestor, ancestorParent);
68         ((AncestorListener)listeners[i+1]).ancestorAdded(ancestorEvent);
69         }
70     }
71     }
72     
73     /*
74      * Notify all listeners that have registered interest for
75      * notification on this event type. The event instance
76      * is lazily created using the parameters passed into
77      * the fire method.
78      * @see EventListenerList
79      */

80     protected void fireAncestorRemoved(JComponent JavaDoc source, int id, Container JavaDoc ancestor, Container JavaDoc ancestorParent) {
81     // Guaranteed to return a non-null array
82
Object JavaDoc[] listeners = listenerList.getListenerList();
83     // Process the listeners last to first, notifying
84
// those that are interested in this event
85
for (int i = listeners.length-2; i>=0; i-=2) {
86         if (listeners[i]==AncestorListener.class) {
87         // Lazily create the event:
88
AncestorEvent ancestorEvent =
89             new AncestorEvent(source, id, ancestor, ancestorParent);
90         ((AncestorListener)listeners[i+1]).ancestorRemoved(ancestorEvent);
91         }
92     }
93     }
94     /*
95      * Notify all listeners that have registered interest for
96      * notification on this event type. The event instance
97      * is lazily created using the parameters passed into
98      * the fire method.
99      * @see EventListenerList
100      */

101     protected void fireAncestorMoved(JComponent JavaDoc source, int id, Container JavaDoc ancestor, Container JavaDoc ancestorParent) {
102     // Guaranteed to return a non-null array
103
Object JavaDoc[] listeners = listenerList.getListenerList();
104     // Process the listeners last to first, notifying
105
// those that are interested in this event
106
for (int i = listeners.length-2; i>=0; i-=2) {
107         if (listeners[i]==AncestorListener.class) {
108         // Lazily create the event:
109
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 JavaDoc ancestor, boolean addToFirst) {
121     Component JavaDoc 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 JavaDoc) {
131             JComponent JavaDoc jAncestor = (JComponent JavaDoc)a;
132
133             jAncestor.addPropertyChangeListener(this);
134         }
135         }
136         if (!a.isVisible() || a.getParent() == null || a instanceof Window JavaDoc) {
137         firstInvisibleAncestor = a;
138         }
139     }
140     if (firstInvisibleAncestor instanceof Window JavaDoc &&
141         firstInvisibleAncestor.isVisible()) {
142         firstInvisibleAncestor = null;
143     }
144     }
145
146     void removeListeners(Component JavaDoc ancestor) {
147     Component JavaDoc a;
148     for (a = ancestor; a != null; a = a.getParent()) {
149         a.removeComponentListener(this);
150         if (a instanceof JComponent JavaDoc) {
151         JComponent JavaDoc jAncestor = (JComponent JavaDoc)a;
152         jAncestor.removePropertyChangeListener(this);
153         }
154         if (a == firstInvisibleAncestor || a instanceof Window JavaDoc) {
155         break;
156         }
157     }
158     }
159
160     public void componentResized(ComponentEvent e) {}
161
162     public void componentMoved(ComponentEvent e) {
163     Component JavaDoc source = e.getComponent();
164
165     fireAncestorMoved(root, AncestorEvent.ANCESTOR_MOVED,
166               (Container JavaDoc)source, source.getParent());
167     }
168
169     public void componentShown(ComponentEvent e) {
170     Component JavaDoc ancestor = e.getComponent();
171
172     if (ancestor == firstInvisibleAncestor) {
173         addListeners(ancestor, false);
174         if (firstInvisibleAncestor == null) {
175         fireAncestorAdded(root, AncestorEvent.ANCESTOR_ADDED,
176                   (Container JavaDoc)ancestor, ancestor.getParent());
177         }
178     }
179     }
180     
181     public void componentHidden(ComponentEvent e) {
182     Component JavaDoc ancestor = e.getComponent();
183     boolean needsNotify = firstInvisibleAncestor == null;
184
185     if ( !(ancestor instanceof Window JavaDoc) ) {
186         removeListeners(ancestor.getParent());
187     }
188     firstInvisibleAncestor = ancestor;
189     if (needsNotify) {
190         fireAncestorRemoved(root, AncestorEvent.ANCESTOR_REMOVED,
191                 (Container JavaDoc)ancestor, ancestor.getParent());
192     }
193     }
194
195     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
196     String JavaDoc s = evt.getPropertyName();
197
198     if (s!=null && (s.equals("parent") || s.equals("ancestor"))) {
199         JComponent JavaDoc component = (JComponent JavaDoc)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 JavaDoc oldParent = (Container JavaDoc)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