Source code: javatools/swing/TextComponentChecker.java
1 /*
2 * TextComponentChecker.java
3 *
4 * Created on 29 gennaio 2002, 12.22
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.swing;
26
27 import javax.swing.text.JTextComponent;
28 import javax.swing.text.Caret;
29 import java.awt.event.KeyEvent;
30
31 /**
32 * It is used to manage checking in a text component.
33 * @author Antonio Petrelli
34 * @version 0.0.1
35 */
36 public abstract class TextComponentChecker {
37
38
39 /** Creates new TextComponentChecker */
40 public TextComponentChecker() {
41 refText = null;
42 shadowText = null;
43 }
44
45 /** Dispatches a <CODE>KeyEvent</CODE> to this object.
46 * Call it IN EACH KEY EVENT (<CODE>KeyPressed, KeyReleased, KeyTyped</CODE>.
47 * @param evt The generated event.
48 */
49 public void dispatchEvent(KeyEvent evt) {
50 Caret ct;
51 int keyCode;
52
53 keyCode = evt.getKeyCode();
54 if (evt.getID() == KeyEvent.KEY_TYPED ||
55 keyCode == KeyEvent.VK_BACK_SPACE ||
56 keyCode == KeyEvent.VK_DELETE) {
57 ct = refText.getCaret();
58 shadowText.setCaretPosition(ct.getMark());
59 shadowText.moveCaretPosition(ct.getDot());
60 shadowText.dispatchEvent(evt);
61 refText.setText((shadowText.getText()));
62 ct = shadowText.getCaret();
63 refText.setCaretPosition(ct.getMark());
64 refText.moveCaretPosition(ct.getDot());
65 }
66 }
67
68 /** Returns the text contained in the managed component.
69 * @return The needed text.
70 */
71 public String getText() {
72 if (shadowText != null)
73 return shadowText.getText();
74 else
75 return null;
76 }
77
78 /** Checks if the component contains a correct value.
79 * @return <CODE>true</CODE>: It is correct.
80 * <CODE>false</CODE>: It is not correct.
81 */
82 public abstract boolean isValid();
83
84 /** Sets the referenced text component.
85 * @param refComp The component to be checked.
86 */
87 public void setRefTextComponent(JTextComponent refComp) {
88 refText = refComp;
89 try {
90 shadowText = (JTextComponent) refText.getClass().newInstance();
91 }
92 catch (InstantiationException e) {
93 System.out.println("Cannot build JTextComponent object!");
94 System.exit(1);
95 }
96 catch (IllegalAccessException e) {
97 System.out.println("Illegal access to JTextComponent object!");
98 System.exit(1);
99 }
100 syncCheckerText();
101 }
102
103 /** It is useful if text in the component changes without control.
104 */
105 public void syncCheckerText() {
106 if (refText != null && shadowText != null)
107 shadowText.setText(refText.getText());
108 }
109
110 private JTextComponent refText;
111 private JTextComponent shadowText;
112 }