"revert" action for committed changelist (IDEADEV-13292); refactoring to introduce...
[fedora-idea.git] / vcs-impl / src / com / intellij / openapi / vcs / changes / actions / RevertChangesAction.java
blob16021b274c39d4374998703812b3d4824f4a034f
1 package com.intellij.openapi.vcs.changes.actions;
3 import com.intellij.openapi.actionSystem.AnAction;
4 import com.intellij.openapi.actionSystem.AnActionEvent;
5 import com.intellij.openapi.actionSystem.PlatformDataKeys;
6 import com.intellij.openapi.diff.impl.patch.ApplyPatchContext;
7 import com.intellij.openapi.diff.impl.patch.FilePatch;
8 import com.intellij.openapi.diff.impl.patch.PatchBuilder;
9 import com.intellij.openapi.project.Project;
10 import com.intellij.openapi.ui.Messages;
11 import com.intellij.openapi.vcs.VcsBundle;
12 import com.intellij.openapi.vcs.VcsDataKeys;
13 import com.intellij.openapi.vcs.VcsException;
14 import com.intellij.openapi.vcs.changes.Change;
15 import com.intellij.openapi.vcs.changes.ChangeList;
16 import com.intellij.openapi.vcs.changes.ChangeListManager;
17 import com.intellij.openapi.vcs.changes.patch.ApplyPatchAction;
18 import com.intellij.openapi.vcs.changes.ui.ChangeListChooser;
19 import com.intellij.openapi.vfs.VirtualFile;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
25 /**
26 * @author yole
28 public class RevertChangesAction extends AnAction {
29 public void actionPerformed(final AnActionEvent e) {
30 final Project project = e.getRequiredData(PlatformDataKeys.PROJECT);
31 final VirtualFile baseDir = project.getBaseDir();
32 assert baseDir != null;
33 final Change[] changes = e.getRequiredData(VcsDataKeys.CHANGES);
34 final List<Change> changesList = new ArrayList<Change>();
35 Collections.addAll(changesList, changes);
37 String defaultName = null;
38 final ChangeList[] changeLists = e.getData(VcsDataKeys.CHANGE_LISTS);
39 if (changeLists != null && changeLists.length > 0) {
40 defaultName = VcsBundle.message("revert.changes.default.name", changeLists[0].getName());
43 ChangeListChooser chooser = new ChangeListChooser(project, ChangeListManager.getInstance(project).getChangeLists(), null,
44 "Select Target Changelist", defaultName);
45 chooser.show();
46 if (!chooser.isOK()) return;
48 List<FilePatch> patches;
49 try {
50 patches = PatchBuilder.buildPatch(changesList, baseDir.getPresentableUrl(), false, true);
52 catch (VcsException ex) {
53 Messages.showErrorDialog(project, "Failed to revert changes: " + ex.getMessage(), VcsBundle.message("revert.changes.title"));
54 return;
56 ApplyPatchContext context = new ApplyPatchContext(baseDir, 0, true, false);
57 ApplyPatchAction.applyPatch(project, patches, context, chooser.getSelectedList());
60 public void update(final AnActionEvent e) {
61 final Project project = e.getData(PlatformDataKeys.PROJECT);
62 final Change[] changes = e.getData(VcsDataKeys.CHANGES);
63 e.getPresentation().setEnabled(project != null && changes != null && changes.length > 0);