git4idea: IDEA-22561: renamed OK buttons to appropriate names in dialogs
[fedora-idea.git] / plugins / git4idea / src / git4idea / merge / GitPullDialog.java
blob7b7420ab1fdd6f3c53757bbbebcdd812f191561a
1 /*
2 * Copyright 2000-2008 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.merge;
18 import com.intellij.ide.util.ElementsChooser;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.ui.DialogWrapper;
21 import com.intellij.openapi.vcs.VcsException;
22 import com.intellij.openapi.vfs.VirtualFile;
23 import com.intellij.ui.DocumentAdapter;
24 import git4idea.GitRemote;
25 import git4idea.GitVcs;
26 import git4idea.commands.GitHandler;
27 import git4idea.commands.GitHandlerUtil;
28 import git4idea.commands.GitLineHandler;
29 import git4idea.commands.GitSimpleHandler;
30 import git4idea.i18n.GitBundle;
31 import git4idea.ui.GitUIUtil;
32 import org.jetbrains.annotations.Nullable;
34 import javax.swing.*;
35 import javax.swing.event.DocumentEvent;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.util.Collections;
39 import java.util.List;
41 /**
42 * Git pull dialog
44 public class GitPullDialog extends DialogWrapper {
45 /**
46 * root panel
48 private JPanel myPanel;
49 /**
50 * The selected git root
52 private JComboBox myGitRoot;
53 /**
54 * Current branch label
56 private JLabel myCurrentBranch;
57 /**
58 * The merge strategy
60 private JComboBox myStrategy;
61 /**
62 * No commit option
64 private JCheckBox myNoCommitCheckBox;
65 /**
66 * Squash commit option
68 private JCheckBox mySquashCommitCheckBox;
69 /**
70 * No fast forward option
72 private JCheckBox myNoFastForwardCheckBox;
73 /**
74 * Add log info to commit option
76 private JCheckBox myAddLogInformationCheckBox;
77 /**
78 * Selected remote option
80 private JComboBox myRemote;
81 /**
82 * Get branches button
84 private JButton myGetBranchesButton;
85 /**
86 * The branch chooser
88 private ElementsChooser<String> myBranchChooser;
89 /**
90 * The context project
92 private final Project myProject;
94 /**
95 * A constructor
97 * @param project a project to select
98 * @param roots a git repository roots for the project
99 * @param defaultRoot a guessed default root
101 public GitPullDialog(Project project, List<VirtualFile> roots, VirtualFile defaultRoot) {
102 super(project, true);
103 setTitle(GitBundle.getString("pull.title"));
104 myProject = project;
105 GitUIUtil.setupRootChooser(myProject, roots, defaultRoot, myGitRoot, myCurrentBranch);
106 myGitRoot.addActionListener(new ActionListener() {
107 public void actionPerformed(final ActionEvent e) {
108 updateRemotes();
111 setOKButtonText(GitBundle.getString("pull.button"));
112 updateRemotes();
113 setupBranches();
114 setupGetBranches();
115 final ElementsChooser.ElementsMarkListener<String> listener = new ElementsChooser.ElementsMarkListener<String>() {
116 public void elementMarkChanged(final String element, final boolean isMarked) {
117 validateDialog();
120 myBranchChooser.addElementsMarkListener(listener);
121 listener.elementMarkChanged(null, true);
122 GitMergeUtil.setupNoCommitCheckbox(myAddLogInformationCheckBox, null, myNoCommitCheckBox);
123 GitMergeUtil.setupStrategies(myBranchChooser, myNoCommitCheckBox, myStrategy);
124 init();
128 * Setup branch updating
130 private void setupBranches() {
131 ((JTextField)myRemote.getEditor().getEditorComponent()).getDocument().addDocumentListener(new DocumentAdapter() {
132 protected void textChanged(final DocumentEvent e) {
133 updateBranches();
136 updateBranches();
140 * Validate dialog and enable buttons
142 private void validateDialog() {
143 if (getRemote().trim().length() == 0) {
144 setOKActionEnabled(false);
145 return;
147 setOKActionEnabled(myBranchChooser.getMarkedElements().size() != 0);
151 * Setup get branches button
153 private void setupGetBranches() {
154 final JTextField textField = (JTextField)myRemote.getEditor().getEditorComponent();
155 final DocumentAdapter listener = new DocumentAdapter() {
156 protected void textChanged(final DocumentEvent e) {
157 validateDialog();
158 myGetBranchesButton.setEnabled(textField.getText().trim().length() != 0);
161 textField.getDocument().addDocumentListener(listener);
162 listener.changedUpdate(null);
163 myGetBranchesButton.addActionListener(new ActionListener() {
164 public void actionPerformed(final ActionEvent e) {
165 GitSimpleHandler h = new GitSimpleHandler(myProject, gitRoot(), GitHandler.LS_REMOTE);
166 h.addParameters("--heads", myRemote.getSelectedItem().toString());
167 String output = GitHandlerUtil.doSynchronously(h, GitBundle.getString("pull.getting.remote.branches"), h.printableCommandLine());
168 if (output == null) {
169 return;
171 myBranchChooser.removeAllElements();
172 for (String line : output.split("\n")) {
173 if (line.length() == 0) {
174 continue;
176 int pos = line.lastIndexOf('/');
177 if (pos == -1) {
178 pos = line.lastIndexOf('\t');
180 myBranchChooser.addElement(line.substring(pos + 1), false);
187 * @return a pull handler configured according to dialog options
189 public GitLineHandler pullHandler() {
190 GitLineHandler h = new GitLineHandler(myProject, gitRoot(), GitHandler.PULL);
191 // ignore merge failure for the pull
192 h.ignoreErrorCode(1);
193 h.addParameters("--no-stat");
194 if (myNoCommitCheckBox.isSelected()) {
195 h.addParameters("--no-commit");
197 else {
198 if (myAddLogInformationCheckBox.isSelected()) {
199 h.addParameters("--log");
202 if (mySquashCommitCheckBox.isSelected()) {
203 h.addParameters("--squash");
205 if (myNoFastForwardCheckBox.isSelected()) {
206 h.addParameters("--no-ff");
208 String strategy = (String)myStrategy.getSelectedItem();
209 if (!GitMergeUtil.DEFAULT_STRATEGY.equals(strategy)) {
210 h.addParameters("--strategy", strategy);
212 h.addParameters("-v");
213 h.addParameters(getRemote());
214 final List<String> markedBranches = myBranchChooser.getMarkedElements();
215 h.addParameters(markedBranches.toArray(new String[markedBranches.size()]));
216 return h;
220 * Update branches
222 private void updateBranches() {
223 try {
224 String item = getRemote();
225 myBranchChooser.removeAllElements();
226 GitRemote r = null;
227 final int count = myRemote.getItemCount();
228 for (int i = 0; i < count; i++) {
229 GitRemote candidate = (GitRemote)myRemote.getItemAt(i);
230 if (candidate.name().equals(item)) {
231 r = candidate;
232 break;
235 if (r == null) {
236 return;
238 GitRemote.Info ri = r.localInfo(myProject, gitRoot());
239 String toSelect = ri.getRemoteForLocal(currentBranch());
240 for (String trackedBranch : ri.trackedBranches()) {
241 myBranchChooser.addElement(trackedBranch, trackedBranch.equals(toSelect));
244 catch (VcsException e) {
245 GitVcs.getInstance(myProject).showErrors(Collections.singletonList(e), GitBundle.getString("pull.retriving.remotes"));
247 finally {
248 validateDialog();
253 * @return current local branch for the git or null
255 @Nullable
256 private String currentBranch() {
257 String text = myCurrentBranch.getText();
258 return text.equals(GitUIUtil.NO_CURRENT_BRANCH) ? null : text;
262 * Update remotes for the git root
264 private void updateRemotes() {
265 GitUIUtil.setupRemotes(myProject, gitRoot(), currentBranch(), myRemote);
269 * @return a currently selected git root
271 public VirtualFile gitRoot() {
272 return (VirtualFile)myGitRoot.getSelectedItem();
277 * Create branch chooser
279 private void createUIComponents() {
280 myBranchChooser = new ElementsChooser<String>(true);
284 * {@inheritDoc}
286 protected JComponent createCenterPanel() {
287 return myPanel;
291 * {@inheritDoc}
293 @Override
294 protected String getDimensionServiceKey() {
295 return getClass().getName();
299 * {@inheritDoc}
301 @Override
302 protected String getHelpId() {
303 return "reference.VersionControl.Git.Pull";
307 * @return remote key
309 public String getRemote() {
310 return ((JTextField)myRemote.getEditor().getEditorComponent()).getText();