KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tax > beans > TreeObjectListProxyListener


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.tax.beans;
20
21 import java.beans.*;
22 import java.util.*;
23
24 import org.netbeans.tax.*;
25
26 /**
27  * This class listens on all members of object list and
28  * joins all member events into this one source.
29  * <p>
30  * <pre>
31  * TreeObjectListProxyListener proxy =
32  * new TreeObjectListProxyListener(list);
33  * proxy.addPropertyChangeListener(WeakListener.propertyChange(this, proxy));
34  * </pre>
35  *
36  * @author Petr Kuzel
37  * @version 1.0
38  */

39 public class TreeObjectListProxyListener implements PropertyChangeListener {
40
41     private final TreeObjectList list;
42     private PropertyChangeSupport changeSupport;
43         
44     /** Creates new TreeObjectListListener */
45     public TreeObjectListProxyListener(TreeObjectList list) {
46         this.list = list;
47         list.addPropertyChangeListener(this);
48         for (Iterator it = list.iterator(); it.hasNext();) {
49             TreeObject next = (TreeObject) it.next();
50             if (next != null) next.addPropertyChangeListener(this);
51         }
52     }
53
54     /*
55      * Update listening on list members, forward member events.
56      */

57     public void propertyChange(final PropertyChangeEvent e) {
58         String JavaDoc name = e.getPropertyName();
59         Object JavaDoc source = e.getSource();
60         
61         if (source == list) {
62             if (TreeObjectList.PROP_CONTENT_INSERT.equals(name)) {
63                 TreeObject newObject = (TreeObject)e.getNewValue();
64                 if (newObject != null) newObject.addPropertyChangeListener(this);
65             } else if (TreeObjectList.PROP_CONTENT_REMOVE.equals(name)) {
66                 TreeObject oldObject = (TreeObject)e.getOldValue();
67                 if (oldObject != null) oldObject.removePropertyChangeListener(this);
68             }
69         }
70         
71         forward(e);
72     }
73     
74     /**
75      */

76     private void forward(final PropertyChangeEvent e) {
77         if (changeSupport != null)
78             changeSupport.firePropertyChange(e);
79     }
80
81     public void addPropertyChangeListener(PropertyChangeListener l) {
82         synchronized (this) {
83             if (changeSupport == null)
84                 changeSupport = new PropertyChangeSupport(this);
85         }
86         changeSupport.addPropertyChangeListener(l);
87     }
88
89     public void removePropertyChangeListener(PropertyChangeListener l) {
90         if (changeSupport != null)
91             changeSupport.removePropertyChangeListener(l);
92     }
93 }
94
Popular Tags