1 19 20 package org.netbeans.api.project; 21 22 import java.beans.PropertyChangeListener ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import javax.swing.Icon ; 29 import javax.swing.ImageIcon ; 30 import org.netbeans.spi.project.SubprojectProvider; 31 import org.netbeans.spi.project.support.GenericSources; 32 import org.openide.filesystems.FileStateInvalidException; 33 import org.openide.util.Mutex; 34 import org.openide.util.Utilities; 35 36 40 public class ProjectUtils { 41 42 private ProjectUtils() {} 43 44 52 public static ProjectInformation getInformation(Project p) { 53 ProjectInformation pi = p.getLookup().lookup(ProjectInformation.class); 54 if (pi != null) { 55 return pi; 56 } else { 57 return new BasicInformation(p); 58 } 59 } 60 61 70 public static Sources getSources(Project p) { 71 Sources s = p.getLookup().lookup(Sources.class); 72 if (s != null) { 73 return s; 74 } else { 75 return GenericSources.genericOnly(p); 76 } 77 } 78 79 115 public static boolean hasSubprojectCycles(final Project master, final Project candidate) { 116 return ProjectManager.mutex().readAccess(new Mutex.Action<Boolean >() { 117 public Boolean run() { 118 return visit(new HashSet <Project>(), new HashMap <Project,Set <? extends Project>>(), master, master, candidate); 119 } 120 }); 121 } 122 123 130 private static boolean visit(Set <Project> encountered, Map <Project,Set <? extends Project>> cache, Project curr, Project master, Project candidate) { 131 if (!encountered.add(curr)) { 132 return true; 133 } 134 Set <? extends Project> subprojects = cache.get(curr); 135 if (subprojects == null) { 136 SubprojectProvider spp = curr.getLookup().lookup(SubprojectProvider.class); 137 subprojects = spp != null ? spp.getSubprojects() : Collections.<Project>emptySet(); 138 cache.put(curr, subprojects); 139 } 140 for (Project child : subprojects) { 141 if (candidate == child) { 142 candidate = null; 143 } 144 if (visit(encountered, cache, child, master, candidate)) { 145 return true; 146 } 147 } 148 if (candidate != null && curr == master) { 149 if (visit(encountered, cache, candidate, master, candidate)) { 150 return true; 151 } 152 } 153 assert encountered.contains(curr); 154 encountered.remove(curr); 155 return false; 156 } 157 158 private static final class BasicInformation implements ProjectInformation { 159 160 private final Project p; 161 162 public BasicInformation(Project p) { 163 this.p = p; 164 } 165 166 public String getName() { 167 try { 168 return p.getProjectDirectory().getURL().toExternalForm(); 169 } catch (FileStateInvalidException e) { 170 return e.toString(); 171 } 172 } 173 174 public String getDisplayName() { 175 return p.getProjectDirectory().getNameExt(); 176 } 177 178 public Icon getIcon() { 179 return new ImageIcon (Utilities.loadImage("org/netbeans/modules/projectapi/resources/empty.gif")); } 181 182 public void addPropertyChangeListener(PropertyChangeListener listener) { 183 } 185 186 public void removePropertyChangeListener(PropertyChangeListener listener) { 187 } 189 190 public Project getProject() { 191 return p; 192 } 193 194 } 195 196 } 197 | Popular Tags |