1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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; 22 import java.awt.FlowLayout; 23 import java.awt.event.ActionEvent; 24 import java.awt.event.ActionListener; 25 import java.io.IOException; 26 import java.util.HashMap; 27 import java.util.Iterator; 28 import java.util.List; 29 import java.util.Map; 30 31 import javax.swing.JButton; 32 import javax.swing.JDialog; 33 import javax.swing.JFrame; 34 import javax.swing.JPanel; 35 import javax.swing.event.ChangeEvent; 36 import javax.swing.event.ChangeListener; 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 public class FunctionHelper extends JDialog implements ActionListener, ChangeListener { 52 private JLabeledChoice functionList; 53 54 private ArgumentsPanel parameterPanel; 55 56 private JLabeledTextField cutPasteFunction; 57 58 private final Map functionMap = new HashMap(); 59 60 private JButton generateButton; 61 62 public FunctionHelper() { 63 super((JFrame) null, JMeterUtils.getResString("function_helper_title"), false); //$NON-NLS-1$ 64 init(); 65 } 66 67 private void init() { 68 parameterPanel = new ArgumentsPanel(JMeterUtils.getResString("function_params")); //$NON-NLS-1$ 69 initializeFunctionList(); 70 this.getContentPane().setLayout(new BorderLayout(10, 10)); 71 JPanel comboPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 72 comboPanel.add(functionList); 73 JButton helpButton = new JButton(JMeterUtils.getResString("help")); //$NON-NLS-1$ 74 helpButton.addActionListener(new HelpListener()); 75 comboPanel.add(helpButton); 76 this.getContentPane().add(comboPanel, BorderLayout.NORTH); 77 this.getContentPane().add(parameterPanel, BorderLayout.CENTER); 78 JPanel resultsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 79 cutPasteFunction = new JLabeledTextField(JMeterUtils.getResString("cut_paste_function"), 35); //$NON-NLS-1$ 80 resultsPanel.add(cutPasteFunction); 81 generateButton = new JButton(JMeterUtils.getResString("generate")); //$NON-NLS-1$ 82 generateButton.addActionListener(this); 83 resultsPanel.add(generateButton); 84 this.getContentPane().add(resultsPanel, BorderLayout.SOUTH); 85 this.pack(); 86 ComponentUtil.centerComponentInWindow(this); 87 } 88 89 private void initializeFunctionList() { 90 try { 91 List functionClasses = ClassFinder.findClassesThatExtend(JMeterUtils.getSearchPaths(), 92 new Class[] { Function.class }, true); 93 Iterator iter = functionClasses.iterator(); 94 String[] functionNames = new String[functionClasses.size()]; 95 int count = 0; 96 while (iter.hasNext()) { 97 Class cl = Class.forName((String) iter.next()); 98 functionNames[count] = ((Function) cl.newInstance()).getReferenceKey(); 99 functionMap.put(functionNames[count], cl); 100 count++; 101 } 102 functionList = new JLabeledChoice(JMeterUtils.getResString("choose_function"), functionNames); //$NON-NLS-1$ 103 functionList.addChangeListener(this); 104 } catch (IOException e) { 105 } catch (ClassNotFoundException e) { 106 } catch (InstantiationException e) { 107 } catch (IllegalAccessException e) { 108 } 109 } 110 111 public void stateChanged(ChangeEvent event) { 112 try { 113 Arguments args = new Arguments(); 114 Function function = (Function) ((Class) functionMap.get(functionList.getText())).newInstance(); 115 List argumentDesc = function.getArgumentDesc(); 116 Iterator iter = argumentDesc.iterator(); 117 while (iter.hasNext()) { 118 String help = (String) iter.next(); 119 args.addArgument(help, ""); //$NON-NLS-1$ 120 } 121 parameterPanel.configure(args); 122 parameterPanel.revalidate(); 123 getContentPane().remove(parameterPanel); 124 this.pack(); 125 getContentPane().add(parameterPanel, BorderLayout.CENTER); 126 this.pack(); 127 this.validate(); 128 this.repaint(); 129 } catch (InstantiationException e) { 130 } catch (IllegalAccessException e) { 131 } 132 } 133 134 public void actionPerformed(ActionEvent e) { 135 StringBuffer functionCall = new StringBuffer("${"); 136 functionCall.append(functionList.getText()); 137 Arguments args = (Arguments) parameterPanel.createTestElement(); 138 if (args.getArguments().size() > 0) { 139 functionCall.append("("); 140 PropertyIterator iter = args.iterator(); 141 boolean first = true; 142 while (iter.hasNext()) { 143 Argument arg = (Argument) iter.next().getObjectValue(); 144 if (!first) { 145 functionCall.append(","); 146 } 147 functionCall.append(arg.getValue()); 148 first = false; 149 } 150 functionCall.append(")"); 151 } 152 functionCall.append("}"); 153 cutPasteFunction.setText(functionCall.toString()); 154 } 155 156 private class HelpListener implements ActionListener { 157 public void actionPerformed(ActionEvent e) { 158 String[] source = new String[] { Help.HELP_FUNCTIONS, functionList.getText() }; 159 ActionEvent helpEvent = new ActionEvent(source, e.getID(), "help"); //$NON-NLS-1$ 160 ActionRouter.getInstance().actionPerformed(helpEvent); 161 } 162 } 163 }