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 import java.util.Arrays; 23 import java.util.Properties; 24 import java.util.Set; 25 26 import javax.swing.Box; 27 import javax.swing.JComboBox; 28 import javax.swing.JLabel; 29 import javax.swing.JPanel; 30 import javax.swing.JScrollPane; 31 import javax.swing.JTextArea; 32 import javax.swing.JTextField; 33 34 import org.apache.jmeter.protocol.java.sampler.BSFSampler; 35 import org.apache.jmeter.samplers.gui.AbstractSamplerGui; 36 import org.apache.jmeter.testelement.TestElement; 37 import org.apache.jmeter.util.JMeterUtils; 38 39 public class BSFSamplerGui extends AbstractSamplerGui { 40 private JTextArea scriptField; 41 42 private JComboBox langField;// Language 43 44 private JTextField filename;// script file name (if present) 45 46 private JTextField parameters;// parameters to pass to script file (or script) 47 48 public BSFSamplerGui() { 49 init(); 50 } 51 52 public void configure(TestElement element) { 53 super.configure(element); 54 BSFSampler sampler = (BSFSampler) element; 55 scriptField.setText(sampler.getScript()); 56 langField.setSelectedItem(sampler.getScriptLanguage()); 57 filename.setText(sampler.getFilename()); 58 parameters.setText(sampler.getParameters()); 59 } 60 61 public TestElement createTestElement() { 62 BSFSampler sampler = new BSFSampler(); 63 modifyTestElement(sampler); 64 return sampler; 65 } 66 67 /** 68 * Modifies a given TestElement to mirror the data in the gui components. 69 * 70 * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement) 71 */ 72 public void modifyTestElement(TestElement te) { 73 te.clear(); 74 this.configureTestElement(te); 75 BSFSampler sampler = (BSFSampler) te; 76 sampler.setFilename(filename.getText()); 77 sampler.setScriptLanguage((String) langField.getSelectedItem()); 78 sampler.setParameters(parameters.getText()); 79 sampler.setScript(scriptField.getText()); 80 } 81 82 /** 83 * Implements JMeterGUIComponent.clearGui 84 */ 85 public void clearGui() { 86 super.clearGui(); 87 88 scriptField.setText(""); //$NON-NLS-1$ 89 langField.setSelectedIndex(0); 90 filename.setText(""); //$NON-NLS-1$ 91 parameters.setText(""); //$NON-NLS-1$ 92 } 93 94 public String getLabelResource() { 95 return "bsf_sampler_title"; // $NON-NLS-1$ 96 } 97 98 private void init() { 99 setLayout(new BorderLayout(0, 5)); 100 setBorder(makeBorder()); 101 102 Box box = Box.createVerticalBox(); 103 box.add(makeTitlePanel()); 104 box.add(createLanguagePanel()); 105 box.add(createFilenamePanel()); 106 box.add(createParameterPanel()); 107 add(box, BorderLayout.NORTH); 108 109 JPanel panel = createScriptPanel(); 110 add(panel, BorderLayout.CENTER); 111 // Don't let the input field shrink too much 112 add(Box.createVerticalStrut(panel.getPreferredSize().height), BorderLayout.WEST); 113 } 114 115 private JPanel createParameterPanel() { 116 JLabel label = new JLabel(JMeterUtils.getResString("bsf_script_parameters")); // $NON-NLS-1$ 117 118 parameters = new JTextField(10); 119 label.setLabelFor(parameters); 120 121 JPanel parameterPanel = new JPanel(new BorderLayout(5, 0)); 122 parameterPanel.add(label, BorderLayout.WEST); 123 parameterPanel.add(parameters, BorderLayout.CENTER); 124 return parameterPanel; 125 } 126 127 private JPanel createFilenamePanel()// TODO ought to be a FileChooser ... 128 { 129 JLabel label = new JLabel(JMeterUtils.getResString("bsf_script_file")); // $NON-NLS-1$ 130 131 filename = new JTextField(10); 132 label.setLabelFor(filename); 133 134 JPanel filenamePanel = new JPanel(new BorderLayout(5, 0)); 135 filenamePanel.add(label, BorderLayout.WEST); 136 filenamePanel.add(filename, BorderLayout.CENTER); 137 return filenamePanel; 138 } 139 140 private JPanel createLanguagePanel() { 141 JLabel label = new JLabel(JMeterUtils.getResString("bsf_script_language")); // $NON-NLS-1$ 142 143 Properties p = JMeterUtils.loadProperties("org/apache/bsf/Languages.properties"); // $NON-NLS-1$ 144 // We have added Jexl in BSFSampler. 145 p.put("jexl", ""); // $NON-NLS-1$ 146 Set keySet = p.keySet(); 147 // TODO - perhaps weed out ones which don't exist? 148 String [] items = (String[]) keySet.toArray(new String[]{}); 149 Arrays.sort(items); 150 151 langField = new JComboBox(items); 152 langField.setEditable(true); 153 label.setLabelFor(langField); 154 155 JPanel langPanel = new JPanel(new BorderLayout(5, 0)); 156 langPanel.add(label, BorderLayout.WEST); 157 langPanel.add(langField, BorderLayout.CENTER); 158 159 return langPanel; 160 } 161 162 private JPanel createScriptPanel() { 163 scriptField = new JTextArea(); 164 scriptField.setRows(4); 165 scriptField.setLineWrap(true); 166 scriptField.setWrapStyleWord(true); 167 168 JLabel label = new JLabel(JMeterUtils.getResString("bsf_script")); // $NON-NLS-1$ 169 label.setLabelFor(scriptField); 170 171 JPanel panel = new JPanel(new BorderLayout()); 172 panel.add(label, BorderLayout.NORTH); 173 panel.add(new JScrollPane(scriptField), BorderLayout.CENTER); 174 return panel; 175 } 176 }