001: package org.apache.ojb.tools.mapping.reversedb2.propertyEditors;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * This is a JTextfield implementing the PropertyEditorComponentInterface. You
020: * just have to specify the editor Key (which is the property key you whish
021: * to display from the target), everything else is being handeled within this
022: * class.
023: *
024: * @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
025: * @version $Id: PropertyEditorJTextField.java,v 1.1.2.1 2005/12/21 22:33:27 tomdz Exp $
026: */
027: public class PropertyEditorJTextField extends javax.swing.JTextField
028: implements PropertyEditorComponentInterface,
029: java.beans.PropertyChangeListener, java.awt.event.KeyListener,
030: java.awt.event.FocusListener, java.awt.event.ActionListener
031:
032: {
033: private String editorKey;
034: private PropertyEditorTarget aTarget;
035:
036: /* Only here to be compliant with JavaBeans spec, which is required for most
037: * graphical editors */
038: public PropertyEditorJTextField() {
039: super ();
040: this .addKeyListener(this );
041: this .addActionListener(this );
042: this .addFocusListener(this );
043: }
044:
045: public String getEditorKey() {
046: return editorKey;
047: }
048:
049: public void setEditorKey(String newKey) {
050: editorKey = newKey;
051: setEditorTarget(this .aTarget);
052: }
053:
054: public void setValue(String key, Object value) {
055: if (editorKey != null && editorKey.equals(key)) {
056: setValue(value);
057: }
058: }
059:
060: public void setValue(Object value) {
061: if (value == null) {
062: setText("");
063: } else {
064: setText(value.toString());
065: }
066: }
067:
068: public void setEditorTarget(PropertyEditorTarget paTarget) {
069: if (aTarget != null)
070: aTarget.removePropertyChangeListener(this );
071: if (paTarget != null) {
072: aTarget = paTarget;
073: aTarget.addPropertyChangeListener(editorKey, this );
074: setValue(aTarget.getAttribute(editorKey));
075: }
076:
077: }
078:
079: /** This method gets called when a bound property is changed.
080: * @param evt A PropertyChangeEvent object describing the event source
081: * and the property that has changed.
082: *
083: */
084: public void propertyChange(java.beans.PropertyChangeEvent evt) {
085: if (evt.getPropertyName().equals(this .editorKey)) {
086: this .setValue(evt.getNewValue());
087: }
088: }
089:
090: public void actionPerformed(java.awt.event.ActionEvent evt) {
091: if (this .aTarget != null)
092: this .aTarget.setAttribute(this .editorKey, getText());
093: }
094:
095: public void focusLost(java.awt.event.FocusEvent evt) {
096: if (this .aTarget != null)
097: this .aTarget.setAttribute(this .editorKey, getText());
098: }
099:
100: public void keyPressed(java.awt.event.KeyEvent evt) {
101: if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ESCAPE
102: && aTarget != null) {
103: setValue(aTarget.getAttribute(this .editorKey));
104: }
105: }
106:
107: /** Invoked when a component gains the keyboard focus.
108: *
109: */
110: public void focusGained(java.awt.event.FocusEvent e) {
111: }
112:
113: /** Invoked when a key has been released.
114: * See the class description for {@link KeyEvent} for a definition of
115: * a key released event.
116: *
117: */
118: public void keyReleased(java.awt.event.KeyEvent e) {
119: }
120:
121: /** Invoked when a key has been typed.
122: * See the class description for {@link KeyEvent} for a definition of
123: * a key typed event.
124: *
125: */
126: public void keyTyped(java.awt.event.KeyEvent e) {
127: }
128:
129: }
|