KickJava   Java API By Example, From Geeks To Geeks.

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


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.Enumeration JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import org.netbeans.api.project.FileOwnerQuery;
30 import org.netbeans.api.project.ProjectInformation;
31 import org.netbeans.api.project.ProjectUtils;
32 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
33 import org.netbeans.spi.project.ui.LogicalViewProvider;
34 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
35 import org.netbeans.spi.project.ui.support.NodeFactorySupport;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.loaders.DataObject;
39 import org.openide.loaders.DataObjectNotFoundException;
40 import org.openide.nodes.Node;
41 import org.openide.util.NbBundle;
42 import org.openide.util.Utilities;
43 import org.openide.util.WeakListeners;
44 import org.openide.util.lookup.Lookups;
45
46 /**
47  * Provides a logical view of a NetBeans suite project.
48  *
49  * @author Jesse Glick, Martin Krauskopf
50  */

51 public final class SuiteLogicalView implements LogicalViewProvider {
52     
53     private final SuiteProject suite;
54     
55     public SuiteLogicalView(final SuiteProject suite) {
56         this.suite = suite;
57     }
58     
59     public Node createLogicalView() {
60         return new SuiteRootNode(suite);
61     }
62     
63     public Node findPath(Node root, Object JavaDoc target) {
64         if (root.getLookup().lookup(SuiteProject.class) != suite) {
65             // Not intended for this project. Should not normally happen anyway.
66
return null;
67         }
68         
69         DataObject file;
70         if (target instanceof FileObject) {
71             try {
72                 file = DataObject.find((FileObject) target);
73             } catch (DataObjectNotFoundException e) {
74                 return null; // OK
75
}
76         } else if (target instanceof DataObject) {
77             file = (DataObject) target;
78         } else {
79             // What is it?
80
return null;
81         }
82         
83         Node impFilesNode = root.getChildren().findChild("important.files"); // NOI18N
84
if (impFilesNode != null) {
85             Node[] impFiles = impFilesNode.getChildren().getNodes(true);
86             for (int i = 0; i < impFiles.length; i++) {
87                 if (impFiles[i].getCookie(DataObject.class) == file) {
88                     return impFiles[i];
89                 }
90             }
91         }
92         
93         return null;
94     }
95     
96     /** Package private for unit test only. */
97     static final class SuiteRootNode extends AnnotatedNode
98             implements PropertyChangeListener JavaDoc {
99         
100         private static final Image JavaDoc ICON = Utilities.loadImage(SuiteProject.SUITE_ICON_PATH, true);
101         
102         private final SuiteProject suite;
103         private final ProjectInformation info;
104         
105         SuiteRootNode(final SuiteProject suite) {
106             super(NodeFactorySupport.createCompositeChildren(suite, "Projects/org-netbeans-modules-apisupport-project-suite/Nodes"),
107                   Lookups.fixed(new Object JavaDoc[] {suite}));
108             this.suite = suite;
109             info = ProjectUtils.getInformation(suite);
110             info.addPropertyChangeListener(WeakListeners.propertyChange(this, info));
111             setFiles(getProjectFiles());
112         }
113         
114         /** Package private for unit test only. */
115         Set JavaDoc getProjectFiles() {
116             Set JavaDoc files = new HashSet JavaDoc();
117             Enumeration JavaDoc en = suite.getProjectDirectory().getChildren(false);
118             while (en.hasMoreElements()) {
119                 FileObject child = (FileObject) en.nextElement();
120                 if (FileOwnerQuery.getOwner(child) == suite) {
121                     files.add(child);
122                 }
123             }
124             return files;
125         }
126         
127         public String JavaDoc getName() {
128             return info.getDisplayName();
129         }
130         
131         public String JavaDoc getDisplayName() {
132             return info.getDisplayName();
133         }
134         
135         public String JavaDoc getShortDescription() {
136             return NbBundle.getMessage(SuiteLogicalView.class, "HINT_suite_project_root_node",
137                     FileUtil.getFileDisplayName(suite.getProjectDirectory()));
138         }
139         
140         public Action JavaDoc[] getActions(boolean context) {
141             return SuiteActions.getProjectActions(suite);
142         }
143         
144         public Image JavaDoc getIcon(int type) {
145             return annotateIcon(ICON, type);
146         }
147         
148         public Image JavaDoc getOpenedIcon(int type) {
149             return getIcon(type); // the same in the meantime
150
}
151         
152         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
153             if (evt.getPropertyName().equals(ProjectInformation.PROP_NAME)) {
154                 fireNameChange(null, getName());
155             } else if (evt.getPropertyName().equals(ProjectInformation.PROP_DISPLAY_NAME)) {
156                 fireDisplayNameChange(null, getDisplayName());
157             }
158         }
159         
160         public boolean canRename() {
161             return true;
162         }
163         
164         public void setName(String JavaDoc name) {
165             DefaultProjectOperations.performDefaultRenameOperation(suite, name);
166         }
167         
168     }
169 }
170
Popular Tags