KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > testbeans > gui > FieldStringEditor


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testbeans/gui/FieldStringEditor.java,v 1.4 2004/02/10 21:24:01 jsalvata Exp $
2
/*
3  * Copyright 2003-2004 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 package org.apache.jmeter.testbeans.gui;
18
19 import java.awt.Component JavaDoc;
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.ActionListener JavaDoc;
22 import java.awt.event.FocusEvent JavaDoc;
23 import java.awt.event.FocusListener JavaDoc;
24 import java.beans.PropertyEditorSupport JavaDoc;
25
26 import javax.swing.JTextField JavaDoc;
27
28 import org.apache.jorphan.logging.LoggingManager;
29 import org.apache.log.Logger;
30
31 /**
32  * This class implements a property editor for non-null String properties
33  * that supports custom editing (i.e.: provides a GUI component) based
34  * on a text field.
35  * <p>
36  * The provided GUI is a simple text field.
37  *
38  * @author <a HREF="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
39  * @version $Revision: 1.4 $ updated on $Date: 2004/02/10 21:24:01 $
40  */

41 class FieldStringEditor extends PropertyEditorSupport JavaDoc
42     implements ActionListener JavaDoc, FocusListener JavaDoc
43 {
44     protected static Logger log= LoggingManager.getLoggerForClass();
45
46     /**
47      * This will hold the text editing component, either a plain JTextField
48      * (in cases where the combo box would not have other options
49      * than 'Edit'), or the text editing component in the combo box.
50      */

51     private JTextField JavaDoc textField;
52
53     /**
54      * Value on which we started the editing. Used to avoid firing
55      * PropertyChanged events when there's not been such change.
56      */

57     private String JavaDoc initialValue= "";
58
59     protected FieldStringEditor()
60     {
61         super();
62
63         textField= new JTextField JavaDoc();
64         textField.addActionListener(this);
65         textField.addFocusListener(this);
66     }
67
68     public String JavaDoc getAsText()
69     {
70         return textField.getText();
71     }
72     
73     public void setAsText(String JavaDoc value)
74     {
75         initialValue= value;
76         textField.setText(value);
77     }
78     
79     public Object JavaDoc getValue()
80     {
81         return getAsText();
82     }
83     
84     public void setValue(Object JavaDoc value)
85     {
86         if (value instanceof String JavaDoc) setAsText((String JavaDoc)value);
87         else throw new IllegalArgumentException JavaDoc();
88     }
89     
90     /* (non-Javadoc)
91      * @see java.beans.PropertyEditor#getCustomEditor()
92      */

93     public Component JavaDoc getCustomEditor()
94     {
95         return textField;
96     }
97
98     /* (non-Javadoc)
99      * Avoid needlessly firing PropertyChanged events.
100      */

101     public void firePropertyChange()
102     {
103         String JavaDoc newValue= getAsText();
104
105         if (initialValue.equals(newValue)) return;
106         initialValue= newValue;
107
108         super.firePropertyChange();
109     }
110
111     /* (non-Javadoc)
112      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
113      */

114     public void actionPerformed(ActionEvent JavaDoc e)
115     {
116         firePropertyChange();
117     }
118     
119     /* (non-Javadoc)
120      * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
121      */

122     public void focusGained(FocusEvent JavaDoc e)
123     {
124     }
125
126     /* (non-Javadoc)
127      * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
128      */

129     public void focusLost(FocusEvent JavaDoc e)
130     {
131         firePropertyChange();
132     }
133     
134     public static class Test extends junit.framework.TestCase
135     {
136         public Test(String JavaDoc name)
137         {
138             super(name);
139         }
140
141         private void testSetGet(ComboStringEditor e, Object JavaDoc value) throws Exception JavaDoc
142         {
143             e.setValue(value);
144             assertEquals(value, e.getValue());
145         }
146         private void testSetGetAsText(ComboStringEditor e, String JavaDoc text) throws Exception JavaDoc
147         {
148             e.setAsText(text);
149             assertEquals(text, e.getAsText());
150         }
151         public void testSetGet() throws Exception JavaDoc
152         {
153             ComboStringEditor e= new ComboStringEditor();
154                 
155             testSetGet(e, "any string");
156             testSetGet(e, "");
157             testSetGet(e, "${var}");
158         }
159         public void testSetGetAsText() throws Exception JavaDoc
160         {
161             ComboStringEditor e= new ComboStringEditor();
162                 
163             testSetGetAsText(e, "any string");
164             testSetGetAsText(e, "");
165             testSetGetAsText(e, "${var}");
166         }
167     }
168 }
Popular Tags