[4939] | 1 | package org.greenstone.gatherer.gui;
|
---|
| 2 |
|
---|
| 3 | import java.awt.*;
|
---|
| 4 | import java.util.regex.*;
|
---|
| 5 | import javax.swing.*;
|
---|
| 6 | import javax.swing.event.*;
|
---|
| 7 | import javax.swing.text.*;
|
---|
[18370] | 8 | import org.greenstone.gatherer.Dictionary;
|
---|
[4939] | 9 |
|
---|
| 10 | public 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();
|
---|
[18370] | 22 | this.setComponentOrientation(Dictionary.getOrientation());
|
---|
[4939] | 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 | }
|
---|