1 19 package org.netbeans.modules.versioning.spi; 20 21 import org.netbeans.modules.versioning.util.Utils; 22 import org.netbeans.modules.versioning.util.FlatFolder; 23 import org.netbeans.api.project.Project; 24 import org.netbeans.api.project.Sources; 25 import org.netbeans.api.project.ProjectUtils; 26 import org.netbeans.api.project.SourceGroup; 27 import org.netbeans.api.fileinfo.NonRecursiveFolder; 28 import org.netbeans.api.queries.SharabilityQuery; 29 import org.openide.nodes.Node; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileUtil; 32 import org.openide.util.Lookup; 33 import org.openide.loaders.DataObject; 34 import org.openide.loaders.DataShadow; 35 36 import java.io.File ; 37 import java.util.*; 38 import java.lang.ref.WeakReference ; 39 import java.lang.ref.Reference ; 40 41 47 public final class VCSContext { 48 49 public static final VCSContext Empty = new VCSContext(null, emptySet(), emptySet() ); 50 51 54 private static VCSContext contextCached; 55 private static Reference <Node[]> contextNodesCached = new WeakReference <Node []>(null); 56 57 private final Node [] nodes; 58 59 private final Set<File > rootFiles; 60 private final Set<File > exclusions; 61 62 70 public static VCSContext forLookup(Lookup lookup) { 71 Lookup.Result<Node> result = lookup.lookup(new Lookup.Template<Node>(Node.class)); 72 Collection<? extends Node> nodes = result.allInstances(); 73 return VCSContext.forNodes(nodes.toArray(new Node[nodes.size()])); 74 } 75 76 82 public static VCSContext forFiles(Set<File > rootFiles) { 83 return new VCSContext(null, Collections.unmodifiableSet(new HashSet<File >(rootFiles)), emptySet()); 84 } 85 86 94 public synchronized static VCSContext forNodes(Node[] nodes) { 95 if (Arrays.equals(contextNodesCached.get(), nodes)) return contextCached; 96 Set<File > files = new HashSet<File >(nodes.length); 97 Set<File > rootFiles = new HashSet<File >(nodes.length); 98 Set<File > rootFileExclusions = new HashSet<File >(5); 99 for (int i = 0; i < nodes.length; i++) { 100 Node node = nodes[i]; 101 File aFile = node.getLookup().lookup(File .class); 102 if (aFile != null) { 103 files.add(aFile); 104 rootFiles.add(aFile); 105 continue; 106 } 107 Project project = node.getLookup().lookup(Project.class); 108 if (project != null) { 109 addProjectFiles(rootFiles, rootFileExclusions, project); 110 continue; 111 } 112 addFileObjects(node, files, rootFiles); 113 } 114 115 contextCached = new VCSContext(nodes, rootFiles, rootFileExclusions); 116 contextNodesCached = new WeakReference <Node []>(nodes); 117 return contextCached; 118 } 119 120 125 public Node[] getNodes() { 126 return nodes; 127 } 128 129 134 public Set<File > getRootFiles() { 135 return rootFiles; 136 } 137 138 143 public Set<File > getExclusions() { 144 return exclusions; 145 } 146 147 154 public boolean contains(File file) { 155 outter : for (File root : rootFiles) { 156 if (Utils.isParentOrEqual(root, file)) { 157 for (File excluded : exclusions) { 158 if (Utils.isParentOrEqual(excluded, file)) { 159 continue outter; 160 } 161 } 162 return true; 163 } 164 } 165 return false; 166 } 167 168 private static void addProjectFiles(Collection<File > rootFiles, Collection<File > rootFilesExclusions, Project project) { 169 Sources sources = ProjectUtils.getSources(project); 170 SourceGroup[] sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC); 171 for (int j = 0; j < sourceGroups.length; j++) { 172 SourceGroup sourceGroup = sourceGroups[j]; 173 FileObject srcRootFo = sourceGroup.getRootFolder(); 174 File rootFile = FileUtil.toFile(srcRootFo); 175 rootFiles.add(rootFile); 176 FileObject [] rootChildren = srcRootFo.getChildren(); 177 for (int i = 0; i < rootChildren.length; i++) { 178 FileObject rootChildFo = rootChildren[i]; 179 File child = FileUtil.toFile(rootChildFo); 180 if (!sourceGroup.contains(rootChildFo) && SharabilityQuery.getSharability(child) != SharabilityQuery.NOT_SHARABLE) { 182 rootFilesExclusions.add(child); 183 } 184 } 185 } 186 } 187 188 private static void addFileObjects(Node node, Set<File > files, Set<File > rootFiles) { 189 Collection<? extends NonRecursiveFolder> folders = node.getLookup().lookup(new Lookup.Template<NonRecursiveFolder>(NonRecursiveFolder.class)).allInstances(); 190 List<File > nodeFiles = new ArrayList<File >(); 191 if (folders.size() > 0) { 192 for (Iterator j = folders.iterator(); j.hasNext();) { 193 NonRecursiveFolder nonRecursiveFolder = (NonRecursiveFolder) j.next(); 194 nodeFiles.add(new FlatFolder(FileUtil.toFile(nonRecursiveFolder.getFolder()).getAbsolutePath())); 195 } 196 } else { 197 Collection<? extends FileObject> fileObjects = node.getLookup().lookup(new Lookup.Template<FileObject>(FileObject.class)).allInstances(); 198 if (fileObjects.size() > 0) { 199 nodeFiles.addAll(toFileCollection(fileObjects)); 200 } else { 201 DataObject dataObject = node.getCookie(DataObject.class); 202 if (dataObject instanceof DataShadow) { 203 dataObject = ((DataShadow) dataObject).getOriginal(); 204 } 205 if (dataObject != null) { 206 Collection<File > doFiles = toFileCollection(dataObject.files()); 207 nodeFiles.addAll(doFiles); 208 } 209 } 210 } 211 files.addAll(nodeFiles); 212 rootFiles.addAll(nodeFiles); 213 } 214 215 private static Collection<File > toFileCollection(Collection<? extends FileObject> fileObjects) { 216 Set<File > files = new HashSet<File >(fileObjects.size()*4/3+1); 217 for (FileObject fo : fileObjects) { 218 files.add(FileUtil.toFile(fo)); 219 } 220 files.remove(null); 221 return files; 222 } 223 224 private VCSContext(Node [] nodes, Set<File > rootFiles, Set<File > exclusions) { 225 this.nodes = nodes; this.rootFiles = Collections.unmodifiableSet(new HashSet<File >(rootFiles)); 227 this.exclusions = Collections.unmodifiableSet(new HashSet<File >(exclusions)); 228 } 229 230 private static final Set<File > emptySet() { 231 return Collections.emptySet(); 232 } 233 } 234 | Popular Tags |