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.*;
|
---|
8 |
|
---|
9 | public class URLField
|
---|
10 | extends JTextField
|
---|
11 | implements DocumentListener {
|
---|
12 |
|
---|
13 | static final public Pattern URL_PATTERN = Pattern.compile("h|ht|htt|http|http:|http:/|http://|http://(([a-zA-Z]([a-zA-Z]|[0-9]|[\\$\\-_\\&\\+]|[!\\*\"\'\\(\\)]|(%[0-9A-Fa-f][0-9A-Fa-f]))*(\\.[a-zA-Z]([a-zA-Z]|[0-9]|[\\$\\-_\\&\\+]|[!\\*\"\'\\(\\)]|(%[0-9A-Fa-f][0-9A-Fa-f]))*)*)|(([0-9])+\\.([0-9])+\\.([0-9])+\\.([0-9])+))(:([0-9])+)?(/([a-zA-Z]|[0-9]|[\\$\\-_\\.\\&\\+]|[!\\*\"\'\\(\\),]|(%[0-9A-Fa-f][0-9A-Fa-f]))+)*");
|
---|
14 |
|
---|
15 | private Color background;
|
---|
16 | private Color foreground;
|
---|
17 | private Color invalid_background;
|
---|
18 | private Color invalid_foreground;
|
---|
19 |
|
---|
20 | public URLField(Color foreground, Color background, Color invalid_foreground, Color invalid_background) {
|
---|
21 | super();
|
---|
22 | this.background = background;
|
---|
23 | this.foreground = foreground;
|
---|
24 | this.invalid_background = invalid_background;
|
---|
25 | this.invalid_foreground = invalid_foreground;
|
---|
26 | setBackground(background);
|
---|
27 | setForeground(foreground);
|
---|
28 | //getDocument().addDocumentListener(this);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public URLField(String email, Color foreground, Color background, Color invalid_foreground, Color invalid_background) {
|
---|
32 | super(email);
|
---|
33 | this.background = background;
|
---|
34 | this.foreground = foreground;
|
---|
35 | this.invalid_background = invalid_background;
|
---|
36 | this.invalid_foreground = invalid_foreground;
|
---|
37 | setBackground(background);
|
---|
38 | //getDocument().addDocumentListener(this);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /** Gives notification that an attribute or set of attributes changed.
|
---|
42 | * @param e
|
---|
43 | */
|
---|
44 | public void changedUpdate(DocumentEvent e) {
|
---|
45 | validateURL();
|
---|
46 | }
|
---|
47 |
|
---|
48 | /** Gives notification that there was an insert into the document.
|
---|
49 | * @param e
|
---|
50 | */
|
---|
51 | public void insertUpdate(DocumentEvent e) {
|
---|
52 | validateURL();
|
---|
53 | }
|
---|
54 |
|
---|
55 | /** Gives notification that a portion of the document has been removed.
|
---|
56 | * @param e
|
---|
57 | */
|
---|
58 | public void removeUpdate(DocumentEvent e) {
|
---|
59 | validateURL();
|
---|
60 | }
|
---|
61 |
|
---|
62 | public boolean validateURL() {
|
---|
63 | boolean result;
|
---|
64 | String text = getText();
|
---|
65 | Matcher m = URL_PATTERN.matcher(text);
|
---|
66 | if(text.length() == 0 || m.matches()) {
|
---|
67 | setBackground(background);
|
---|
68 | setForeground(foreground);
|
---|
69 | result = true;
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | setBackground(invalid_background);
|
---|
73 | setForeground(invalid_foreground);
|
---|
74 | result = false;
|
---|
75 | }
|
---|
76 | m = null;
|
---|
77 | return result;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static public void main(String[] args) {
|
---|
81 | JFrame frame = new JFrame("URLField test");
|
---|
82 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
83 | frame.setSize(300,60);
|
---|
84 | JPanel panel = (JPanel) frame.getContentPane();
|
---|
85 | panel.setLayout(new BorderLayout());
|
---|
86 | panel.add(new URLField(Color.black, Color.white, Color.white, Color.red), BorderLayout.CENTER);
|
---|
87 | frame.show();
|
---|
88 | }
|
---|
89 | }
|
---|