1 19 20 package org.netbeans.modules.java.j2seproject.queries; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.concurrent.CopyOnWriteArrayList ; 31 import javax.swing.event.ChangeEvent ; 32 import javax.swing.event.ChangeListener ; 33 import org.netbeans.api.java.queries.BinaryForSourceQuery; 34 import org.netbeans.api.java.queries.BinaryForSourceQuery.Result; 35 import org.netbeans.modules.java.j2seproject.SourceRoots; 36 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; 37 import org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation; 38 import org.netbeans.spi.project.support.ant.AntProjectHelper; 39 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 40 import org.openide.util.Exceptions; 41 42 46 public class BinaryForSourceQueryImpl implements BinaryForSourceQueryImplementation { 47 48 49 private final Map <URL ,BinaryForSourceQuery.Result> cache = new HashMap <URL ,BinaryForSourceQuery.Result>(); 50 private final SourceRoots src; 51 private final SourceRoots test; 52 private final PropertyEvaluator eval; 53 private final AntProjectHelper helper; 54 55 56 public BinaryForSourceQueryImpl(SourceRoots src, SourceRoots test, AntProjectHelper helper, PropertyEvaluator eval) { 57 assert src != null; 58 assert test != null; 59 assert helper != null; 60 assert eval != null; 61 this.src = src; 62 this.test = test; 63 this.eval = eval; 64 this.helper = helper; 65 } 66 67 public Result findBinaryRoots(URL sourceRoot) { 68 assert sourceRoot != null; 69 BinaryForSourceQuery.Result result = cache.get(sourceRoot); 70 if (result == null) { 71 for (URL root : this.src.getRootURLs()) { 72 if (root.equals(sourceRoot)) { 73 result = new R (J2SEProjectProperties.BUILD_CLASSES_DIR); 74 cache.put (sourceRoot,result); 75 break; 76 } 77 } 78 for (URL root : this.test.getRootURLs()) { 79 if (root.equals(sourceRoot)) { 80 result = new R (J2SEProjectProperties.BUILD_TEST_CLASSES_DIR); 81 cache.put (sourceRoot,result); 82 break; 83 } 84 } 85 } 86 return result; 87 } 88 89 class R implements BinaryForSourceQuery.Result, PropertyChangeListener { 90 91 private final String propName; 92 private final List <ChangeListener > listeners = new CopyOnWriteArrayList <ChangeListener >(); 93 94 R (final String propName) { 95 assert propName != null; 96 this.propName = propName; 97 eval.addPropertyChangeListener(this); 98 } 99 100 public URL [] getRoots() { 101 String val = eval.getProperty(propName); 102 if (val != null) { 103 File f = helper.resolveFile(val); 104 if (f != null) { 105 try { 106 return new URL [] {f.toURI().toURL()}; 107 } catch (MalformedURLException e) { 108 Exceptions.printStackTrace(e); 109 } 110 } 111 } 112 return new URL [0]; 113 } 114 115 public void addChangeListener(ChangeListener l) { 116 assert l != null; 117 this.listeners.add (l); 118 } 119 120 public void removeChangeListener(ChangeListener l) { 121 assert l != null; 122 this.listeners.remove (l); 123 } 124 125 public void propertyChange(PropertyChangeEvent event) { 126 ChangeEvent ce = new ChangeEvent (this); 127 for (ChangeListener l : listeners) { 128 l.stateChanged(ce); 129 } 130 } 131 } 132 133 } 134 | Popular Tags |