update copyright
[fedora-idea.git] / plugins / cvs / cvs-plugin / src / com / intellij / cvsSupport2 / cvsoperations / common / FindAllRoots.java
blob117008ec4963894e88b28d0c9e0d5bafc4e66813
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.cvsSupport2.cvsoperations.common;
18 import com.intellij.cvsSupport2.application.CvsEntriesManager;
19 import com.intellij.cvsSupport2.connections.CvsEnvironment;
20 import com.intellij.cvsSupport2.util.CvsVfsUtil;
21 import com.intellij.cvsSupport2.CvsUtil;
22 import com.intellij.openapi.application.ApplicationManager;
23 import com.intellij.openapi.progress.ProgressIndicator;
24 import com.intellij.openapi.progress.ProgressManager;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.roots.ProjectRootManager;
27 import com.intellij.openapi.vcs.FilePath;
28 import com.intellij.openapi.vfs.VfsUtil;
29 import com.intellij.openapi.vfs.VirtualFile;
30 import com.intellij.CvsBundle;
32 import java.io.File;
33 import java.util.*;
35 /**
36 * author: lesya
39 public class FindAllRoots {
40 private final CvsEntriesManager myManager = CvsEntriesManager.getInstance();
41 private int myProcessedFiles;
42 private int mySuitableFiles;
43 private final Collection<File> myRepositories = new HashSet<File>();
45 private final Collection<VirtualFile> myResult = new ArrayList<VirtualFile>();
46 private final ProgressIndicator myProgress;
47 private final ProjectRootManager myProjectRootManager;
49 public FindAllRoots(Project project) {
50 myProgress = ProgressManager.getInstance().getProgressIndicator();
51 myProjectRootManager = ProjectRootManager.getInstance(project);
54 public Collection<VirtualFile> executeOn(final FilePath[] roots) {
55 final Collection<FilePath> rootsWithoutIntersections = getRootsWithoutIntersections(roots);
56 setText(CvsBundle.message("progress.text.searching.for.cvs.root"));
57 myManager.lockSynchronizationActions();
58 mySuitableFiles = calcVirtualFilesUnderCvsIn(rootsWithoutIntersections) * 2;
59 myProcessedFiles = 0;
60 try {
61 ApplicationManager.getApplication().runReadAction(new Runnable() {
62 public void run() {
63 for (final FilePath file : rootsWithoutIntersections) {
64 VirtualFile virtualFile = file.getVirtualFile();
65 if (virtualFile != null) {
66 myResult.add(virtualFile);
67 process(virtualFile);
69 else {
70 VirtualFile virtualFileParent = file.getVirtualFileParent();
71 if (virtualFileParent != null) {
72 myResult.add(virtualFileParent);
77 });
79 finally {
80 myManager.unlockSynchronizationActions();
83 return myResult;
86 private int calcVirtualFilesUnderCvsIn(Collection<FilePath> rootsWithoutIntersections) {
87 int result = 0;
88 for (final FilePath cvsFileWrapper : rootsWithoutIntersections) {
89 if (!cvsFileWrapper.isDirectory()) {
90 result += 1;
92 else {
93 result += calcVirtualFilesUnderCvsIn(cvsFileWrapper.getVirtualFile());
96 return result;
99 private int calcVirtualFilesUnderCvsIn(VirtualFile file) {
100 if (file == null || !file.isDirectory()) return 0;
101 if (!myProjectRootManager.getFileIndex().isInContent(file)) return 0;
102 int result = 0;
103 if (file.findChild(CvsUtil.CVS) == null) return result;
104 result += 1;
105 VirtualFile[] children = file.getChildren();
106 if (children == null) return result;
107 for (VirtualFile child : children) {
108 if (child.getName() == CvsUtil.CVS) continue;
109 result += calcVirtualFilesUnderCvsIn(child);
111 return result;
114 private static Collection<FilePath> getRootsWithoutIntersections(FilePath[] roots) {
115 ArrayList<FilePath> result = new ArrayList<FilePath>();
116 List<FilePath> list = Arrays.asList(roots);
117 Collections.sort(list, new Comparator<FilePath>() {
118 public int compare(FilePath file, FilePath file1) {
119 return file.getPath().compareTo(file1.getPath());
122 FilePath[] sortedRoots = list.toArray(new FilePath[list.size()]);
123 for (int i = 0; i < sortedRoots.length; i++) {
124 FilePath root = sortedRoots[i];
125 if (i == 0) {
126 result.add(root);
128 else {
129 FilePath prevRoot = result.isEmpty() ? null : result.get(result.size() - 1);
130 if ((prevRoot == null) || ! VfsUtil.isAncestor(prevRoot.getIOFile(), root.getIOFile(), false)){
131 result.add(root);
136 return result;
139 private void setText(final String text) {
140 if (myProgress == null) return;
141 myProgress.setText(text);
142 ProgressManager.getInstance().checkCanceled();
145 private void setText2(final String text) {
146 if (myProgress == null) return;
147 myProgress.setText2(text);
148 ProgressManager.getInstance().checkCanceled();
151 private void process(VirtualFile root) {
152 if (!myProjectRootManager.getFileIndex().isInContent(root)) return;
153 setText2(CvsVfsUtil.getPresentablePathFor(root));
154 myProcessedFiles++;
155 setFraction();
156 VirtualFile[] children = root.getChildren();
157 if (children == null) return;
158 CvsEnvironment parentEnv = myManager.getCvsConnectionSettingsFor(root);
159 if (!parentEnv.isValid()) return;
160 myManager.cacheCvsAdminInfoIn(root);
161 myRepositories.add(CvsVfsUtil.getFileFor(root));
162 for (VirtualFile child : children) {
163 if (!child.isDirectory()) continue;
164 if (!myProjectRootManager.getFileIndex().isInContent(child)) continue;
165 CvsEnvironment childEnv = myManager.getCvsConnectionSettingsFor(child);
166 if (childEnv == null || !childEnv.isValid()) continue;
167 if (!childEnv.equals(parentEnv)) {
168 myResult.add(child);
170 process(child);
174 private void setFraction() {
175 if (myProgress == null) return;
176 myProgress.setFraction((double)myProcessedFiles / (double)mySuitableFiles);
177 ProgressManager.getInstance().checkCanceled();
180 public Collection<File> getDirectoriesToBeUpdated() {
181 return myRepositories;