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.
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.vcs.VcsException;
20 import com.intellij.openapi.vfs.VirtualFile;
21 import com.intellij.ui.DocumentAdapter;
22 import git4idea.GitRevisionNumber;
23 import git4idea.GitUtil;
24 import git4idea.util.GitUIUtil;
27 import javax.swing.event.DocumentEvent;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
32 * The class setups validation for references in the text fields.
34 public class GitReferenceValidator {
36 * The result of last validation
38 private boolean myLastResult;
40 * The text that was used for last validation
42 private String myLastResultText = null;
46 private final Project myProject;
48 * The git root combobox
50 private final JComboBox myGitRoot;
52 * The text field that contains object reference
54 private final JTextField myTextField;
56 * The button that initiates validation action
58 private final JButton myButton;
61 * A constructor from fields
63 * @param project the project to use
64 * @param gitRoot the git root directory
65 * @param textField the text field that contains object reference
66 * @param button the button that initiates validation action
67 * @param statusChanged the action that is invoked when validation status changed
69 public GitReferenceValidator(final Project project,
70 final JComboBox gitRoot,
71 final JTextField textField,
73 final Runnable statusChanged) {
76 myTextField = textField;
78 myGitRoot.addActionListener(new ActionListener() {
79 public void actionPerformed(final ActionEvent e) {
81 myLastResultText = null;
84 myTextField.getDocument().addDocumentListener(new DocumentAdapter() {
85 protected void textChanged(final DocumentEvent e) {
86 // note that checkOkButton is called in other listener
87 myButton.setEnabled(myTextField.getText().trim().length() != 0);
90 myButton.addActionListener(new ActionListener() {
91 public void actionPerformed(final ActionEvent e) {
92 final String revisionExpression = myTextField.getText();
93 myLastResultText = revisionExpression;
96 GitRevisionNumber revision = GitRevisionNumber.resolve(myProject, gitRoot(), revisionExpression);
97 GitUtil.showSubmittedFiles(myProject, revision.asString(), gitRoot(), false, false);
100 catch (VcsException ex) {
101 GitUIUtil.showOperationError(myProject, ex, "Validating revision: " + revisionExpression);
103 if (statusChanged != null) {
108 myButton.setEnabled(myTextField.getText().length() != 0);
112 * @return true if the reference is known to be invalid
114 public boolean isInvalid() {
115 final String revisionExpression = myTextField.getText();
116 return revisionExpression.equals(myLastResultText) && !myLastResult;
120 * @return currently selected git root
122 private VirtualFile gitRoot() {
123 return (VirtualFile)myGitRoot.getSelectedItem();