001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: package org.apache.jmeter.testbeans.gui;
018:
019: import java.awt.Component;
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.awt.event.FocusEvent;
023: import java.awt.event.FocusListener;
024: import java.beans.PropertyEditorSupport;
025:
026: import javax.swing.JTextField;
027:
028: import org.apache.jorphan.logging.LoggingManager;
029: import org.apache.log.Logger;
030:
031: /**
032: * This class implements a property editor for non-null String properties that
033: * supports custom editing (i.e.: provides a GUI component) based on a text
034: * field.
035: * <p>
036: * The provided GUI is a simple text field.
037: *
038: * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
039: * @version $Revision: 493779 $ updated on $Date: 2007-01-07 17:46:38 +0000 (Sun, 07 Jan 2007) $
040: */
041: class FieldStringEditor extends PropertyEditorSupport implements
042: ActionListener, FocusListener {
043: protected static Logger log = LoggingManager.getLoggerForClass();
044:
045: /**
046: * This will hold the text editing component, either a plain JTextField (in
047: * cases where the combo box would not have other options than 'Edit'), or
048: * the text editing component in the combo box.
049: */
050: private JTextField textField;
051:
052: /**
053: * Value on which we started the editing. Used to avoid firing
054: * PropertyChanged events when there's not been such change.
055: */
056: private String initialValue = "";
057:
058: protected FieldStringEditor() {
059: super ();
060:
061: textField = new JTextField();
062: textField.addActionListener(this );
063: textField.addFocusListener(this );
064: }
065:
066: public String getAsText() {
067: return textField.getText();
068: }
069:
070: public void setAsText(String value) {
071: initialValue = value;
072: textField.setText(value);
073: }
074:
075: public Object getValue() {
076: return getAsText();
077: }
078:
079: public void setValue(Object value) {
080: if (value instanceof String)
081: setAsText((String) value);
082: else
083: throw new IllegalArgumentException();
084: }
085:
086: /*
087: * (non-Javadoc)
088: *
089: * @see java.beans.PropertyEditor#getCustomEditor()
090: */
091: public Component getCustomEditor() {
092: return textField;
093: }
094:
095: /*
096: * (non-Javadoc) Avoid needlessly firing PropertyChanged events.
097: */
098: public void firePropertyChange() {
099: String newValue = getAsText();
100:
101: if (initialValue.equals(newValue))
102: return;
103: initialValue = newValue;
104:
105: super .firePropertyChange();
106: }
107:
108: /*
109: * (non-Javadoc)
110: *
111: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
112: */
113: public void actionPerformed(ActionEvent e) {
114: firePropertyChange();
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
121: */
122: public void focusGained(FocusEvent e) {
123: }
124:
125: /*
126: * (non-Javadoc)
127: *
128: * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
129: */
130: public void focusLost(FocusEvent e) {
131: firePropertyChange();
132: }
133: }
|