KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > SourceNodeFactory


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.project.ui;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.AbstractAction JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import org.netbeans.api.java.project.JavaProjectConstants;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.api.project.ProjectUtils;
35 import org.netbeans.api.project.SourceGroup;
36 import org.netbeans.api.project.Sources;
37 import org.netbeans.modules.web.project.WebProject;
38 import org.netbeans.modules.web.project.ui.customizer.CustomizerProviderImpl;
39 import org.netbeans.spi.java.project.support.ui.PackageView;
40 import org.netbeans.spi.project.ui.support.NodeFactory;
41 import org.netbeans.spi.project.ui.support.NodeList;
42 import org.openide.filesystems.FileObject;
43 import org.openide.nodes.FilterNode;
44 import org.openide.nodes.Node;
45 import org.openide.util.NbBundle;
46
47 /**
48  *
49  * @author mkleint
50  */

51 public final class SourceNodeFactory implements NodeFactory {
52     public SourceNodeFactory() {
53     }
54     
55     public NodeList createNodes(Project p) {
56         WebProject project = (WebProject)p.getLookup().lookup(WebProject.class);
57         assert project != null;
58         return new SourcesNodeList(project);
59     }
60     
61     private static class SourcesNodeList implements NodeList<SourceGroupKey>, ChangeListener JavaDoc {
62         
63         private final WebProject project;
64         
65         private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
66         
67         public SourcesNodeList(WebProject proj) {
68             project = proj;
69         }
70         
71         public List JavaDoc<SourceGroupKey> keys() {
72             if (this.project.getProjectDirectory() == null || !this.project.getProjectDirectory().isValid()) {
73                 return Collections.EMPTY_LIST;
74             }
75             Sources sources = getSources();
76             SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
77             
78             List JavaDoc result = new ArrayList JavaDoc(groups.length);
79             for( int i = 0; i < groups.length; i++ ) {
80                 result.add(new SourceGroupKey(groups[i]));
81             }
82             return result;
83         }
84         
85         public synchronized void addChangeListener(ChangeListener JavaDoc l) {
86             listeners.add(l);
87         }
88         
89         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
90             listeners.remove(l);
91         }
92         
93         private void fireChange() {
94             ArrayList JavaDoc<ChangeListener JavaDoc> list = new ArrayList JavaDoc<ChangeListener JavaDoc>();
95             synchronized (this) {
96                 list.addAll(listeners);
97             }
98             Iterator JavaDoc<ChangeListener JavaDoc> it = list.iterator();
99             while (it.hasNext()) {
100                 ChangeListener JavaDoc elem = it.next();
101                 elem.stateChanged(new ChangeEvent JavaDoc( this ));
102             }
103         }
104         
105         public Node node(SourceGroupKey key) {
106             return new PackageViewFilterNode(key.group, project);
107         }
108         
109         public void addNotify() {
110             getSources().addChangeListener(this);
111         }
112         
113         public void removeNotify() {
114             getSources().removeChangeListener(this);
115         }
116         
117         public void stateChanged(ChangeEvent JavaDoc e) {
118             // setKeys(getKeys());
119
// The caller holds ProjectManager.mutex() read lock
120
SwingUtilities.invokeLater(new Runnable JavaDoc() {
121                 public void run() {
122                     fireChange();
123                 }
124             });
125         }
126         
127         private Sources getSources() {
128             return ProjectUtils.getSources(project);
129         }
130         
131     }
132     
133     private static class SourceGroupKey {
134         
135         public final SourceGroup group;
136         public final FileObject fileObject;
137         
138         SourceGroupKey(SourceGroup group) {
139             this.group = group;
140             this.fileObject = group.getRootFolder();
141         }
142         
143         public int hashCode() {
144             return fileObject.hashCode();
145         }
146         
147         public boolean equals(Object JavaDoc obj) {
148             if (!(obj instanceof SourceGroupKey)) {
149                 return false;
150             } else {
151                 SourceGroupKey otherKey = (SourceGroupKey) obj;
152                 String JavaDoc thisDisplayName = this.group.getDisplayName();
153                 String JavaDoc otherDisplayName = otherKey.group.getDisplayName();
154                 // XXX what is the operator binding order supposed to be here??
155
return fileObject.equals(otherKey.fileObject) &&
156                         thisDisplayName == null ? otherDisplayName == null : thisDisplayName.equals(otherDisplayName);
157             }
158         }
159         
160     }
161     
162     /** Yet another cool filter node just to add properties action
163      */

164     private static class PackageViewFilterNode extends FilterNode {
165         
166         private final String JavaDoc nodeName;
167         private final Project project;
168         
169         Action JavaDoc[] actions;
170         
171         public PackageViewFilterNode(SourceGroup sourceGroup, Project project) {
172             super(PackageView.createPackageView(sourceGroup));
173             this.project = project;
174             this.nodeName = "Sources";
175         }
176         
177         
178         public Action JavaDoc[] getActions(boolean context) {
179             if (!context) {
180                 if (actions == null) {
181                     Action JavaDoc superActions[] = super.getActions(context);
182                     actions = new Action JavaDoc[superActions.length + 2];
183                     System.arraycopy(superActions, 0, actions, 0, superActions.length);
184                     actions[superActions.length] = null;
185                     actions[superActions.length + 1] = new PreselectPropertiesAction(project, nodeName);
186                 }
187                 return actions;
188             } else {
189                 return super.getActions(context);
190             }
191         }
192         
193     }
194     
195     
196     /** The special properties action
197      */

198     static class PreselectPropertiesAction extends AbstractAction JavaDoc {
199         
200         private final Project project;
201         private final String JavaDoc nodeName;
202         private final String JavaDoc panelName;
203         
204         public PreselectPropertiesAction(Project project, String JavaDoc nodeName) {
205             this(project, nodeName, null);
206         }
207         
208         public PreselectPropertiesAction(Project project, String JavaDoc nodeName, String JavaDoc panelName) {
209             super(NbBundle.getMessage(SourceNodeFactory.class, "LBL_Properties_Action")); //NOI18N
210
this.project = project;
211             this.nodeName = nodeName;
212             this.panelName = panelName;
213         }
214         
215         public void actionPerformed(ActionEvent JavaDoc e) {
216             // J2SECustomizerProvider cp = (J2SECustomizerProvider) project.getLookup().lookup(J2SECustomizerProvider.class);
217
CustomizerProviderImpl cp = (CustomizerProviderImpl) project.getLookup().lookup(CustomizerProviderImpl.class);
218             if (cp != null) {
219                 cp.showCustomizer(nodeName, panelName);
220             }
221             
222         }
223     }
224     
225 }
226
Popular Tags