KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > group > GroupNodeChildren


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
20
21 package org.netbeans.modules.group;
22
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.util.Collections JavaDoc;
26 import org.openide.loaders.DataObject;
27 import org.openide.nodes.Children;
28 import org.openide.nodes.Node;
29 import org.openide.util.RequestProcessor;
30 import org.openide.util.WeakListeners;
31
32 /** This class defines children of a <code>GroupNode</code>. */
33 class GroupNodeChildren extends Children.Keys {
34
35     /** data object represented by these children's parent node */
36     private final GroupShadow groupShadow;
37     /** listener listening for changes of the group's content */
38     private GroupContentListener groupContentListener;
39     /**
40      * weak listener listening on the group and notifying
41      * the {@link #groupContentListener}
42      */

43     private PropertyChangeListener JavaDoc propChangeListener;
44
45     /**
46      * Creates a set of children.
47      *
48      * @param groupShadow data object represented by these children's parent
49      * node
50      */

51     public GroupNodeChildren(GroupShadow groupShadow) {
52         this.groupShadow = groupShadow;
53     }
54     
55     /**
56      * Listener listening for changes of the group's content.
57      * When a change is detected, {@linkplain #update updates}
58      * this children object.
59      */

60     private class GroupContentListener implements PropertyChangeListener JavaDoc {
61         public void propertyChange(PropertyChangeEvent JavaDoc e) {
62             String JavaDoc propName = e.getPropertyName();
63             if (DataObject.Container.PROP_CHILDREN.equals(propName)) {
64                 GroupNodeChildren.this.update();
65             }
66         }
67     }
68
69     /** */
70     protected void addNotify() {
71         setKeys(Collections.EMPTY_SET);
72         RequestProcessor.postRequest(new Runnable JavaDoc() {
73                                          public void run() {
74                                              update();
75                                          }
76                                      });
77         groupContentListener = new GroupContentListener();
78         groupShadow.addPropertyChangeListener(
79                 propChangeListener = WeakListeners.propertyChange(
80                         groupContentListener,
81                         groupShadow));
82     }
83
84     /** */
85     protected void removeNotify() {
86         setKeys(Collections.EMPTY_SET);
87         groupShadow.removePropertyChangeListener(propChangeListener);
88         propChangeListener = null;
89         groupContentListener = null;
90     }
91
92     /**
93      * Updates the list of children according to the current contents
94      * of the group shadow's primary file.
95      */

96     void update() {
97         setKeys(groupShadow.getLinks());
98     }
99
100     /** */
101     protected Node[] createNodes(Object JavaDoc key) {
102         Node nodes[] = new Node[1];
103
104         if (key instanceof DataObject) {
105             nodes[0] = new LinkNode(groupShadow,
106                                     ((DataObject) key).getNodeDelegate());
107         } else {
108             nodes[0] = new ErrorNode(groupShadow, (String JavaDoc) key);
109         }
110         return nodes;
111     }
112 }
113
Popular Tags