1 19 20 package org.netbeans.modules.web.project.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.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileStateInvalidException; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.nodes.Children; 46 import org.openide.nodes.AbstractNode; 47 import org.openide.nodes.Node; 48 import org.openide.util.NbBundle; 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.spi.project.support.ant.PropertyEvaluator; 56 import org.netbeans.spi.java.project.support.ui.PackageView; 57 import org.openide.util.actions.SystemAction; 58 import org.openide.util.lookup.Lookups; 59 import org.openide.xml.XMLUtil; 60 61 62 63 71 class PlatformNode extends AbstractNode implements ChangeListener { 72 73 private static final String PLATFORM_ICON = "org/netbeans/modules/web/project/ui/resources/platform"; private static final String ARCHIVE_ICON = "org/netbeans/modules/web/project/ui/resources/jar.gif"; 76 private final PlatformProvider pp; 77 78 private PlatformNode(PlatformProvider pp) { 79 super (new PlatformContentChildren (), Lookups.singleton (new JavadocProvider(pp))); 80 this.pp = pp; 81 this.pp.addChangeListener(this); 82 setIconBase(PLATFORM_ICON); 83 } 84 85 public String getName () { 86 return this.getDisplayName(); 87 } 88 89 public String getDisplayName () { 90 JavaPlatform plat = pp.getPlatform(); 91 String name; 92 if (plat != null) { 93 name = plat.getDisplayName(); 94 } 95 else { 96 String platformId = pp.getPlatformId (); 97 if (platformId == null) { 98 name = NbBundle.getMessage(PlatformNode.class,"TXT_BrokenPlatform"); 99 } 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>"; } 118 else { 119 return null; 120 } 121 } 122 123 public boolean canCopy() { 124 return false; 125 } 126 127 public Action [] getActions(boolean context) { 128 return new Action [] { 129 SystemAction.get (ShowJavadocAction.class) 130 }; 131 } 132 133 public void stateChanged(ChangeEvent e) { 134 this.fireNameChange(null,null); 135 this.fireDisplayNameChange(null,null); 136 LibrariesNode.rp.post (new Runnable () { 138 public void run () { 139 ((PlatformContentChildren)getChildren()).addNotify (); 140 } 141 }); 142 } 143 144 151 static PlatformNode create (PropertyEvaluator eval, String platformPropName) { 152 PlatformProvider pp = new PlatformProvider (eval, platformPropName); 153 return new PlatformNode (pp); 154 } 155 156 private static class PlatformContentChildren extends Children.Keys { 157 158 PlatformContentChildren () { 159 } 160 161 protected void addNotify() { 162 this.setKeys (this.getKeys()); 163 } 164 165 protected void removeNotify() { 166 this.setKeys(Collections.EMPTY_SET); 167 } 168 169 protected Node[] createNodes(Object key) { 170 SourceGroup sg = (SourceGroup) key; 171 return new Node[] {ActionFilterNode.create(PackageView.createPackageView(sg), null,null,null,null,null,null)}; 172 } 173 174 private List getKeys () { 175 JavaPlatform platform = ((PlatformNode)this.getNode()).pp.getPlatform(); 176 if (platform == null) { 177 return Collections.EMPTY_LIST; 178 } 179 FileObject[] roots = platform.getBootstrapLibraries().getRoots(); 181 List result = new ArrayList (roots.length); 182 for (int i=0; i<roots.length; i++) { 183 try { 184 FileObject file; 185 Icon icon; 186 Icon openedIcon; 187 if ("jar".equals(roots[i].getURL().getProtocol())) { file = FileUtil.getArchiveFile (roots[i]); 189 icon = openedIcon = new ImageIcon (Utilities.loadImage(ARCHIVE_ICON)); 190 } 191 else { 192 file = roots[i]; 193 icon = null; 194 openedIcon = null; 195 } 196 197 if (file.isValid()) { 198 result.add (new LibrariesSourceGroup(roots[i],file.getNameExt(),icon, openedIcon)); 199 } 200 } catch (FileStateInvalidException e) { 201 ErrorManager.getDefault().notify(e); 202 } 203 } 204 return result; 205 } 206 } 207 208 private static class PlatformProvider implements PropertyChangeListener { 209 210 private final PropertyEvaluator evaluator; 211 private final String platformPropName; 212 private JavaPlatform platformCache; 213 private List listeners; 214 215 public PlatformProvider (PropertyEvaluator evaluator, String platformPropName) { 216 this.evaluator = evaluator; 217 this.platformPropName = platformPropName; 218 this.evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this,evaluator)); 219 } 220 221 public String getPlatformId () { 222 return this.evaluator.getProperty(this.platformPropName); 223 } 224 225 public JavaPlatform getPlatform () { 226 if (platformCache == null) { 227 String platformSystemName = getPlatformId(); 228 if (platformSystemName == null) { 229 platformCache = JavaPlatformManager.getDefault().getDefaultPlatform(); 230 } 231 else { 232 JavaPlatform[] platforms = JavaPlatformManager.getDefault().getInstalledPlatforms(); 233 for (int i=0; i<platforms.length; i++) { 234 if (platformSystemName.equals(platforms[i].getProperties().get("platform.ant.name"))) { if (platforms[i].getInstallFolders().size()>0) { 236 platformCache = platforms[i]; 237 } 238 break; 239 } 240 } 241 } 242 } 243 return platformCache; 244 } 245 246 public synchronized void addChangeListener (ChangeListener l) { 247 if (this.listeners == null) { 248 this.listeners = new ArrayList (); 249 } 250 this.listeners.add (l); 251 } 252 253 public synchronized void removeChangeListener (ChangeListener l) { 254 if (this.listeners == null) { 255 return; 256 } 257 this.listeners.remove(l); 258 } 259 260 public void propertyChange(PropertyChangeEvent evt) { 261 if (platformPropName.equals (evt.getPropertyName())) { 262 platformCache = null; 263 this.fireChange (); 264 } 265 } 266 267 private void fireChange () { 268 ChangeListener [] _listeners; 269 synchronized (this) { 270 if (this.listeners == null) { 271 return; 272 } 273 _listeners = (ChangeListener []) this.listeners.toArray(new ChangeListener [listeners.size()]); 274 } 275 ChangeEvent event = new ChangeEvent (this); 276 for (int i=0; i< _listeners.length; i++) { 277 _listeners[i].stateChanged(event); 278 } 279 } 280 281 } 282 283 private static class JavadocProvider implements ShowJavadocAction.JavadocProvider { 284 285 PlatformProvider platformProvider; 286 287 private JavadocProvider (PlatformProvider platformProvider) { 288 this.platformProvider = platformProvider; 289 } 290 291 public boolean hasJavadoc() { 292 JavaPlatform platform = platformProvider.getPlatform(); 293 if (platform == null) { 294 return false; 295 } 296 URL [] javadocRoots = getJavadocRoots(platform); 297 return javadocRoots.length > 0; 298 } 299 300 public void showJavadoc() { 301 JavaPlatform platform = platformProvider.getPlatform(); 302 if (platform != null) { 303 URL [] javadocRoots = getJavadocRoots(platform); 304 URL pageURL = ShowJavadocAction.findJavadoc("/overview-summary.html",javadocRoots); 305 if (pageURL == null) { 306 pageURL = ShowJavadocAction.findJavadoc("/index.html",javadocRoots); 307 } 308 ShowJavadocAction.showJavaDoc(pageURL, platform.getDisplayName()); 309 } 310 } 311 312 313 private static URL [] getJavadocRoots (JavaPlatform platform) { 314 Set result = new HashSet (); 315 List l = platform.getBootstrapLibraries().entries(); 316 for (Iterator it = l.iterator(); it.hasNext();) { 317 ClassPath.Entry e = (ClassPath.Entry) it.next (); 318 result.addAll(Arrays.asList(JavadocForBinaryQuery.findJavadoc (e.getURL()).getRoots())); 319 } 320 return (URL []) result.toArray (new URL [result.size()]); 321 } 322 323 324 } 325 326 } 327 328 329 | Popular Tags |