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
;
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;
61 ApplicationManager
.getApplication().runReadAction(new Runnable() {
63 for (final FilePath file
: rootsWithoutIntersections
) {
64 VirtualFile virtualFile
= file
.getVirtualFile();
65 if (virtualFile
!= null) {
66 myResult
.add(virtualFile
);
70 VirtualFile virtualFileParent
= file
.getVirtualFileParent();
71 if (virtualFileParent
!= null) {
72 myResult
.add(virtualFileParent
);
80 myManager
.unlockSynchronizationActions();
86 private int calcVirtualFilesUnderCvsIn(Collection
<FilePath
> rootsWithoutIntersections
) {
88 for (final FilePath cvsFileWrapper
: rootsWithoutIntersections
) {
89 if (!cvsFileWrapper
.isDirectory()) {
93 result
+= calcVirtualFilesUnderCvsIn(cvsFileWrapper
.getVirtualFile());
99 private int calcVirtualFilesUnderCvsIn(VirtualFile file
) {
100 if (file
== null || !file
.isDirectory()) return 0;
101 if (!myProjectRootManager
.getFileIndex().isInContent(file
)) return 0;
103 if (file
.findChild(CvsUtil
.CVS
) == null) return result
;
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
);
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
];
129 FilePath prevRoot
= result
.isEmpty() ?
null : result
.get(result
.size() - 1);
130 if ((prevRoot
== null) || ! VfsUtil
.isAncestor(prevRoot
.getIOFile(), root
.getIOFile(), false)){
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
));
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
)) {
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
;