1 19 package org.netbeans.modules.j2ee.clientproject.queries; 20 21 import java.io.File ; 22 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 23 import org.netbeans.spi.project.support.ant.AntProjectHelper; 24 import org.openide.ErrorManager; 25 import org.openide.filesystems.FileObject; 26 import org.openide.filesystems.FileUtil; 27 28 import java.net.URL ; 29 import java.net.MalformedURLException ; 30 import java.beans.PropertyChangeListener ; 31 import java.beans.PropertyChangeEvent ; 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.HashMap ; 36 import javax.swing.event.ChangeListener ; 37 import javax.swing.event.ChangeEvent ; 38 39 import org.netbeans.api.java.queries.SourceForBinaryQuery; 40 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 41 import org.netbeans.modules.j2ee.clientproject.SourceRoots; 42 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties; 43 44 48 public class CompiledSourceForBinaryQuery implements SourceForBinaryQueryImplementation { 49 50 private final AntProjectHelper helper; 51 private final PropertyEvaluator evaluator; 52 private final SourceRoots sourceRoots; 53 private final SourceRoots testRoots; 54 private Map <URL ,SourceForBinaryQuery.Result> cache = new HashMap <URL ,SourceForBinaryQuery.Result>(); 55 56 public CompiledSourceForBinaryQuery(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots srcRoots, SourceRoots testRoots) { 57 this.helper = helper; 58 this.evaluator = evaluator; 59 this.sourceRoots = srcRoots; 60 this.testRoots = testRoots; 61 } 62 63 public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { 64 if (FileUtil.getArchiveFile(binaryRoot) != null) { 65 binaryRoot = FileUtil.getArchiveFile(binaryRoot); 66 } 68 SourceForBinaryQuery.Result res = cache.get(binaryRoot); 69 if (res != null) { 70 return res; 71 } 72 SourceRoots src = null; 73 if (hasSources(binaryRoot, AppClientProjectProperties.BUILD_CLASSES_DIR)) { src = this.sourceRoots; 75 } else if (hasSources(binaryRoot, AppClientProjectProperties.DIST_JAR)) { src = this.sourceRoots; 77 } else if (hasSources(binaryRoot, AppClientProjectProperties.BUILD_TEST_CLASSES_DIR)) { src = this.testRoots; 79 } 80 if (src == null) { 81 return null; 82 } else { 83 res = new Result(src); 84 cache.put(binaryRoot, res); 85 return res; 86 } 87 } 88 89 90 private boolean hasSources(URL binaryRoot, String binaryProperty) { 91 try { 92 String outDir = evaluator.getProperty(binaryProperty); 93 if (outDir != null) { 94 File f = helper.resolveFile(outDir); 95 URL url = f.toURI().toURL(); 96 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { assert !url.toExternalForm().endsWith("/") : f; url = new URL (url.toExternalForm() + "/"); } 101 if (url.equals(binaryRoot)) { 102 return true; 103 } 104 } 105 } catch (MalformedURLException malformedURL) { 106 ErrorManager.getDefault().notify(malformedURL); 107 } 108 return false; 109 } 110 111 private static class Result implements SourceForBinaryQuery.Result, PropertyChangeListener { 112 113 private ArrayList <ChangeListener > listeners; 114 private SourceRoots sourceRoots; 115 116 public Result(SourceRoots sourceRoots) { 117 this.sourceRoots = sourceRoots; 118 this.sourceRoots.addPropertyChangeListener(this); 119 } 120 121 public FileObject[] getRoots() { 122 return this.sourceRoots.getRoots(); } 124 125 public synchronized void addChangeListener(ChangeListener l) { 126 if (this.listeners == null) { 127 this.listeners = new ArrayList <ChangeListener >(); 128 } 129 this.listeners.add(l); 130 } 131 132 public synchronized void removeChangeListener(ChangeListener l) { 133 if (this.listeners == null) { 134 return; 135 } 136 this.listeners.remove(l); 137 } 138 139 public void propertyChange(PropertyChangeEvent evt) { 140 if (SourceRoots.PROP_ROOTS.equals(evt.getPropertyName())) { 141 this.fireChange(); 142 } 143 } 144 145 private void fireChange() { 146 Iterator <ChangeListener > it; 147 synchronized (this) { 148 if (this.listeners == null) { 149 return; 150 } 151 @SuppressWarnings ("unchecked") 152 ArrayList <ChangeListener > cloned = (ArrayList ) this.listeners.clone(); 153 it = cloned.iterator(); 154 } 155 ChangeEvent event = new ChangeEvent (this); 156 while (it.hasNext()) { 157 (it.next()).stateChanged(event); 158 } 159 } 160 161 } 162 163 } 164 | Popular Tags |