KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > SourcesNodeFactory


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.apisupport.project.ui;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27 import org.netbeans.api.java.project.JavaProjectConstants;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectUtils;
30 import org.netbeans.api.project.SourceGroup;
31 import org.netbeans.api.project.Sources;
32 import org.netbeans.modules.apisupport.project.NbModuleProject;
33 import org.netbeans.spi.java.project.support.ui.PackageView;
34 import org.netbeans.spi.project.support.GenericSources;
35 import org.netbeans.spi.project.ui.support.NodeFactory;
36 import org.netbeans.spi.project.ui.support.NodeList;
37 import org.openide.filesystems.FileObject;
38 import org.openide.nodes.Node;
39 import org.openide.util.NbBundle;
40
41 /**
42  *
43  * @author mkleint
44  */

45 public class SourcesNodeFactory implements NodeFactory {
46     
47     /** Creates a new instance of SourcesNodeFactory */
48     public SourcesNodeFactory() {
49     }
50     
51     public NodeList createNodes(Project p) {
52         NbModuleProject prj = p.getLookup().lookup(NbModuleProject.class);
53         return new SourceNL(prj);
54     }
55
56     
57     private static class SourceNL implements NodeList<SourceGroup>, ChangeListener JavaDoc {
58         private List JavaDoc<ChangeListener JavaDoc> list = new ArrayList JavaDoc<ChangeListener JavaDoc>();
59         private static final String JavaDoc[] SOURCE_GROUP_TYPES = {
60             JavaProjectConstants.SOURCES_TYPE_JAVA,
61             NbModuleProject.SOURCES_TYPE_JAVAHELP,
62         };
63         
64         private final NbModuleProject project;
65         
66         SourceNL(NbModuleProject prj) {
67             project = prj;
68         }
69         public void addChangeListener(ChangeListener JavaDoc l) {
70             list.add(l);
71         }
72
73         public void removeChangeListener(ChangeListener JavaDoc l) {
74             list.remove(l);
75         }
76
77         public void addNotify() {
78             ProjectUtils.getSources(project).addChangeListener(this);
79         }
80
81         public void removeNotify() {
82             ProjectUtils.getSources(project).removeChangeListener(this);
83         }
84
85         public Node node(SourceGroup key) {
86             return PackageView.createPackageView(key);
87         }
88
89         public List JavaDoc<SourceGroup> keys() {
90             List JavaDoc<SourceGroup> l = new ArrayList JavaDoc<SourceGroup>();
91             Sources s = ProjectUtils.getSources(project);
92             for (int i = 0; i < SOURCE_GROUP_TYPES.length; i++) {
93                 SourceGroup[] groups = s.getSourceGroups(SOURCE_GROUP_TYPES[i]);
94                 l.addAll(Arrays.asList(groups));
95             }
96             SourceGroup javadocDocfiles = makeJavadocDocfilesSourceGroup();
97             if (javadocDocfiles != null) {
98                 l.add(javadocDocfiles);
99             }
100             return l;
101         }
102         
103         private SourceGroup makeJavadocDocfilesSourceGroup() {
104             String JavaDoc propname = "javadoc.docfiles"; // NOI18N
105
FileObject root = resolveFileObjectFromProperty(propname);
106             if(root == null) {
107                 return null;
108             }
109             return GenericSources.group(project, root, propname, NbBundle.getMessage(ModuleLogicalView.class, "LBL_extra_javadoc_files"), null, null);
110         }
111         
112         private FileObject resolveFileObjectFromProperty(String JavaDoc property){
113             String JavaDoc filename = project.evaluator().getProperty(property);
114             if (filename == null) {
115                 return null;
116             }
117             return project.getHelper().resolveFileObject(filename);
118         }
119
120         public void stateChanged(ChangeEvent JavaDoc arg0) {
121             for (ChangeListener JavaDoc lst : list) {
122                 lst.stateChanged(new ChangeEvent JavaDoc(this));
123             }
124         }
125 }
126 }
127
Popular Tags