1 19 20 package org.netbeans.modules.web.project; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeEvent ; 24 import java.io.File ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 import javax.swing.event.ChangeEvent ; 29 import javax.swing.event.ChangeListener ; 30 31 import org.openide.filesystems.FileUtil; 32 import org.openide.util.Mutex; 33 34 import org.netbeans.api.project.Sources; 35 import org.netbeans.api.project.SourceGroup; 36 import org.netbeans.api.project.ProjectManager; 37 import org.netbeans.api.project.FileOwnerQuery; 38 import org.netbeans.api.java.project.JavaProjectConstants; 39 import org.netbeans.spi.project.support.ant.SourcesHelper; 40 import org.netbeans.spi.project.support.ant.AntProjectHelper; 41 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 42 43 public class JavaSources implements Sources, PropertyChangeListener { 44 45 private final AntProjectHelper helper; 46 private final PropertyEvaluator evaluator; 47 private final SourceRoots sourceRoots; 48 private final SourceRoots testRoots; 49 private Sources delegate; 50 private final List listeners = new ArrayList (); 51 52 JavaSources(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots, SourceRoots testRoots) { 53 this.helper = helper; 54 this.evaluator = evaluator; 55 this.sourceRoots = sourceRoots; 56 this.testRoots = testRoots; 57 this.sourceRoots.addPropertyChangeListener(this); 58 this.testRoots.addPropertyChangeListener(this); 59 initSources(); } 61 62 public SourceGroup[] getSourceGroups(final String type) { 63 return (SourceGroup[]) ProjectManager.mutex().readAccess(new Mutex.Action() { 64 public Object run() { 65 if (delegate == null) { 66 delegate = initSources(); 67 } 68 return delegate.getSourceGroups(type); 69 } 70 }); 71 } 72 73 private Sources initSources() { 74 final SourcesHelper h = new SourcesHelper(helper, evaluator); 75 File projectDir = FileUtil.toFile(this.helper.getProjectDirectory()); 76 String [] propNames = sourceRoots.getRootProperties(); 77 String [] rootNames = sourceRoots.getRootNames(); 78 for (int i = 0; i < propNames.length; i++) { 79 String displayName = rootNames[i]; 80 String prop = "${" + propNames[i] + "}"; 81 if (displayName.length() ==0) { 82 if ("src.dir".equals(propNames[i])) { displayName = SourceRoots.DEFAULT_SOURCE_LABEL; 85 } 86 else { 87 File sourceRoot = helper.resolveFile(evaluator.evaluate(prop)); 90 if (sourceRoot != null) { 91 String srPath = sourceRoot.getAbsolutePath(); 92 String pdPath = projectDir.getAbsolutePath() + File.separatorChar; 93 if (srPath.startsWith(pdPath)) { 94 displayName = srPath.substring(pdPath.length()); 95 } 96 else { 97 displayName = sourceRoot.getAbsolutePath(); 98 } 99 } 100 else { 101 displayName = SourceRoots.DEFAULT_SOURCE_LABEL; 102 } 103 } 104 } 105 h.addPrincipalSourceRoot(prop, displayName, null, null); 106 h.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, null, null); 107 } 108 propNames = testRoots.getRootProperties(); 109 rootNames = testRoots.getRootNames(); 110 for (int i = 0; i < propNames.length; i++) { 111 String displayName = rootNames[i]; 112 String prop = "${" + propNames[i] + "}"; 113 if (displayName.length() ==0) { 114 if ("test.src.dir".equals(propNames[i])) { displayName = SourceRoots.DEFAULT_TEST_LABEL; 117 } 118 else { 119 File sourceRoot = helper.resolveFile(evaluator.evaluate(prop)); 122 if (sourceRoot != null) { 123 String srPath = sourceRoot.getAbsolutePath(); 124 String pdPath = projectDir.getAbsolutePath() + File.separatorChar; 125 if (srPath.startsWith(pdPath)) { 126 displayName = srPath.substring(pdPath.length()); 127 } 128 else { 129 displayName = sourceRoot.getAbsolutePath(); 130 } 131 } 132 else { 133 displayName = SourceRoots.DEFAULT_TEST_LABEL; 134 } 135 } 136 } 137 h.addPrincipalSourceRoot(prop, displayName, null, null); 138 h.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, null, null); 139 } 140 ProjectManager.mutex().postWriteRequest(new Runnable () { 142 public void run() { 143 h.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 144 } 145 }); 146 return h.createSources(); 147 } 148 149 public void addChangeListener(ChangeListener changeListener) { 150 synchronized (listeners) { 151 listeners.add(changeListener); 152 } 153 } 154 155 public void removeChangeListener(ChangeListener changeListener) { 156 synchronized (listeners) { 157 listeners.remove(changeListener); 158 } 159 } 160 161 private void fireChange() { 162 ChangeListener [] _listeners; 163 synchronized (listeners) { 164 delegate = null; 165 if (listeners.isEmpty()) { 166 return; 167 } 168 _listeners = (ChangeListener [])listeners.toArray(new ChangeListener [listeners.size()]); 169 } 170 ChangeEvent ev = new ChangeEvent (this); 171 for (int i = 0; i < _listeners.length; i++) { 172 _listeners[i].stateChanged(ev); 173 } 174 } 175 176 public void propertyChange(PropertyChangeEvent evt) { 177 if (SourceRoots.PROP_ROOT_PROPERTIES.equals(evt.getPropertyName())) { 178 this.fireChange(); 179 } 180 } 181 182 } 183 | Popular Tags |