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