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
.application
;
18 import com
.intellij
.cvsSupport2
.CvsUtil
;
19 import com
.intellij
.cvsSupport2
.CvsVcs2
;
20 import com
.intellij
.openapi
.command
.undo
.UndoManager
;
21 import com
.intellij
.openapi
.command
.undo
.DocumentReference
;
22 import com
.intellij
.openapi
.command
.undo
.DocumentReferenceManager
;
23 import com
.intellij
.openapi
.diagnostic
.Logger
;
24 import com
.intellij
.openapi
.project
.Project
;
25 import com
.intellij
.openapi
.vcs
.AbstractVcs
;
26 import com
.intellij
.openapi
.vcs
.ProjectLevelVcsManager
;
27 import com
.intellij
.openapi
.vfs
.LocalFileOperationsHandler
;
28 import com
.intellij
.openapi
.vfs
.VirtualFile
;
29 import com
.intellij
.util
.ThrowableConsumer
;
30 import org
.jetbrains
.annotations
.Nullable
;
33 import java
.io
.IOException
;
38 public class CvsFileOperationsHandler
implements LocalFileOperationsHandler
{
39 private static final Logger LOG
= Logger
.getInstance("#com.intellij.cvsSupport2.application.CvsFileOperationsHandler");
41 private final Project myProject
;
42 private final CvsStorageSupportingDeletionComponent myComponent
;
43 private boolean myInternalDelete
= false;
45 public CvsFileOperationsHandler(final Project project
, final CvsStorageSupportingDeletionComponent component
) {
47 myComponent
= component
;
50 public boolean delete(final VirtualFile file
) throws IOException
{
51 return processDeletedFile(file
);
54 private boolean processDeletedFile(final VirtualFile file
) throws IOException
{
55 if (myInternalDelete
) return false;
56 final AbstractVcs vcs
= ProjectLevelVcsManager
.getInstance(myProject
).getVcsFor(file
);
57 if (vcs
!= CvsVcs2
.getInstance(myProject
)) return false;
58 file
.putUserData(CvsStorageSupportingDeletionComponent
.FILE_VCS
, vcs
);
59 if (!CvsUtil
.fileIsUnderCvs(file
)) return false;
60 myComponent
.getDeleteHandler().addDeletedRoot(file
);
61 if (file
.isDirectory()) {
62 myInternalDelete
= true;
64 deleteFilesInVFS(file
);
67 myInternalDelete
= false;
74 private void deleteFilesInVFS(final VirtualFile file
) throws IOException
{
75 for(VirtualFile child
: file
.getChildren()) {
76 if (child
.isDirectory()) {
77 if (DeletedCVSDirectoryStorage
.isAdminDir(child
)) continue;
78 deleteFilesInVFS(child
);
86 public boolean move(final VirtualFile file
, final VirtualFile toDir
) throws IOException
{
87 return doMoveRename(file
, toDir
, file
.getName());
91 public File
copy(final VirtualFile file
, final VirtualFile toDir
, final String copyName
) throws IOException
{
95 public boolean rename(final VirtualFile file
, final String newName
) throws IOException
{
96 return doMoveRename(file
, file
.getParent(), newName
);
99 private boolean doMoveRename(final VirtualFile file
, final VirtualFile newParent
, final String newName
) throws IOException
{
100 if (!CvsUtil
.fileIsUnderCvs(file
)) return false;
101 if (newParent
== null) return false;
102 File newFile
= new File(newParent
.getPath(), newName
);
103 myComponent
.getDeleteHandler().addDeletedRoot(file
);
104 if (!file
.isDirectory()) {
105 myComponent
.getAddHandler().addFile(newFile
);
109 copyDirectoryStructure(file
, newFile
);
110 myComponent
.getAddHandler().addFile(newFile
);
111 DocumentReference ref
= DocumentReferenceManager
.getInstance().create(file
);
112 UndoManager
.getInstance(myProject
).nonundoableActionPerformed(ref
, false);
116 private static void copyDirectoryStructure(final VirtualFile file
, final File newFile
) throws IOException
{
117 for(VirtualFile child
: file
.getChildren()) {
118 File newChild
= new File(newFile
, child
.getName());
119 if (child
.isDirectory()) {
120 if (DeletedCVSDirectoryStorage
.isAdminDir(child
)) continue;
122 copyDirectoryStructure(child
, newChild
);
125 new File(child
.getPath()).renameTo(newChild
);
130 public boolean createFile(final VirtualFile dir
, final String name
) throws IOException
{
134 public boolean createDirectory(final VirtualFile dir
, final String name
) throws IOException
{
138 public void afterDone(final ThrowableConsumer
<LocalFileOperationsHandler
, IOException
> invoker
) {