1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import javax.swing.event.ChangeEvent ; 25 import javax.swing.event.ChangeListener ; 26 import org.netbeans.api.project.FileOwnerQuery; 27 import org.netbeans.api.project.ProjectManager; 28 import org.netbeans.api.project.SourceGroup; 29 import org.netbeans.api.project.Sources; 30 import org.netbeans.modules.ant.freeform.spi.support.Util; 31 import org.netbeans.spi.project.support.ant.AntProjectEvent; 32 import org.netbeans.spi.project.support.ant.AntProjectListener; 33 import org.netbeans.spi.project.support.ant.SourcesHelper; 34 import org.openide.util.Mutex; 35 import org.w3c.dom.Element ; 36 37 42 final class FreeformSources implements Sources, AntProjectListener { 43 44 private final FreeformProject project; 45 46 public FreeformSources(FreeformProject project) { 47 this.project = project; 48 project.helper().addAntProjectListener(this); 49 initSources(); } 51 52 private Sources delegate; 53 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 54 55 public SourceGroup[] getSourceGroups(final String type) { 56 return ProjectManager.mutex().readAccess(new Mutex.Action<SourceGroup[]>() { 57 public SourceGroup[] run() { 58 if (delegate == null) { 59 delegate = initSources(); 60 } 61 return delegate.getSourceGroups(type); 62 } 63 }); 64 } 65 66 private Sources initSources() { 67 final SourcesHelper h = new SourcesHelper(project.helper(), project.evaluator()); 68 Element genldata = project.getPrimaryConfigurationData(); 69 Element foldersE = Util.findElement(genldata, "folders", FreeformProjectType.NS_GENERAL); if (foldersE != null) { 71 for (Element folderE : Util.findSubElements(foldersE)) { 72 Element locationE = Util.findElement(folderE, "location", FreeformProjectType.NS_GENERAL); String location = Util.findText(locationE); 74 if (folderE.getLocalName().equals("build-folder")) { h.addNonSourceRoot(location); 76 } else { 77 assert folderE.getLocalName().equals("source-folder") : folderE; 78 Element nameE = Util.findElement(folderE, "label", FreeformProjectType.NS_GENERAL); String name = Util.findText(nameE); 80 Element typeE = Util.findElement(folderE, "type", FreeformProjectType.NS_GENERAL); String includes = null; 82 Element includesE = Util.findElement(folderE, "includes", FreeformProjectType.NS_GENERAL); if (includesE != null) { 84 includes = Util.findText(includesE); 85 } 86 String excludes = null; 87 Element excludesE = Util.findElement(folderE, "excludes", FreeformProjectType.NS_GENERAL); if (excludesE != null) { 89 excludes = Util.findText(excludesE); 90 } 91 if (typeE != null) { 92 String type = Util.findText(typeE); 93 h.addTypedSourceRoot(location, includes, excludes, type, name, null, null); 94 } else { 95 h.addPrincipalSourceRoot(location, includes, excludes, name, null, null); 96 } 97 } 98 } 99 } 100 ProjectManager.mutex().postWriteRequest(new Runnable () { 101 public void run() { 102 h.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); 103 } 104 }); 105 return h.createSources(); 106 } 107 108 public void addChangeListener(ChangeListener changeListener) { 109 synchronized (listeners) { 110 listeners.add(changeListener); 111 } 112 } 113 114 public void removeChangeListener(ChangeListener changeListener) { 115 synchronized (listeners) { 116 listeners.remove(changeListener); 117 } 118 } 119 120 private void fireChange() { 121 ChangeListener [] _listeners; 122 synchronized (listeners) { 123 delegate = null; 124 if (listeners.isEmpty()) { 125 return; 126 } 127 _listeners = listeners.toArray(new ChangeListener [listeners.size()]); 128 } 129 ChangeEvent ev = new ChangeEvent (this); 130 for (ChangeListener l : _listeners) { 131 l.stateChanged(ev); 132 } 133 } 134 135 public void configurationXmlChanged(AntProjectEvent ev) { 136 fireChange(); 137 } 138 139 public void propertiesChanged(AntProjectEvent ev) { 140 } 142 143 } 144 | Popular Tags |