1 19 package org.netbeans.modules.j2ee.clientproject.classpath; 20 21 import java.beans.PropertyChangeEvent ; 22 import org.netbeans.spi.java.classpath.ClassPathImplementation; 23 import org.netbeans.spi.java.classpath.PathResourceImplementation; 24 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 25 import org.netbeans.spi.project.support.ant.AntProjectHelper; 26 import org.openide.filesystems.FileUtil; 27 28 import java.util.List ; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.beans.PropertyChangeListener ; 32 import java.beans.PropertyChangeSupport ; 33 import java.net.MalformedURLException ; 34 import java.io.File ; 35 import java.net.URL ; 36 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 37 38 import org.netbeans.spi.project.support.ant.PropertyUtils; 39 import org.openide.util.WeakListeners; 40 41 final class ProjectClassPathImplementation implements ClassPathImplementation, PropertyChangeListener { 42 43 private PropertyChangeSupport support = new PropertyChangeSupport (this); 44 private AntProjectHelper helper; 45 private String expression; 46 private String resolved; 47 private List <PathResourceImplementation> resources; 48 private boolean isProperty; 49 private final PropertyEvaluator evaluator; 50 51 public ProjectClassPathImplementation (AntProjectHelper helper, String expression, PropertyEvaluator evaluator, boolean isProperty) { 52 assert helper != null && expression != null; 53 this.helper = helper; 54 this.evaluator = evaluator; 55 this.expression = expression; 56 this.isProperty = isProperty; 57 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator)); 58 if (!isProperty) { 59 resolved = evaluator.evaluate (expression); 60 } 61 } 62 63 public ProjectClassPathImplementation (AntProjectHelper helper, String property, PropertyEvaluator evaluator) { 64 this(helper, property, evaluator, true); 65 } 66 67 public synchronized List <PathResourceImplementation> getResources() { 68 if (this.resources == null) { 69 this.resources = this.getPath (); 70 } 71 return this.resources; 72 } 73 74 public void addPropertyChangeListener(PropertyChangeListener listener) { 75 support.addPropertyChangeListener (listener); 76 } 77 78 public void removePropertyChangeListener(PropertyChangeListener listener) { 79 support.removePropertyChangeListener (listener); 80 } 81 82 public void propertyChange(PropertyChangeEvent evt) { 83 if (isProperty && !evt.getPropertyName().equals(expression)) { 84 return; 86 } 87 if (!isProperty) { 88 String eval = evaluator.evaluate (expression); 89 if (eval == resolved) { 90 return; 91 } else { 92 resolved = eval; 93 } 94 } 95 96 List <PathResourceImplementation> newRoots = getPath(); 97 boolean fire = false; 98 synchronized (this) { 99 if (this.resources != null && !this.resources.equals(newRoots)) { 100 this.resources = newRoots; 101 fire = true; 102 } 103 } 104 if (fire) { 105 support.firePropertyChange (PROP_RESOURCES,null,null); 106 } 107 } 108 109 private List <PathResourceImplementation> getPath() { 110 List <PathResourceImplementation> result = new ArrayList <PathResourceImplementation>(); 111 String prop = isProperty ? evaluator.getProperty(expression) : resolved; 112 if (prop != null) { 113 String [] pieces = PropertyUtils.tokenizePath(prop); 114 for (int i = 0; i < pieces.length; i++) { 115 File f = helper.resolveFile(pieces[i]); 116 try { 117 URL entry = f.toURI().toURL(); 118 if (FileUtil.isArchiveFile(entry)) { 119 entry = FileUtil.getArchiveRoot(entry); 120 } else if (!f.exists()) { 121 assert !entry.toExternalForm().endsWith("/") : f; entry = new URL (entry.toExternalForm() + "/"); } 127 result.add(ClassPathSupport.createResource(entry)); 128 } catch (MalformedURLException mue) { 129 assert false : mue; 130 } 131 } 132 } 133 return Collections.unmodifiableList(result); 134 } 135 } 136 | Popular Tags |