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.extractor.gui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.awt.GridBagConstraints;
024: import java.awt.GridBagLayout;
025: import java.util.List;
026:
027: import javax.swing.BorderFactory;
028: import javax.swing.Box;
029: import javax.swing.ButtonGroup;
030: import javax.swing.JPanel;
031: import javax.swing.JRadioButton;
032:
033: import org.apache.jmeter.extractor.RegexExtractor;
034: import org.apache.jmeter.processor.gui.AbstractPostProcessorGui;
035: import org.apache.jmeter.testelement.TestElement;
036: import org.apache.jmeter.util.JMeterUtils;
037: import org.apache.jorphan.gui.JLabeledTextField;
038:
039: /**
040: * Regular Expression Extractor Post-Processor GUI
041: */
042: public class RegexExtractorGui extends AbstractPostProcessorGui {
043: private JLabeledTextField regexField;
044:
045: private JLabeledTextField templateField;
046:
047: private JLabeledTextField defaultField;
048:
049: private JLabeledTextField matchNumberField;
050:
051: private JLabeledTextField refNameField;
052:
053: private JRadioButton useBody;
054:
055: private JRadioButton useHeaders;
056:
057: private JRadioButton useURL;
058:
059: private JRadioButton useCode;
060:
061: private JRadioButton useMessage;
062:
063: private ButtonGroup group;
064:
065: public RegexExtractorGui() {
066: super ();
067: init();
068: }
069:
070: public String getLabelResource() {
071: return "regex_extractor_title"; //$NON-NLS-1$
072: }
073:
074: public void configure(TestElement el) {
075: super .configure(el);
076: if (el instanceof RegexExtractor) {
077: RegexExtractor re = (RegexExtractor) el;
078: useHeaders.setSelected(re.useHeaders());
079: useBody.setSelected(re.useBody());
080: useURL.setSelected(re.useUrl());
081: useCode.setSelected(re.useCode());
082: useMessage.setSelected(re.useMessage());
083: regexField.setText(re.getRegex());
084: templateField.setText(re.getTemplate());
085: defaultField.setText(re.getDefaultValue());
086: matchNumberField.setText(re.getMatchNumberAsString());
087: refNameField.setText(re.getRefName());
088: }
089: }
090:
091: /**
092: * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
093: */
094: public TestElement createTestElement() {
095: RegexExtractor extractor = new RegexExtractor();
096: modifyTestElement(extractor);
097: return extractor;
098: }
099:
100: /**
101: * Modifies a given TestElement to mirror the data in the gui components.
102: *
103: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
104: */
105: public void modifyTestElement(TestElement extractor) {
106: super .configureTestElement(extractor);
107: if (extractor instanceof RegexExtractor) {
108: RegexExtractor regex = (RegexExtractor) extractor;
109: regex.setUseField(group.getSelection().getActionCommand());
110: regex.setRefName(refNameField.getText());
111: regex.setRegex(regexField.getText());
112: regex.setTemplate(templateField.getText());
113: regex.setDefaultValue(defaultField.getText());
114: regex.setMatchNumber(matchNumberField.getText());
115: }
116: }
117:
118: /**
119: * Implements JMeterGUIComponent.clearGui
120: */
121: public void clearGui() {
122: super .clearGui();
123:
124: useBody.setSelected(true);
125:
126: regexField.setText(""); //$NON-NLS-1$
127: templateField.setText(""); //$NON-NLS-1$
128: defaultField.setText(""); //$NON-NLS-1$
129: refNameField.setText(""); //$NON-NLS-1$
130: matchNumberField.setText(""); //$NON-NLS-1$
131: }
132:
133: private void init() {
134: setLayout(new BorderLayout());
135: setBorder(makeBorder());
136:
137: Box box = Box.createVerticalBox();
138: box.add(makeTitlePanel());
139: box.add(makeSourcePanel());
140: add(box, BorderLayout.NORTH);
141: add(makeParameterPanel(), BorderLayout.CENTER);
142: }
143:
144: private JPanel makeSourcePanel() {
145: JPanel panel = new JPanel();
146: panel.setBorder(BorderFactory.createTitledBorder(JMeterUtils
147: .getResString("regex_source"))); //$NON-NLS-1$
148:
149: useBody = new JRadioButton(JMeterUtils
150: .getResString("regex_src_body")); //$NON-NLS-1$
151: useHeaders = new JRadioButton(JMeterUtils
152: .getResString("regex_src_hdrs")); //$NON-NLS-1$
153: useURL = new JRadioButton(JMeterUtils
154: .getResString("regex_src_url")); //$NON-NLS-1$
155: useCode = new JRadioButton(JMeterUtils
156: .getResString("assertion_code_resp")); //$NON-NLS-1$
157: useMessage = new JRadioButton(JMeterUtils
158: .getResString("assertion_message_resp")); //$NON-NLS-1$
159:
160: group = new ButtonGroup();
161: group.add(useBody);
162: group.add(useHeaders);
163: group.add(useURL);
164: group.add(useCode);
165: group.add(useMessage);
166:
167: panel.add(useBody);
168: panel.add(useHeaders);
169: panel.add(useURL);
170: panel.add(useCode);
171: panel.add(useMessage);
172:
173: useBody.setSelected(true);
174:
175: // So we know which button is selected
176: useBody.setActionCommand(RegexExtractor.USE_BODY);
177: useHeaders.setActionCommand(RegexExtractor.USE_HDRS);
178: useURL.setActionCommand(RegexExtractor.USE_URL);
179: useCode.setActionCommand(RegexExtractor.USE_CODE);
180: useMessage.setActionCommand(RegexExtractor.USE_MESSAGE);
181:
182: return panel;
183: }
184:
185: private JPanel makeParameterPanel() {
186: regexField = new JLabeledTextField(JMeterUtils
187: .getResString("regex_field")); //$NON-NLS-1$
188: templateField = new JLabeledTextField(JMeterUtils
189: .getResString("template_field")); //$NON-NLS-1$
190: defaultField = new JLabeledTextField(JMeterUtils
191: .getResString("default_value_field")); //$NON-NLS-1$
192: refNameField = new JLabeledTextField(JMeterUtils
193: .getResString("ref_name_field")); //$NON-NLS-1$
194: matchNumberField = new JLabeledTextField(JMeterUtils
195: .getResString("match_num_field")); //$NON-NLS-1$
196:
197: JPanel panel = new JPanel(new GridBagLayout());
198: GridBagConstraints gbc = new GridBagConstraints();
199: initConstraints(gbc);
200: addField(panel, refNameField, gbc);
201: resetContraints(gbc);
202: addField(panel, regexField, gbc);
203: resetContraints(gbc);
204: addField(panel, templateField, gbc);
205: resetContraints(gbc);
206: addField(panel, matchNumberField, gbc);
207: resetContraints(gbc);
208: gbc.weighty = 1;
209: addField(panel, defaultField, gbc);
210: return panel;
211: }
212:
213: private void addField(JPanel panel, JLabeledTextField field,
214: GridBagConstraints gbc) {
215: List item = field.getComponentList();
216: panel.add((Component) item.get(0), gbc.clone());
217: gbc.gridx++;
218: gbc.weightx = 1;
219: gbc.fill = GridBagConstraints.HORIZONTAL;
220: panel.add((Component) item.get(1), gbc.clone());
221: }
222:
223: // Next line
224: private void resetContraints(GridBagConstraints gbc) {
225: gbc.gridx = 0;
226: gbc.gridy++;
227: gbc.weightx = 0;
228: gbc.fill = GridBagConstraints.NONE;
229: }
230:
231: private void initConstraints(GridBagConstraints gbc) {
232: gbc.anchor = GridBagConstraints.NORTHWEST;
233: gbc.fill = GridBagConstraints.NONE;
234: gbc.gridheight = 1;
235: gbc.gridwidth = 1;
236: gbc.gridx = 0;
237: gbc.gridy = 0;
238: gbc.weightx = 0;
239: gbc.weighty = 0;
240: }
241: }
|