View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  
18  /*
19   * Project: KernelConfigurator
20   * Created on 25-feb-2004
21   *
22   * Copyright (c)2003 Grid Systems
23   */
24  package com.gridsystems.config.tools;
25  
26  import javax.swing.InputVerifier;
27  import javax.swing.JComponent;
28  import javax.swing.JTextField;
29  
30  /**
31   * InputVerifier subclass that can be also used in non-Swing environments.
32   * <p>
33   * It substitutes the default verify(JComponent) method by a
34   * verify(String) method. Although not so general as its superclass, it is a
35   * valid implementation for text fields.
36   *
37   * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
38   * @version 1.0
39   */
40  public abstract class TextVerifier extends InputVerifier {
41    /**
42     * The error message.
43     */
44    protected ErrorSupport support;
45  
46    /**
47     * Creates an instance.
48     * @param support Error message support.
49     */
50    public TextVerifier(ErrorSupport support) {
51      this.support = support;
52    }
53  
54    /**
55     * {@inheritDoc}
56     */
57    public boolean verify(JComponent c) {
58      boolean isValid = true;
59      if (c instanceof JTextField) {
60        JTextField field = (JTextField)c;
61        isValid = verify(field.getText());
62      }
63      return isValid;
64    }
65  
66    /**
67     * Validates a text string.
68     *
69     * @param text The string to validate
70     * @return true if the text is valid
71     */
72    public abstract boolean verify(String text);
73  
74    /**
75     * Shows the error message if a validation error occurs.
76     *
77     * @param id  The error identifier
78     * @param add The error action
79     */
80    protected void updateError(String id, boolean add) {
81      if (this.support != null) {
82        this.support.updateError(id, add);
83      }
84    }
85  
86    /**
87     * Removes from the error support manager the list of errors of this verifier.
88     * <p>
89     * This method will be used on field deactivation.
90     */
91    public abstract void cleanErrors();
92  }