1 19 20 package org.netbeans.modules.java.j2seproject; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeEvent ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.swing.event.ChangeEvent ; 27 import javax.swing.event.ChangeListener ; 28 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; 29 import org.openide.util.Mutex; 30 import org.netbeans.api.project.Sources; 31 import org.netbeans.api.project.SourceGroup; 32 import org.netbeans.api.project.ProjectManager; 33 import org.netbeans.api.project.FileOwnerQuery; 34 import org.netbeans.api.java.project.JavaProjectConstants; 35 import org.netbeans.spi.project.support.ant.SourcesHelper; 36 import org.netbeans.spi.project.support.ant.AntProjectHelper; 37 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 38 39 42 public class J2SESources implements Sources, PropertyChangeListener , ChangeListener { 43 44 private static final String BUILD_DIR_PROP = "${" + J2SEProjectProperties.BUILD_DIR + "}"; private static final String DIST_DIR_PROP = "${" + J2SEProjectProperties.DIST_DIR + "}"; 47 private final AntProjectHelper helper; 48 private final PropertyEvaluator evaluator; 49 private final SourceRoots sourceRoots; 50 private final SourceRoots testRoots; 51 private SourcesHelper sourcesHelper; 52 private Sources delegate; 53 56 private boolean externalRootsRegistered; 57 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 58 59 J2SESources(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 ProjectManager.mutex().readAccess(new Mutex.Action<SourceGroup[]>() { 80 public SourceGroup[] run() { 81 Sources _delegate; 82 synchronized (J2SESources.this) { 83 if (delegate == null) { 84 delegate = initSources(); 85 delegate.addChangeListener(J2SESources.this); 86 } 87 _delegate = delegate; 88 } 89 return _delegate.getSourceGroups(type); 90 } 91 }); 92 } 93 94 private Sources initSources() { 95 this.sourcesHelper = new SourcesHelper(helper, evaluator); register(sourceRoots); 97 register(testRoots); 98 this.sourcesHelper.addNonSourceRoot (BUILD_DIR_PROP); 99 this.sourcesHelper.addNonSourceRoot(DIST_DIR_PROP); 100 externalRootsRegistered = false; 101 ProjectManager.mutex().postWriteRequest(new Runnable () { 102 public void run() { 103 if (!externalRootsRegistered) { 104 sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 105 externalRootsRegistered = true; 106 } 107 } 108 }); 109 return this.sourcesHelper.createSources(); 110 } 111 112 private void register(SourceRoots roots) { 113 String [] propNames = roots.getRootProperties(); 114 String [] rootNames = roots.getRootNames(); 115 for (int i = 0; i < propNames.length; i++) { 116 String prop = propNames[i]; 117 String displayName = roots.getRootDisplayName(rootNames[i], prop); 118 String loc = "${" + prop + "}"; String includes = "${" + J2SEProjectProperties.INCLUDES + "}"; String excludes = "${" + J2SEProjectProperties.EXCLUDES + "}"; sourcesHelper.addPrincipalSourceRoot(loc, includes, excludes, displayName, null, null); sourcesHelper.addTypedSourceRoot(loc, includes, excludes, JavaProjectConstants.SOURCES_TYPE_JAVA, displayName, null, null); } 124 } 125 126 public void addChangeListener(ChangeListener changeListener) { 127 synchronized (listeners) { 128 listeners.add(changeListener); 129 } 130 } 131 132 public void removeChangeListener(ChangeListener changeListener) { 133 synchronized (listeners) { 134 listeners.remove(changeListener); 135 } 136 } 137 138 private void fireChange() { 139 ChangeListener [] _listeners; 140 synchronized (this) { 141 if (delegate != null) { 142 delegate.removeChangeListener(this); 143 delegate = null; 144 } 145 } 146 synchronized (listeners) { 147 if (listeners.isEmpty()) { 148 return; 149 } 150 _listeners = listeners.toArray(new ChangeListener [listeners.size()]); 151 } 152 ChangeEvent ev = new ChangeEvent (this); 153 for (ChangeListener l : _listeners) { 154 l.stateChanged(ev); 155 } 156 } 157 158 public void propertyChange(PropertyChangeEvent evt) { 159 String propName = evt.getPropertyName(); 160 if (SourceRoots.PROP_ROOT_PROPERTIES.equals(propName) || 161 J2SEProjectProperties.BUILD_DIR.equals(propName) || 162 J2SEProjectProperties.DIST_DIR.equals(propName)) { 163 this.fireChange(); 164 } 165 } 166 167 public void stateChanged (ChangeEvent event) { 168 this.fireChange(); 169 } 170 171 } 172 | Popular Tags |