1 19 20 package org.netbeans.modules.ruby.spi.project.support.rake; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import org.netbeans.api.queries.SharabilityQuery; 28 import org.netbeans.spi.queries.SharabilityQueryImplementation; 29 import org.openide.util.WeakListeners; 30 31 35 final class SharabilityQueryImpl implements SharabilityQueryImplementation, PropertyChangeListener { 36 37 private final RakeProjectHelper h; 38 private final PropertyEvaluator eval; 39 private final String [] includes; 40 private final String [] excludes; 41 42 private String [] includePaths; 43 44 private String [] excludePaths; 45 46 SharabilityQueryImpl(RakeProjectHelper h, PropertyEvaluator eval, String [] includes, String [] excludes) { 47 this.h = h; 48 this.eval = eval; 49 this.includes = includes; 50 this.excludes = excludes; 51 computeFiles(); 52 eval.addPropertyChangeListener(WeakListeners.propertyChange(this, eval)); 53 } 54 55 56 private void computeFiles() { 57 String [] _includePaths = computeFrom(includes); 58 String [] _excludePaths = computeFrom(excludes); 59 synchronized (this) { 60 includePaths = _includePaths; 61 excludePaths = _excludePaths; 62 } 63 } 64 65 66 private String [] computeFrom(String [] list) { 67 List <String > result = new ArrayList <String >(list.length); 68 for (String s : list) { 69 String val = eval.evaluate(s); 70 if (val != null) { 71 File f = h.resolveFile(val); 72 result.add(f.getAbsolutePath()); 73 } 74 } 75 return result.toArray(new String [result.size()]); 77 } 78 79 public synchronized int getSharability(File file) { 80 String path = file.getAbsolutePath(); 81 if (contains(path, excludePaths, false)) { 82 return SharabilityQuery.NOT_SHARABLE; 83 } 84 return contains(path, includePaths, false) ? 85 (contains(path, excludePaths, true) ? SharabilityQuery.MIXED : SharabilityQuery.SHARABLE) : 86 SharabilityQuery.UNKNOWN; 87 } 88 89 97 private static boolean contains(String path, String [] list, boolean reverse) { 98 for (String s : list) { 99 if (path.equals(s)) { 100 return true; 101 } else { 102 if (reverse ? s.startsWith(path + File.separatorChar) : path.startsWith(s + File.separatorChar)) { 103 return true; 104 } 105 } 106 } 107 return false; 108 } 109 110 public void propertyChange(PropertyChangeEvent evt) { 111 computeFiles(); 112 } 113 114 } 115 | Popular Tags |