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 package org.apache.jmeter.extractor.gui; 19 20 import java.awt.BorderLayout; 21 import java.awt.Component; 22 import java.awt.GridBagConstraints; 23 import java.awt.GridBagLayout; 24 import java.awt.event.ActionEvent; 25 import java.awt.event.ActionListener; 26 import java.util.List; 27 28 import javax.swing.BorderFactory; 29 import javax.swing.Box; 30 import javax.swing.JCheckBox; 31 import javax.swing.JPanel; 32 33 import org.apache.jmeter.extractor.XPathExtractor; 34 import org.apache.jmeter.processor.gui.AbstractPostProcessorGui; 35 import org.apache.jmeter.testelement.TestElement; 36 import org.apache.jmeter.util.JMeterUtils; 37 import org.apache.jorphan.gui.JLabeledTextField; 38 /** 39 * GUI for XPathExtractor class. 40 */ 41 /* This file is inspired by RegexExtractor. 42 * author <a href="mailto:hpaluch@gitus.cz">Henryk Paluch</a> 43 * of <a href="http://www.gitus.com">Gitus a.s.</a> 44 * See Bugzilla: 37183 45 */ 46 public class XPathExtractorGui extends AbstractPostProcessorGui { 47 48 private JLabeledTextField defaultField; 49 50 private JLabeledTextField xpathQueryField; 51 52 private JLabeledTextField refNameField; 53 54 private JCheckBox tolerant; // Should Tidy be run? 55 56 private JCheckBox quiet; // Should Tidy be quiet? 57 58 private JCheckBox reportErrors; // Report Tidy errors as Assertion failure? 59 60 private JCheckBox showWarnings; // Show Tidy warnings ? 61 62 private JCheckBox nameSpace; // Should parser be namespace aware? 63 64 // We could perhaps add validate/whitespace options, but they're probably not necessary for 65 // the XPathExtractor 66 67 public String getLabelResource() { 68 return "xpath_extractor_title"; //$NON-NLS-1$ 69 } 70 71 public XPathExtractorGui(){ 72 super(); 73 init(); 74 } 75 76 public void configure(TestElement el) { 77 super.configure(el); 78 XPathExtractor xpe = (XPathExtractor) el; 79 xpathQueryField.setText(xpe.getXPathQuery()); 80 defaultField.setText(xpe.getDefaultValue()); 81 refNameField.setText(xpe.getRefName()); 82 tolerant.setSelected(xpe.isTolerant()); 83 quiet.setSelected(xpe.isQuiet()); 84 showWarnings.setSelected(xpe.showWarnings()); 85 reportErrors.setSelected(xpe.reportErrors()); 86 nameSpace.setSelected(xpe.useNameSpace()); 87 setTidyOptions(tolerant.isSelected()); 88 } 89 90 91 public TestElement createTestElement() { 92 XPathExtractor extractor = new XPathExtractor(); 93 modifyTestElement(extractor); 94 return extractor; 95 } 96 97 /* (non-Javadoc) 98 * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(org.apache.jmeter.testelement.TestElement) 99 */ 100 public void modifyTestElement(TestElement extractor) { 101 super.configureTestElement(extractor); 102 if ( extractor instanceof XPathExtractor){ 103 XPathExtractor xpath = (XPathExtractor)extractor; 104 xpath.setDefaultValue(defaultField.getText()); 105 xpath.setRefName(refNameField.getText()); 106 xpath.setXPathQuery(xpathQueryField.getText()); 107 xpath.setTolerant(tolerant.isSelected()); 108 xpath.setNameSpace(nameSpace.isSelected()); 109 xpath.setShowWarnings(showWarnings.isSelected()); 110 xpath.setReportErrors(reportErrors.isSelected()); 111 xpath.setQuiet(quiet.isSelected()); 112 } 113 } 114 115 /** 116 * Implements JMeterGUIComponent.clearGui 117 */ 118 public void clearGui() { 119 super.clearGui(); 120 121 xpathQueryField.setText(""); // $NON-NLS-1$ 122 defaultField.setText(""); // $NON-NLS-1$ 123 refNameField.setText(""); // $NON-NLS-1$ 124 tolerant.setSelected(false); 125 nameSpace.setSelected(true); 126 quiet.setSelected(true); 127 reportErrors.setSelected(false); 128 showWarnings.setSelected(false); 129 } 130 131 private void setTidyOptions(boolean tidySelected){ 132 quiet.setEnabled(tidySelected); 133 reportErrors.setEnabled(tidySelected); 134 showWarnings.setEnabled(tidySelected); 135 nameSpace.setEnabled(!tidySelected); 136 } 137 138 private void init() { 139 setLayout(new BorderLayout()); 140 setBorder(makeBorder()); 141 142 Box box = Box.createVerticalBox(); 143 box.add(makeTitlePanel()); 144 Box options = Box.createHorizontalBox(); 145 tolerant = new JCheckBox(JMeterUtils.getResString("xpath_extractor_tolerant"));//$NON-NLS-1$ 146 quiet = new JCheckBox(JMeterUtils.getResString("xpath_tidy_quiet"),true);//$NON-NLS-1$ 147 reportErrors = new JCheckBox(JMeterUtils.getResString("xpath_tidy_report_errors"),true);//$NON-NLS-1$ 148 showWarnings = new JCheckBox(JMeterUtils.getResString("xpath_tidy_show_warnings"),true);//$NON-NLS-1$ 149 nameSpace = new JCheckBox(JMeterUtils.getResString("xpath_extractor_namespace"),true);//$NON-NLS-1$ 150 151 tolerant.addActionListener( 152 new ActionListener(){ 153 public void actionPerformed(ActionEvent e) { 154 setTidyOptions(tolerant.isSelected()); 155 }}); 156 157 setTidyOptions(tolerant.isSelected()); 158 159 Box tidyOptions = Box.createHorizontalBox(); 160 tidyOptions.add(tolerant); 161 tidyOptions.add(quiet); 162 tidyOptions.add(reportErrors); 163 tidyOptions.add(showWarnings); 164 tidyOptions.setBorder(BorderFactory.createEtchedBorder()); 165 options.add(tidyOptions); 166 options.add(nameSpace); 167 box.add(options); 168 add(box, BorderLayout.NORTH); 169 add(makeParameterPanel(), BorderLayout.CENTER); 170 } 171 172 173 private JPanel makeParameterPanel() { 174 xpathQueryField = new JLabeledTextField(JMeterUtils.getResString("xpath_extractor_query"));//$NON-NLS-1$ 175 defaultField = new JLabeledTextField(JMeterUtils.getResString("default_value_field"));//$NON-NLS-1$ 176 refNameField = new JLabeledTextField(JMeterUtils.getResString("ref_name_field"));//$NON-NLS-1$ 177 178 JPanel panel = new JPanel(new GridBagLayout()); 179 GridBagConstraints gbc = new GridBagConstraints(); 180 initConstraints(gbc); 181 addField(panel, refNameField, gbc); 182 resetContraints(gbc); 183 addField(panel, xpathQueryField, gbc); 184 resetContraints(gbc); 185 gbc.weighty = 1; 186 addField(panel, defaultField, gbc); 187 return panel; 188 } 189 190 private void addField(JPanel panel, JLabeledTextField field, GridBagConstraints gbc) { 191 List item = field.getComponentList(); 192 panel.add((Component) item.get(0), gbc.clone()); 193 gbc.gridx++; 194 gbc.weightx = 1; 195 gbc.fill=GridBagConstraints.HORIZONTAL; 196 panel.add((Component) item.get(1), gbc.clone()); 197 } 198 199 private void resetContraints(GridBagConstraints gbc) { 200 gbc.gridx = 0; 201 gbc.gridy++; 202 gbc.weightx = 0; 203 gbc.fill=GridBagConstraints.NONE; 204 } 205 206 private void initConstraints(GridBagConstraints gbc) { 207 gbc.anchor = GridBagConstraints.NORTHWEST; 208 gbc.fill = GridBagConstraints.NONE; 209 gbc.gridheight = 1; 210 gbc.gridwidth = 1; 211 gbc.gridx = 0; 212 gbc.gridy = 0; 213 gbc.weightx = 0; 214 gbc.weighty = 0; 215 } 216 }