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