KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.apisupport.project.ui;
21
22 import java.awt.Image JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import org.netbeans.api.project.ProjectUtils;
29 import org.netbeans.spi.java.project.support.ui.PackageView;
30 import org.netbeans.api.project.ProjectInformation;
31 import org.netbeans.api.project.SourceGroup;
32 import org.netbeans.api.project.Sources;
33 import org.netbeans.modules.apisupport.project.NbModuleProject;
34 import org.netbeans.spi.project.ui.LogicalViewProvider;
35 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
36 import org.netbeans.spi.project.ui.support.NodeFactorySupport;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.loaders.DataObject;
40 import org.openide.loaders.DataObjectNotFoundException;
41 import org.openide.nodes.Node;
42 import org.openide.util.NbBundle;
43 import org.openide.util.lookup.Lookups;
44
45 /**
46  * Provides a logical view of a NetBeans module project.
47  * @author Jesse Glick
48  */

49 public final class ModuleLogicalView implements LogicalViewProvider {
50     
51     
52     private final NbModuleProject project;
53     
54     public ModuleLogicalView(NbModuleProject project) {
55         this.project = project;
56     }
57     
58     public Node createLogicalView() {
59         return new RootNode(project);
60     }
61     
62     /** cf. #45952 */
63     public Node findPath(Node root, Object JavaDoc target) {
64         if (root.getLookup().lookup(NbModuleProject.class) != project) {
65             // Not intended for this project. Should not normally happen anyway.
66
return null;
67         }
68         
69         Node[] rootChildren = root.getChildren().getNodes(true);
70         DataObject file;
71         
72         if (target instanceof FileObject) {
73             try {
74                 file = DataObject.find((FileObject) target);
75             } catch (DataObjectNotFoundException e) {
76                 throw new AssertionError JavaDoc(e);
77             }
78         } else if (target instanceof DataObject) {
79             file = (DataObject) target;
80         } else {
81             // What is it?
82
return null;
83         }
84         
85         for (int i = 0; i < rootChildren.length; i++) {
86             Node found = PackageView.findPath(rootChildren[i], target);
87             //System.err.println("found " + found + " for " + target + " in " + rootChildren[i]);
88
if (found != null) {
89                 return found;
90             }
91             // For Important Files node:
92
if (rootChildren[i].getName().equals(ImportantFilesNodeFactory.IMPORTANT_FILES_NAME)) {
93                 Node[] ifChildren = rootChildren[i].getChildren().getNodes(true);
94                 for (int j = 0; j < ifChildren.length; j++) {
95                     if (ifChildren[j].getCookie(DataObject.class) == file) {
96                         return ifChildren[j];
97                     }
98                 }
99             }
100         }
101         
102         return null;
103     }
104     
105     private static final class RootNode extends AnnotatedNode {
106         
107         private final NbModuleProject project;
108         
109         public RootNode(NbModuleProject project) {
110             
111             // XXX add a NodePathResolver impl to lookup
112
super(NodeFactorySupport.createCompositeChildren(project, "Projects/org-netbeans-modules-apisupport-project/Nodes"),
113                   Lookups.fixed(new Object JavaDoc[] {project}));
114             this.project = project;
115             setForceAnnotation(true);
116             setIconBaseWithExtension(NbModuleProject.NB_PROJECT_ICON_PATH);
117             ProjectInformation pi = ProjectUtils.getInformation(project);
118             setDisplayName(pi.getDisplayName());
119             setShortDescription(NbBundle.getMessage(ModuleLogicalView.class, "HINT_project_root_node", FileUtil.getFileDisplayName(project.getProjectDirectory())));
120             setFiles(getProjectFiles());
121             pi.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
122                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
123                     if (evt.getPropertyName() == ProjectInformation.PROP_DISPLAY_NAME) {
124                         RootNode.this.setDisplayName((String JavaDoc) evt.getNewValue());
125                     } else if (evt.getPropertyName() == ProjectInformation.PROP_NAME) {
126                         RootNode.this.setName((String JavaDoc) evt.getNewValue());
127                     }
128                 }
129             });
130         }
131         
132         private Set JavaDoc getProjectFiles() {
133             Set JavaDoc roots = new HashSet JavaDoc();
134             Sources sources = ProjectUtils.getSources(project);
135             
136             // TODO add Sources.addChangeListener(this)
137
SourceGroup[] groups = sources.getSourceGroups(Sources.TYPE_GENERIC);
138             for (int i = 0; i<groups.length; i++) {
139                 SourceGroup group = groups[i];
140                 FileObject fo = group.getRootFolder();
141                 if (fo != null) {
142                     FileObject [] files = fo.getChildren();
143                     for (int j = 0; j < files.length; j++) {
144                         FileObject file = files[j];
145                         if (group.contains(file)) {
146                             roots.add(file);
147                         }
148                     }
149                 }
150             }
151             return roots;
152         }
153         
154         public Action JavaDoc[] getActions(boolean ignore) {
155             return ModuleActions.getProjectActions(project);
156         }
157         
158         public Image JavaDoc getIcon(int type) {
159             return annotateIcon(super.getIcon(type), type);
160         }
161         
162         public Image JavaDoc getOpenedIcon(int type) {
163             return getIcon(type); // the same in the meantime
164
}
165         
166         public boolean canRename() {
167             return true;
168         }
169         
170         public String JavaDoc getName() {
171             return ProjectUtils.getInformation(project).getDisplayName();
172         }
173         
174         public void setName(String JavaDoc name) {
175             DefaultProjectOperations.performDefaultRenameOperation(project, name);
176         }
177         
178     }
179 }
180
Popular Tags