KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testbeans/gui/FileEditor.java,v 1.8.2.1 2005/03/05 01:19:12 sebb 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.BorderLayout JavaDoc;
20 import java.awt.Component JavaDoc;
21 import java.awt.Graphics JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyEditor JavaDoc;
27 import java.beans.PropertyEditorSupport JavaDoc;
28 import java.io.File JavaDoc;
29
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.JFileChooser JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33
34 import org.apache.jmeter.gui.util.FileDialoger;
35 import org.apache.jorphan.logging.LoggingManager;
36 import org.apache.log.Logger;
37
38 /**
39  * A property editor for File properties.
40  * <p>
41  * Note that it never gives out File objects, but always Strings. This is
42  * because JMeter is now too dumb to handle File objects (there's no
43  * FileProperty).
44  *
45  * @author <a HREF="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
46  * @version $Revision: 1.8.2.1 $ updated on $Date: 2005/03/05 01:19:12 $
47  */

48 public class FileEditor implements PropertyEditor JavaDoc, ActionListener JavaDoc
49 {
50     private static final Logger log= LoggingManager.getLoggerForClass();
51
52     /**
53      * The editor's panel.
54      */

55     private JPanel JavaDoc panel;
56
57     /**
58      * The editor handling the text field inside:
59      */

60     PropertyEditor JavaDoc editor;
61
62     public FileEditor()
63     {
64         // Create a button to trigger the file chooser:
65
JButton JavaDoc button= new JButton JavaDoc("Browse...");
66         button.addActionListener(this);
67
68         // Get a WrapperEditor to provide the field or combo -- we'll delegate
69
// most methods to it:
70
editor= new WrapperEditor(
71             this,
72             new SimpleFileEditor(),
73             new ComboStringEditor(),
74             true, true, true, null);
75
76         // Create a panel containing the combo and the button:
77
panel= new JPanel JavaDoc(new BorderLayout JavaDoc(5,0));
78         panel.add(editor.getCustomEditor(), BorderLayout.CENTER);
79         panel.add(button, BorderLayout.EAST);//JDK1.4: was LINE_END
80
}
81
82     /* (non-Javadoc)
83      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
84      */

85     public void actionPerformed(ActionEvent JavaDoc e)
86     {
87         JFileChooser JavaDoc chooser = FileDialoger.promptToOpenFile();
88
89         File JavaDoc file = chooser.getSelectedFile();
90
91         setValue(file.getPath());
92     }
93
94     /**
95      * @param listener
96      */

97     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener)
98     {
99         editor.addPropertyChangeListener(listener);
100     }
101
102     /**
103      * @return the text
104      */

105     public String JavaDoc getAsText()
106     {
107         return editor.getAsText();
108     }
109
110     /**
111      * @return custom editor panel
112      */

113     public Component JavaDoc getCustomEditor()
114     {
115         return panel;
116     }
117
118     /**
119      * @return the Java initialisation string
120      */

121     public String JavaDoc getJavaInitializationString()
122     {
123         return editor.getJavaInitializationString();
124     }
125
126     /**
127      * @return the editor tags
128      */

129     public String JavaDoc[] getTags()
130     {
131         return editor.getTags();
132     }
133
134     /**
135      * @return the value
136      */

137     public Object JavaDoc getValue()
138     {
139         return editor.getValue();
140     }
141
142     /**
143      * @return true if the editor is paintable
144      */

145     public boolean isPaintable()
146     {
147         return editor.isPaintable();
148     }
149
150     /**
151      * @param gfx
152      * @param box
153      */

154     public void paintValue(Graphics JavaDoc gfx, Rectangle JavaDoc box)
155     {
156         editor.paintValue(gfx, box);
157     }
158
159     /**
160      * @param listener
161      */

162     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener)
163     {
164         editor.removePropertyChangeListener(listener);
165     }
166
167     /**
168      * @param text
169      * @throws java.lang.IllegalArgumentException
170      */

171     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc
172     {
173         editor.setAsText(text);
174     }
175
176     /**
177      * @param value
178      */

179     public void setValue(Object JavaDoc value)
180     {
181         editor.setValue(value);
182     }
183
184     /**
185      * @return true if supports a custom editor
186      */

187     public boolean supportsCustomEditor()
188     {
189         return editor.supportsCustomEditor();
190     }
191
192     private static class SimpleFileEditor extends PropertyEditorSupport JavaDoc
193     {
194         /* (non-Javadoc)
195          * @see java.beans.PropertyEditor#getAsText()
196          */

197         public String JavaDoc getAsText()
198         {
199             return ((File JavaDoc)super.getValue()).getPath();
200         }
201
202         /* (non-Javadoc)
203          * @see java.beans.PropertyEditor#setAsText(java.lang.String)
204          */

205         public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc
206         {
207             super.setValue(new File JavaDoc(text));
208         }
209         
210         /*
211          * Oh, I forgot: JMeter doesn't support File properties yet. Need to work
212          * on this as a String :-(
213          */

214         public Object JavaDoc getValue()
215         {
216             return getAsText(); // should be super.getValue();
217
}
218         
219         /**
220          * Tsk, tsk... I need to handle Strings when setting too.
221          */

222         public void setValue(Object JavaDoc file)
223         {
224             setAsText((String JavaDoc)file);
225         }
226     }
227 }
Popular Tags