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.protocol.java.control.gui; 20 21 import java.awt.BorderLayout; 22 23 import javax.swing.Box; 24 import javax.swing.JCheckBox; 25 import javax.swing.JLabel; 26 import javax.swing.JPanel; 27 import javax.swing.JScrollPane; 28 import javax.swing.JTextArea; 29 import javax.swing.JTextField; 30 31 import org.apache.jmeter.protocol.java.sampler.BeanShellSampler; 32 import org.apache.jmeter.samplers.gui.AbstractSamplerGui; 33 import org.apache.jmeter.testelement.TestElement; 34 import org.apache.jmeter.testelement.property.BooleanProperty; 35 import org.apache.jmeter.util.JMeterUtils; 36 37 public class BeanShellSamplerGui extends AbstractSamplerGui { 38 39 private JCheckBox resetInterpreter;// reset the bsh.Interpreter before each execution 40 41 private JTextField filename;// script file name (if present) 42 43 private JTextField parameters;// parameters to pass to script file (or script) 44 45 private JTextArea scriptField;// script area 46 47 public BeanShellSamplerGui() { 48 init(); 49 } 50 51 public void configure(TestElement element) { 52 scriptField.setText(element.getPropertyAsString(BeanShellSampler.SCRIPT)); 53 filename.setText(element.getPropertyAsString(BeanShellSampler.FILENAME)); 54 parameters.setText(element.getPropertyAsString(BeanShellSampler.PARAMETERS)); 55 resetInterpreter.setSelected(element.getPropertyAsBoolean(BeanShellSampler.RESET_INTERPRETER)); 56 super.configure(element); 57 } 58 59 public TestElement createTestElement() { 60 BeanShellSampler sampler = new BeanShellSampler(); 61 modifyTestElement(sampler); 62 return sampler; 63 } 64 65 /** 66 * Modifies a given TestElement to mirror the data in the gui components. 67 * 68 * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement) 69 */ 70 public void modifyTestElement(TestElement te) { 71 te.clear(); 72 this.configureTestElement(te); 73 te.setProperty(BeanShellSampler.SCRIPT, scriptField.getText()); 74 te.setProperty(BeanShellSampler.FILENAME, filename.getText()); 75 te.setProperty(BeanShellSampler.PARAMETERS, parameters.getText()); 76 te.setProperty(new BooleanProperty(BeanShellSampler.RESET_INTERPRETER, resetInterpreter.isSelected())); 77 } 78 79 /** 80 * Implements JMeterGUIComponent.clearGui 81 */ 82 public void clearGui() { 83 super.clearGui(); 84 85 filename.setText(""); //$NON-NLS-1$ 86 parameters.setText(""); //$NON-NLS-1$ 87 scriptField.setText(""); //$NON-NLS-1$ 88 resetInterpreter.setSelected(false); 89 } 90 91 public String getLabelResource() { 92 return "bsh_sampler_title"; // $NON-NLS-1$ 93 } 94 95 private JPanel createFilenamePanel()// TODO ought to be a FileChooser ... 96 { 97 JLabel label = new JLabel(JMeterUtils.getResString("bsh_script_file")); // $NON-NLS-1$ 98 99 filename = new JTextField(10); 100 filename.setName(BeanShellSampler.FILENAME); 101 label.setLabelFor(filename); 102 103 JPanel filenamePanel = new JPanel(new BorderLayout(5, 0)); 104 filenamePanel.add(label, BorderLayout.WEST); 105 filenamePanel.add(filename, BorderLayout.CENTER); 106 return filenamePanel; 107 } 108 109 private JPanel createParameterPanel() { 110 JLabel label = new JLabel(JMeterUtils.getResString("bsh_script_parameters")); // $NON-NLS-1$ 111 112 parameters = new JTextField(10); 113 parameters.setName(BeanShellSampler.PARAMETERS); 114 label.setLabelFor(parameters); 115 116 JPanel parameterPanel = new JPanel(new BorderLayout(5, 0)); 117 parameterPanel.add(label, BorderLayout.WEST); 118 parameterPanel.add(parameters, BorderLayout.CENTER); 119 return parameterPanel; 120 } 121 122 private JPanel createResetPanel() { 123 resetInterpreter = new JCheckBox(JMeterUtils.getResString("bsh_script_reset_interpreter")); // $NON-NLS-1$ 124 resetInterpreter.setName(BeanShellSampler.PARAMETERS); 125 126 JPanel resetInterpreterPanel = new JPanel(new BorderLayout()); 127 resetInterpreterPanel.add(resetInterpreter, BorderLayout.WEST); 128 return resetInterpreterPanel; 129 } 130 131 private void init() { 132 setLayout(new BorderLayout(0, 5)); 133 setBorder(makeBorder()); 134 135 Box box = Box.createVerticalBox(); 136 box.add(makeTitlePanel()); 137 box.add(createResetPanel()); 138 box.add(createParameterPanel()); 139 box.add(createFilenamePanel()); 140 add(box, BorderLayout.NORTH); 141 142 JPanel panel = createScriptPanel(); 143 add(panel, BorderLayout.CENTER); 144 // Don't let the input field shrink too much 145 add(Box.createVerticalStrut(panel.getPreferredSize().height), BorderLayout.WEST); 146 } 147 148 private JPanel createScriptPanel() { 149 scriptField = new JTextArea(); 150 scriptField.setRows(4); 151 scriptField.setLineWrap(true); 152 scriptField.setWrapStyleWord(true); 153 154 JLabel label = new JLabel(JMeterUtils.getResString("bsh_script")); // $NON-NLS-1$ 155 label.setLabelFor(scriptField); 156 157 JPanel panel = new JPanel(new BorderLayout()); 158 panel.add(label, BorderLayout.NORTH); 159 panel.add(new JScrollPane(scriptField), BorderLayout.CENTER); 160 161 JTextArea explain = new JTextArea(JMeterUtils.getResString("bsh_script_variables")); //$NON-NLS-1$ 162 explain.setLineWrap(true); 163 explain.setEditable(false); 164 explain.setBackground(this.getBackground()); 165 panel.add(explain, BorderLayout.SOUTH); 166 167 return panel; 168 } 169 }