KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > ui > logicalview > libraries > 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.j2ee.ejbjarproject.ui.logicalview.libraries;
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.text.MessageFormat JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Set JavaDoc;
33 import javax.swing.Action JavaDoc;
34 import javax.swing.Icon JavaDoc;
35 import org.netbeans.api.project.FileOwnerQuery;
36 import org.netbeans.api.project.ProjectUtils;
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.j2ee.ejbjarproject.classpath.ClassPathSupport;
58 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties;
59 import org.netbeans.modules.j2ee.ejbjarproject.UpdateHelper;
60 import org.openide.util.Lookup;
61
62
63
64 /**
65  * ProjectNode represents a dependent project under the Libraries Node.
66  * It is a leaf node with the following actions: {@link OpenProjectAction},
67  * {@link ShowJavadocAction} and {@link RemoveClassPathRootAction}
68  * @author Tomas Zezula
69  */

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

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

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