KickJava   Java API By Example, From Geeks To Geeks.

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

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

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