1 19 package org.netbeans.modules.java.platform.queries; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.net.URL ; 24 import java.net.MalformedURLException ; 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.ArrayList ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.event.ChangeListener ; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 import org.openide.filesystems.URLMapper; 34 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 35 import org.netbeans.api.java.classpath.ClassPath; 36 import org.netbeans.api.java.platform.JavaPlatformManager; 37 import org.netbeans.api.java.platform.JavaPlatform; 38 import org.netbeans.api.java.queries.SourceForBinaryQuery; 39 import org.openide.util.Exceptions; 40 import org.openide.util.WeakListeners; 41 42 43 47 48 public class PlatformSourceForBinaryQuery implements SourceForBinaryQueryImplementation { 49 50 private static final String JAR_FILE = "jar:file:"; private static final String RTJAR_PATH = "/jre/lib/rt.jar!/"; private static final String SRC_ZIP = "/src.zip"; 54 private Map cache = new HashMap (); 55 56 public PlatformSourceForBinaryQuery () { 57 } 58 59 64 public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { 65 SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) this.cache.get (binaryRoot); 66 if (res != null) { 67 return res; 68 } 69 JavaPlatformManager mgr = JavaPlatformManager.getDefault(); 70 JavaPlatform[] platforms = mgr.getInstalledPlatforms(); 71 for (int i=0; i< platforms.length; i++) { 72 ClassPath cp = platforms[i].getBootstrapLibraries(); 73 for (Iterator it = cp.entries().iterator(); it.hasNext();) { 74 ClassPath.Entry entry = (ClassPath.Entry) it.next(); 75 if (entry.getURL().equals (binaryRoot)) { 76 res = new Result (platforms[i]); 77 this.cache.put (binaryRoot, res); 78 return res; 79 } 80 } 81 } 82 String binaryRootS = binaryRoot.toExternalForm(); 83 if (binaryRootS.startsWith(JAR_FILE)) { 84 if (binaryRootS.endsWith(RTJAR_PATH)) { 85 String srcZipS = binaryRootS.substring(4,binaryRootS.length() - RTJAR_PATH.length()) + SRC_ZIP; 87 try { 88 URL srcZip = FileUtil.getArchiveRoot(new URL (srcZipS)); 89 FileObject fo = URLMapper.findFileObject(srcZip); 90 if (fo != null) { 91 return new UnregisteredPlatformResult (fo); 92 } 93 } catch (MalformedURLException mue) { 94 Exceptions.printStackTrace(mue); 95 } 96 } 97 } 98 return null; 99 } 100 101 private static class Result implements SourceForBinaryQuery.Result, PropertyChangeListener { 102 103 private JavaPlatform platform; 104 private ArrayList listeners; 105 106 public Result (JavaPlatform platform) { 107 this.platform = platform; 108 this.platform.addPropertyChangeListener ((PropertyChangeListener )WeakListeners.create(PropertyChangeListener .class,this,this.platform)); 109 } 110 111 public FileObject[] getRoots () { ClassPath sources = this.platform.getSourceFolders(); 113 return sources.getRoots(); 114 } 115 116 public synchronized void addChangeListener (ChangeListener l) { 117 assert l != null : "Listener can not be null"; if (this.listeners == null) { 119 this.listeners = new ArrayList (); 120 } 121 this.listeners.add (l); 122 } 123 124 public synchronized void removeChangeListener (ChangeListener l) { 125 assert l != null : "Listener can not be null"; if (this.listeners == null) { 127 return; 128 } 129 this.listeners.remove (l); 130 } 131 132 public void propertyChange (PropertyChangeEvent event) { 133 if (JavaPlatform.PROP_SOURCE_FOLDER.equals(event.getPropertyName())) { 134 this.fireChange (); 135 } 136 } 137 138 private void fireChange () { 139 Iterator it = null; 140 synchronized (this) { 141 if (this.listeners == null) { 142 return; 143 } 144 it = ((ArrayList )this.listeners.clone()).iterator (); 145 } 146 ChangeEvent event = new ChangeEvent (this); 147 while (it.hasNext()) { 148 ((ChangeListener )it.next()).stateChanged(event); 149 } 150 } 151 } 152 153 private static class UnregisteredPlatformResult implements SourceForBinaryQuery.Result { 154 155 private FileObject srcRoot; 156 157 private UnregisteredPlatformResult (FileObject fo) { 158 assert fo != null; 159 srcRoot = fo; 160 } 161 162 public FileObject[] getRoots() { 163 return srcRoot.isValid() ? new FileObject[] {srcRoot} : new FileObject[0]; 164 } 165 166 public void addChangeListener(ChangeListener l) { 167 } 169 170 public void removeChangeListener(ChangeListener l) { 171 } 173 }} 174 175 | Popular Tags |