1 19 20 package org.netbeans.modules.java.j2seproject.ui; 21 22 import java.awt.Component ; 23 import java.awt.Image ; 24 import java.awt.Panel ; 25 import java.io.IOException ; 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.Set ; 35 import java.util.ArrayList ; 36 import java.util.List ; 37 import javax.swing.Action ; 38 import javax.swing.Icon ; 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 74 class ProjectNode extends AbstractNode { 75 76 private static final String PROJECT_ICON = "org/netbeans/modules/java/j2seproject/ui/resources/projectDependencies.gif"; 78 private final AntArtifact antArtifact; 79 private final URI artifactLocation; 80 private Image cachedIcon; 81 82 ProjectNode (AntArtifact antArtifact, URI artifactLocation, UpdateHelper helper,ReferenceHelper refHelper, String classPathId, String 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 getDisplayName () { 89 ProjectInformation info = getProjectInformation(); 90 if (info != null) { 91 return MessageFormat.format(NbBundle.getMessage(ProjectNode.class,"TXT_ProjectArtifactFormat"), 92 new Object [] {info.getDisplayName(), artifactLocation.toString()}); 93 } 94 else { 95 return NbBundle.getMessage (ProjectNode.class,"TXT_UnknownProjectName"); 96 } 97 } 98 99 public String getName () { 100 return this.getDisplayName(); 101 } 102 103 public Image getIcon(int type) { 104 if (cachedIcon == null) { 105 ProjectInformation info = getProjectInformation(); 106 if (info != null) { 107 Icon 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 getOpenedIcon(int type) { 118 return this.getIcon(type); 119 } 120 121 public boolean canCopy() { 122 return false; 123 } 124 125 public Action [] getActions(boolean context) { 126 return new Action [] { 127 SystemAction.get (OpenProjectAction.class), 128 SystemAction.get (ShowJavadocAction.class), 129 SystemAction.get (RemoveClassPathRootAction.class), 130 }; 131 } 132 133 public Action 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 artifactLocation, 146 UpdateHelper helper, ReferenceHelper refHelper, String classPathId, String entryId) { 147 Project p = antArtifact.getProject(); 148 Object [] content; 149 if (p == null) { 150 content = new Object [1]; 151 } 152 else { 153 content = new Object [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 artifactLocation; 166 167 JavadocProvider (AntArtifact antArtifact, URI 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 us = findJavadoc(); 179 URL [] urls = (URL [])us.toArray(new URL [us.size()]); 180 URL 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 findJavadoc() { 194 File scriptLocation = this.antArtifact.getScriptLocation(); 195 Set urls = new HashSet (); 196 try { 197 URL 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 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 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 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 classPathId; 252 private final String entryId; 253 254 Removable (UpdateHelper helper, ReferenceHelper refHelper, String classPathId, String entryId) { 255 this.helper = helper; 256 this.refHelper = refHelper; 257 this.classPathId = classPathId; 258 this.entryId = entryId; 259 } 260 261 public boolean canRemove () { 262 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 () { 269 public void run() { 270 EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 271 String cp = props.getProperty (classPathId); 272 if (cp != null) { 273 String [] entries = PropertyUtils.tokenizePath(cp); 274 List result = new ArrayList (); 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 )result.get(size-1) + ':'); } 281 result.add (entries[i]); 282 } 283 } 284 props.setProperty (classPathId, (String [])result.toArray(new String [result.size()])); 285 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props); 286 String ref = "${"+entryId+"}"; 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 ioe) { 297 ErrorManager.getDefault().notify(ioe); 298 } 299 } 300 } 301 }); 302 } 303 } 304 } 305 | Popular Tags |