source: trunk/gli/src/org/greenstone/gatherer/gui/URLField.java@ 8236

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 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.DebugStream;
9
10
11public class URLField
12 extends JTextField
13 implements DocumentListener {
14
15 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]))*)*");
16
17 // Crap RegEx that matches everything dammit
18 //static final public Pattern URL_PATTERN = Pattern.compile("((.*?)://)?(([^:]*):([^@]*)@)?(([^/:]*)(:[^/]*))?([^\\?#]*/?)?(\\?([^?#]*))?(#(.*))?");
19
20 // Crap RegEx that demands URL starts with www
21 //static final public Pattern URL_PATTERN = Pattern.compile("(h|ht|htt|http|http:|http:/|http://|http://)?[\\w]+(\\.[\\w]+)([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?");
22
23 private Color background;
24 private Color foreground;
25 private Color invalid_background;
26 private Color invalid_foreground;
27
28 public URLField(Color foreground, Color background, Color invalid_foreground, Color invalid_background) {
29 super();
30 this.background = background;
31 this.foreground = foreground;
32 this.invalid_background = invalid_background;
33 this.invalid_foreground = invalid_foreground;
34 setBackground(background);
35 setForeground(foreground);
36 //getDocument().addDocumentListener(this);
37 }
38
39 public URLField(String email, Color foreground, Color background, Color invalid_foreground, Color invalid_background) {
40 super(email);
41 this.background = background;
42 this.foreground = foreground;
43 this.invalid_background = invalid_background;
44 this.invalid_foreground = invalid_foreground;
45 setBackground(background);
46 //getDocument().addDocumentListener(this);
47 }
48
49 /** Gives notification that an attribute or set of attributes changed.
50 * @param e
51 */
52 public void changedUpdate(DocumentEvent e) {
53 validateURL();
54 }
55
56 /** Gives notification that there was an insert into the document.
57 * @param e
58 */
59 public void insertUpdate(DocumentEvent e) {
60 validateURL();
61 }
62
63 public boolean isEmpty() {
64 return (this.getText().length() == 0);
65 }
66
67 /** Gives notification that a portion of the document has been removed.
68 * @param e
69 */
70 public void removeUpdate(DocumentEvent e) {
71 validateURL();
72 }
73
74 public boolean validateURL() {
75 boolean result;
76 String text = getText();
77 Matcher m = URL_PATTERN.matcher(text);
78 if(text.length() == 0 || m.matches()) {
79 setBackground(background);
80 setForeground(foreground);
81 result = true;
82 }
83 else {
84 setBackground(invalid_background);
85 setForeground(invalid_foreground);
86 result = false;
87 }
88 m = null;
89 DebugStream.println("Validating: " + text + " -> " + (result ? "GOOD" : "BAD"));
90 text = null;
91 return result;
92 }
93
94 static public void main(String[] args) {
95 JFrame frame = new JFrame("URLField test");
96 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
97 frame.setSize(300,60);
98 JPanel panel = (JPanel) frame.getContentPane();
99 panel.setLayout(new BorderLayout());
100 panel.add(new URLField(Color.black, Color.white, Color.white, Color.red), BorderLayout.CENTER);
101 frame.show();
102 }
103}
Note: See TracBrowser for help on using the repository browser.