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.modules.web.api.webmodule.WebProjectConstants; 40 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 41 import org.netbeans.spi.project.support.ant.SourcesHelper; 42 import org.netbeans.spi.project.support.ant.AntProjectHelper; 43 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 44 45 48 public class WebSources implements Sources, PropertyChangeListener , ChangeListener { 49 50 private static final String BUILD_DIR_PROP = "${" + WebProjectProperties.BUILD_DIR + "}"; private static final String DIST_DIR_PROP = "${" + WebProjectProperties.DIST_DIR + "}"; 53 private final AntProjectHelper helper; 54 private final PropertyEvaluator evaluator; 55 private final SourceRoots sourceRoots; 56 private final SourceRoots testRoots; 57 private Sources delegate; 58 61 private boolean externalRootsRegistered; 62 private final List listeners = new ArrayList (); 63 private SourcesHelper sourcesHelper; 64 65 WebSources(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots, SourceRoots testRoots) { 66 this.helper = helper; 67 this.evaluator = evaluator; 68 this.sourceRoots = sourceRoots; 69 this.testRoots = testRoots; 70 this.sourceRoots.addPropertyChangeListener(this); 71 this.testRoots.addPropertyChangeListener(this); 72 this.evaluator.addPropertyChangeListener(this); 73 initSources(); } 75 76 83 public SourceGroup[] getSourceGroups(final String type) { 84 return (SourceGroup[]) ProjectManager.mutex().readAccess(new Mutex.Action() { 85 public Object run() { 86 Sources _delegate; 87 synchronized (WebSources.this) { 88 if (delegate == null) { 89 delegate = initSources(); 90 delegate.addChangeListener(WebSources.this); 91 } 92 _delegate = delegate; 93 } 94 return _delegate.getSourceGroups(type); 95 } 96 }); 97 } 98 99 private Sources initSources() { 100 sourcesHelper = new SourcesHelper(helper, evaluator); 101 File projectDir = FileUtil.toFile(this.helper.getProjectDirectory()); 102 String [] propNames = sourceRoots.getRootProperties(); 103 String [] rootNames = sourceRoots.getRootNames(); 104 for (int i = 0; i < propNames.length; i++) { 105 String displayName = rootNames[i]; 106 String prop = "${" + propNames[i] + "}"; 107 if (displayName.length() ==0) { 108 if ("src.dir".equals(propNames[i])) { displayName = SourceRoots.DEFAULT_SOURCE_LABEL; 111 } 112 else { 113 File sourceRoot = helper.resolveFile(evaluator.evaluate(prop)); 116 if (sourceRoot != null) { 117 String srPath = sourceRoot.getAbsolutePath(); 118 String pdPath = projectDir.getAbsolutePath() + File.separatorChar; 119 if (srPath.startsWith(pdPath)) { 120 displayName = srPath.substring(pdPath.length()); 121 } 122 else { 123 displayName = sourceRoot.getAbsolutePath(); 124 } 125 } 126 else { 127 displayName = SourceRoots.DEFAULT_SOURCE_LABEL; 128 } 129 } 130 } 131 sourcesHelper.addPrincipalSourceRoot(prop, displayName, null, null); 132 sourcesHelper.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, null, null); 133 } 134 propNames = testRoots.getRootProperties(); 135 rootNames = testRoots.getRootNames(); 136 for (int i = 0; i < propNames.length; i++) { 137 String displayName = rootNames[i]; 138 String prop = "${" + propNames[i] + "}"; 139 if (displayName.length() ==0) { 140 if ("test.src.dir".equals(propNames[i])) { displayName = SourceRoots.DEFAULT_TEST_LABEL; 143 } 144 else { 145 File sourceRoot = helper.resolveFile(evaluator.evaluate(prop)); 148 if (sourceRoot != null) { 149 String srPath = sourceRoot.getAbsolutePath(); 150 String pdPath = projectDir.getAbsolutePath() + File.separatorChar; 151 if (srPath.startsWith(pdPath)) { 152 displayName = srPath.substring(pdPath.length()); 153 } 154 else { 155 displayName = sourceRoot.getAbsolutePath(); 156 } 157 } 158 else { 159 displayName = SourceRoots.DEFAULT_TEST_LABEL; 160 } 161 } 162 } 163 sourcesHelper.addPrincipalSourceRoot(prop, displayName, null, null); 164 sourcesHelper.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, null, null); 165 } 166 167 String webModuleLabel = org.openide.util.NbBundle.getMessage(WebSources.class, "LBL_Node_WebModule"); String webPagesLabel = org.openide.util.NbBundle.getMessage(WebSources.class, "LBL_Node_DocBase"); sourcesHelper.addPrincipalSourceRoot("${"+ WebProjectProperties.SOURCE_ROOT+"}", webModuleLabel, null, null); sourcesHelper.addPrincipalSourceRoot("${"+ WebProjectProperties.WEB_DOCBASE_DIR+"}", webPagesLabel, null, null); sourcesHelper.addTypedSourceRoot("${"+ WebProjectProperties.WEB_DOCBASE_DIR+"}", WebProjectConstants.TYPE_DOC_ROOT, webPagesLabel, null, null); sourcesHelper.addTypedSourceRoot("${"+ WebProjectProperties.WEB_DOCBASE_DIR+"}/WEB-INF", WebProjectConstants.TYPE_WEB_INF, "WEB-INF", null, null); 175 sourcesHelper.addNonSourceRoot(BUILD_DIR_PROP); 176 sourcesHelper.addNonSourceRoot(DIST_DIR_PROP); 177 178 externalRootsRegistered = false; 179 ProjectManager.mutex().postWriteRequest(new Runnable () { 180 public void run() { 181 if (!externalRootsRegistered) { 182 sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 183 externalRootsRegistered = true; 184 } 185 } 186 }); 187 return sourcesHelper.createSources(); 188 } 189 190 public void addChangeListener(ChangeListener changeListener) { 191 synchronized (listeners) { 192 listeners.add(changeListener); 193 } 194 } 195 196 public void removeChangeListener(ChangeListener changeListener) { 197 synchronized (listeners) { 198 listeners.remove(changeListener); 199 } 200 } 201 202 private void fireChange() { 203 ChangeListener [] _listeners; 204 synchronized (listeners) { 205 delegate = null; 206 if (listeners.isEmpty()) { 207 return; 208 } 209 _listeners = (ChangeListener [])listeners.toArray(new ChangeListener [listeners.size()]); 210 } 211 ChangeEvent ev = new ChangeEvent (this); 212 for (int i = 0; i < _listeners.length; i++) { 213 _listeners[i].stateChanged(ev); 214 } 215 } 216 217 public void propertyChange(PropertyChangeEvent evt) { 218 String propName = evt.getPropertyName(); 219 if (SourceRoots.PROP_ROOT_PROPERTIES.equals(propName) || WebProjectProperties.BUILD_DIR.equals(propName) || WebProjectProperties.DIST_DIR.equals(propName)) 220 this.fireChange(); 221 } 222 223 public void stateChanged (ChangeEvent event) { 224 this.fireChange(); 225 } 226 227 } 228 | Popular Tags |