1 19 package org.netbeans.modules.j2ee.ejbjarproject.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.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties; 28 import org.netbeans.spi.project.support.ant.AntProjectHelper; 29 import org.openide.ErrorManager; 30 import org.openide.filesystems.FileUtil; 31 import java.net.URL ; 32 import java.net.MalformedURLException ; 33 import javax.swing.event.ChangeListener ; 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 final AntProjectHelper helper; 46 private final PropertyEvaluator evaluator; 47 48 public JavadocForBinaryQueryImpl(AntProjectHelper helper, PropertyEvaluator evaluator) { 49 this.helper = helper; 50 this.evaluator = evaluator; 51 } 52 53 public JavadocForBinaryQuery.Result findJavadoc(final URL binaryRoot) { 54 55 class R implements JavadocForBinaryQuery.Result, PropertyChangeListener { 56 57 private List listeners; 58 private URL [] result; 59 60 public R () { 61 JavadocForBinaryQueryImpl.this.evaluator.addPropertyChangeListener (WeakListeners.propertyChange(this,JavadocForBinaryQueryImpl.this.evaluator)); 62 } 63 64 public synchronized URL [] getRoots() { 65 if (this.result == null) { 66 String javadocDir = evaluator.getProperty(EjbJarProjectProperties.DIST_JAVADOC_DIR); 67 if (javadocDir != null) { 68 File f = helper.resolveFile(javadocDir); 69 try { 70 URL url = f.toURI().toURL(); 71 if (!f.exists()) { 72 assert !url.toExternalForm().endsWith("/") : f; url = new URL (url.toExternalForm() + "/"); } 75 this.result = new URL [] {url}; 76 } catch (MalformedURLException e) { 77 this.result = new URL [0]; 78 ErrorManager.getDefault().notify(e); 79 } 80 } 81 else { 82 this.result = new URL [0]; 83 } 84 } 85 return this.result; 86 } 87 public synchronized void addChangeListener(final ChangeListener l) { 88 assert l != null; 89 if (this.listeners == null) { 90 this.listeners = new ArrayList (); 91 } 92 this.listeners.add (l); 93 } 94 public synchronized void removeChangeListener(final ChangeListener l) { 95 assert l != null; 96 if (this.listeners == null) { 97 return; 98 } 99 this.listeners.remove (l); 100 } 101 102 public void propertyChange (final PropertyChangeEvent event) { 103 if (EjbJarProjectProperties.DIST_JAVADOC_DIR.equals(event.getPropertyName())) { 104 synchronized (this) { 105 result = null; 106 } 107 this.fireChange (); 108 } 109 } 110 111 private void fireChange () { 112 ChangeListener [] _listeners; 113 synchronized (this) { 114 if (this.listeners == null) { 115 return; 116 } 117 _listeners = (ChangeListener []) this.listeners.toArray (new ChangeListener [this.listeners.size()]); 118 } 119 ChangeEvent event = new ChangeEvent (this); 120 for (int i=0; i<_listeners.length; i++) { 121 _listeners[i].stateChanged(event); 122 } 123 } 124 } 125 if (isRootOwner(binaryRoot, EjbJarProjectProperties.BUILD_CLASSES_DIR) || isRootOwner (binaryRoot, EjbJarProjectProperties.DIST_JAR)) { 126 return new R(); 127 } 128 return null; 129 } 130 131 private boolean isRootOwner (URL binaryRoot, String binaryProperty) { 132 try { 133 if (FileUtil.getArchiveFile(binaryRoot) != null) { 134 binaryRoot = FileUtil.getArchiveFile(binaryRoot); 135 } 137 String outDir = evaluator.getProperty(binaryProperty); 138 if (outDir != null) { 139 File f = helper.resolveFile (outDir); 140 URL url = f.toURI().toURL(); 141 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { assert !url.toExternalForm().endsWith("/") : f; url = new URL (url.toExternalForm() + "/"); } 145 return url.equals(binaryRoot) || 146 binaryRoot.toExternalForm().startsWith(url.toExternalForm()); 147 } 148 } catch (MalformedURLException malformedURL) { 149 ErrorManager.getDefault().notify(malformedURL); 150 } 151 return false; 152 } 153 154 182 } 183 | Popular Tags |