1 19 20 package org.netbeans.modules.j2ee.clientproject; 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 import javax.swing.event.ChangeEvent ; 28 import javax.swing.event.ChangeListener ; 29 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.util.Mutex; 32 import org.netbeans.api.project.Sources; 33 import org.netbeans.api.project.SourceGroup; 34 import org.netbeans.api.project.ProjectManager; 35 import org.netbeans.api.project.FileOwnerQuery; 36 import org.netbeans.api.java.project.JavaProjectConstants; 37 import org.netbeans.modules.j2ee.clientproject.ui.AppClientLogicalViewProvider; 38 import org.netbeans.spi.project.support.ant.SourcesHelper; 39 import org.netbeans.spi.project.support.ant.AntProjectHelper; 40 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 41 42 43 46 public class AppClientSources implements Sources, PropertyChangeListener , ChangeListener { 47 48 private static final String BUILD_DIR_PROP = "${" + AppClientProjectProperties.BUILD_DIR + "}"; private static final String DIST_DIR_PROP = "${" + AppClientProjectProperties.DIST_DIR + "}"; 51 private final AntProjectHelper helper; 52 private final PropertyEvaluator evaluator; 53 private final SourceRoots sourceRoots; 54 private final SourceRoots testRoots; 55 private SourcesHelper sourcesHelper; 56 private Sources delegate; 57 60 private boolean externalRootsRegistered; 61 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 62 63 AppClientSources(AntProjectHelper helper, PropertyEvaluator evaluator, 64 SourceRoots sourceRoots, SourceRoots testRoots) { 65 this.helper = helper; 66 this.evaluator = evaluator; 67 this.sourceRoots = sourceRoots; 68 this.testRoots = testRoots; 69 this.sourceRoots.addPropertyChangeListener(this); 70 this.testRoots.addPropertyChangeListener(this); 71 this.evaluator.addPropertyChangeListener(this); 72 initSources(); } 74 75 82 public SourceGroup[] getSourceGroups(final String type) { 83 return (SourceGroup[]) ProjectManager.mutex().readAccess(new Mutex.Action() { 84 public Object run() { 85 Sources _delegate; 86 synchronized (AppClientSources.this) { 87 if (delegate == null) { 88 delegate = initSources(); 89 delegate.addChangeListener(AppClientSources.this); 90 } 91 _delegate = delegate; 92 } 93 return _delegate.getSourceGroups(type); 94 } 95 }); 96 } 97 98 private Sources initSources() { 99 sourcesHelper = new SourcesHelper(helper, evaluator); 100 File projectDir = FileUtil.toFile(this.helper.getProjectDirectory()); 101 String [] propNames = sourceRoots.getRootProperties(); 102 String [] rootNames = sourceRoots.getRootNames(); 103 for (int i = 0; i < propNames.length; i++) { 104 String displayName = rootNames[i]; 105 String prop = "${" + propNames[i] + "}"; if (displayName.length() ==0) { 107 if ("src.dir".equals(propNames[i])) { displayName = SourceRoots.DEFAULT_SOURCE_LABEL; 110 } 111 else { 112 File sourceRoot = helper.resolveFile(evaluator.evaluate(prop)); 115 if (sourceRoot != null) { 116 String srPath = sourceRoot.getAbsolutePath(); 117 String pdPath = projectDir.getAbsolutePath() + File.separatorChar; 118 if (srPath.startsWith(pdPath)) { 119 displayName = srPath.substring(pdPath.length()); 120 } 121 else { 122 displayName = sourceRoot.getAbsolutePath(); 123 } 124 } 125 else { 126 displayName = SourceRoots.DEFAULT_SOURCE_LABEL; 127 } 128 } 129 } 130 sourcesHelper.addPrincipalSourceRoot(prop, displayName, null, null); 131 sourcesHelper.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, null, null); 132 } 133 propNames = testRoots.getRootProperties(); 134 rootNames = testRoots.getRootNames(); 135 for (int i = 0; i < propNames.length; i++) { 136 String displayName = rootNames[i]; 137 String prop = "${" + propNames[i] + "}"; if (displayName.length() ==0) { 139 if ("test.src.dir".equals(propNames[i])) { displayName = SourceRoots.DEFAULT_TEST_LABEL; 142 } 143 else { 144 File sourceRoot = helper.resolveFile(evaluator.evaluate(prop)); 147 if (sourceRoot != null) { 148 String srPath = sourceRoot.getAbsolutePath(); 149 String pdPath = projectDir.getAbsolutePath() + File.separatorChar; 150 if (srPath.startsWith(pdPath)) { 151 displayName = srPath.substring(pdPath.length()); 152 } 153 else { 154 displayName = sourceRoot.getAbsolutePath(); 155 } 156 } 157 else { 158 displayName = SourceRoots.DEFAULT_TEST_LABEL; 159 } 160 } 161 } 162 sourcesHelper.addPrincipalSourceRoot(prop, displayName, null, null); 163 sourcesHelper.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, null, null); 164 } 165 166 String configFilesLabel = org.openide.util.NbBundle.getMessage(AppClientLogicalViewProvider.class, "LBL_Node_ConfFiles"); sourcesHelper.addPrincipalSourceRoot("${"+AppClientProjectProperties.META_INF+"}", configFilesLabel, null, null); 170 sourcesHelper.addNonSourceRoot(BUILD_DIR_PROP); 171 sourcesHelper.addNonSourceRoot(DIST_DIR_PROP); 172 173 externalRootsRegistered = false; 174 ProjectManager.mutex().postWriteRequest(new Runnable () { 175 public void run() { 176 if (!externalRootsRegistered) { 177 sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 178 externalRootsRegistered = true; 179 } 180 } 181 }); 182 return sourcesHelper.createSources(); 183 } 184 185 public void addChangeListener(ChangeListener changeListener) { 186 synchronized (listeners) { 187 listeners.add(changeListener); 188 } 189 } 190 191 public void removeChangeListener(ChangeListener changeListener) { 192 synchronized (listeners) { 193 listeners.remove(changeListener); 194 } 195 } 196 197 private void fireChange() { 198 ChangeListener [] _listeners; 199 synchronized (this) { 200 if (delegate != null) { 201 delegate.removeChangeListener(this); 202 delegate = null; 203 } 204 } 205 synchronized (listeners) { 206 if (listeners.isEmpty()) { 207 return; 208 } 209 _listeners = 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) || 220 AppClientProjectProperties.BUILD_DIR.equals(propName) || 221 AppClientProjectProperties.DIST_DIR.equals(propName)) { 222 this.fireChange(); 223 } 224 } 225 226 public void stateChanged (ChangeEvent event) { 227 this.fireChange(); 228 } 229 230 } 231 | Popular Tags |