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
.cvshandlers
;
18 import com
.intellij
.CvsBundle
;
19 import com
.intellij
.cvsSupport2
.CvsVcs2
;
20 import com
.intellij
.cvsSupport2
.actions
.update
.UpdateSettings
;
21 import com
.intellij
.cvsSupport2
.config
.CvsConfiguration
;
22 import com
.intellij
.cvsSupport2
.connections
.CvsRootProvider
;
23 import com
.intellij
.cvsSupport2
.cvsExecution
.ModalityContext
;
24 import com
.intellij
.cvsSupport2
.cvsoperations
.common
.FindAllRoots
;
25 import com
.intellij
.cvsSupport2
.cvsoperations
.common
.FindAllRootsHelper
;
26 import com
.intellij
.cvsSupport2
.cvsoperations
.common
.PostCvsActivity
;
27 import com
.intellij
.cvsSupport2
.cvsoperations
.cvsUpdate
.MergedWithConflictProjectOrModuleFile
;
28 import com
.intellij
.cvsSupport2
.cvsoperations
.cvsUpdate
.UpdateOperation
;
29 import com
.intellij
.cvsSupport2
.cvsoperations
.cvsUpdate
.ui
.CorruptedProjectFilesDialog
;
30 import com
.intellij
.cvsSupport2
.errorHandling
.CannotFindCvsRootException
;
31 import com
.intellij
.openapi
.progress
.ProcessCanceledException
;
32 import com
.intellij
.openapi
.progress
.ProgressIndicator
;
33 import com
.intellij
.openapi
.progress
.ProgressManager
;
34 import com
.intellij
.openapi
.project
.Project
;
35 import com
.intellij
.openapi
.vcs
.FilePath
;
36 import com
.intellij
.openapi
.vcs
.VcsKey
;
37 import com
.intellij
.openapi
.vcs
.update
.FileGroup
;
38 import com
.intellij
.openapi
.vcs
.update
.UpdatedFiles
;
39 import com
.intellij
.openapi
.vfs
.VirtualFile
;
40 import com
.intellij
.util
.Options
;
41 import com
.intellij
.util
.containers
.HashSet
;
42 import org
.jetbrains
.annotations
.NotNull
;
43 import org
.jetbrains
.annotations
.Nullable
;
44 import org
.netbeans
.lib
.cvsclient
.file
.ICvsFileSystem
;
47 import java
.util
.ArrayList
;
48 import java
.util
.Collection
;
53 public class UpdateHandler
extends CommandCvsHandler
implements PostCvsActivity
{
54 private final FilePath
[] myFiles
;
55 private final Collection
<VirtualFile
> myRoots
= new ArrayList
<VirtualFile
>();
56 private final Collection
<File
> myNotProcessedRepositories
= new HashSet
<File
>();
57 private double myDirectoriesToBeProcessedCount
;
59 private final static CvsMessagePattern UPDATE_PATTERN
= new CvsMessagePattern(new String
[]{"cvs server: Updating ", "*"}, 2);
60 private final Project myProject
;
61 private final Collection
<MergedWithConflictProjectOrModuleFile
> myCorruptedFiles
= new ArrayList
<MergedWithConflictProjectOrModuleFile
>();
62 private final UpdatedFiles myUpdatedFiles
;
63 private final UpdateSettings myUpdateSettings
;
65 public UpdateHandler(FilePath
[] files
, UpdateSettings updateSettings
, Project project
, @NotNull UpdatedFiles updatedFiles
) {
66 super(CvsBundle
.message("operation.name.update"), new UpdateOperation(new FilePath
[0], updateSettings
, project
),
67 FileSetToBeUpdated
.selectedFiles(files
));
70 myUpdatedFiles
= updatedFiles
;
71 myUpdateSettings
= updateSettings
;
74 public void beforeLogin() {
77 FindAllRoots findAllRoots
= new FindAllRoots(myProject
);
78 final FilePath
[] filteredFiles
= FindAllRootsHelper
.findVersionedUnder(myFiles
);
79 myRoots
.addAll(findAllRoots
.executeOn(filteredFiles
));
80 myNotProcessedRepositories
.addAll(findAllRoots
.getDirectoriesToBeUpdated());
81 myDirectoriesToBeProcessedCount
= myNotProcessedRepositories
.size();
82 for(VirtualFile file
: myRoots
) {
83 if (getValidCvsRoot(file
) != null) {
84 ((UpdateOperation
)myCvsOperation
).addFile(file
);
88 catch (ProcessCanceledException ex
) {
95 private static CvsRootProvider
getValidCvsRoot(final VirtualFile file
) {
97 return CvsRootProvider
.createOn(new File(file
.getPath()));
99 catch (CannotFindCvsRootException e
) {
104 public void addFileMessage(String message
, ICvsFileSystem cvsFileSystem
) {
105 super.addFileMessage(message
, cvsFileSystem
);
106 ProgressIndicator progress
= getProgress();
107 if (progress
== null) return;
108 if (UPDATE_PATTERN
.matches(message
)) {
109 String relativeFileName
= UPDATE_PATTERN
.getRelativeFileName(message
);
110 myNotProcessedRepositories
.remove(cvsFileSystem
.getLocalFileSystem().getFile(relativeFileName
));
111 int notProcessedSize
= myNotProcessedRepositories
.size();
112 progress
.setFraction(0.5 + (myDirectoriesToBeProcessedCount
- notProcessedSize
) / (2 * myDirectoriesToBeProcessedCount
));
116 public void registerCorruptedProjectOrModuleFile(MergedWithConflictProjectOrModuleFile mergedWithConflictProjectOrModuleFile
) {
117 myCorruptedFiles
.add(mergedWithConflictProjectOrModuleFile
);
120 protected void onOperationFinished(ModalityContext modalityContext
) {
122 if (myUpdateSettings
.getPruneEmptyDirectories()) {
123 final IOFilesBasedDirectoryPruner pruner
= new IOFilesBasedDirectoryPruner(ProgressManager
.getInstance().getProgressIndicator());
124 for (final VirtualFile myRoot
: myRoots
) {
125 pruner
.addFile(new File(myRoot
.getPath()));
131 if (!myCorruptedFiles
.isEmpty()) {
132 int showOptions
= CvsConfiguration
.getInstance(myProject
).SHOW_CORRUPTED_PROJECT_FILES
;
134 if (showOptions
== Options
.PERFORM_ACTION_AUTOMATICALLY
) {
135 for (final MergedWithConflictProjectOrModuleFile myCorruptedFile
: myCorruptedFiles
) {
136 myCorruptedFile
.setShouldBeCheckedOut();
139 else if (showOptions
== Options
.SHOW_DIALOG
){
140 modalityContext
.runInDispatchThread(new Runnable() {
142 new CorruptedProjectFilesDialog(myProject
, myCorruptedFiles
).show();
148 final VcsKey vcsKey
= CvsVcs2
.getKey();
149 for (final MergedWithConflictProjectOrModuleFile myCorruptedFile
: myCorruptedFiles
) {
150 if (myCorruptedFile
.shouldBeCheckedOut()) {
151 addFileToCheckout(myCorruptedFile
.getOriginal());
154 myUpdatedFiles
.getGroupById(FileGroup
.MODIFIED_ID
).add(myCorruptedFile
.getOriginal().getPath(), vcsKey
, null);
162 protected PostCvsActivity
getPostActivityHandler() {