KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > generator > ValidatingTextField


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.tools.generator;
20
21 import java.awt.*;
22 import java.awt.event.*;
23
24 import javax.swing.*;
25 import javax.swing.text.*;
26 import javax.swing.event.*;
27
28 /**
29  * This is a on change validator giving a user color and tooltip
30  * feedback on entering invalid value.
31  * It cen also act as a ComboBoxEditor.
32  *
33  * @serial The serialization is not implemented.
34  *
35  * @author Petr Kuzel
36  * @version 1.0
37  */

38 public class ValidatingTextField extends JTextField implements ComboBoxEditor {
39
40     private static final long serialVersionUID = 23746913L;
41     
42     private transient Validator validator = null;
43     
44     private transient String JavaDoc tooltip = null; // original tooltip that is overwritten by validation tooltip
45

46     private transient DocumentListener adapter = null;
47     
48     public ValidatingTextField() {
49         
50         // let ENTER pushes default button
51
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
52         Keymap map = getKeymap();
53         map.removeKeyStrokeBinding(enter);
54         
55     }
56     
57     /**
58      * Set new validator that will be used for test input value validity.
59      * @param validator or null if validation should be switched off.
60      */

61     public void setValidator(Validator validator) {
62
63         Validator old = this.validator;
64         
65         this.validator = validator;
66         
67         if (old == null && validator != null) {
68         
69             // attach very simple validation
70

71             adapter = new DocumentListener() {
72                 public void insertUpdate(DocumentEvent e) { feedback(); }
73                 public void removeUpdate(DocumentEvent e) { feedback(); }
74                 public void changedUpdate(DocumentEvent e) {}
75             };
76                         
77             getDocument().addDocumentListener(adapter);
78             
79         } else if (old != null && validator == null) {
80             
81             // remove attached validator
82

83             getDocument().removeDocumentListener(adapter);
84                                     
85             adapter = null;
86         }
87         
88         feedback();
89         
90     }
91
92     //
93
// depending on validity set foreground color and tooltip
94
//
95
private void feedback() {
96         
97         // document callback can come from whatever thread place into AWT thread
98

99         SwingUtilities.invokeLater( new Runnable JavaDoc() {
100             public void run() {
101                 if (validator == null || validator.isValid(getText())) {
102                     ValidatingTextField.super.setToolTipText(tooltip);
103                     ValidatingTextField.this.setForeground(Color.black);
104                 } else {
105                     String JavaDoc reason = validator.getReason();
106                     ValidatingTextField.super.setToolTipText(reason == null ? Util.THIS.getString("MSG_invalid") : reason);
107                     ValidatingTextField.this.setForeground(Color.red);
108                 }
109             }
110         });
111     }
112     
113     // ~~~~~~ ComboBoxEditor interface implementation ~~~~~~~~~~~~~~~~~~~~
114

115     public java.lang.Object JavaDoc getItem() {
116         return getText();
117     }
118         
119     public void setItem(java.lang.Object JavaDoc obj) {
120         setText((String JavaDoc) obj);
121     }
122             
123     public java.awt.Component JavaDoc getEditorComponent() {
124         return this;
125     }
126
127     /*
128      * Set new tooltip that will be displyed whenever entered value is valid.
129      */

130     public void setToolTipText(String JavaDoc text) {
131         tooltip = text;
132         feedback();
133     }
134     
135     /*
136      * Reentarant validator of entered value.
137      * It can provide reason of invalidity.
138      */

139     public static interface Validator {
140         
141         /*
142          * Test the passed value returning false on invalidity.
143          */

144         public boolean isValid(String JavaDoc text);
145         
146         /**
147          * Return invalidity reason on null. It is used for tooltip.
148          */

149         public String JavaDoc getReason();
150     }
151    
152 }
153
Popular Tags