1 19 20 package org.netbeans.modules.web.project.ui; 21 22 import java.awt.Image ; 23 import java.net.URI ; 24 import java.net.URL ; 25 import java.net.MalformedURLException ; 26 import java.io.File ; 27 import java.util.Arrays ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Set ; 32 import javax.swing.Action ; 33 import javax.swing.Icon ; 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 69 class ProjectNode extends AbstractNode { 70 71 private static final String PROJECT_ICON = "org/netbeans/modules/web/project/ui/resources/projectDependencies.gif"; 73 private final AntArtifact antArtifact; 74 private final URI artifactLocation; 75 private Image cachedIcon; 76 77 ProjectNode (AntArtifact antArtifact, URI artifactLocation, UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper, String classPathId, String entryId, String 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 getDisplayName () { 84 ProjectInformation info = getProjectInformation(); 85 if (info != null) { 86 return NbBundle.getMessage(ProjectNode.class,"TXT_ProjectArtifactFormat", 87 new Object [] {info.getDisplayName(), artifactLocation.toString()}); 88 } 89 else { 90 return NbBundle.getMessage (ProjectNode.class,"TXT_UnknownProjectName"); 91 } 92 } 93 94 public String getName () { 95 return this.getDisplayName(); 96 } 97 98 public Image getIcon(int type) { 99 if (cachedIcon == null) { 100 ProjectInformation info = getProjectInformation(); 101 if (info != null) { 102 Icon 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 getOpenedIcon(int type) { 113 return this.getIcon(type); 114 } 115 116 public boolean canCopy() { 117 return false; 118 } 119 120 public Action [] getActions(boolean context) { 121 return new Action [] { 122 SystemAction.get (OpenProjectAction.class), 123 SystemAction.get (ShowJavadocAction.class), 124 SystemAction.get (RemoveClassPathRootAction.class), 125 }; 126 } 127 128 public Action 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 artifactLocation, 141 UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper, 142 String classPathId, String entryId, String webModuleElementName) { 143 Project p = antArtifact.getProject(); 144 Object [] content; 145 if (p == null) { 146 content = new Object [1]; 147 } 148 else { 149 content = new Object [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 artifactLocation; 162 163 JavadocProvider (AntArtifact antArtifact, URI 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 us = findJavadoc(); 175 URL [] urls = (URL [])us.toArray(new URL [us.size()]); 176 URL 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 findJavadoc() { 190 File scriptLocation = this.antArtifact.getScriptLocation(); 191 Set urls = new HashSet (); 192 try { 193 URL 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 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 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 classPathId; 257 private final String entryId; 258 private final String webModuleElementName; 259 260 private final ClassPathSupport cs; 261 262 Removable (UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper, String classPathId, String entryId, String 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 EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 280 return props.getProperty (classPathId) != null; 281 } 282 283 public Project remove() { 284 287 boolean removed = false; 288 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 289 String raw = props.getProperty (classPathId); 290 List resources = cs.itemsList( raw, webModuleElementName ); 291 for (Iterator 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 [] itemRefs = cs.encodeToStrings(resources.iterator(), webModuleElementName); 300 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); props.setProperty (classPathId, itemRefs); 302 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 303 304 String ref = "${" + entryId + "}"; 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 |