KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > util > TextAdapter


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.modeler.util;
57
58 import java.awt.Color JavaDoc;
59 import java.awt.event.ActionEvent JavaDoc;
60 import java.awt.event.ActionListener JavaDoc;
61
62 import javax.swing.InputVerifier JavaDoc;
63 import javax.swing.JComponent JavaDoc;
64 import javax.swing.JTextArea JavaDoc;
65 import javax.swing.JTextField JavaDoc;
66 import javax.swing.event.DocumentEvent JavaDoc;
67 import javax.swing.event.DocumentListener JavaDoc;
68 import javax.swing.text.JTextComponent JavaDoc;
69
70 import org.objectstyle.cayenne.modeler.dialog.validator.ValidatorDialog;
71 import org.objectstyle.cayenne.validation.ValidationException;
72
73 /**
74  * A validating adapter for JTextComponent. Implement {@link #updateModel(String)}to
75  * initialize model on text change.
76  *
77  * @author Andrei Adamchik
78  */

79 public abstract class TextAdapter {
80
81     protected Color JavaDoc defaultBGColor;
82     protected Color JavaDoc errorColor;
83     protected JTextComponent JavaDoc textComponent;
84     protected String JavaDoc defaultToolTip;
85     protected boolean modelUpdateDisabled;
86
87     public TextAdapter(JTextField JavaDoc textField) {
88         this(textField, true, false, true);
89     }
90
91     public TextAdapter(JTextField JavaDoc textField, boolean checkOnFocusLost,
92             boolean checkOnTyping, boolean checkOnEnter) {
93         this(textField, true, false);
94
95         if (checkOnEnter) {
96
97             textField.addActionListener(new ActionListener JavaDoc() {
98
99                 public void actionPerformed(ActionEvent JavaDoc e) {
100                     updateModel();
101                 }
102             });
103         }
104     }
105
106     public TextAdapter(JTextArea JavaDoc textField) {
107         this(textField, false, true);
108     }
109
110     public TextAdapter(JTextComponent JavaDoc textComponent, boolean checkOnFocusLost,
111             boolean checkOnTyping) {
112         this.errorColor = ValidatorDialog.WARNING_COLOR;
113         this.defaultBGColor = textComponent.getBackground();
114         this.defaultToolTip = textComponent.getToolTipText();
115         this.textComponent = textComponent;
116
117         if (checkOnFocusLost) {
118             textComponent.setInputVerifier(new InputVerifier JavaDoc() {
119
120                 public boolean verify(JComponent JavaDoc c) {
121                     updateModel();
122                     // release focus after coloring the field...
123
return true;
124                 }
125             });
126         }
127
128         if (checkOnTyping) {
129             textComponent.getDocument().addDocumentListener(new DocumentListener JavaDoc() {
130
131                 public void insertUpdate(DocumentEvent JavaDoc e) {
132                     verifyTextChange(e);
133                 }
134
135                 public void changedUpdate(DocumentEvent JavaDoc e) {
136                     verifyTextChange(e);
137                 }
138
139                 public void removeUpdate(DocumentEvent JavaDoc e) {
140                     verifyTextChange(e);
141                 }
142
143                 void verifyTextChange(DocumentEvent JavaDoc e) {
144                     if (!modelUpdateDisabled) {
145                         updateModel();
146                     }
147                 }
148             });
149         }
150     }
151
152     /**
153      * Updates bound model with document text.
154      */

155     protected abstract void updateModel(String JavaDoc text) throws ValidationException;
156
157     /**
158      * Returns internal text component.
159      */

160     public JTextComponent JavaDoc getComponent() {
161         return textComponent;
162     }
163
164     /**
165      * Sets the text of the underlying text field.
166      */

167     public void setText(String JavaDoc text) {
168         modelUpdateDisabled = true;
169         try {
170             clear();
171             textComponent.setText(text);
172         }
173         finally {
174             modelUpdateDisabled = false;
175         }
176     }
177
178     protected void updateModel() {
179         try {
180             updateModel(textComponent.getText());
181             clear();
182         }
183         catch (ValidationException vex) {
184             textComponent.setBackground(errorColor);
185             textComponent.setToolTipText(vex.getUnlabeledMessage());
186         }
187     }
188
189     protected void clear() {
190         textComponent.setBackground(defaultBGColor);
191         textComponent.setToolTipText(defaultToolTip);
192     }
193 }
Popular Tags