1 19 20 package org.netbeans.bluej; 21 22 import java.io.File ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import javax.swing.event.ChangeListener ; 28 import org.netbeans.api.java.queries.JavadocForBinaryQuery; 29 import org.netbeans.api.java.queries.SourceForBinaryQuery; 30 import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation; 31 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 32 import org.netbeans.spi.project.support.ant.AntProjectHelper; 33 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 34 import org.openide.ErrorManager; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 38 42 public class SFBQueryImpl implements SourceForBinaryQueryImplementation, JavadocForBinaryQueryImplementation { 43 44 private final AntProjectHelper helper; 45 private final PropertyEvaluator evaluator; 46 private Map cache = new HashMap (); 47 private DocResult docResult; 48 49 private BluejProject project; 50 51 public SFBQueryImpl(BluejProject proj, AntProjectHelper helper, PropertyEvaluator evaluator) { 52 this.helper = helper; 53 this.evaluator = evaluator; 54 project = proj; 55 } 56 57 public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { 58 if (FileUtil.getArchiveFile(binaryRoot) != null) { 59 binaryRoot = FileUtil.getArchiveFile(binaryRoot); 60 } 62 SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) cache.get (binaryRoot); 63 if (res != null) { 64 return res; 65 } 66 FileObject src = null; 67 if (matches(binaryRoot,"build.classes.dir")) { src = project.getProjectDirectory(); 69 } 70 else if (matches (binaryRoot,"dist.jar")) { src = project.getProjectDirectory(); 72 } 73 else if (matches (binaryRoot,"build.test.classes.dir")) { src = project.getProjectDirectory(); 75 } 76 if (src == null) { 77 return null; 78 } 79 else { 80 res = new Result(src); 81 cache.put (binaryRoot, res); 82 return res; 83 } 84 } 85 86 87 private boolean matches (URL binaryRoot, String binaryProperty) { 88 try { 89 String outDir = evaluator.getProperty(binaryProperty); 90 if (outDir != null) { 91 File f = helper.resolveFile (outDir); 92 URL url = f.toURI().toURL(); 93 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { assert !url.toExternalForm().endsWith("/") : f; url = new URL (url.toExternalForm() + "/"); } 98 if (url.equals (binaryRoot)) { 99 return true; 100 } 101 } 102 } catch (MalformedURLException malformedURL) { 103 ErrorManager.getDefault().notify(malformedURL); 104 } 105 return false; 106 } 107 108 public JavadocForBinaryQuery.Result findJavadoc(URL binaryRoot) { 109 if (FileUtil.getArchiveFile(binaryRoot) != null) { 110 binaryRoot = FileUtil.getArchiveFile(binaryRoot); 111 } 113 if (matches (binaryRoot, "build.classes.dir") || matches (binaryRoot, "dist.jar") || matches (binaryRoot, "build.test.classes.dir")) { if (docResult == null) { 116 File fil = new File (FileUtil.toFile(project.getProjectDirectory()), "doc"); try { 120 docResult = new DocResult(fil.toURI().toURL()); 121 } catch (MalformedURLException ex) { 122 ex.printStackTrace(); 123 } 124 } 125 } 126 return docResult; 127 } 128 129 private static class Result implements SourceForBinaryQuery.Result { 130 private FileObject[] sourceRoots; 131 public Result(FileObject fo) { 132 this.sourceRoots = new FileObject[] {fo}; 133 } 134 135 public FileObject[] getRoots () { 136 return this.sourceRoots; 137 } 138 139 public synchronized void addChangeListener (ChangeListener l) { 140 } 141 142 public synchronized void removeChangeListener (ChangeListener l) { 143 } 144 } 145 146 private static class DocResult implements JavadocForBinaryQuery.Result { 147 148 private URL [] urls; 149 public DocResult(URL url) { 150 if (!url.toExternalForm().endsWith("/")) { try { 152 url = new URL (url.toExternalForm() + "/"); } catch (MalformedURLException ex) { 154 ex.printStackTrace(); 155 } } 157 urls = new URL [] {url}; 158 159 } 160 161 public URL [] getRoots() { 162 return urls; 163 } 164 165 public void addChangeListener(ChangeListener l) { 166 } 167 168 public void removeChangeListener(ChangeListener l) { 169 } 170 } 171 172 } 173 | Popular Tags |