source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/gui/EmailField.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 1.8 KB
Line 
1package org.greenstone.gatherer.gui;
2
3import java.awt.*;
4import java.util.regex.*;
5import javax.swing.*;
6import javax.swing.event.*;
7import javax.swing.text.*;
8import org.greenstone.gatherer.Dictionary;
9
10public class EmailField
11 extends JTextField
12 implements DocumentListener {
13
14 static final public Pattern EMAIL_PATTERN = Pattern.compile("([^()\\-<> @,;:\"][^()<> @,;:\"]*|\"[^()<>@,;:\"]+\")@([A-Za-z](-*[A-Za-z0-9])*(\\.[A-Za-z](-*[A-Za-z0-9])*)*)");
15
16 private boolean invalid = false;
17 private Color background;
18 private Color invalid_background;
19
20 public EmailField(Color invalid_background) {
21 super();
22 this.setComponentOrientation(Dictionary.getOrientation());
23 this.invalid_background = invalid_background;
24 }
25
26 public EmailField(String email, Color invalid_background) {
27 super(email);
28 this.invalid_background = invalid_background;
29 }
30
31 /** Gives notification that an attribute or set of attributes changed.
32 * @param e
33 */
34 public void changedUpdate(DocumentEvent e) {
35 validateEmail();
36 }
37
38 /** Gives notification that there was an insert into the document.
39 * @param e
40 */
41 public void insertUpdate(DocumentEvent e) {
42 validateEmail();
43 }
44
45 /** Gives notification that a portion of the document has been removed.
46 * @param e
47 */
48 public void removeUpdate(DocumentEvent e) {
49 validateEmail();
50 }
51
52 private void validateEmail() {
53 Matcher m = EMAIL_PATTERN.matcher(getText());
54 if(m.matches()) {
55 // It was invalid, but now its valid again
56 if(invalid) {
57 setBackground(background);
58 invalid = false;
59 }
60 // Otherwise nothings changed so why do anything
61 }
62 else {
63 background = getBackground();
64 setBackground(invalid_background);
65 invalid = true;
66 }
67 m = null;
68 }
69}
Note: See TracBrowser for help on using the repository browser.