update copyright
[fedora-idea.git] / plugins / cvs / cvs-plugin / src / com / intellij / cvsSupport2 / application / CvsFileOperationsHandler.java
blobe8a095a8620b2164a110ce0535f54fefa127a618
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.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;
32 import java.io.File;
33 import java.io.IOException;
35 /**
36 * @author yole
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) {
46 myProject = project;
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;
63 try {
64 deleteFilesInVFS(file);
66 finally {
67 myInternalDelete = false;
69 return true;
71 return 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);
80 else {
81 child.delete(this);
86 public boolean move(final VirtualFile file, final VirtualFile toDir) throws IOException {
87 return doMoveRename(file, toDir, file.getName());
90 @Nullable
91 public File copy(final VirtualFile file, final VirtualFile toDir, final String copyName) throws IOException {
92 return null;
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);
106 return false;
108 newFile.mkdir();
109 copyDirectoryStructure(file, newFile);
110 myComponent.getAddHandler().addFile(newFile);
111 DocumentReference ref = DocumentReferenceManager.getInstance().create(file);
112 UndoManager.getInstance(myProject).nonundoableActionPerformed(ref, false);
113 return true;
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;
121 newChild.mkdir();
122 copyDirectoryStructure(child, newChild);
124 else {
125 new File(child.getPath()).renameTo(newChild);
130 public boolean createFile(final VirtualFile dir, final String name) throws IOException {
131 return false;
134 public boolean createDirectory(final VirtualFile dir, final String name) throws IOException {
135 return false;
138 public void afterDone(final ThrowableConsumer<LocalFileOperationsHandler, IOException> invoker) {