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

Last change on this file since 7183 was 6659, checked in by jmt12, 20 years ago

Due to popular demand, the URLField now has basic validation again. This means it prevent some perfectly good URLs, but only when mirrorings, my precious.

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