1 19 20 package org.netbeans.modules.j2ee.ejbjarproject; 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.ejbjarproject.ui.EjbJarLogicalViewProvider; 30 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties; 31 import org.openide.filesystems.FileUtil; 32 import org.openide.util.Mutex; 33 import org.netbeans.api.project.Sources; 34 import org.netbeans.api.project.SourceGroup; 35 import org.netbeans.api.project.ProjectManager; 36 import org.netbeans.api.project.FileOwnerQuery; 37 import org.netbeans.api.java.project.JavaProjectConstants; 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 public class EjbJarSources implements Sources, PropertyChangeListener , ChangeListener { 43 44 private static final String BUILD_DIR_PROP = "${" + EjbJarProjectProperties.BUILD_DIR + "}"; private static final String DIST_DIR_PROP = "${" + EjbJarProjectProperties.DIST_DIR + "}"; 47 private final AntProjectHelper helper; 48 private final PropertyEvaluator evaluator; 49 private final SourceRoots sourceRoots; 50 private final SourceRoots testRoots; 51 private Sources delegate; 52 55 private boolean externalRootsRegistered; 56 private final List listeners = new ArrayList (); 57 private SourcesHelper sourcesHelper; 58 59 EjbJarSources(AntProjectHelper helper, PropertyEvaluator evaluator, 60 SourceRoots sourceRoots, SourceRoots testRoots) { 61 this.helper = helper; 62 this.evaluator = evaluator; 63 this.sourceRoots = sourceRoots; 64 this.testRoots = testRoots; 65 this.sourceRoots.addPropertyChangeListener(this); 66 this.testRoots.addPropertyChangeListener(this); 67 this.evaluator.addPropertyChangeListener(this); 68 initSources(); } 70 71 78 public SourceGroup[] getSourceGroups(final String type) { 79 return (SourceGroup[]) ProjectManager.mutex().readAccess(new Mutex.Action() { 80 public Object run() { 81 Sources _delegate; 82 synchronized (EjbJarSources.this) { 83 if (delegate == null) { 84 delegate = initSources(); 85 delegate.addChangeListener(EjbJarSources.this); 86 } 87 _delegate = delegate; 88 } 89 return _delegate.getSourceGroups(type); 90 } 91 }); 92 } 93 94 private Sources initSources() { 95 sourcesHelper = new SourcesHelper(helper, evaluator); 96 File projectDir = FileUtil.toFile(this.helper.getProjectDirectory()); 97 String [] propNames = sourceRoots.getRootProperties(); 98 String [] rootNames = sourceRoots.getRootNames(); 99 for (int i = 0; i < propNames.length; i++) { 100 String displayName = rootNames[i]; 101 String prop = "${" + propNames[i] + "}"; 102 if (displayName.length() ==0) { 103 if ("src.dir".equals(propNames[i])) { displayName = SourceRoots.DEFAULT_SOURCE_LABEL; 106 } 107 else { 108 File sourceRoot = helper.resolveFile(evaluator.evaluate(prop)); 111 if (sourceRoot != null) { 112 String srPath = sourceRoot.getAbsolutePath(); 113 String pdPath = projectDir.getAbsolutePath() + File.separatorChar; 114 if (srPath.startsWith(pdPath)) { 115 displayName = srPath.substring(pdPath.length()); 116 } 117 else { 118 displayName = sourceRoot.getAbsolutePath(); 119 } 120 } 121 else { 122 displayName = SourceRoots.DEFAULT_SOURCE_LABEL; 123 } 124 } 125 } 126 sourcesHelper.addPrincipalSourceRoot(prop, displayName, null, null); 127 sourcesHelper.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, null, null); 128 } 129 propNames = testRoots.getRootProperties(); 130 rootNames = testRoots.getRootNames(); 131 for (int i = 0; i < propNames.length; i++) { 132 String displayName = rootNames[i]; 133 String prop = "${" + propNames[i] + "}"; 134 if (displayName.length() ==0) { 135 if ("test.src.dir".equals(propNames[i])) { displayName = SourceRoots.DEFAULT_TEST_LABEL; 138 } 139 else { 140 File sourceRoot = helper.resolveFile(evaluator.evaluate(prop)); 143 if (sourceRoot != null) { 144 String srPath = sourceRoot.getAbsolutePath(); 145 String pdPath = projectDir.getAbsolutePath() + File.separatorChar; 146 if (srPath.startsWith(pdPath)) { 147 displayName = srPath.substring(pdPath.length()); 148 } 149 else { 150 displayName = sourceRoot.getAbsolutePath(); 151 } 152 } 153 else { 154 displayName = SourceRoots.DEFAULT_TEST_LABEL; 155 } 156 } 157 } 158 sourcesHelper.addPrincipalSourceRoot(prop, displayName, null, null); 159 sourcesHelper.addTypedSourceRoot(prop, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, null, null); 160 } 161 162 String configFilesLabel = org.openide.util.NbBundle.getMessage(EjbJarLogicalViewProvider.class, "LBL_Node_DocBase"); sourcesHelper.addPrincipalSourceRoot("${"+EjbJarProjectProperties.META_INF+"}", configFilesLabel, null, null); 165 166 sourcesHelper.addNonSourceRoot(BUILD_DIR_PROP); 167 sourcesHelper.addNonSourceRoot(DIST_DIR_PROP); 168 169 externalRootsRegistered = false; 170 ProjectManager.mutex().postWriteRequest(new Runnable () { 171 public void run() { 172 if (!externalRootsRegistered) { 173 sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 174 externalRootsRegistered = true; 175 } 176 } 177 }); 178 return sourcesHelper.createSources(); 179 } 180 181 public void addChangeListener(ChangeListener changeListener) { 182 synchronized (listeners) { 183 listeners.add(changeListener); 184 } 185 } 186 187 public void removeChangeListener(ChangeListener changeListener) { 188 synchronized (listeners) { 189 listeners.remove(changeListener); 190 } 191 } 192 193 private void fireChange() { 194 ChangeListener [] _listeners; 195 synchronized (this) { 196 if (delegate != null) { 197 delegate.removeChangeListener(this); 198 delegate = null; 199 } 200 } 201 synchronized (listeners) { 202 if (listeners.isEmpty()) { 203 return; 204 } 205 _listeners = (ChangeListener [])listeners.toArray(new ChangeListener [listeners.size()]); 206 } 207 ChangeEvent ev = new ChangeEvent (this); 208 for (int i = 0; i < _listeners.length; i++) { 209 _listeners[i].stateChanged(ev); 210 } 211 } 212 213 public void propertyChange(PropertyChangeEvent evt) { 214 String propName = evt.getPropertyName(); 215 if (SourceRoots.PROP_ROOT_PROPERTIES.equals(propName) || EjbJarProjectProperties.BUILD_DIR.equals(propName) || EjbJarProjectProperties.DIST_DIR.equals(propName)) 216 this.fireChange(); 217 } 218 219 public void stateChanged (ChangeEvent event) { 220 this.fireChange(); 221 } 222 223 } 224 | Popular Tags |