View Javadoc

1   package at.ac.tuwien.ifs.bpse.basic.gui;
2   
3   import java.awt.Color;
4   import java.util.regex.Pattern;
5   
6   import javax.swing.JComponent;
7   import javax.swing.JTextField;
8   import javax.swing.text.Document;
9   
10  /**
11   * RegexTextField combines a TextField with a Regular Expression.
12   * 
13   * @author The SE-Team
14   * @version 1.0
15   * @see javax.swing.JTextField
16   */
17  public final class RegexTextField extends JTextField {
18  
19  	/**
20  	 * serialVersionUID, generated by eclipse.
21  	 */
22  	private static final long serialVersionUID = -1298714928521185271L;
23  
24  	/**
25  	 * The Regular Expression to be matched against by isMatching() method.
26  	 */
27  	private String regex = ".*";
28  
29  	/**
30  	 * For user feedback in the GUI.
31  	 */
32  	private JComponent validationIndicator = null;
33  
34  	/**
35  	 * The color set by validationIndicator
36  	 */
37  	private Color errorColor = Color.RED, originalColor = null;
38  
39  	/**
40  	 * Constructs a new <code>RegexTextField</code>. A default
41  	 * <code>model</code> is created, the initial string is <code>null</code>,
42  	 * and the number of columns is set to 0.
43  	 * 
44  	 */
45  	public RegexTextField() {
46  		super();
47  	}
48  
49  	/**
50  	 * Constructs a new <code>RegexTextField</code> initialized with the
51  	 * specified text. A default <code>model</code> is created and the number
52  	 * of columns is set to 0.
53  	 * 
54  	 * @param text
55  	 *            the text to be displayed, or <code>null</code>.
56  	 * 
57  	 */
58  	public RegexTextField(String text) {
59  		super(text);
60  	}
61  
62  	/**
63  	 * Constructs a new empty <code>TextField</code> with the specified number
64  	 * of columns. A default model is created and the initial string is set to
65  	 * <code>null</code>.
66  	 * 
67  	 * @param columns
68  	 *            the number of columns to use to calculate the preferred width;
69  	 *            if columns is set to <code>zero</code>, the preferred width
70  	 *            will be whatever naturally results from the component
71  	 *            implementation.
72  	 */
73  	public RegexTextField(int columns) {
74  		super(columns);
75  	}
76  
77  	/**
78  	 * Constructs a new <code>TextField</code> initialized with the specified
79  	 * text and columns. A default model is created.
80  	 * 
81  	 * @param text
82  	 *            the text to be displayed, or <code>null</code>.
83  	 * @param columns
84  	 *            the number of columns to use to calculate the preferred width;
85  	 *            if columns is set to <code>zero</code>, the preferred width
86  	 *            will be whatever naturally results from the component
87  	 *            implementation.
88  	 * 
89  	 */
90  	public RegexTextField(String text, int columns) {
91  		super(text, columns);
92  	}
93  
94  	/**
95  	 * Constructs a new <code>JTextField</code> that uses the given text
96  	 * storage model and the given number of columns. This is the constructor
97  	 * through which the other constructors feed. If the document is
98  	 * <code>null</code>, a default model is created.
99  	 * 
100 	 * @param doc
101 	 *            the text storage to use; if this is <code>null</code>, a
102 	 *            default will be provided by calling the
103 	 *            <code>createDefaultModel</code> method.
104 	 * @param text
105 	 *            the text to be displayed, or <code>null</code>.
106 	 * @param columns
107 	 *            the number of columns to use to calculate the preferred width;
108 	 *            if columns is set to <code>zero</code>, the preferred width
109 	 *            will be whatever naturally results from the component
110 	 *            implementation.
111 	 */
112 	public RegexTextField(Document doc, String text, int columns) {
113 		super(doc, text, columns);
114 	}
115 
116 	/**
117 	 * Returns the error-color, the error-color indicates a mismatch of the
118 	 * regex and the text.
119 	 * 
120 	 * @return Returns the errorColor.
121 	 * @see #getRegex()
122 	 * @see #getText()
123 	 */
124 	public Color getErrorColor() {
125 		return errorColor;
126 	}
127 
128 	/**
129 	 * Sets the errorColor which indicates a mismatch of the regex and the text.
130 	 * 
131 	 * @param errorColor
132 	 *            The errorColor to set.
133 	 * @see #setRegex(String)
134 	 * @see #setText(String)
135 	 */
136 	public void setErrorColor(Color errorColor) {
137 		this.errorColor = errorColor;
138 	}
139 
140 	/**
141 	 * Returns the regular expression the text is validated against.
142 	 * 
143 	 * @return Returns the regex
144 	 * @see #getText()
145 	 * @see #isMatching()
146 	 */
147 	public String getRegex() {
148 		return regex;
149 	}
150 
151 	/**
152 	 * Sets the regular expression the text is validated against.
153 	 * 
154 	 * @param regex
155 	 *            The regex to set.
156 	 * @see #isMatching()
157 	 */
158 	public void setRegex(String regex) {
159 		this.regex = regex;
160 	}
161 
162 	/**
163 	 * Returns the validationIndicator. It is a <code>JComponent</code> which
164 	 * is used to indicate a mismatch of the regex and the text. In case of a
165 	 * mismatch, the <code>JComponent</code>s foreground is set to the
166 	 * errorColor.
167 	 * 
168 	 * @return Returns the validationIndicator, or <code>null</code>.
169 	 * @see #isMatching()
170 	 * @see #getErrorColor()
171 	 */
172 	public JComponent getValidationIndicator() {
173 		return validationIndicator;
174 	}
175 
176 	/**
177 	 * Sets the validationInidcator. This is a <code>JComponent</code> which
178 	 * is used to indicate a mismatch of the regex and the text. In case of a
179 	 * mismatch, the <code>JComponent</code>s foreground is set to the
180 	 * errorColor.
181 	 * 
182 	 * @param validationIndicator
183 	 *            The validationIndicator to set, or <code>null</code>.
184 	 */
185 	public void setValidationIndicator(JComponent validationIndicator) {
186 		this.validationIndicator = validationIndicator;
187 	}
188 
189 	/**
190 	 * Determines whether the text is matching the regular expression. The text
191 	 * is matched against the regex, using the method
192 	 * <code>Pattern.matches()</code> from <code>java.util.regex</code>. In
193 	 * case of a mismatch, the
194 	 * <code>validationIndicator</coed>'s foreground is set to <code>errorColor</code>. 
195 	 * @return <code>true</code> if the text matches the regex,
196 	 *         <code>false</code> otherwise.
197 	 * @see #getRegex()
198 	 * @see Pattern#matches(java.lang.String, java.lang.CharSequence)
199 	 * @see #getValidationIndicator()
200 	 * @see #getErrorColor()
201 	 */
202 	public boolean isMatching() {
203 		boolean erg = Pattern.matches(regex, super.getText());
204 		if (validationIndicator != null) {
205 			validationIndicator.setForeground(erg ? originalColor : errorColor);
206 		}
207 		return erg;
208 	}
209 
210 	@Override
211 	public void setForeground(Color fg) {
212 		super.setForeground(fg);
213 		originalColor = fg;
214 	}
215 
216 	@Override
217 	public String getText() {
218 		// isMatching();
219 		return super.getText();
220 	}
221 
222 	@Override
223 	public void setText(String t) {
224 		super.setText(t);
225 		// isMatching();
226 	}
227 }