1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.io.IOException ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Set ; 29 import javax.swing.event.ChangeListener ; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.project.ProjectManager; 32 import org.netbeans.modules.ant.freeform.spi.support.Util; 33 import org.netbeans.spi.project.SubprojectProvider; 34 import org.openide.ErrorManager; 35 import org.openide.filesystems.FileObject; 36 import org.w3c.dom.Element ; 37 38 43 final class Subprojects implements SubprojectProvider { 44 45 private final FreeformProject project; 46 47 public Subprojects(FreeformProject project) { 48 this.project = project; 49 } 50 51 public Set <? extends Project> getSubprojects() { 52 return new LazySubprojectsSet(); 53 } 54 55 67 private Set <Project> createSubprojects(Set <Project> subprojects) { 68 Element config = project.getPrimaryConfigurationData(); 69 Element subprjsEl = Util.findElement(config, "subprojects", FreeformProjectType.NS_GENERAL); if (subprjsEl != null) { 71 for (Element prjEl : Util.findSubElements(subprjsEl)) { 72 assert prjEl.getLocalName().equals("project") : "Bad element " + prjEl + " in <subprojects> for " + project; 73 String rawtext = Util.findText(prjEl); 74 assert rawtext != null : "Need text content for <project> in " + project; 75 String evaltext = project.evaluator().evaluate(rawtext); 76 if (evaltext == null) { 77 continue; 78 } 79 FileObject subprjDir = project.helper().resolveFileObject(evaltext); 80 if (subprjDir == null) { 81 continue; 82 } 83 try { 84 Project p = ProjectManager.getDefault().findProject(subprjDir); 85 if (p != null) { 86 if (subprojects == null) 87 return Collections.emptySet(); 88 else 89 subprojects.add(p); 90 } 91 } catch (IOException e) { 92 org.netbeans.modules.ant.freeform.Util.err.notify(ErrorManager.INFORMATIONAL, e); 93 } 94 } 95 } 96 97 return subprojects; 98 } 99 100 public void addChangeListener(ChangeListener listener) { 101 } 103 104 public void removeChangeListener(ChangeListener listener) { 105 } 107 108 111 private final class LazySubprojectsSet implements Set <Project> { 112 113 private Set <Project> delegateTo = null; 114 115 private synchronized Set <Project> getDelegateTo() { 116 if (delegateTo == null) { 117 delegateTo = createSubprojects(new HashSet <Project>()); 118 } 119 120 return delegateTo; 121 } 122 123 public boolean contains(Object o) { 124 return getDelegateTo().contains(o); 125 } 126 127 public boolean add(Project p) { 128 throw new UnsupportedOperationException (); 129 } 130 131 public boolean remove(Object o) { 132 throw new UnsupportedOperationException (); 133 } 134 135 public boolean removeAll(Collection c) { 136 throw new UnsupportedOperationException (); 137 } 138 139 public boolean retainAll(Collection c) { 140 throw new UnsupportedOperationException (); 141 } 142 143 public boolean addAll(Collection <? extends Project> c) { 144 throw new UnsupportedOperationException (); 145 } 146 147 public boolean containsAll(Collection c) { 148 return getDelegateTo().containsAll(c); 149 } 150 151 public <T> T[] toArray(T[] a) { 152 return getDelegateTo().toArray(a); 153 } 154 155 public void clear() { 156 throw new UnsupportedOperationException (); 157 } 158 159 public int size() { 160 return getDelegateTo().size(); 161 } 162 163 public synchronized boolean isEmpty() { 164 if (delegateTo == null) { 165 return createSubprojects(null) == null; 166 } else { 167 return delegateTo.isEmpty(); 168 } 169 } 170 171 public Iterator <Project> iterator() { 172 return getDelegateTo().iterator(); 173 } 174 175 public Object [] toArray() { 176 return getDelegateTo().toArray(); 177 } 178 179 public int hashCode() { 180 return getDelegateTo().hashCode(); 181 } 182 183 public boolean equals(Object obj) { 184 return getDelegateTo().equals(obj); 185 } 186 187 public String toString() { 188 return getDelegateTo().toString(); 189 } 190 191 } 192 193 } 194 | Popular Tags |