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: package org.apache.jmeter.extractor.gui;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Component;
022: import java.awt.GridBagConstraints;
023: import java.awt.GridBagLayout;
024: import java.util.List;
025:
026: import javax.swing.Box;
027: import javax.swing.JCheckBox;
028: import javax.swing.JPanel;
029:
030: import org.apache.jmeter.extractor.XPathExtractor;
031: import org.apache.jmeter.processor.gui.AbstractPostProcessorGui;
032: import org.apache.jmeter.testelement.TestElement;
033: import org.apache.jmeter.util.JMeterUtils;
034: import org.apache.jorphan.gui.JLabeledTextField;
035:
036: /**
037: * GUI for XPathExtractor class.
038: */
039: /* This file is inspired by RegexExtractor.
040: * author <a href="mailto:hpaluch@gitus.cz">Henryk Paluch</a>
041: * of <a href="http://www.gitus.com">Gitus a.s.</a>
042: * See Bugzilla: 37183
043: */
044: public class XPathExtractorGui extends AbstractPostProcessorGui {
045:
046: private JLabeledTextField defaultField;
047:
048: private JLabeledTextField xpathQueryField;
049:
050: private JLabeledTextField refNameField;
051:
052: private JCheckBox tolerant; // Should Tidy be run?
053:
054: private JCheckBox nameSpace; // Should parser be namespace aware?
055:
056: // We could perhaps add validate/whitespace options, but they're probably not necessary for
057: // the XPathExtractor
058:
059: public String getLabelResource() {
060: return "xpath_extractor_title"; //$NON-NLS-1$
061: }
062:
063: public XPathExtractorGui() {
064: super ();
065: init();
066: }
067:
068: public void configure(TestElement el) {
069: super .configure(el);
070: XPathExtractor xpe = (XPathExtractor) el;
071: xpathQueryField.setText(xpe.getXPathQuery());
072: defaultField.setText(xpe.getDefaultValue());
073: refNameField.setText(xpe.getRefName());
074: tolerant.setSelected(xpe.isTolerant());
075: nameSpace.setSelected(xpe.useNameSpace());
076: }
077:
078: public TestElement createTestElement() {
079: XPathExtractor extractor = new XPathExtractor();
080: modifyTestElement(extractor);
081: return extractor;
082: }
083:
084: /* (non-Javadoc)
085: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(org.apache.jmeter.testelement.TestElement)
086: */
087: public void modifyTestElement(TestElement extractor) {
088: super .configureTestElement(extractor);
089: if (extractor instanceof XPathExtractor) {
090: XPathExtractor xpath = (XPathExtractor) extractor;
091: xpath.setDefaultValue(defaultField.getText());
092: xpath.setRefName(refNameField.getText());
093: xpath.setXPathQuery(xpathQueryField.getText());
094: xpath.setTolerant(tolerant.isSelected());
095: xpath.setNameSpace(nameSpace.isSelected());
096: }
097: }
098:
099: /**
100: * Implements JMeterGUIComponent.clearGui
101: */
102: public void clearGui() {
103: super .clearGui();
104:
105: xpathQueryField.setText(""); // $NON-NLS-1$
106: defaultField.setText(""); // $NON-NLS-1$
107: refNameField.setText(""); // $NON-NLS-1$
108: tolerant.setSelected(false);
109: nameSpace.setSelected(true);
110: }
111:
112: private void init() {
113: setLayout(new BorderLayout());
114: setBorder(makeBorder());
115:
116: Box box = Box.createVerticalBox();
117: box.add(makeTitlePanel());
118: Box options = Box.createHorizontalBox();
119: tolerant = new JCheckBox(JMeterUtils
120: .getResString("xpath_extractor_tolerant"));//$NON-NLS-1$
121: nameSpace = new JCheckBox(JMeterUtils
122: .getResString("xpath_extractor_namespace"), true);//$NON-NLS-1$
123: options.add(tolerant);
124: options.add(nameSpace);
125: box.add(options);
126: add(box, BorderLayout.NORTH);
127: add(makeParameterPanel(), BorderLayout.CENTER);
128: }
129:
130: private JPanel makeParameterPanel() {
131: xpathQueryField = new JLabeledTextField(JMeterUtils
132: .getResString("xpath_extractor_query"));//$NON-NLS-1$
133: defaultField = new JLabeledTextField(JMeterUtils
134: .getResString("default_value_field"));//$NON-NLS-1$
135: refNameField = new JLabeledTextField(JMeterUtils
136: .getResString("ref_name_field"));//$NON-NLS-1$
137:
138: JPanel panel = new JPanel(new GridBagLayout());
139: GridBagConstraints gbc = new GridBagConstraints();
140: initConstraints(gbc);
141: addField(panel, refNameField, gbc);
142: resetContraints(gbc);
143: addField(panel, xpathQueryField, gbc);
144: resetContraints(gbc);
145: gbc.weighty = 1;
146: addField(panel, defaultField, gbc);
147: return panel;
148: }
149:
150: private void addField(JPanel panel, JLabeledTextField field,
151: GridBagConstraints gbc) {
152: List item = field.getComponentList();
153: panel.add((Component) item.get(0), gbc.clone());
154: gbc.gridx++;
155: gbc.weightx = 1;
156: gbc.fill = GridBagConstraints.HORIZONTAL;
157: panel.add((Component) item.get(1), gbc.clone());
158: }
159:
160: private void resetContraints(GridBagConstraints gbc) {
161: gbc.gridx = 0;
162: gbc.gridy++;
163: gbc.weightx = 0;
164: gbc.fill = GridBagConstraints.NONE;
165: }
166:
167: private void initConstraints(GridBagConstraints gbc) {
168: gbc.anchor = GridBagConstraints.NORTHWEST;
169: gbc.fill = GridBagConstraints.NONE;
170: gbc.gridheight = 1;
171: gbc.gridwidth = 1;
172: gbc.gridx = 0;
173: gbc.gridy = 0;
174: gbc.weightx = 0;
175: gbc.weighty = 0;
176: }
177: }
|