update copyright
[fedora-idea.git] / plugins / cvs / cvs-plugin / src / com / intellij / cvsSupport2 / ui / experts / importToCvs / CvsFieldValidator.java
blob02d943fdc34238aa8558cda212c5ae82befafe2c
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.cvsSupport2.ui.experts.importToCvs;
18 import com.intellij.cvsSupport2.cvsoperations.cvsTagOrBranch.ui.TagNameFieldOwner;
19 import com.intellij.ui.DocumentAdapter;
20 import com.intellij.CvsBundle;
21 import org.jetbrains.annotations.NonNls;
23 import javax.swing.*;
24 import javax.swing.event.DocumentEvent;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
28 /**
29 * author: lesya
31 public class CvsFieldValidator {
32 public static final char[] INVALID_CHARACTERS = new char[]{'`', '$', '.', ',', ':', ';', '@', '\'', ' '};
34 private CvsFieldValidator() {
37 private static void reportError(JLabel errorLabel, String message, TagNameFieldOwner tagNameFieldOwner) {
38 @NonNls final String text = "<html><font color='red'><b>" + message + "</b></font></html>";
39 errorLabel.setText(text);
40 if (tagNameFieldOwner != null) {
41 tagNameFieldOwner.disableOkAction(message);
45 public static void installOn(final TagNameFieldOwner dialog, final JTextField field, final JLabel label) {
46 installOn(dialog, field, label, new AbstractButton[0]);
47 field.getDocument().addDocumentListener(new DocumentAdapter() {
48 public void textChanged(DocumentEvent event) {
49 checkTagNameField(dialog, field, label);
51 });
52 checkTagNameField(dialog, field, label);
55 public static void installOn(final TagNameFieldOwner dialog,
56 final JTextField field,
57 final JLabel label,
58 AbstractButton[] buttons) {
59 field.getDocument().addDocumentListener(new DocumentAdapter() {
60 public void textChanged(DocumentEvent event) {
61 checkTagNameField(dialog, field, label);
63 });
65 for (AbstractButton button : buttons) {
66 button.addActionListener(new ActionListener() {
67 public void actionPerformed(ActionEvent e) {
68 checkTagNameField(dialog, field, label);
70 });
73 checkTagNameField(dialog, field, label);
76 private static void checkTagNameField(TagNameFieldOwner dialog, JTextField field, JLabel label) {
77 if (!dialog.tagFieldIsActive()) {
78 label.setText(" ");
79 dialog.enableOkAction();
81 else if (checkField(field, new JTextField[0], true, label, dialog)) {
82 dialog.enableOkAction();
84 else {
85 field.requestFocus();
90 public static boolean checkField(JTextField field,
91 JTextField[] shouldDifferFrom,
92 boolean shouldStartFromLetter,
93 JLabel errorMessage, TagNameFieldOwner tagNameFieldOwner) {
94 String text = field.getText().trim();
95 if (text.length() == 0) {
96 reportError(errorMessage, CvsBundle.message("error.message.field.cannot.be.empty"), tagNameFieldOwner);
97 return false;
100 for (char invalidCharacter : INVALID_CHARACTERS) {
101 if (text.indexOf(invalidCharacter) != -1) {
102 reportError(errorMessage, CvsBundle.message("error.message.field.contains.invalid.characters"), tagNameFieldOwner);
103 return false;
107 for (JTextField jTextField : shouldDifferFrom) {
108 if (jTextField == field) continue;
109 if (jTextField.getText().trim().equals(text)) {
110 reportError(errorMessage, CvsBundle.message("error.message.duplicate.field.value"), tagNameFieldOwner);
111 return false;
116 if (shouldStartFromLetter && !Character.isLetter(text.charAt(0))) {
117 reportError(errorMessage, CvsBundle.message("error.message.field.value.must.start.with.a.letter"), tagNameFieldOwner);
118 return false;
121 errorMessage.setText(" ");
122 return true;