KickJava   Java API By Example, From Geeks To Geeks.

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


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.web.project.ui;
21
22 import java.awt.Image JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.io.File JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.Icon JavaDoc;
34 import org.netbeans.api.project.FileOwnerQuery;
35 import org.netbeans.api.project.ProjectUtils;
36 import org.netbeans.modules.web.project.classpath.ClassPathSupport;
37 import org.openide.ErrorManager;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.nodes.AbstractNode;
40 import org.openide.nodes.Children;
41 import org.openide.nodes.Node;
42 import org.openide.util.NbBundle;
43 import org.openide.util.Utilities;
44 import org.openide.util.HelpCtx;
45 import org.openide.util.lookup.Lookups;
46 import org.openide.util.actions.SystemAction;
47 import org.openide.util.actions.NodeAction;
48 import org.netbeans.api.project.Project;
49 import org.netbeans.api.project.ProjectInformation;
50 import org.netbeans.api.project.ant.AntArtifact;
51 import org.netbeans.api.project.ui.OpenProjects;
52 import org.netbeans.api.java.queries.JavadocForBinaryQuery;
53 import org.netbeans.spi.project.support.ant.EditableProperties;
54 import org.netbeans.spi.project.support.ant.AntProjectHelper;
55 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
56 import org.netbeans.spi.project.support.ant.ReferenceHelper;
57 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
58 import org.netbeans.modules.web.project.UpdateHelper;
59 import org.openide.util.Lookup;
60
61
62
63 /**
64  * ProjectNode represents a dependent project under the Libraries Node.
65  * It is a leaf node with the following actions: {@link OpenProjectAction},
66  * {@link ShowJavadocAction} and {@link RemoveClassPathRootAction}
67  * @author Tomas Zezula
68  */

