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