001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.java.control.gui;
020:
021: import java.awt.BorderLayout;
022: import java.util.Arrays;
023: import java.util.Properties;
024: import java.util.Set;
025:
026: import javax.swing.Box;
027: import javax.swing.JComboBox;
028: import javax.swing.JLabel;
029: import javax.swing.JPanel;
030: import javax.swing.JScrollPane;
031: import javax.swing.JTextArea;
032: import javax.swing.JTextField;
033:
034: import org.apache.jmeter.protocol.java.sampler.BSFSampler;
035: import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
036: import org.apache.jmeter.testelement.TestElement;
037: import org.apache.jmeter.util.JMeterUtils;
038:
039: public class BSFSamplerGui extends AbstractSamplerGui {
040: private JTextArea scriptField;
041:
042: private JComboBox langField;// Language
043:
044: private JTextField filename;// script file name (if present)
045:
046: private JTextField parameters;// parameters to pass to script file (or script)
047:
048: public BSFSamplerGui() {
049: init();
050: }
051:
052: public void configure(TestElement element) {
053: super .configure(element);
054: BSFSampler sampler = (BSFSampler) element;
055: scriptField.setText(sampler.getScript());
056: langField.setSelectedItem(sampler.getScriptLanguage());
057: filename.setText(sampler.getFilename());
058: parameters.setText(sampler.getParameters());
059: }
060:
061: public TestElement createTestElement() {
062: BSFSampler sampler = new BSFSampler();
063: modifyTestElement(sampler);
064: return sampler;
065: }
066:
067: /**
068: * Modifies a given TestElement to mirror the data in the gui components.
069: *
070: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
071: */
072: public void modifyTestElement(TestElement te) {
073: te.clear();
074: this .configureTestElement(te);
075: BSFSampler sampler = (BSFSampler) te;
076: sampler.setFilename(filename.getText());
077: sampler.setScriptLanguage((String) langField.getSelectedItem());
078: sampler.setParameters(parameters.getText());
079: sampler.setScript(scriptField.getText());
080: }
081:
082: /**
083: * Implements JMeterGUIComponent.clearGui
084: */
085: public void clearGui() {
086: super .clearGui();
087:
088: scriptField.setText(""); //$NON-NLS-1$
089: langField.setSelectedIndex(0);
090: filename.setText(""); //$NON-NLS-1$
091: parameters.setText(""); //$NON-NLS-1$
092: }
093:
094: public String getLabelResource() {
095: return "bsf_sampler_title"; // $NON-NLS-1$
096: }
097:
098: private void init() {
099: 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),
113: BorderLayout.WEST);
114: }
115:
116: private JPanel createParameterPanel() {
117: JLabel label = new JLabel(JMeterUtils
118: .getResString("bsf_script_parameters")); // $NON-NLS-1$
119:
120: parameters = new JTextField(10);
121: label.setLabelFor(parameters);
122:
123: JPanel parameterPanel = new JPanel(new BorderLayout(5, 0));
124: parameterPanel.add(label, BorderLayout.WEST);
125: parameterPanel.add(parameters, BorderLayout.CENTER);
126: return parameterPanel;
127: }
128:
129: private JPanel createFilenamePanel()// TODO ought to be a FileChooser ...
130: {
131: JLabel label = new JLabel(JMeterUtils
132: .getResString("bsf_script_file")); // $NON-NLS-1$
133:
134: filename = new JTextField(10);
135: label.setLabelFor(filename);
136:
137: JPanel filenamePanel = new JPanel(new BorderLayout(5, 0));
138: filenamePanel.add(label, BorderLayout.WEST);
139: filenamePanel.add(filename, BorderLayout.CENTER);
140: return filenamePanel;
141: }
142:
143: private JPanel createLanguagePanel() {
144: JLabel label = new JLabel(JMeterUtils
145: .getResString("bsf_script_language")); // $NON-NLS-1$
146:
147: Properties p = JMeterUtils
148: .loadProperties("org/apache/bsf/Languages.properties"); // $NON-NLS-1$
149: Set keySet = p.keySet();
150: String[] items = (String[]) keySet.toArray(new String[] {});
151: Arrays.sort(items);
152:
153: langField = new JComboBox(items);
154: langField.setEditable(true);
155: label.setLabelFor(langField);
156:
157: JPanel langPanel = new JPanel(new BorderLayout(5, 0));
158: langPanel.add(label, BorderLayout.WEST);
159: langPanel.add(langField, BorderLayout.CENTER);
160:
161: return langPanel;
162: }
163:
164: private JPanel createScriptPanel() {
165: scriptField = new JTextArea();
166: scriptField.setRows(4);
167: scriptField.setLineWrap(true);
168: scriptField.setWrapStyleWord(true);
169:
170: JLabel label = new JLabel(JMeterUtils
171: .getResString("bsf_script")); // $NON-NLS-1$
172: label.setLabelFor(scriptField);
173:
174: JPanel panel = new JPanel(new BorderLayout());
175: panel.add(label, BorderLayout.NORTH);
176: panel.add(new JScrollPane(scriptField), BorderLayout.CENTER);
177: return panel;
178: }
179: }
|