source: gli/trunk/src/org/greenstone/gatherer/gui/EmailField.java@ 18376

Last change on this file since 18376 was 18370, checked in by kjdon, 15 years ago

committed code submitted by Amin Hedjazi for making the GLI right to left. I worked on this code on the rtl-gli branch, then merged the branch back to the trunk at revision 18368. The branch code was slightly different in a couple of places where it shouldn't have been. So don't use the branch code next time. Start a new branch.

  • Property svn:keywords set to Author Date Id Revision
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.