1 19 package org.netbeans.modules.java.j2seproject.queries; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.io.File ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.swing.event.ChangeEvent ; 27 import org.netbeans.spi.project.support.ant.AntProjectHelper; 28 import org.openide.ErrorManager; 29 import org.openide.filesystems.FileUtil; 30 import java.net.URL ; 31 import java.net.MalformedURLException ; 32 import javax.swing.event.ChangeListener ; 33 import javax.xml.transform.Result ; 34 import org.netbeans.api.java.queries.JavadocForBinaryQuery; 35 import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation; 36 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 37 import org.openide.util.WeakListeners; 38 39 43 public class JavadocForBinaryQueryImpl implements JavadocForBinaryQueryImplementation { 44 45 private static final String PROP_JAVADOC_DIR = "dist.javadoc.dir"; 47 private final AntProjectHelper helper; 48 private final PropertyEvaluator evaluator; 49 50 public JavadocForBinaryQueryImpl(AntProjectHelper helper, PropertyEvaluator evaluator) { 51 this.helper = helper; 52 this.evaluator = evaluator; 53 } 54 55 public JavadocForBinaryQuery.Result findJavadoc(final URL binaryRoot) { 56 57 class R implements JavadocForBinaryQuery.Result, PropertyChangeListener { 58 59 private List listeners; 60 private URL [] result; 61 62 public R () { 63 JavadocForBinaryQueryImpl.this.evaluator.addPropertyChangeListener (WeakListeners.propertyChange(this,JavadocForBinaryQueryImpl.this.evaluator)); 64 } 65 66 public synchronized URL [] getRoots() { 67 if (this.result == null) { 68 String javadocDir = evaluator.getProperty(PROP_JAVADOC_DIR); 69 if (javadocDir != null) { 70 File f = helper.resolveFile(javadocDir); 71 try { 72 URL url = f.toURI().toURL(); 73 if (!f.exists()) { 74 assert !url.toExternalForm().endsWith("/") : f; url = new URL (url.toExternalForm() + "/"); } 77 this.result = new URL [] {url}; 78 } catch (MalformedURLException e) { 79 this.result = new URL [0]; 80 ErrorManager.getDefault().notify(e); 81 } 82 } 83 else { 84 this.result = new URL [0]; 85 } 86 } 87 return this.result; 88 } 89 public synchronized void addChangeListener(final ChangeListener l) { 90 assert l != null; 91 if (this.listeners == null) { 92 this.listeners = new ArrayList (); 93 } 94 this.listeners.add (l); 95 } 96 public synchronized void removeChangeListener(final ChangeListener l) { 97 assert l != null; 98 if (this.listeners == null) { 99 return; 100 } 101 this.listeners.remove (l); 102 } 103 104 public void propertyChange (final PropertyChangeEvent event) { 105 if (PROP_JAVADOC_DIR.equals(event.getPropertyName())) { 106 synchronized (this) { 107 result = null; 108 } 109 this.fireChange (); 110 } 111 } 112 113 private void fireChange () { 114 ChangeListener [] _listeners; 115 synchronized (this) { 116 if (this.listeners == null) { 117 return; 118 } 119 _listeners = (ChangeListener []) this.listeners.toArray (new ChangeListener [this.listeners.size()]); 120 } 121 ChangeEvent event = new ChangeEvent (this); 122 for (int i=0; i<_listeners.length; i++) { 123 _listeners[i].stateChanged(event); 124 } 125 } 126 } 127 if (isRootOwner(binaryRoot, "build.classes.dir") || isRootOwner (binaryRoot, "dist.jar")) { return new R(); 129 } 130 return null; 131 } 132 133 private boolean isRootOwner (URL binaryRoot, String binaryProperty) { 134 try { 135 if (FileUtil.getArchiveFile(binaryRoot) != null) { 136 binaryRoot = FileUtil.getArchiveFile(binaryRoot); 137 } 139 String outDir = evaluator.getProperty(binaryProperty); 140 if (outDir != null) { 141 File f = helper.resolveFile (outDir); 142 URL url = f.toURI().toURL(); 143 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { assert !url.toExternalForm().endsWith("/") : f; url = new URL (url.toExternalForm() + "/"); } 147 return url.equals(binaryRoot) || 148 binaryRoot.toExternalForm().startsWith(url.toExternalForm()); 149 } 150 } catch (MalformedURLException malformedURL) { 151 ErrorManager.getDefault().notify(malformedURL); 152 } 153 return false; 154 } 155 156 184 } 185 | Popular Tags |