KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > java > config > gui > JavaConfigGui


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/java/org/apache/jmeter/protocol/java/config/gui/JavaConfigGui.java,v 1.18.2.1 2004/10/11 00:54:19 sebb Exp $
2
/*
3  * Copyright 2002-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 */

18
19 package org.apache.jmeter.protocol.java.config.gui;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.swing.JComboBox JavaDoc;
28 import javax.swing.JLabel JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30
31 import org.apache.jmeter.config.Argument;
32 import org.apache.jmeter.config.Arguments;
33 import org.apache.jmeter.config.gui.AbstractConfigGui;
34 import org.apache.jmeter.config.gui.ArgumentsPanel;
35 import org.apache.jmeter.gui.util.HorizontalPanel;
36 import org.apache.jmeter.protocol.java.config.JavaConfig;
37 import org.apache.jmeter.protocol.java.sampler.JavaSampler;
38 import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
39 import org.apache.jmeter.testelement.TestElement;
40 import org.apache.jmeter.testelement.property.PropertyIterator;
41 import org.apache.jmeter.util.JMeterUtils;
42 import org.apache.jorphan.reflect.ClassFinder;
43 import org.apache.jorphan.logging.LoggingManager;
44 import org.apache.log.Logger;
45
46
47 /**
48  * The <code>JavaConfigGui</code> class provides the user interface for
49  * the {@link JavaConfig} object.
50  *
51  * @version $Revision: 1.18.2.1 $ on $Date: 2004/10/11 00:54:19 $
52  */

53 public class JavaConfigGui extends AbstractConfigGui implements ActionListener JavaDoc
54 {
55     /** Logging */
56     private static transient Logger log = LoggingManager.getLoggerForClass();
57
58     /** The name of the classnameCombo JComboBox */
59     private static final String JavaDoc CLASSNAMECOMBO = "classnamecombo";
60
61     /** A combo box allowing the user to choose a test class. */
62     private JComboBox JavaDoc classnameCombo;
63
64     /**
65      * Indicates whether or not the name of this component should be displayed
66      * as part of the GUI. If true, this is a standalone component. If false,
67      * it is embedded in some other component.
68      */

69     private boolean displayName = true;
70     
71     /** A panel allowing the user to set arguments for this test. */
72     private ArgumentsPanel argsPanel;
73
74     /**
75      * Create a new JavaConfigGui as a standalone component.
76      */

77     public JavaConfigGui()
78     {
79         this(true);
80     }
81
82     /**
83      * Create a new JavaConfigGui as either a standalone or an embedded
84      * component.
85      *
86      * @param displayNameField tells whether the component name should be
87      * displayed with the GUI. If true, this is a
88      * standalone component. If false, this component
89      * is embedded in some other component.
90      */

91     public JavaConfigGui(boolean displayNameField)
92     {
93         this.displayName = displayNameField;
94         init();
95     }
96
97     public String JavaDoc getLabelResource()
98     {
99         return "java_request_defaults";
100     }
101
102     /**
103      * Initialize the GUI components and layout.
104      */

105     protected void init()
106     {
107         setLayout(new BorderLayout JavaDoc(0, 5));
108         
109         if (displayName)
110         {
111             setBorder(makeBorder());
112             add(makeTitlePanel(), BorderLayout.NORTH);
113         }
114         
115         JPanel JavaDoc classnameRequestPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(0, 5));
116         classnameRequestPanel.add(createClassnamePanel(), BorderLayout.NORTH);
117         classnameRequestPanel.add(createParameterPanel(), BorderLayout.CENTER);
118
119         add(classnameRequestPanel, BorderLayout.CENTER);
120     }
121
122     /**
123      * Create a panel with GUI components allowing the user to select a test
124      * class.
125      *
126      * @return a panel containing the relevant components
127      */

