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