1 19 20 package org.netbeans.modules.apisupport.project.ui; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.CharConversionException ; 25 import java.net.URL ; 26 import java.text.MessageFormat ; 27 import java.util.Arrays ; 28 import java.util.Collections ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.ArrayList ; 33 import java.util.Set ; 34 import javax.swing.Action ; 35 import javax.swing.Icon ; 36 import javax.swing.ImageIcon ; 37 import javax.swing.event.ChangeEvent ; 38 import javax.swing.event.ChangeListener ; 39 import org.netbeans.api.java.classpath.ClassPath; 40 import org.netbeans.api.java.queries.JavadocForBinaryQuery; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileStateInvalidException; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.nodes.Children; 45 import org.openide.nodes.AbstractNode; 46 import org.openide.nodes.Node; 47 import org.openide.util.NbBundle; 48 import org.openide.util.RequestProcessor; 49 import org.openide.util.Utilities; 50 import org.openide.util.WeakListeners; 51 import org.openide.ErrorManager; 52 import org.netbeans.api.java.platform.JavaPlatform; 53 import org.netbeans.api.java.platform.JavaPlatformManager; 54 import org.netbeans.api.project.SourceGroup; 55 import org.netbeans.modules.apisupport.project.ui.customizer.ModuleProperties; 56 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 57 import org.netbeans.spi.java.project.support.ui.PackageView; 58 import org.openide.util.actions.SystemAction; 59 import org.openide.util.lookup.Lookups; 60 import org.openide.xml.XMLUtil; 61 62 65 73 final class PlatformNode extends AbstractNode implements ChangeListener { 74 75 private static final String PLATFORM_ICON = "org/netbeans/modules/apisupport/project/ui/resources/platform.gif"; private static final String ARCHIVE_ICON = "org/netbeans/modules/apisupport/project/ui/resources/jar.gif"; 78 private final PlatformProvider pp; 79 80 private PlatformNode(PlatformProvider pp) { 81 super(new PlatformContentChildren(), Lookups.singleton(new JavadocProvider(pp))); 82 this.pp = pp; 83 this.pp.addChangeListener(this); 84 setIconBaseWithExtension(PLATFORM_ICON); 85 } 86 87 public String getName() { 88 return this.getDisplayName(); 89 } 90 91 public String getDisplayName() { 92 JavaPlatform plat = pp.getPlatform(); 93 String name; 94 if (plat != null) { 95 name = plat.getDisplayName(); 96 } else { 97 String platformId = pp.getPlatformID(); 98 if (platformId == null) { 99 name = NbBundle.getMessage(PlatformNode.class,"TXT_BrokenPlatform"); 100 } else { 101 name = MessageFormat.format(NbBundle.getMessage(PlatformNode.class,"FMT_BrokenPlatform"), new Object [] {platformId}); 102 } 103 } 104 return name; 105 } 106 107 public String getHtmlDisplayName() { 108 if (pp.getPlatform() == null) { 109 String displayName = this.getDisplayName(); 110 try { 111 displayName = XMLUtil.toElementContent(displayName); 112 } catch (CharConversionException ex) { 113 return null; 115 } 116 return "<font color=\"#A40000\">" + displayName + "</font>"; } else { 118 return null; 119 } 120 } 121 122 public boolean canCopy() { 123 return false; 124 } 125 126 public Action [] getActions(boolean context) { 127 return new Action [] { 128 SystemAction.get(ShowJavadocAction.class) 129 }; 130 } 131 132 public void stateChanged(ChangeEvent e) { 133 this.fireNameChange(null, null); 134 this.fireDisplayNameChange(null, null); 135 RequestProcessor.getDefault().post(new Runnable () { 137 public void run() { 138 ((PlatformContentChildren) getChildren()).addNotify(); 139 } 140 }); 141 } 142 143 149 static PlatformNode create(PropertyEvaluator eval, String platformPropName) { 150 PlatformProvider pp = new PlatformProvider(eval, platformPropName); 151 return new PlatformNode(pp); 152 } 153 154 private static class PlatformContentChildren extends Children.Keys { 155 156 PlatformContentChildren() { 157 } 158 159 protected void addNotify() { 160 this.setKeys(this.getKeys()); 161 } 162 163 protected void removeNotify() { 164 this.setKeys(Collections.EMPTY_SET); 165 } 166 167 protected Node[] createNodes(Object key) { 168 SourceGroup sg = (SourceGroup) key; 169 return new Node[] { ActionFilterNode.create(PackageView.createPackageView(sg)) }; 170 } 171 172 private List getKeys() { 173 JavaPlatform platform = ((PlatformNode)this.getNode()).pp.getPlatform(); 174 if (platform == null) { 175 return Collections.EMPTY_LIST; 176 } 177 FileObject[] roots = platform.getBootstrapLibraries().getRoots(); 179 List result = new ArrayList (roots.length); 180 for (int i=0; i<roots.length; i++) { 181 try { 182 FileObject file; 183 Icon icon; 184 if ("jar".equals(roots[i].getURL().getProtocol())) { file = FileUtil.getArchiveFile(roots[i]); 186 icon = new ImageIcon (Utilities.loadImage(ARCHIVE_ICON)); 187 } else { 188 file = roots[i]; 189 icon = null; 190 } 191 if (file.isValid()) { 192 result.add(new LibrariesSourceGroup(roots[i], file.getNameExt(), icon, icon)); 193 } 194 } catch (FileStateInvalidException e) { 195 ErrorManager.getDefault().notify(e); 196 } 197 } 198 return result; 199 } 200 } 201 202 private static class PlatformProvider implements PropertyChangeListener { 203 204 private final PropertyEvaluator evaluator; 205 private final String platformPropName; 206 private JavaPlatform platformCache; 207 private List <ChangeListener > listeners; 208 209 public PlatformProvider(PropertyEvaluator evaluator, String platformPropName) { 210 this.evaluator = evaluator; 211 this.platformPropName = platformPropName; 212 this.evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this,evaluator)); 213 } 214 215 public String getPlatformID() { 216 return this.evaluator.getProperty(this.platformPropName); 217 } 218 219 public JavaPlatform getPlatform() { 220 if (platformCache == null) { 221 final String platformPropName = getPlatformID(); 222 platformCache = ModuleProperties.findJavaPlatformByLocation(platformPropName); 223 if (platformCache != null && platformCache.getInstallFolders().size() == 0) { 224 platformCache = null; 226 } 227 if (ErrorManager.getDefault().isLoggable(ErrorManager.INFORMATIONAL) && platformCache == null) { 229 StringBuffer message = new StringBuffer ("RequestedPlatform: " + platformPropName + " not found.\nInstalled Platforms:\n"); JavaPlatform[] platforms = JavaPlatformManager.getDefault().getInstalledPlatforms(); 231 for (int i=0; i<platforms.length; i++) { 232 message.append("Name: "+platforms[i].getProperties().get("platform.ant.name")+ " Broken: "+ (platforms[i].getInstallFolders().size() == 0) + "\n"); } 235 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, message.toString()); 236 } 237 } 238 return platformCache; 239 } 240 241 public synchronized void addChangeListener(ChangeListener l) { 242 if (this.listeners == null) { 243 this.listeners = new ArrayList (); 244 } 245 this.listeners.add(l); 246 } 247 248 public synchronized void removeChangeListener(ChangeListener l) { 249 if (this.listeners == null) { 250 return; 251 } 252 this.listeners.remove(l); 253 } 254 255 public void propertyChange(PropertyChangeEvent evt) { 256 if (platformPropName.equals(evt.getPropertyName())) { 257 platformCache = null; 258 this.fireChange(); 259 } 260 } 261 262 private void fireChange() { 263 ChangeListener [] _listeners; 264 synchronized (this) { 265 if (this.listeners == null) { 266 return; 267 } 268 _listeners = (ChangeListener []) this.listeners.toArray(new ChangeListener [listeners.size()]); 269 } 270 ChangeEvent event = new ChangeEvent (this); 271 for (int i=0; i< _listeners.length; i++) { 272 _listeners[i].stateChanged(event); 273 } 274 } 275 276 } 277 278 private static class JavadocProvider implements ShowJavadocAction.JavadocProvider { 279 280 PlatformProvider platformProvider; 281 282 private JavadocProvider(PlatformProvider platformProvider) { 283 this.platformProvider = platformProvider; 284 } 285 286 public boolean hasJavadoc() { 287 JavaPlatform platform = platformProvider.getPlatform(); 288 if (platform == null) { 289 return false; 290 } 291 URL [] javadocRoots = getJavadocRoots(platform); 292 return javadocRoots.length > 0; 293 } 294 295 public void showJavadoc() { 296 JavaPlatform platform = platformProvider.getPlatform(); 297 if (platform != null) { 298 URL [] javadocRoots = getJavadocRoots(platform); 299 URL pageURL = ShowJavadocAction.findJavadoc("overview-summary.html",javadocRoots); 300 if (pageURL == null) { 301 pageURL = ShowJavadocAction.findJavadoc("index.html",javadocRoots); 302 } 303 ShowJavadocAction.showJavaDoc(pageURL, platform.getDisplayName()); 304 } 305 } 306 307 private static URL [] getJavadocRoots(JavaPlatform platform) { 308 Set result = new HashSet (); 309 List <ClassPath.Entry> l = platform.getBootstrapLibraries().entries(); 310 for (Iterator it = l.iterator(); it.hasNext();) { 311 ClassPath.Entry e = (ClassPath.Entry) it.next(); 312 result.addAll(Arrays.asList(JavadocForBinaryQuery.findJavadoc(e.getURL()).getRoots())); 313 } 314 return (URL []) result.toArray(new URL [result.size()]); 315 } 316 317 } 318 319 } 320 321 322 | Popular Tags |