git4idea: VfsListener now removes and adds files in background
[fedora-idea.git] / plugins / git4idea / src / git4idea / vfs / GitVFSListener.java
blob9b4f4f9dafd4e9e870590bd5895e9dd375f596e4
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 git4idea.vfs;
18 import com.intellij.openapi.progress.ProgressIndicator;
19 import com.intellij.openapi.progress.Task;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.vcs.FilePath;
22 import com.intellij.openapi.vcs.VcsException;
23 import com.intellij.openapi.vcs.VcsVFSListener;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import com.intellij.util.ui.UIUtil;
26 import com.intellij.vcsUtil.VcsUtil;
27 import git4idea.GitUtil;
28 import git4idea.GitVcs;
29 import git4idea.commands.GitFileUtils;
30 import git4idea.i18n.GitBundle;
31 import org.jetbrains.annotations.NotNull;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.List;
36 import java.util.Map;
38 /**
39 * Git virtual file adapter
41 public class GitVFSListener extends VcsVFSListener {
43 /**
44 * A constructor for listener
46 * @param project a project
47 * @param vcs a vcs for that project
49 public GitVFSListener(final Project project, final GitVcs vcs) {
50 super(project, vcs);
53 /**
54 * {@inheritDoc}
56 protected String getAddTitle() {
57 return GitBundle.getString("vfs.listener.add.title");
60 /**
61 * {@inheritDoc}
63 protected String getSingleFileAddTitle() {
64 return GitBundle.getString("vfs.listener.add.single.title");
67 /**
68 * {@inheritDoc}
70 protected String getSingleFileAddPromptTemplate() {
71 return GitBundle.getString("vfs.listener.add.single.prompt");
74 /**
75 * {@inheritDoc}
77 protected void performAdding(final Collection<VirtualFile> addedFiles, final Map<VirtualFile, VirtualFile> copyFromMap) {
78 final Map<VirtualFile, List<VirtualFile>> sortedFiles;
79 try {
80 sortedFiles = GitUtil.sortFilesByGitRoot(addedFiles, true);
82 catch (VcsException e) {
83 gitVcs().showMessages(e.getMessage());
84 return;
86 gitVcs().runInBackground(new Task.Backgroundable(myProject, GitBundle.getString("add.adding")) {
87 @Override
88 public void run(@NotNull ProgressIndicator indicator) {
89 // note that copied files are not processed because they are included into added files.
90 for (Map.Entry<VirtualFile, List<VirtualFile>> e : sortedFiles.entrySet()) {
91 try {
92 final VirtualFile root = e.getKey();
93 indicator.setText(root.getPresentableUrl());
94 GitFileUtils.addFiles(myProject, root, e.getValue());
95 GitUtil.markFilesDirty(myProject, e.getValue());
97 catch (final VcsException ex) {
98 UIUtil.invokeLaterIfNeeded(new Runnable() {
99 public void run() {
100 gitVcs().showMessages(ex.getMessage());
110 * @return casted vcs instance
112 private GitVcs gitVcs() {
113 return ((GitVcs)myVcs);
117 * Perform adding the files using file paths
119 * @param addedFiles the added files
121 private void performAdding(Collection<FilePath> addedFiles) {
122 final Map<VirtualFile, List<FilePath>> sortedFiles;
123 try {
124 sortedFiles = GitUtil.sortFilePathsByGitRoot(addedFiles, true);
126 catch (VcsException e) {
127 gitVcs().showMessages(e.getMessage());
128 return;
130 gitVcs().runInBackground(new Task.Backgroundable(myProject, GitBundle.getString("add.adding")) {
131 @Override
132 public void run(@NotNull ProgressIndicator indicator) {
133 for (Map.Entry<VirtualFile, List<FilePath>> e : sortedFiles.entrySet()) {
134 try {
135 final VirtualFile root = e.getKey();
136 indicator.setText(root.getPresentableUrl());
137 GitFileUtils.addPaths(myProject, root, e.getValue());
138 GitUtil.markFilesDirty(myProject, e.getValue());
140 catch (final VcsException ex) {
141 UIUtil.invokeLaterIfNeeded(new Runnable() {
142 public void run() {
143 gitVcs().showMessages(ex.getMessage());
153 * {@inheritDoc}
155 protected String getDeleteTitle() {
156 return GitBundle.getString("vfs.listener.delete.title");
160 * {@inheritDoc}
162 protected String getSingleFileDeleteTitle() {
163 return GitBundle.getString("vfs.listener.delete.single.title");
167 * {@inheritDoc}
169 protected String getSingleFileDeletePromptTemplate() {
170 return GitBundle.getString("vfs.listener.delete.single.prompt");
174 * {@inheritDoc}
176 protected void performDeletion(final List<FilePath> filesToDelete) {
177 final Map<VirtualFile, List<FilePath>> sortedFiles;
178 try {
179 sortedFiles = GitUtil.sortFilePathsByGitRoot(filesToDelete, true);
181 catch (VcsException e) {
182 gitVcs().showMessages(e.getMessage());
183 return;
185 gitVcs().runInBackground(new Task.Backgroundable(myProject, GitBundle.getString("remove.removing")) {
186 @Override
187 public void run(@NotNull ProgressIndicator indicator) {
188 for (Map.Entry<VirtualFile, List<FilePath>> e : sortedFiles.entrySet()) {
189 try {
190 final VirtualFile root = e.getKey();
191 indicator.setText(root.getPresentableUrl());
192 GitFileUtils.delete(myProject, root, e.getValue());
193 GitUtil.markFilesDirty(myProject, e.getValue());
195 catch (final VcsException ex) {
196 UIUtil.invokeLaterIfNeeded(new Runnable() {
197 public void run() {
198 gitVcs().showMessages(ex.getMessage());
208 * {@inheritDoc}
210 protected void performMoveRename(final List<MovedFileInfo> movedFiles) {
211 // because git does not tracks moves, the file are just added and deleted.
212 ArrayList<FilePath> added = new ArrayList<FilePath>();
213 ArrayList<FilePath> removed = new ArrayList<FilePath>();
214 for (MovedFileInfo m : movedFiles) {
215 added.add(VcsUtil.getFilePath(m.myNewPath));
216 removed.add(VcsUtil.getFilePath(m.myOldPath));
218 performAdding(added);
219 performDeletion(removed);
223 * {@inheritDoc}
225 protected boolean isDirectoryVersioningSupported() {
226 return false;
230 * {@inheritDoc}
232 @Override
233 protected Collection<FilePath> selectFilePathsToDelete(final List<FilePath> deletedFiles) {
234 // For git asking about vcs delete does not make much sense. The result is practically identical.
235 return deletedFiles;