KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testbeans/gui/ComboStringEditor.java,v 1.3 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.ItemEvent JavaDoc;
21 import java.awt.event.ItemListener JavaDoc;
22 import java.beans.PropertyEditorSupport JavaDoc;
23
24 import javax.swing.DefaultComboBoxModel JavaDoc;
25 import javax.swing.JComboBox JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27
28 import org.apache.jmeter.util.JMeterUtils;
29 import org.apache.jorphan.logging.LoggingManager;
30 import org.apache.log.Logger;
31
32 /**
33  * This class implements a property editor for possibly null String properties
34  * that supports custom editing (i.e.: provides a GUI component) based
35  * on a combo box.
36  * <p>
37  * The provided GUI is a combo box with:
38  * <ul>
39  * <li>An option for "undefined" (corresponding to the null value), unless
40  * the <b>noUndefined</b> property is set.
41  * <li>An option for each value in the <b>tags</b> property.
42  * <li>The possibility to write your own value, unless the <b>noEdit</b>
43  * property is set.
44  * </ul>
45  *
46  * @author <a HREF="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
47  * @version $Revision: 1.3 $ updated on $Date: 2004/02/10 21:24:01 $
48  */

49 class ComboStringEditor extends PropertyEditorSupport JavaDoc
50     implements ItemListener JavaDoc
51 {
52     protected static Logger log= LoggingManager.getLoggerForClass();
53
54     /**
55      * The list of options to be offered by this editor.
56      */

57     private String JavaDoc[] tags= new String JavaDoc[0];
58
59     /**
60      * True iif the editor should not accept (nor produce) a null value.
61      */

62     private boolean noUndefined= false;
63
64     /**
65      * True iif the editor should not accept (nor produce) any non-null
66      * values different from the provided tags.
67      */

68     private boolean noEdit= false;
69
70     /**
71      * The edited property's default value.
72      */

73     private String JavaDoc initialEditValue;
74
75     private JComboBox JavaDoc combo;
76     private DefaultComboBoxModel JavaDoc model;
77     private boolean startingEdit= false;
78         /* True iif we're currently processing an event triggered by the user
79          * selecting the "Edit" option. Used to prevent reverting the combo
80          * to non-editable during processing of secondary events.
81          */

82
83     private static final Object JavaDoc UNDEFINED=
84         new UniqueObject(JMeterUtils.getResString("property_undefined"));
85     private static final Object JavaDoc EDIT=
86         new UniqueObject(JMeterUtils.getResString("property_edit"));
87
88     ComboStringEditor()
89     {
90         // Create the combo box we will use to edit this property:
91

92         model= new DefaultComboBoxModel JavaDoc();
93         model.addElement(UNDEFINED);
94         model.addElement(EDIT);
95
96         combo= new JComboBox JavaDoc(model);
97         combo.addItemListener(this);
98         combo.setEditable(false);
99     }
100
101     /* (non-Javadoc)
102      * @see java.beans.PropertyEditor#supportsCustomEditor()
103      */

104     public boolean supportsCustomEditor()
105     {
106         return true;
107     }
108
109     /* (non-Javadoc)
110      * @see java.beans.PropertyEditor#getCustomEditor()
111      */

112     public Component JavaDoc getCustomEditor()
113     {
114         return combo;
115     }
116
117     /* (non-Javadoc)
118      * @see java.beans.PropertyEditor#getValue()
119      */

120     public Object JavaDoc getValue()
121     {
122         return getAsText();
123     }
124     
125     /* (non-Javadoc)
126      * @see java.beans.PropertyEditor#getAsText()
127      */

128      public String JavaDoc getAsText()
129      {
130         Object JavaDoc value= combo.getSelectedItem();
131
132         if (value == UNDEFINED) return null;
133         else return (String JavaDoc)value;
134     }
135
136     /* (non-Javadoc)
137      * @see java.beans.PropertyEditor#setValue()
138      */

139     public void setValue(Object JavaDoc value)
140     {
141         setAsText((String JavaDoc)value);
142     }
143     
144     /* (non-Javadoc)
145      * @see java.beans.PropertyEditor#setAsText()
146      */

147     public void setAsText(String JavaDoc value)
148     {
149         combo.setEditable(true);
150
151         if (value == null) combo.setSelectedItem(UNDEFINED);
152         else combo.setSelectedItem(value);
153
154         if (! startingEdit && combo.getSelectedIndex() >= 0)
155         {
156             combo.setEditable(false);
157         }
158     }
159     
160     /* (non-Javadoc)
161      * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
162      */

163     public void itemStateChanged(ItemEvent JavaDoc e)
164     {
165         if (e.getStateChange() == ItemEvent.SELECTED)
166         {
167             if (e.getItem() == EDIT) {
168                 startingEdit= true;
169                 startEditing();
170                 startingEdit= false;
171             }
172             else
173             {
174                 if (!startingEdit && combo.getSelectedIndex() >= 0)
175                 {
176                     combo.setEditable(false);
177                 }
178
179                 firePropertyChange();
180             }
181         }
182     }
183
184     private void startEditing()
185     {
186         JTextComponent JavaDoc textField=
187             (JTextComponent JavaDoc)combo.getEditor().getEditorComponent();
188
189         combo.setEditable(true);
190
191         textField.requestFocus();
192
193         String JavaDoc text= initialEditValue;
194         if (initialEditValue == null) text= ""; // will revert to last valid value if invalid
195

196         combo.setSelectedItem(text);
197
198         int i= text.indexOf("${}");
199         if (i != -1) textField.setCaretPosition(i+2);
200         else textField.selectAll();
201     }
202
203     /**
204      * @return
205      */

206     public String JavaDoc getInitialEditValue()
207     {
208         return initialEditValue;
209     }
210
211     /**
212      * @return
213      */

214     public boolean getNoEdit()
215     {
216         return noEdit;
217     }
218
219     /**
220      * @return
221      */

222     public boolean getNoUndefined()
223     {
224         return noUndefined;
225     }
226
227     /**
228      * @return
229      */

230     public String JavaDoc[] getTags()
231     {
232         return tags;
233     }
234
235     /**
236      * @param object
237      */

238     public void setInitialEditValue(String JavaDoc object)
239     {
240         initialEditValue= object;
241     }
242
243     /**
244      * @param b
245      */

246     public void setNoEdit(boolean b)
247     {
248         if (noEdit == b) return;
249         noEdit= b;
250
251         if (noEdit) model.removeElement(EDIT);
252         else model.addElement(EDIT);
253     }
254
255     /**
256      * @param b
257      */

258     public void setNoUndefined(boolean b)
259     {
260         if (noUndefined == b) return;
261         noUndefined= b;
262
263         if (noUndefined) model.removeElement(UNDEFINED);
264         else model.insertElementAt(UNDEFINED, 0);
265     }
266
267     /**
268      * @param strings
269      */

270     public void setTags(String JavaDoc[] strings)
271     {
272         if (tags.equals(strings)) return;
273
274         for (int i=0; i<tags.length; i++) model.removeElement(tags[i]);
275
276         tags= strings==null ? new String JavaDoc[0] : strings;
277         
278         int b= noUndefined ? 0 : 1; // base index for tags
279
for (int i=0; i<tags.length; i++) model.insertElementAt(tags[i], b+i);
280     }
281
282     /**
283      * This is a funny hack: if you use a plain String,
284      * entering the text of the string in the editor will make the
285      * combo revert to that option -- which actually amounts to
286      * making that string 'reserved'. I preferred to avoid this by
287      * using a different type having a controlled .toString().
288      */

289     private static class UniqueObject
290     {
291         private String JavaDoc s;
292         
293         UniqueObject(String JavaDoc s)
294         {
295             this.s= s;
296         }
297         
298         public String JavaDoc toString()
299         {
300             return s;
301         }
302     }
303     
304     public static class Test extends junit.framework.TestCase
305     {
306         public Test(String JavaDoc name)
307         {
308             super(name);
309         }
310
311         private void testSetGet(ComboStringEditor e, Object JavaDoc value) throws Exception JavaDoc
312         {
313             e.setValue(value);
314             assertEquals(value, e.getValue());
315         }
316         private void testSetGetAsText(ComboStringEditor e, String JavaDoc text) throws Exception JavaDoc
317         {
318             e.setAsText(text);
319             assertEquals(text, e.getAsText());
320         }
321         public void testSetGet() throws Exception JavaDoc
322         {
323             ComboStringEditor e= new ComboStringEditor();
324                 
325             testSetGet(e, "any string");
326             testSetGet(e, "");
327             testSetGet(e, null);
328             testSetGet(e, "${var}");
329         }
330         public void testSetGetAsText() throws Exception JavaDoc
331         {
332             ComboStringEditor e= new ComboStringEditor();
333                 
334             testSetGetAsText(e, "any string");
335             testSetGetAsText(e, "");
336             testSetGetAsText(e, null);
337             testSetGetAsText(e, "${var}");
338
339             // Check "Undefined" does not become a "reserved word":
340
e.setAsText(UNDEFINED.toString());
341             assertNotNull(e.getAsText());
342         }
343     }
344 }
Popular Tags