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.
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
;
32 import javax
.swing
.event
.DocumentEvent
;
33 import java
.awt
.event
.ActionEvent
;
34 import java
.awt
.event
.ActionListener
;
36 import java
.util
.HashSet
;
37 import java
.util
.List
;
41 * The tag dialog for the git
43 public class GitTagDialog
extends DialogWrapper
{
47 private JPanel myPanel
;
51 private JComboBox myGitRootComboBox
;
53 * Current branch label
55 private JLabel myCurrentBranch
;
59 private JTextField myTagNameTextField
;
61 * Force tag creation checkbox
63 private JCheckBox myForceCheckBox
;
65 * Text area that contains tag message if non-empty
67 private JTextArea myMessageTextArea
;
69 * The name of commit to tag
71 private JTextField myCommitTextField
;
75 private JButton myValidateButton
;
77 * The validator for commit text field
79 private final GitReferenceValidator myCommitTextFieldValidator
;
83 private final Project myProject
;
85 * Existing tags for the project
87 private final Set
<String
> myExistingTags
= new HashSet
<String
>();
89 * Prefix for message file name
91 @NonNls private static final String MESSAGE_FILE_PREFIX
= "git-tag-message-";
93 * Suffix for message file name
95 @NonNls private static final String MESSAGE_FILE_SUFFIX
= ".txt";
97 * Encoding for the message file
99 @NonNls private static final String MESSAGE_FILE_ENCODING
= "UTF-8";
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"));
113 GitUIUtil
.setupRootChooser(myProject
, roots
, defaultRoot
, myGitRootComboBox
, myCurrentBranch
);
114 myGitRootComboBox
.addActionListener(new ActionListener() {
115 public void actionPerformed(final ActionEvent e
) {
121 myTagNameTextField
.getDocument().addDocumentListener(new DocumentAdapter() {
122 protected void textChanged(final DocumentEvent e
) {
126 myCommitTextFieldValidator
= new GitReferenceValidator(project
, myGitRootComboBox
, myCommitTextField
, myValidateButton
, new Runnable() {
131 myForceCheckBox
.addActionListener(new ActionListener() {
132 public void actionPerformed(final ActionEvent e
) {
133 if (myForceCheckBox
.isEnabled()) {
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
;
153 messageFile
= File
.createTempFile(MESSAGE_FILE_PREFIX
, MESSAGE_FILE_SUFFIX
);
154 messageFile
.deleteOnExit();
155 Writer out
= new OutputStreamWriter(new FileOutputStream(messageFile
), MESSAGE_FILE_ENCODING
);
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"));
173 GitSimpleHandler h
= new GitSimpleHandler(myProject
, getGitRoot(), GitHandler
.TAG
);
176 h
.addParameters("-a");
178 if (myForceCheckBox
.isEnabled() && myForceCheckBox
.isSelected()) {
179 h
.addParameters("-f");
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
);
190 GitHandlerUtil
.doSynchronously(h
, GitBundle
.getString("tagging.title"), h
.printableCommandLine());
193 exceptions
.addAll(h
.errors());
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);
218 myForceCheckBox
.setEnabled(false);
219 myForceCheckBox
.setSelected(false);
221 if (myCommitTextFieldValidator
.isInvalid()) {
222 setErrorText(GitBundle
.getString("tag.error.invalid.commit"));
223 setOKActionEnabled(false);
226 if (text
.length() == 0) {
228 setOKActionEnabled(false);
232 setOKActionEnabled(true);
238 private void fetchTags() {
239 myExistingTags
.clear();
240 GitSimpleHandler h
= new GitSimpleHandler(myProject
, getGitRoot(), GitHandler
.TAG
);
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) {
249 myExistingTags
.add(line
);
254 * @return the current git root
256 private VirtualFile
getGitRoot() {
257 return (VirtualFile
)myGitRootComboBox
.getSelectedItem();
263 protected JComponent
createCenterPanel() {
271 protected String
getDimensionServiceKey() {
272 return getClass().getName();
279 protected String
getHelpId() {
280 return "reference.VersionControl.Git.TagFiles";