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