KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > 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.java.j2seproject.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.awt.Panel JavaDoc;
25 import java.io.IOException 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.Set JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.List JavaDoc;
37 import javax.swing.Action JavaDoc;
38 import javax.swing.Icon JavaDoc;
39 import org.netbeans.api.project.FileOwnerQuery;
40 import org.netbeans.api.project.ProjectUtils;
41 import org.netbeans.modules.java.j2seproject.classpath.ClassPathSupport;
42 import org.openide.ErrorManager;
43 import org.openide.filesystems.FileUtil;
44 import org.openide.nodes.AbstractNode;
45 import org.openide.nodes.Children;
46 import org.openide.nodes.Node;
47 import org.openide.util.Lookup;
48 import org.openide.util.NbBundle;
49 import org.openide.util.Utilities;
50 import org.openide.util.HelpCtx;
51 import org.openide.util.lookup.Lookups;
52 import org.openide.util.actions.SystemAction;
53 import org.openide.util.actions.NodeAction;
54 import org.netbeans.api.project.Project;
55 import org.netbeans.api.project.ProjectInformation;
56 import org.netbeans.api.project.ProjectManager;
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.PropertyUtils;
63 import org.netbeans.spi.project.support.ant.ReferenceHelper;
64 import org.netbeans.modules.java.j2seproject.UpdateHelper;
65
66
67
68 /**
69  * ProjectNode represents a dependent project under the Libraries Node.
70  * It is a leaf node with the following actions: {@link OpenProjectAction},
71  * {@link ShowJavadocAction} and {@link RemoveClassPathRootAction}
72  * @author Tomas Zezula
73  */

74 class ProjectNode extends AbstractNode {
75
76     private static final String JavaDoc PROJECT_ICON = "org/netbeans/modules/java/j2seproject/ui/resources/projectDependencies.gif"; //NOI18N
77

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