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

Last change on this file since 5590 was 5240, checked in by jmt12, 21 years ago

URLField should now only validate when asked (not on keypress)

  • Property svn:keywords set to Author Date Id Revision
File size: 2.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.*;
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 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}
Note: See TracBrowser for help on using the repository browser.