1 19 20 package org.netbeans.spi.java.project.classpath.support; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.io.File ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Collections ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import org.netbeans.api.project.ProjectManager; 34 import org.netbeans.spi.java.classpath.ClassPathImplementation; 35 import org.netbeans.spi.java.classpath.PathResourceImplementation; 36 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 37 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 38 import org.netbeans.spi.project.support.ant.PropertyUtils; 39 import org.openide.ErrorManager; 40 import org.openide.filesystems.FileUtil; 41 import org.openide.util.WeakListeners; 42 43 46 final class ProjectClassPathImplementation implements ClassPathImplementation, PropertyChangeListener , Runnable { 47 48 private final PropertyChangeSupport support = new PropertyChangeSupport (this); 49 private final File projectFolder; 50 private List <PathResourceImplementation> resources; 51 private final PropertyEvaluator evaluator; 52 private boolean dirty = false; 53 private final List <String > propertyNames; 54 55 61 public ProjectClassPathImplementation(File projectFolder, String [] propertyNames, PropertyEvaluator evaluator) { 62 assert projectFolder != null && propertyNames != null && evaluator != null; 63 this.projectFolder = projectFolder; 64 this.evaluator = evaluator; 65 this.propertyNames = Arrays.asList(propertyNames); 66 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator)); 67 } 68 69 public synchronized List <PathResourceImplementation> getResources() { 70 if (this.resources == null) { 71 this.resources = this.getPath (); 72 } 73 return this.resources; 74 } 75 76 public void addPropertyChangeListener(PropertyChangeListener listener) { 77 support.addPropertyChangeListener (listener); 78 } 79 80 public void removePropertyChangeListener(PropertyChangeListener listener) { 81 support.removePropertyChangeListener (listener); 82 } 83 84 85 public void propertyChange(PropertyChangeEvent evt) { 86 String prop = evt.getPropertyName(); 87 if (prop != null && !propertyNames.contains(evt.getPropertyName())) { 88 return; 90 } 91 if (!dirty) { 94 dirty = true; 95 ProjectManager.mutex().postReadRequest(this); 96 } 97 } 98 99 public void run() { 100 dirty = false; 101 List <PathResourceImplementation> newRoots = getPath(); 102 boolean fire = false; 103 synchronized (this) { 104 if (this.resources != null && !this.resources.equals(newRoots)) { 105 this.resources = newRoots; 106 fire = true; 107 } 108 } 109 if (fire) { 110 support.firePropertyChange (PROP_RESOURCES,null,null); 111 } 112 } 113 114 private List <PathResourceImplementation> getPath() { 115 List <PathResourceImplementation> result = new ArrayList <PathResourceImplementation>(); 116 for (String p : propertyNames) { 117 String prop = evaluator.getProperty(p); 118 if (prop != null) { 119 for (String piece : PropertyUtils.tokenizePath(prop)) { 120 File f = PropertyUtils.resolveFile(this.projectFolder, piece); 121 try { 122 URL entry = f.toURI().toURL(); 123 if (FileUtil.isArchiveFile(entry) || (f.isFile() && f.length()<4)) { entry = FileUtil.getArchiveRoot(entry); 125 } else if (!f.exists()) { 126 assert !entry.toExternalForm().endsWith("/") : f; entry = new URL (entry.toExternalForm() + "/"); } 132 else if (f.isFile()) { 133 ErrorManager.getDefault().log(ErrorManager.ERROR,"ProjectClassPathImplementation: file: "+f.getAbsolutePath() 134 +" is not a valid archive file."); continue; 136 } 137 result.add(ClassPathSupport.createResource(entry)); 138 } catch (MalformedURLException mue) { 139 assert false : mue; 140 } 141 } 142 } 143 } 144 return Collections.unmodifiableList(result); 145 } 146 147 } 148 | Popular Tags |