128     private JPanel JavaDoc createClassnamePanel()
129     {
130         List JavaDoc possibleClasses = null;
131
132         try
133         {
134             // Find all the classes which implement the JavaSamplerClient
135
// interface.
136
possibleClasses =
137                 ClassFinder.findClassesThatExtend(
138                     JMeterUtils.getSearchPaths(),
139                     new Class JavaDoc[] { JavaSamplerClient.class });
140
141             // Remove the JavaConfig class from the list since it only
142
// implements the interface for error conditions.
143

144             possibleClasses.remove(
145                 JavaSampler.class.getName() + "$ErrorSamplerClient");
146         }
147         catch (Exception JavaDoc e)
148         {
149             log.debug("Exception getting interfaces.", e);
150         }
151
152         JLabel JavaDoc label =
153             new JLabel JavaDoc(JMeterUtils.getResString("protocol_java_classname"));
154
155         classnameCombo = new JComboBox JavaDoc(possibleClasses.toArray());
156         classnameCombo.addActionListener(this);
157         classnameCombo.setName(CLASSNAMECOMBO);
158         classnameCombo.setEditable(false);
159         label.setLabelFor(classnameCombo);
160         
161         HorizontalPanel panel = new HorizontalPanel();
162         panel.add(label);
163         panel.add(classnameCombo);
164
165         return panel;
166     }
167
168     /**
169      * Handle action events for this component. This method currently handles
170      * events for the classname combo box.
171      *
172      * @param evt the ActionEvent to be handled
173      */

174     public void actionPerformed(ActionEvent JavaDoc evt)
175     {
176         if (evt.getSource() == classnameCombo)
177         {
178             String JavaDoc className =
179                 ((String JavaDoc) classnameCombo.getSelectedItem()).trim();
180             try
181             {
182                 JavaSamplerClient client = (JavaSamplerClient) Class.forName(
183                             className,
184                             true,
185                             Thread.currentThread().getContextClassLoader()
186                         ).newInstance();
187
188                 Arguments currArgs = new Arguments();
189                 argsPanel.modifyTestElement(currArgs);
190                 Map JavaDoc currArgsMap = currArgs.getArgumentsAsMap();
191
192                 Arguments newArgs = new Arguments();
193                 Arguments testParams = null;
194                 try
195                 {
196                     testParams = client.getDefaultParameters();
197                 }
198                 catch (AbstractMethodError JavaDoc e)
199                 {
200                     log.warn ("JavaSamplerClient doesn't implement "
201                             + "getDefaultParameters. Default parameters won't "
202                             + "be shown. Please update your client class: "
203                             + className);
204                 }
205                 
206                 if (testParams != null)
207                 {
208                     PropertyIterator i = testParams.getArguments().iterator();
209                     while (i.hasNext())
210                     {
211                         Argument arg = (Argument) i.next().getObjectValue();
212                         String JavaDoc name = arg.getName();
213                         String JavaDoc value = arg.getValue();
214
215                         // If a user has set parameters in one test, and then
216
// selects a different test which supports the same
217
// parameters, those parameters should have the same
218
// values that they did in the original test.
219
if (currArgsMap.containsKey(name))
220                         {
221                             String JavaDoc newVal = (String JavaDoc) currArgsMap.get(name);
222                             if (newVal != null
223                                     && newVal.length() > 0)
224                             {
225                                 value = newVal;
226                             }
227                         }
228                         newArgs.addArgument(name, value);
229                     }
230                 }
231
232                 argsPanel.configure(newArgs);
233             }
234             catch (Exception JavaDoc e)
235             {
236                 log.error("Error getting argument list for " + className, e);
237             }
238         }
239     }
240
241     /**
242      * Create a panel containing components allowing the user to provide
243      * arguments to be passed to the test class instance.
244      *
245      * @return a panel containing the relevant components
246      */

247     private JPanel JavaDoc createParameterPanel()
248     {
249         argsPanel = new ArgumentsPanel(JMeterUtils.getResString("paramtable"));
250         return argsPanel;
251     }
252
253     /* Overrides AbstractJMeterGuiComponent.configure(TestElement) */
254     public void configure(TestElement config)
255     {
256         super.configure(config);
257         
258         argsPanel.configure(
259             (Arguments) config
260                 .getProperty(JavaSampler.ARGUMENTS)
261                 .getObjectValue());
262                 
263         classnameCombo.setSelectedItem(
264             config.getPropertyAsString(JavaSampler.CLASSNAME));
265     }
266
267     /* Implements JMeterGUIComponent.createTestElement() */
268     public TestElement createTestElement()
269     {
270         JavaConfig config = new JavaConfig();
271         modifyTestElement(config);
272         return config;
273     }
274
275     /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
276     public void modifyTestElement(TestElement config)
277     {
278         configureTestElement(config);
279         ((JavaConfig) config).setArguments(
280             (Arguments) argsPanel.createTestElement());
281         ((JavaConfig) config).setClassname(
282             classnameCombo.getSelectedItem().toString());
283     }
284 }
285
Popular Tags