IDEA-27603 (Indicate which branch is being worked on)
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / ui / RollbackChangesDialog.java
blob8c7dfe723d8d3c38d2905021d788d815c1246e28
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.openapi.vcs.changes.ui;
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.ui.DialogWrapper;
20 import com.intellij.openapi.ui.Messages;
21 import com.intellij.openapi.vcs.AbstractVcs;
22 import com.intellij.openapi.vcs.VcsBundle;
23 import com.intellij.openapi.vcs.changes.*;
24 import com.intellij.openapi.vcs.rollback.RollbackEnvironment;
25 import com.intellij.util.ui.UIUtil;
26 import gnu.trove.THashSet;
27 import org.jetbrains.annotations.Nullable;
29 import javax.swing.*;
30 import java.awt.*;
31 import java.util.*;
32 import java.util.List;
34 /**
35 * @author max
37 public class RollbackChangesDialog extends DialogWrapper {
38 private final Project myProject;
39 private final boolean myRefreshSynchronously;
40 private final Runnable myAfterVcsRefreshInAwt;
41 private final MultipleChangeListBrowser myBrowser;
42 @Nullable private JCheckBox myDeleteLocallyAddedFiles;
44 public static void rollbackChanges(final Project project, final Collection<Change> changes) {
45 rollbackChanges(project, changes, true);
48 public static void rollbackChanges(final Project project, final Collection<Change> changes, boolean refreshSynchronously) {
49 rollbackChanges(project, changes, refreshSynchronously, null);
52 public static void rollbackChanges(final Project project, final Collection<Change> changes, boolean refreshSynchronously,
53 final Runnable afterVcsRefreshInAwt) {
54 final ChangeListManagerEx manager = (ChangeListManagerEx) ChangeListManager.getInstance(project);
56 if (changes.isEmpty()) {
57 Messages.showWarningDialog(project, VcsBundle.message("commit.dialog.no.changes.detected.text"),
58 VcsBundle.message("commit.dialog.no.changes.detected.title"));
59 return;
62 final ArrayList<Change> validChanges = new ArrayList<Change>();
63 final Set<LocalChangeList> lists = new THashSet<LocalChangeList>();
64 lists.addAll(manager.getInvolvedListsFilterChanges(changes, validChanges));
66 rollback(project, new ArrayList<LocalChangeList>(lists), validChanges, refreshSynchronously, afterVcsRefreshInAwt);
69 public static void rollback(final Project project,
70 final List<LocalChangeList> changeLists,
71 final List<Change> changes,
72 final boolean refreshSynchronously, final Runnable afterVcsRefreshInAwt) {
73 new RollbackChangesDialog(project, changeLists, changes, refreshSynchronously, afterVcsRefreshInAwt).show();
76 public RollbackChangesDialog(final Project project,
77 List<LocalChangeList> changeLists,
78 final List<Change> changes,
79 final boolean refreshSynchronously, final Runnable afterVcsRefreshInAwt) {
80 super(project, true);
82 myProject = project;
83 myRefreshSynchronously = refreshSynchronously;
84 myAfterVcsRefreshInAwt = afterVcsRefreshInAwt;
85 myBrowser = new MultipleChangeListBrowser(project, changeLists, changes, null, true, true, null, null);
86 myBrowser.setToggleActionTitle("Include in rollback");
88 setOKButtonText(VcsBundle.message("changes.action.rollback.text"));
89 setTitle(VcsBundle.message("changes.action.rollback.title"));
91 Set<AbstractVcs> affectedVcs = new HashSet<AbstractVcs>();
92 for (Change c : changes) {
93 final AbstractVcs vcs = ChangesUtil.getVcsForChange(c, project);
94 if (vcs != null) {
95 // vcs may be null if we have turned off VCS integration and are in process of refreshing
96 affectedVcs.add(vcs);
99 if (affectedVcs.size() == 1) {
100 AbstractVcs vcs = (AbstractVcs)affectedVcs.toArray()[0];
101 final RollbackEnvironment rollbackEnvironment = vcs.getRollbackEnvironment();
102 if (rollbackEnvironment != null) {
103 String rollbackOperationName = rollbackEnvironment.getRollbackOperationName();
104 int pos = rollbackOperationName.indexOf(UIUtil.MNEMONIC);
105 if (pos >= 0) {
106 setOKButtonMnemonic(Character.toUpperCase(rollbackOperationName.charAt(pos + 1)));
107 rollbackOperationName = rollbackOperationName.replace(Character.toString(UIUtil.MNEMONIC), "");
109 setTitle(VcsBundle.message("changes.action.rollback.custom.title", rollbackOperationName).replace("_", ""));
110 setOKButtonText(rollbackOperationName);
114 for (Change c : changes) {
115 if (c.getType() == Change.Type.NEW) {
116 myDeleteLocallyAddedFiles = new JCheckBox(VcsBundle.message("changes.checkbox.delete.locally.added.files"));
117 break;
121 init();
124 @Override
125 protected void dispose() {
126 super.dispose();
127 myBrowser.dispose();
130 @Override
131 protected void doOKAction() {
132 super.doOKAction();
133 new RollbackWorker(myProject, myRefreshSynchronously).doRollback(myBrowser.getCurrentIncludedChanges(),
134 myDeleteLocallyAddedFiles != null && myDeleteLocallyAddedFiles.isSelected(),
135 myAfterVcsRefreshInAwt, null);
138 @Nullable
139 protected JComponent createCenterPanel() {
140 if (myDeleteLocallyAddedFiles != null) {
141 JPanel panel = new JPanel(new BorderLayout());
142 panel.add(myBrowser, BorderLayout.CENTER);
143 panel.add(myDeleteLocallyAddedFiles, BorderLayout.SOUTH);
144 return panel;
146 return myBrowser;
149 public JComponent getPreferredFocusedComponent() {
150 return myBrowser.getPrefferedFocusComponent();
153 protected String getDimensionServiceKey() {
154 return "RollbackChangesDialog";