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

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

More modifications to mirroring including testing for a valid version of Wget (and complaining if its missing or it is old) and rearranging buttons on the GProgressBar

  • 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.*;
8
9public 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 // Crap RegEx that matches everything dammit
16 //static final public Pattern URL_PATTERN = Pattern.compile("((.*?)://)?(([^:]*):([^@]*)@)?(([^/:]*)(:[^/]*))?([^\\?#]*/?)?(\\?([^?#]*))?(#(.*))?");
17
18 //static final public Pattern URL_PATTERN = Pattern.compile("(h|ht|htt|http|http:|http:/|http://|http://)?[a-zA-Z0-9-]*(\.[a-zA-Z0-9-])*");
19
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 return result;
89 }
90
91 static public void main(String[] args) {
92 JFrame frame = new JFrame("URLField test");
93 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
94 frame.setSize(300,60);
95 JPanel panel = (JPanel) frame.getContentPane();
96 panel.setLayout(new BorderLayout());
97 panel.add(new URLField(Color.black, Color.white, Color.white, Color.red), BorderLayout.CENTER);
98 frame.show();
99 }
100}
Note: See TracBrowser for help on using the repository browser.