KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tools > mapping > reversedb2 > propertyEditors > PropertyEditorJTextField


1 package org.apache.ojb.tools.mapping.reversedb2.propertyEditors;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /**
19  * This is a JTextfield implementing the PropertyEditorComponentInterface. You
20  * just have to specify the editor Key (which is the property key you whish
21  * to display from the target), everything else is being handeled within this
22  * class.
23  *
24  * @author <a HREF="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
25  * @version $Id: PropertyEditorJTextField.java,v 1.1.2.1 2005/12/21 22:33:27 tomdz Exp $
26  */

27 public class PropertyEditorJTextField extends javax.swing.JTextField JavaDoc
28     implements PropertyEditorComponentInterface,
29                java.beans.PropertyChangeListener JavaDoc,
30                java.awt.event.KeyListener JavaDoc,
31                java.awt.event.FocusListener JavaDoc,
32                java.awt.event.ActionListener JavaDoc
33                
34 {
35     private String JavaDoc editorKey;
36     private PropertyEditorTarget aTarget;
37     
38     /* Only here to be compliant with JavaBeans spec, which is required for most
39      * graphical editors */

40     public PropertyEditorJTextField()
41     {
42         super();
43         this.addKeyListener(this);
44         this.addActionListener(this);
45         this.addFocusListener(this);
46     }
47     
48     public String JavaDoc getEditorKey()
49     {
50         return editorKey;
51     }
52     
53     public void setEditorKey(String JavaDoc newKey)
54     {
55         editorKey = newKey;
56         setEditorTarget(this.aTarget);
57     }
58     
59     public void setValue(String JavaDoc key, Object JavaDoc value)
60     {
61         if (editorKey != null && editorKey.equals(key))
62         {
63             setValue(value);
64         }
65     }
66     
67     public void setValue(Object JavaDoc value)
68     {
69         if (value == null)
70         {
71             setText("");
72         }
73         else
74         {
75             setText(value.toString());
76         }
77     }
78     
79     public void setEditorTarget(PropertyEditorTarget paTarget)
80     {
81         if (aTarget != null) aTarget.removePropertyChangeListener(this);
82         if (paTarget != null)
83         {
84             aTarget = paTarget;
85             aTarget.addPropertyChangeListener(editorKey, this);
86             setValue(aTarget.getAttribute(editorKey));
87         }
88         
89     }
90     
91     /** This method gets called when a bound property is changed.
92      * @param evt A PropertyChangeEvent object describing the event source
93      * and the property that has changed.
94      *
95      */

96     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt)
97     {
98         if (evt.getPropertyName().equals(this.editorKey))
99         {
100             this.setValue(evt.getNewValue());
101         }
102     }
103     
104     public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
105     {
106         if (this.aTarget != null)
107             this.aTarget.setAttribute(this.editorKey, getText());
108     }
109
110     public void focusLost(java.awt.event.FocusEvent JavaDoc evt)
111     {
112         if (this.aTarget != null)
113             this.aTarget.setAttribute(this.editorKey, getText());
114     }
115     public void keyPressed(java.awt.event.KeyEvent JavaDoc evt)
116     {
117         if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ESCAPE && aTarget != null)
118         {
119             setValue(aTarget.getAttribute(this.editorKey));
120         }
121     }
122     
123     /** Invoked when a component gains the keyboard focus.
124      *
125      */

126     public void focusGained(java.awt.event.FocusEvent JavaDoc e)
127     {
128     }
129     
130     /** Invoked when a key has been released.
131      * See the class description for {@link KeyEvent} for a definition of
132      * a key released event.
133      *
134      */

135     public void keyReleased(java.awt.event.KeyEvent JavaDoc e)
136     {
137     }
138     
139     /** Invoked when a key has been typed.
140      * See the class description for {@link KeyEvent} for a definition of
141      * a key typed event.
142      *
143      */

144     public void keyTyped(java.awt.event.KeyEvent JavaDoc e)
145     {
146     }
147     
148 }
149
Popular Tags