git4idea: IDEA-22561: renamed OK buttons to appropriate names in dialogs
[fedora-idea.git] / plugins / git4idea / src / git4idea / ui / GitTagDialog.java
blob88d227dc19ae7a960b6acdc94ac68e2548bbec2d
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.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.VcsException;
22 import com.intellij.openapi.vfs.VirtualFile;
23 import com.intellij.ui.DocumentAdapter;
24 import git4idea.commands.GitHandler;
25 import git4idea.commands.GitHandlerUtil;
26 import git4idea.commands.GitSimpleHandler;
27 import git4idea.commands.StringScanner;
28 import git4idea.i18n.GitBundle;
29 import org.jetbrains.annotations.NonNls;
31 import javax.swing.*;
32 import javax.swing.event.DocumentEvent;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.io.*;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Set;
40 /**
41 * The tag dialog for the git
43 public class GitTagDialog extends DialogWrapper {
44 /**
45 * Root panel
47 private JPanel myPanel;
48 /**
49 * Git root selector
51 private JComboBox myGitRootComboBox;
52 /**
53 * Current branch label
55 private JLabel myCurrentBranch;
56 /**
57 * Tag name
59 private JTextField myTagNameTextField;
60 /**
61 * Force tag creation checkbox
63 private JCheckBox myForceCheckBox;
64 /**
65 * Text area that contains tag message if non-empty
67 private JTextArea myMessageTextArea;
68 /**
69 * The name of commit to tag
71 private JTextField myCommitTextField;
72 /**
73 * The validate button
75 private JButton myValidateButton;
76 /**
77 * The validator for commit text field
79 private final GitReferenceValidator myCommitTextFieldValidator;
80 /**
81 * The current project
83 private final Project myProject;
84 /**
85 * Existing tags for the project
87 private final Set<String> myExistingTags = new HashSet<String>();
88 /**
89 * Prefix for message file name
91 @NonNls private static final String MESSAGE_FILE_PREFIX = "git-tag-message-";
92 /**
93 * Suffix for message file name
95 @NonNls private static final String MESSAGE_FILE_SUFFIX = ".txt";
96 /**
97 * Encoding for the message file
99 @NonNls private static final String MESSAGE_FILE_ENCODING = "UTF-8";
102 * A constructor
104 * @param project a project to select
105 * @param roots a git repository roots for the project
106 * @param defaultRoot a guessed default root
108 public GitTagDialog(Project project, List<VirtualFile> roots, VirtualFile defaultRoot) {
109 super(project, true);
110 setTitle(GitBundle.getString("tag.title"));
111 setOKButtonText(GitBundle.getString("tag.button"));
112 myProject = project;
113 GitUIUtil.setupRootChooser(myProject, roots, defaultRoot, myGitRootComboBox, myCurrentBranch);
114 myGitRootComboBox.addActionListener(new ActionListener() {
115 public void actionPerformed(final ActionEvent e) {
116 fetchTags();
117 validateFields();
120 fetchTags();
121 myTagNameTextField.getDocument().addDocumentListener(new DocumentAdapter() {
122 protected void textChanged(final DocumentEvent e) {
123 validateFields();
126 myCommitTextFieldValidator = new GitReferenceValidator(project, myGitRootComboBox, myCommitTextField, myValidateButton, new Runnable() {
127 public void run() {
128 validateFields();
131 myForceCheckBox.addActionListener(new ActionListener() {
132 public void actionPerformed(final ActionEvent e) {
133 if (myForceCheckBox.isEnabled()) {
134 validateFields();
138 init();
139 validateFields();
143 * Perform tagging according to selected options
145 * @param exceptions the list where exceptions are collected
147 public void runAction(final List<VcsException> exceptions) {
148 final String message = myMessageTextArea.getText();
149 final boolean hasMessage = message.trim().length() != 0;
150 final File messageFile;
151 if (hasMessage) {
152 try {
153 messageFile = File.createTempFile(MESSAGE_FILE_PREFIX, MESSAGE_FILE_SUFFIX);
154 messageFile.deleteOnExit();
155 Writer out = new OutputStreamWriter(new FileOutputStream(messageFile), MESSAGE_FILE_ENCODING);
156 try {
157 out.write(message);
159 finally {
160 out.close();
163 catch (IOException ex) {
164 Messages.showErrorDialog(myProject, GitBundle.message("tag.error.creating.message.file.message", ex.toString()),
165 GitBundle.getString("tag.error.creating.message.file.title"));
166 return;
169 else {
170 messageFile = null;
172 try {
173 GitSimpleHandler h = new GitSimpleHandler(myProject, getGitRoot(), GitHandler.TAG);
174 h.setNoSSH(true);
175 if (hasMessage) {
176 h.addParameters("-a");
178 if (myForceCheckBox.isEnabled() && myForceCheckBox.isSelected()) {
179 h.addParameters("-f");
181 if (hasMessage) {
182 h.addParameters("-F", messageFile.getAbsolutePath());
184 h.addParameters(myTagNameTextField.getText());
185 String object = myCommitTextField.getText().trim();
186 if (object.length() != 0) {
187 h.addParameters(object);
189 try {
190 GitHandlerUtil.doSynchronously(h, GitBundle.getString("tagging.title"), h.printableCommandLine());
192 finally {
193 exceptions.addAll(h.errors());
196 finally {
197 if (messageFile != null) {
198 //noinspection ResultOfMethodCallIgnored
199 messageFile.delete();
205 * Validate dialog fields
207 private void validateFields() {
208 String text = myTagNameTextField.getText();
209 if (myExistingTags.contains(text)) {
210 myForceCheckBox.setEnabled(true);
211 if (!myForceCheckBox.isSelected()) {
212 setErrorText(GitBundle.getString("tag.error.tag.exists"));
213 setOKActionEnabled(false);
214 return;
217 else {
218 myForceCheckBox.setEnabled(false);
219 myForceCheckBox.setSelected(false);
221 if (myCommitTextFieldValidator.isInvalid()) {
222 setErrorText(GitBundle.getString("tag.error.invalid.commit"));
223 setOKActionEnabled(false);
224 return;
226 if (text.length() == 0) {
227 setErrorText(null);
228 setOKActionEnabled(false);
229 return;
231 setErrorText(null);
232 setOKActionEnabled(true);
236 * Fetch tags
238 private void fetchTags() {
239 myExistingTags.clear();
240 GitSimpleHandler h = new GitSimpleHandler(myProject, getGitRoot(), GitHandler.TAG);
241 h.setNoSSH(true);
242 h.setSilent(true);
243 String output = GitHandlerUtil.doSynchronously(h, GitBundle.getString("tag.getting.existing.tags"), h.printableCommandLine());
244 for (StringScanner s = new StringScanner(output); s.hasMoreData();) {
245 String line = s.line();
246 if (line.length() == 0) {
247 continue;
249 myExistingTags.add(line);
254 * @return the current git root
256 private VirtualFile getGitRoot() {
257 return (VirtualFile)myGitRootComboBox.getSelectedItem();
261 * {@inheritDoc}
263 protected JComponent createCenterPanel() {
264 return myPanel;
268 * {@inheritDoc}
270 @Override
271 protected String getDimensionServiceKey() {
272 return getClass().getName();
276 * {@inheritDoc}
278 @Override
279 protected String getHelpId() {
280 return "reference.VersionControl.Git.TagFiles";