KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > functions > gui > FunctionHelper


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/functions/gui/FunctionHelper.java,v 1.11 2004/02/14 03:34:30 sebb Exp $
2
/*
3  * Copyright 2001-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.functions.gui;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.FlowLayout JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JDialog JavaDoc;
33 import javax.swing.JFrame JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.event.ChangeEvent JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37
38 import org.apache.jmeter.config.Argument;
39 import org.apache.jmeter.config.Arguments;
40 import org.apache.jmeter.config.gui.ArgumentsPanel;
41 import org.apache.jmeter.functions.Function;
42 import org.apache.jmeter.gui.action.ActionRouter;
43 import org.apache.jmeter.gui.action.Help;
44 import org.apache.jmeter.testelement.property.PropertyIterator;
45 import org.apache.jmeter.util.JMeterUtils;
46 import org.apache.jorphan.gui.ComponentUtil;
47 import org.apache.jorphan.gui.JLabeledChoice;
48 import org.apache.jorphan.gui.JLabeledTextField;
49 import org.apache.jorphan.reflect.ClassFinder;
50
51 /**
52  * @version $Revision: 1.11 $
53  */

54 public class FunctionHelper
55     extends JDialog JavaDoc
56     implements ActionListener JavaDoc, ChangeListener JavaDoc
57 {
58     JLabeledChoice functionList;
59     ArgumentsPanel parameterPanel;
60     JLabeledTextField cutPasteFunction;
61     private Map JavaDoc functionMap = new HashMap JavaDoc();
62     JButton JavaDoc generateButton;
63
64     public FunctionHelper()
65     {
66         super(
67             (JFrame JavaDoc) null,
68             JMeterUtils.getResString("function_helper_title"),
69             false);
70         init();
71     }
72
73     private void init()
74     {
75         parameterPanel =
76             new ArgumentsPanel(JMeterUtils.getResString("function_params"));
77         initializeFunctionList();
78         this.getContentPane().setLayout(new BorderLayout JavaDoc(10, 10));
79         JPanel JavaDoc comboPanel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.CENTER));
80         comboPanel.add(functionList);
81         JButton JavaDoc helpButton = new JButton JavaDoc(JMeterUtils.getResString("help"));
82         helpButton.addActionListener(new HelpListener());
83         comboPanel.add(helpButton);
84         this.getContentPane().add(comboPanel, BorderLayout.NORTH);
85         this.getContentPane().add(parameterPanel, BorderLayout.CENTER);
86         JPanel JavaDoc resultsPanel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.CENTER));
87         cutPasteFunction =
88             new JLabeledTextField(
89                 JMeterUtils.getResString("cut_paste_function"),
90                 35);
91         resultsPanel.add(cutPasteFunction);
92         generateButton = new JButton JavaDoc(JMeterUtils.getResString("generate"));
93         generateButton.addActionListener(this);
94         resultsPanel.add(generateButton);
95         this.getContentPane().add(resultsPanel, BorderLayout.SOUTH);
96         this.pack();
97         ComponentUtil.centerComponentInWindow(this);
98     }
99
100     private void initializeFunctionList()
101     {
102         try
103         {
104             List JavaDoc functionClasses =
105                 ClassFinder.findClassesThatExtend(
106                     JMeterUtils.getSearchPaths(),
107                     new Class JavaDoc[] { Function.class },
108                     true);
109             Iterator JavaDoc iter = functionClasses.iterator();
110             String JavaDoc[] functionNames = new String JavaDoc[functionClasses.size()];
111             int count = 0;
112             while (iter.hasNext())
113             {
114                 Class JavaDoc cl = Class.forName((String JavaDoc) iter.next());
115                 functionNames[count] =
116                     ((Function) cl.newInstance()).getReferenceKey();
117                 functionMap.put(functionNames[count], cl);
118                 count++;
119             }
120             functionList =
121                 new JLabeledChoice(
122                     JMeterUtils.getResString("choose_function"),
123                     functionNames);
124             functionList.addChangeListener(this);
125         }
126         catch (IOException JavaDoc e)
127         {
128         }
129         catch (ClassNotFoundException JavaDoc e)
130         {
131         }
132         catch (InstantiationException JavaDoc e)
133         {
134         }
135         catch (IllegalAccessException JavaDoc e)
136         {
137         }
138     }
139
140     public void stateChanged(ChangeEvent JavaDoc event)
141     {
142         try
143         {
144             Arguments args = new Arguments();
145             Function function =
146                 (Function) ((Class JavaDoc) functionMap.get(functionList.getText()))
147                     .newInstance();
148             List JavaDoc argumentDesc = function.getArgumentDesc();
149             Iterator JavaDoc iter = argumentDesc.iterator();
150             while (iter.hasNext())
151             {
152                 String JavaDoc help = (String JavaDoc) iter.next();
153                 args.addArgument(help, "");
154             }
155             parameterPanel.configure(args);
156             parameterPanel.revalidate();
157             getContentPane().remove(parameterPanel);
158             this.pack();
159             getContentPane().add(parameterPanel, BorderLayout.CENTER);
160             this.pack();
161             this.validate();
162             this.repaint();
163         }
164         catch (InstantiationException JavaDoc e)
165         {
166         }
167         catch (IllegalAccessException JavaDoc e)
168         {
169         }
170     }
171
172     public void actionPerformed(ActionEvent JavaDoc e)
173     {
174         StringBuffer JavaDoc functionCall = new StringBuffer JavaDoc("${");
175         functionCall.append(functionList.getText());
176         Arguments args = (Arguments) parameterPanel.createTestElement();
177         if (args.getArguments().size() > 0)
178         {
179             functionCall.append("(");
180             PropertyIterator iter = args.iterator();
181             boolean first = true;
182             while (iter.hasNext())
183             {
184                 Argument arg = (Argument) iter.next().getObjectValue();
185                 if (!first)
186                 {
187                     functionCall.append(",");
188                 }
189                 functionCall.append((String JavaDoc) arg.getValue());
190                 first = false;
191             }
192             functionCall.append(")");
193         }
194         functionCall.append("}");
195         cutPasteFunction.setText(functionCall.toString());
196     }
197
198     private class HelpListener implements ActionListener JavaDoc
199     {
200         public void actionPerformed(ActionEvent JavaDoc e)
201         {
202             String JavaDoc[] source =
203                 new String JavaDoc[] { Help.HELP_FUNCTIONS,
204                     functionList.getText()};
205             ActionEvent JavaDoc helpEvent = new ActionEvent JavaDoc(source, e.getID(), "help");
206             ActionRouter.getInstance().actionPerformed(helpEvent);
207         }
208     }
209 }
210
Popular Tags