69 class ProjectNode extends AbstractNode {
70
71     private static final String JavaDoc PROJECT_ICON = "org/netbeans/modules/web/project/ui/resources/projectDependencies.gif"; //NOI18N
72

73     private final AntArtifact antArtifact;
74     private final URI JavaDoc artifactLocation;
75     private Image JavaDoc cachedIcon;
76
77     ProjectNode (AntArtifact antArtifact, URI JavaDoc artifactLocation, UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper, String JavaDoc classPathId, String JavaDoc entryId, String JavaDoc webModuleElementName) {
78         super (Children.LEAF, createLookup (antArtifact, artifactLocation, helper, eval, refHelper, classPathId, entryId, webModuleElementName));
79         this.antArtifact = antArtifact;
80         this.artifactLocation = artifactLocation;
81     }
82
83     public String JavaDoc getDisplayName () {
84         ProjectInformation info = getProjectInformation();
85         if (info != null) {
86             return NbBundle.getMessage(ProjectNode.class,"TXT_ProjectArtifactFormat",
87                     new Object JavaDoc[] {info.getDisplayName(), artifactLocation.toString()});
88         }
89         else {
90             return NbBundle.getMessage (ProjectNode.class,"TXT_UnknownProjectName");
91         }
92     }
93
94     public String JavaDoc getName () {
95         return this.getDisplayName();
96     }
97
98     public Image JavaDoc getIcon(int type) {
99         if (cachedIcon == null) {
100             ProjectInformation info = getProjectInformation();
101             if (info != null) {
102                 Icon JavaDoc icon = info.getIcon();
103                 cachedIcon = Utilities.icon2Image(icon);
104             }
105             else {
106                 cachedIcon = Utilities.loadImage(PROJECT_ICON);
107             }
108         }
109         return cachedIcon;
110     }
111
112     public Image JavaDoc getOpenedIcon(int type) {
113         return this.getIcon(type);
114     }
115
116     public boolean canCopy() {
117         return false;
118     }
119
120     public Action JavaDoc[] getActions(boolean context) {
121         return new Action JavaDoc[] {
122             SystemAction.get (OpenProjectAction.class),
123             SystemAction.get (ShowJavadocAction.class),
124             SystemAction.get (RemoveClassPathRootAction.class),
125         };
126     }
127
128     public Action JavaDoc getPreferredAction () {
129         return getActions(false)[0];
130     }
131     
132     private ProjectInformation getProjectInformation () {
133         Project p = this.antArtifact.getProject();
134         if (p != null) {
135             return ProjectUtils.getInformation(p);
136         }
137         return null;
138     }
139     
140     private static Lookup createLookup (AntArtifact antArtifact, URI JavaDoc artifactLocation,
141             UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper,
142             String JavaDoc classPathId, String JavaDoc entryId, String JavaDoc webModuleElementName) {
143         Project p = antArtifact.getProject();
144         Object JavaDoc[] content;
145         if (p == null) {
146             content = new Object JavaDoc[1];
147         }
148         else {
149             content = new Object JavaDoc[3];
150             content[1] = new JavadocProvider(antArtifact, artifactLocation);
151             content[2] = p;
152         }
153         content[0] = new Removable(helper, eval, refHelper, classPathId, entryId, webModuleElementName);
154         Lookup lkp = Lookups.fixed(content);
155         return lkp;
156     }
157
158     private static class JavadocProvider implements ShowJavadocAction.JavadocProvider {
159
160         private final AntArtifact antArtifact;
161         private final URI JavaDoc artifactLocation;
162         
163         JavadocProvider (AntArtifact antArtifact, URI JavaDoc artifactLocation) {
164             this.antArtifact = antArtifact;
165             this.artifactLocation = artifactLocation;
166         }
167
168
169         public boolean hasJavadoc() {
170             return findJavadoc().size() > 0;
171         }
172
173         public void showJavadoc() {
174             Set JavaDoc us = findJavadoc();
175             URL JavaDoc[] urls = (URL JavaDoc[])us.toArray(new URL JavaDoc[us.size()]);
176             URL JavaDoc pageURL = ShowJavadocAction.findJavadoc("overview-summary.html",urls);
177             if (pageURL == null) {
178                 pageURL = ShowJavadocAction.findJavadoc("index.html",urls);
179             }
180             ProjectInformation info = null;
181             Project p = this.antArtifact.getProject ();
182             if (p != null) {
183                 info = ProjectUtils.getInformation(p);
184             }
185             ShowJavadocAction.showJavaDoc (pageURL, info == null ?
186                 NbBundle.getMessage (ProjectNode.class,"TXT_UnknownProjectName") : info.getDisplayName());
187         }
188         
189         private Set JavaDoc findJavadoc() {
190             File JavaDoc scriptLocation = this.antArtifact.getScriptLocation();
191             Set JavaDoc urls = new HashSet JavaDoc();
192             try {
193                 URL JavaDoc artifactURL = scriptLocation.toURI().resolve(this.artifactLocation).normalize().toURL();
194                 if (FileUtil.isArchiveFile(artifactURL)) {
195                     artifactURL = FileUtil.getArchiveRoot(artifactURL);
196                 }
197                 urls.addAll(Arrays.asList(JavadocForBinaryQuery.findJavadoc(artifactURL).getRoots()));
198             } catch (MalformedURLException JavaDoc mue) {
199                 ErrorManager.getDefault().notify (mue);
200             }
201             return urls;
202         }
203         
204     }
205
206     private static class OpenProjectAction extends NodeAction {
207
208         protected void performAction(Node[] activatedNodes) {
209             Project[] projects = new Project[activatedNodes.length];
210             for (int i=0; i<projects.length;i++) {
211                 projects[i] = (Project) activatedNodes[i].getLookup().lookup(Project.class);
212             }
213             OpenProjects.getDefault().open(projects, false);
214         }
215
216         protected boolean enable(Node[] activatedNodes) {
217         Project[] openProjs = OpenProjects.getDefault().getOpenProjects();
218         for (int i = 0; i < activatedNodes.length; i++) {
219         Project proj = (Project) activatedNodes[i].getLookup().lookup(Project.class);
220         if (proj == null) {
221             return false;
222         }
223
224         boolean opened = false;
225         for (int j = 0; j < openProjs.length; j++) {
226             if (proj == openProjs[j]) {
227             opened = true;
228             break;
229             }
230         }
231         if (opened == false) {
232             return true;
233         }
234         }
235
236         return false;
237         }
238
239         public String JavaDoc getName() {
240             return NbBundle.getMessage (ProjectNode.class,"CTL_OpenProject");
241         }
242
243         public HelpCtx getHelpCtx() {
244             return new HelpCtx (OpenProjectAction.class);
245         }
246
247         protected boolean asynchronous() {
248             return false;
249         }
250     }
251
252     private static class Removable implements RemoveClassPathRootAction.Removable {
253
254         private final UpdateHelper helper;
255         private final ReferenceHelper refHelper;
256         private final String JavaDoc classPathId;
257         private final String JavaDoc entryId;
258         private final String JavaDoc webModuleElementName;
259
260         private final ClassPathSupport cs;
261
262         Removable (UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper, String JavaDoc classPathId, String JavaDoc entryId, String JavaDoc webModuleElementName) {
263             this.helper = helper;
264             this.refHelper = refHelper;
265             this.classPathId = classPathId;
266             this.entryId = entryId;
267             this.webModuleElementName = webModuleElementName;
268             
269             this.cs = new ClassPathSupport( eval, refHelper, helper.getAntProjectHelper(),
270                                         WebProjectProperties.WELL_KNOWN_PATHS,
271                                         WebProjectProperties.LIBRARY_PREFIX,
272                                         WebProjectProperties.LIBRARY_SUFFIX,
273                                         WebProjectProperties.ANT_ARTIFACT_PREFIX );
274
275         }
276
277         public boolean canRemove () {
278             //Allow to remove only entries from PROJECT_PROPERTIES, same behaviour as the project customizer
279
EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
280             return props.getProperty (classPathId) != null;
281         }
282
283         public Project remove() {
284             // The caller has write access to ProjectManager
285
// and ensures the project will be saved.
286

287             boolean removed = false;
288             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
289             String JavaDoc raw = props.getProperty (classPathId);
290             List JavaDoc resources = cs.itemsList( raw, webModuleElementName );
291             for (Iterator JavaDoc i = resources.iterator(); i.hasNext();) {
292                 ClassPathSupport.Item item = (ClassPathSupport.Item)i.next();
293                 if (entryId.equals(WebProjectProperties.getAntPropertyName(item.getReference()))) {
294                     i.remove();
295                     removed = true;
296                 }
297             }
298             if (removed) {
299                 String JavaDoc[] itemRefs = cs.encodeToStrings(resources.iterator(), webModuleElementName);
300                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
301
props.setProperty (classPathId, itemRefs);
302                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
303
304                 String JavaDoc ref = "${" + entryId + "}"; //NOI18N
305
if (!RemoveClassPathRootAction.isReferenced (new EditableProperties[] {
306                     props,
307                     helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH)}, ref)) {
308                     refHelper.destroyReference(ref);
309                 }
310
311                 return FileOwnerQuery.getOwner(helper.getAntProjectHelper().getProjectDirectory());
312             } else {
313                 return null;
314             }
315         }
316     }
317 }
318
Popular Tags