1 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 54 public class FunctionHelper 55 extends JDialog 56 implements ActionListener , ChangeListener 57 { 58 JLabeledChoice functionList; 59 ArgumentsPanel parameterPanel; 60 JLabeledTextField cutPasteFunction; 61 private Map functionMap = new HashMap (); 62 JButton generateButton; 63 64 public FunctionHelper() 65 { 66 super( 67 (JFrame ) 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 (10, 10)); 79 JPanel comboPanel = new JPanel (new FlowLayout (FlowLayout.CENTER)); 80 comboPanel.add(functionList); 81 JButton helpButton = new JButton (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 resultsPanel = new JPanel (new FlowLayout (FlowLayout.CENTER)); 87 cutPasteFunction = 88 new JLabeledTextField( 89 JMeterUtils.getResString("cut_paste_function"), 90 35); 91 resultsPanel.add(cutPasteFunction); 92 generateButton = new JButton (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 functionClasses = 105 ClassFinder.findClassesThatExtend( 106 JMeterUtils.getSearchPaths(), 107 new Class [] { Function.class }, 108 true); 109 Iterator iter = functionClasses.iterator(); 110 String [] functionNames = new String [functionClasses.size()]; 111 int count = 0; 112 while (iter.hasNext()) 113 { 114 Class cl = Class.forName((String ) 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 e) 127 { 128 } 129 catch (ClassNotFoundException e) 130 { 131 } 132 catch (InstantiationException e) 133 { 134 } 135 catch (IllegalAccessException e) 136 { 137 } 138 } 139 140 public void stateChanged(ChangeEvent event) 141 { 142 try 143 { 144 Arguments args = new Arguments(); 145 Function function = 146 (Function) ((Class ) functionMap.get(functionList.getText())) 147 .newInstance(); 148 List argumentDesc = function.getArgumentDesc(); 149 Iterator iter = argumentDesc.iterator(); 150 while (iter.hasNext()) 151 { 152 String help = (String ) 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 e) 165 { 166 } 167 catch (IllegalAccessException e) 168 { 169 } 170 } 171 172 public void actionPerformed(ActionEvent e) 173 { 174 StringBuffer functionCall = new StringBuffer ("${"); 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 ) 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 199 { 200 public void actionPerformed(ActionEvent e) 201 { 202 String [] source = 203 new String [] { Help.HELP_FUNCTIONS, 204 functionList.getText()}; 205 ActionEvent helpEvent = new ActionEvent (source, e.getID(), "help"); 206 ActionRouter.getInstance().actionPerformed(helpEvent); 207 } 208 } 209 } 210 | Popular Tags |