KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > 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.ruby.railsprojects.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.openide.ErrorManager;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.nodes.AbstractNode;
44 import org.openide.nodes.Children;
45 import org.openide.nodes.Node;
46 import org.openide.util.Lookup;
47 import org.openide.util.NbBundle;
48 import org.openide.util.Utilities;
49 import org.openide.util.HelpCtx;
50 import org.openide.util.lookup.Lookups;
51 import org.openide.util.actions.SystemAction;
52 import org.openide.util.actions.NodeAction;
53 import org.netbeans.api.project.Project;
54 import org.netbeans.api.project.ProjectInformation;
55 import org.netbeans.api.project.ProjectManager;
56 import org.netbeans.modules.ruby.api.project.rake.RakeArtifact;
57 import org.netbeans.api.project.ui.OpenProjects;
58 import org.netbeans.modules.ruby.spi.project.support.rake.EditableProperties;
59 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
60 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyUtils;
61 import org.netbeans.modules.ruby.spi.project.support.rake.ReferenceHelper;
62 import org.netbeans.modules.ruby.railsprojects.UpdateHelper;
63
64
65
66 /**
67  * ProjectNode represents a dependent project under the Libraries Node.
68  * It is a leaf node with the following actions: {@link OpenProjectAction},
69  * {@link ShowRDocAction} and {@link RemoveClassPathRootAction}
70  *
71  * @author Tomas Zezula
72  */

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

77     private final RakeArtifact antArtifact;
78     private final URI JavaDoc artifactLocation;
79     private Image JavaDoc cachedIcon;
80
81     ProjectNode (RakeArtifact antArtifact, URI JavaDoc artifactLocation, UpdateHelper helper,ReferenceHelper refHelper, String JavaDoc classPathId, String JavaDoc entryId) {
82         super (Children.LEAF, createLookup (antArtifact, artifactLocation, helper, refHelper, classPathId, entryId));
83         this.antArtifact = antArtifact;
84         this.artifactLocation = artifactLocation;
85     }
86
87     public String JavaDoc getDisplayName () {
88         ProjectInformation info = getProjectInformation();
89         if (info != null) {
90             return MessageFormat.format(NbBundle.getMessage(ProjectNode.class,"TXT_ProjectArtifactFormat"),
91                     new Object JavaDoc[] {info.getDisplayName(), artifactLocation.toString()});
92         }
93         else {
94             return NbBundle.getMessage (ProjectNode.class,"TXT_UnknownProjectName");
95         }
96     }
97
98     public String JavaDoc getName () {
99         return this.getDisplayName();
100     }
101
102     public Image JavaDoc getIcon(int type) {
103         if (cachedIcon == null) {
104             ProjectInformation info = getProjectInformation();
105             if (info != null) {
106                 Icon JavaDoc icon = info.getIcon();
107                 cachedIcon = Utilities.icon2Image(icon);
108             }
109             else {
110                 cachedIcon = Utilities.loadImage(PROJECT_ICON);
111             }
112         }
113         return cachedIcon;
114     }
115
116     public Image JavaDoc getOpenedIcon(int type) {
117         return this.getIcon(type);
118     }
119
120     public boolean canCopy() {
121         return false;
122     }
123
124     public Action JavaDoc[] getActions(boolean context) {
125         return new Action JavaDoc[] {
126             SystemAction.get (OpenProjectAction.class),
127             SystemAction.get (ShowRDocAction.class),
128             SystemAction.get (RemoveClassPathRootAction.class),
129         };
130     }
131
132     public Action JavaDoc getPreferredAction () {
133         return getActions(false)[0];
134     }
135     
136     private ProjectInformation getProjectInformation () {
137         Project p = this.antArtifact.getProject();
138         if (p != null) {
139             return ProjectUtils.getInformation(p);
140         }
141         return null;
142     }
143     
144     private static Lookup createLookup (RakeArtifact antArtifact, URI JavaDoc artifactLocation,
145             UpdateHelper helper, ReferenceHelper refHelper, String JavaDoc classPathId, String JavaDoc entryId) {
146         Project p = antArtifact.getProject();
147         Object JavaDoc[] content;
148 // if (p == null) {
149
// content = new Object[1];
150
// }
151
// else {
152
// content = new Object[3];
153
// content[1] = new JavadocProvider(antArtifact, artifactLocation);
154
// content[2] = p;
155
// }
156
// content[0] = new Removable(helper,refHelper, classPathId, entryId);
157
if (p == null) {
158             content = new Object JavaDoc[0];
159         } else {
160             content = new Object JavaDoc[1];
161             //content[0] = new JavadocProvider(antArtifact, artifactLocation);
162
content[0] = p;
163         }
164         Lookup lkp = Lookups.fixed(content);
165         return lkp;
166     }
167
168     private static class OpenProjectAction extends NodeAction {
169
170         protected void performAction(Node[] activatedNodes) {
171             Project[] projects = new Project[activatedNodes.length];
172             for (int i=0; i<projects.length;i++) {
173                 projects[i] = (Project) activatedNodes[i].getLookup().lookup(Project.class);
174             }
175             OpenProjects.getDefault().open(projects, false);
176         }
177
178         protected boolean enable(Node[] activatedNodes) {
179             final Collection JavaDoc/*<Project>*/ openedProjects =Arrays.asList(OpenProjects.getDefault().getOpenProjects());
180             for (int i=0; i<activatedNodes.length; i++) {
181                 Project p;
182                 if ((p = (Project) activatedNodes[i].getLookup().lookup(Project.class)) == null) {
183                     return false;
184                 }
185                 if (openedProjects.contains(p)) {
186                     return false;
187                 }
188             }
189             return true;
190         }
191
192         public String JavaDoc getName() {
193             return NbBundle.getMessage (ProjectNode.class,"CTL_OpenProject");
194         }
195
196         public HelpCtx getHelpCtx() {
197             return new HelpCtx (OpenProjectAction.class);
198         }
199
200         protected boolean asynchronous() {
201             return false;
202         }
203     }
204 }
205
Popular Tags