update copyright
[fedora-idea.git] / plugins / cvs / cvs-plugin / src / com / intellij / cvsSupport2 / application / CvsStorageSupportingDeletionComponent.java
blobc4ab154db3e06ee144a8effc60dff70d49a7084c
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.application.PathManager;
21 import com.intellij.openapi.command.CommandEvent;
22 import com.intellij.openapi.command.CommandListener;
23 import com.intellij.openapi.command.CommandProcessor;
24 import com.intellij.openapi.components.ServiceManager;
25 import com.intellij.openapi.diagnostic.Logger;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.util.Key;
28 import com.intellij.openapi.vcs.AbstractVcs;
29 import com.intellij.openapi.vcs.ProjectLevelVcsManager;
30 import com.intellij.openapi.vfs.*;
32 import java.io.File;
34 public class CvsStorageSupportingDeletionComponent extends CvsStorageComponent implements CommandListener {
35 private static final Logger LOG = Logger.getInstance("#" + CvsStorageSupportingDeletionComponent.class.getName());
36 private Project myProject;
38 private DeletedCVSDirectoryStorage myDeletedStorage;
39 private DeleteHandler myDeleteHandler = null;
40 private AddHandler myAddHandler = null;
41 private CvsFileOperationsHandler myFileOperationsHandler;
43 private int myCommandLevel = 0;
45 private boolean myAnotherProjectCommand = false;
46 static final Key<AbstractVcs> FILE_VCS = new Key<AbstractVcs>("File VCS");
48 public void commandStarted(CommandEvent event) {
49 myCommandLevel++;
50 if (myCommandLevel == 1) {
51 myAnotherProjectCommand = (event.getProject() != null) != (event.getProject() == myProject);
54 if (LOG.isDebugEnabled()) {
55 LOG.debug("Started" + event.getCommandName() + ", commandLevel: " + myCommandLevel);
59 public void beforeCommandFinished(CommandEvent event) {
63 public void undoTransparentActionStarted() {
66 public void undoTransparentActionFinished() {
69 public void commandFinished(CommandEvent event) {
70 myCommandLevel--;
71 if (myCommandLevel == 0) myAnotherProjectCommand = false;
72 if (LOG.isDebugEnabled()) {
73 LOG.debug("Finished" + event.getCommandName() + ", commandLevel: " + myCommandLevel);
75 execute();
78 public void init(Project project, boolean sync) {
79 myProject = project;
80 initializeDeletedStorage();
81 VirtualFileManager.getInstance().addVirtualFileListener(this);
82 CvsEntriesManager.getInstance().registerAsVirtualFileListener();
83 CommandProcessor.getInstance().addCommandListener(this);
84 myFileOperationsHandler = new CvsFileOperationsHandler(project, this);
85 LocalFileSystem.getInstance().registerAuxiliaryFileOperationsHandler(myFileOperationsHandler);
86 myIsActive = true;
89 public void dispose() {
90 VirtualFileManager.getInstance().removeVirtualFileListener(this);
91 CvsEntriesManager.getInstance().unregisterAsVirtualFileListener();
92 CommandProcessor.getInstance().removeCommandListener(this);
93 LocalFileSystem.getInstance().unregisterAuxiliaryFileOperationsHandler(myFileOperationsHandler);
94 myFileOperationsHandler = null;
95 myIsActive = false;
96 myProject = null;
99 public void beforeFileDeletion(VirtualFileEvent event) {
102 public DeleteHandler getDeleteHandler() {
103 if (myDeleteHandler == null) {
104 myDeleteHandler = myDeletedStorage.createDeleteHandler(myProject, this);
106 return myDeleteHandler;
109 public void fileDeleted(VirtualFileEvent event) {
112 private boolean shouldProcessEvent(VirtualFileEvent event, boolean parentShouldBeUnderCvs) {
113 if (myAnotherProjectCommand) return false;
114 VirtualFile file = event.getFile();
115 if (disabled(file)) return false;
116 if (event.isFromRefresh()) return false;
117 if (isStorageEvent(event)) return false;
118 if (!isUnderCvsManagedModuleRoot(file)) return false;
119 return !(parentShouldBeUnderCvs && !parentIsUnderCvs(file));
122 private static boolean isStorageEvent(VirtualFileEvent event) {
123 return event.getRequestor() instanceof DeletedCVSDirectoryStorage;
126 private static boolean parentIsUnderCvs(VirtualFile file) {
127 return CvsUtil.fileIsUnderCvs(file.getParent());
130 public void beforeFileMovement(VirtualFileMoveEvent event) {
133 public void fileMoved(VirtualFileMoveEvent event) {
134 fileCreated(event);
137 private boolean processMoveOrRename() {
138 return myDeleteHandler != null;
141 private AbstractVcs getFileVcs(VirtualFile file) {
142 AbstractVcs storedData = file.getUserData(FILE_VCS);
143 if (storedData != null) {
144 return storedData;
146 else {
147 return ProjectLevelVcsManager.getInstance(myProject).getVcsFor(file);
151 private boolean isUnderCvsManagedModuleRoot(VirtualFile file) {
152 return getFileVcs(file) == CvsVcs2.getInstance(myProject);
155 private boolean disabled(VirtualFile file) {
156 return ProjectLevelVcsManager.getInstance(myProject).getVcsFor(file) !=
157 CvsVcs2.getInstance(myProject);
160 public void beforePropertyChange(VirtualFilePropertyEvent event) {
161 if (!event.getPropertyName().equals(VirtualFile.PROP_NAME)) return;
162 if (!CvsUtil.fileIsUnderCvs(event.getFile())) return;
163 beforeFileDeletion(event);
166 public void propertyChanged(VirtualFilePropertyEvent event) {
167 if (processMoveOrRename()) {
168 fileDeleted(event);
169 fileCreated(event);
173 public void fileCreated(final VirtualFileEvent event) {
174 if (!shouldProcessEvent(event, false)) return;
175 final Project project = myProject;
176 if (project == null) return; // already disposed
178 final VirtualFile file = event.getFile();
179 if (myDeleteHandler != null) {
180 myDeleteHandler.removeDeletedRoot(file);
182 myDeletedStorage.checkNeedForPurge(VfsUtil.virtualToIoFile(file));
183 deleteIfAdminDirCreated(file);
184 getAddHandler().addFile(file);
186 execute();
189 public AddHandler getAddHandler() {
190 if (myAddHandler == null) {
191 myAddHandler = new AddHandler(myProject, this);
193 return myAddHandler;
196 private void execute() {
197 if (myCommandLevel > 0) return;
198 //myDeletedStorage.sync();
199 if (myDeleteHandler != null) myDeleteHandler.execute();
200 if (myAddHandler != null) myAddHandler.execute();
202 myDeleteHandler = null;
203 myAddHandler = null;
206 private void initializeDeletedStorage() {
207 File storageRoot = getStorageRoot();
208 storageRoot.mkdirs();
209 myDeletedStorage = new DeletedCVSDirectoryStorage(storageRoot);
212 private static File getStorageRoot() {
213 //noinspection HardCodedStringLiteral
214 return new File(PathManager.getSystemPath(), "CVS-TO-DELETE");
217 public void sync() {
218 //myDeletedStorage.sync();
221 public static CvsStorageComponent getInstance(Project project) {
222 return ServiceManager.getService(project, CvsStorageComponent.class);
225 public void deleteIfAdminDirCreated(VirtualFile addedFile) {
226 myDeletedStorage.deleteIfAdminDirCreated(addedFile);