1 19 20 package org.netbeans.modules.j2ee.clientproject.ui; 21 22 import java.awt.Component ; 23 import java.awt.Image ; 24 import java.awt.Panel ; 25 import java.awt.image.BufferedImage ; 26 import java.net.URI ; 27 import java.net.URL ; 28 import java.net.MalformedURLException ; 29 import java.io.File ; 30 import java.text.MessageFormat ; 31 import java.util.Arrays ; 32 import java.util.Collection ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.Set ; 36 import java.util.List ; 37 import javax.swing.Action ; 38 import javax.swing.Icon ; 39 import javax.swing.ImageIcon ; 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 75 class ProjectNode extends AbstractNode { 76 77 private static final String PROJECT_ICON = "org/netbeans/modules/j2ee/clientproject/ui/resources/projectDependencies.gif"; private static final Component CONVERTOR_COMPONENT = new Panel (); 79 80 private final AntArtifact antArtifact; 81 private final URI artifactLocation; 82 private Image cachedIcon; 83 84 ProjectNode (AntArtifact antArtifact, URI artifactLocation, UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper, String classPathId, String entryId, String 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 getDisplayName () { 91 ProjectInformation info = getProjectInformation(); 92 if (info != null) { 93 return MessageFormat.format(NbBundle.getMessage(ProjectNode.class,"TXT_ProjectArtifactFormat"), 94 new Object [] {info.getDisplayName(), artifactLocation.toString()}); 95 } 96 else { 97 return NbBundle.getMessage (ProjectNode.class,"TXT_UnknownProjectName"); 98 } 99 } 100 101 public String getName () { 102 return this.getDisplayName(); 103 } 104 105 public Image getIcon(int type) { 106 if (cachedIcon == null) { 107 ProjectInformation info = getProjectInformation(); 108 if (info != null) { 109 Icon icon = info.getIcon(); 110 if (icon instanceof ImageIcon ) { 113 cachedIcon = ((ImageIcon )icon).getImage(); 114 } 115 else { 116 int height = icon.getIconHeight(); 117 int width = icon.getIconWidth(); 118 cachedIcon = new BufferedImage ( 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 getOpenedIcon(int type) { 130 return this.getIcon(type); 131 } 132 133 public boolean canCopy() { 134 return false; 135 } 136 137 public Action [] getActions(boolean context) { 138 return new Action [] { 139 SystemAction.get (OpenProjectAction.class), 140 SystemAction.get (ShowJavadocAction.class), 141 SystemAction.get (RemoveClassPathRootAction.class), 142 }; 143 } 144 145 public Action 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 artifactLocation, 158 UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper, 159 String classPathId, String entryId, String includedLibrariesElement) { 160 Project p = antArtifact.getProject(); 161 Object [] content; 162 if (p == null) { 163 content = new Object [1]; 164 } 165 else { 166 content = new Object [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 artifactLocation; 179 180 JavadocProvider (AntArtifact antArtifact, URI 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 <URL > us = findJavadoc(); 192 URL [] urls = us.toArray(new URL [us.size()]); 193 URL pageURL = ShowJavadocAction.findJavadoc("overview-summary.html",urls); if (pageURL == null) { 195 pageURL = ShowJavadocAction.findJavadoc("index.html",urls); } 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 <URL > findJavadoc() { 207 File scriptLocation = this.antArtifact.getScriptLocation(); 208 Set <URL > urls = new HashSet <URL >(); 209 try { 210 URL 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 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 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 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 classPathId; 266 private final String entryId; 267 private final String includedLibrariesElement; 268 private final ClassPathSupport cs; 269 270 Removable (UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper, String classPathId, String entryId, String 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 EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 288 return props.getProperty (classPathId) != null; 289 } 290 291 public Project remove() { 292 295 boolean removed = false; 296 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 297 String raw = props.getProperty (classPathId); 298 List resources = cs.itemsList(raw, includedLibrariesElement); 299 for (Iterator 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 [] itemRefs = cs.encodeToStrings(resources.iterator(), includedLibrariesElement); 308 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); props.setProperty(classPathId, itemRefs); 310 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 311 312 String ref = "${" + entryId + "}"; 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 |