KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > extractor > gui > RegexExtractorGui


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */

17
18 package org.apache.jmeter.extractor.gui;
19
20 import java.awt.BorderLayout JavaDoc;
21 import java.awt.Component JavaDoc;
22 import java.awt.GridBagConstraints JavaDoc;
23 import java.awt.GridBagLayout JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.swing.BorderFactory JavaDoc;
27 import javax.swing.Box JavaDoc;
28 import javax.swing.ButtonGroup JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.JRadioButton JavaDoc;
31
32 import org.apache.jmeter.extractor.RegexExtractor;
33 import org.apache.jmeter.processor.gui.AbstractPostProcessorGui;
34 import org.apache.jmeter.testelement.TestElement;
35 import org.apache.jmeter.util.JMeterUtils;
36 import org.apache.jorphan.gui.JLabeledTextField;
37 import org.apache.jorphan.util.JOrphanUtils;
38
39 /**
40  */

41 public class RegexExtractorGui extends AbstractPostProcessorGui
42 {
43     private JLabeledTextField regexField;
44     private JLabeledTextField templateField;
45     private JLabeledTextField defaultField;
46     private JLabeledTextField matchNumberField;
47     private JLabeledTextField refNameField;
48     
49     //NOTUSED private JCheckBox scanHeader;
50
private JRadioButton JavaDoc useBody;
51     private JRadioButton JavaDoc useHeaders;
52     
53     public RegexExtractorGui()
54     {
55         super();
56         init();
57     }
58
59     public String JavaDoc getLabelResource()
60     {
61         return "regex_extractor_title";
62     }
63     
64     public void configure(TestElement el)
65     {
66         super.configure(el);
67         useHeaders.setSelected(el.getPropertyAsBoolean(RegexExtractor.USEHEADERS));
68         useBody.setSelected(!el.getPropertyAsBoolean(RegexExtractor.USEHEADERS));
69         regexField.setText(el.getPropertyAsString(RegexExtractor.REGEX));
70         templateField.setText(el.getPropertyAsString(RegexExtractor.TEMPLATE));
71         defaultField.setText(el.getPropertyAsString(RegexExtractor.DEFAULT));
72         matchNumberField.setText(
73             el.getPropertyAsString(RegexExtractor.MATCH_NUMBER));
74         refNameField.setText(el.getPropertyAsString(RegexExtractor.REFNAME));
75     }
76
77     /**
78      * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
79      */

80     public TestElement createTestElement()
81     {
82         RegexExtractor extractor = new RegexExtractor();
83         modifyTestElement(extractor);
84         return extractor;
85     }
86
87     /**
88      * Modifies a given TestElement to mirror the data in the gui components.
89      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
90      */

91     public void modifyTestElement(TestElement extractor)
92     {
93         super.configureTestElement(extractor);
94         extractor.setProperty(
95                 RegexExtractor.USEHEADERS,
96                 JOrphanUtils.booleanToString(useHeaders.isSelected()));
97         extractor.setProperty(
98             RegexExtractor.MATCH_NUMBER,
99             matchNumberField.getText());
100         if(extractor instanceof RegexExtractor)
101         {
102             RegexExtractor regex = (RegexExtractor)extractor;
103             regex.setRefName(refNameField.getText());
104             regex.setRegex(regexField.getText());
105             regex.setTemplate(templateField.getText());
106             regex.setDefaultValue(defaultField.getText());
107         }
108     }
109     
110     private void init()
111     {
112         setLayout(new BorderLayout JavaDoc());
113         setBorder(makeBorder());
114         
115         Box JavaDoc box = Box.createVerticalBox();
116         box.add(makeTitlePanel());
117         box.add(makeSourcePanel());
118         add(box,BorderLayout.NORTH);
119         add(makeParameterPanel(),BorderLayout.CENTER);
120     }
121     
122     private JPanel JavaDoc makeSourcePanel()
123     {
124         JPanel JavaDoc panel = new JPanel JavaDoc();
125         panel.setBorder(
126             BorderFactory.createTitledBorder(
127                 JMeterUtils.getResString("regex_source")));
128
129         useBody =
130             new JRadioButton JavaDoc(JMeterUtils.getResString("regex_src_body"));
131         useHeaders =
132             new JRadioButton JavaDoc(JMeterUtils.getResString("regex_src_hdrs"));
133         
134         ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
135         group.add(useBody);
136         group.add(useHeaders);
137         
138         panel.add(useBody);
139         panel.add(useHeaders);
140         
141         useBody.setSelected(true);
142         return panel;
143     }
144
145     private JPanel JavaDoc makeParameterPanel()
146     {
147         regexField =
148             new JLabeledTextField(JMeterUtils.getResString("regex_field"));
149         templateField =
150             new JLabeledTextField(JMeterUtils.getResString("template_field"));
151         defaultField =
152             new JLabeledTextField(
153                 JMeterUtils.getResString("default_value_field"));
154         refNameField =
155             new JLabeledTextField(JMeterUtils.getResString("ref_name_field"));
156         matchNumberField =
157             new JLabeledTextField(JMeterUtils.getResString("match_num_field"));
158
159         JPanel JavaDoc panel = new JPanel JavaDoc(new GridBagLayout JavaDoc());
160         GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
161         initConstraints(gbc);
162         addField(panel,refNameField,gbc);
163         resetContraints(gbc);
164         addField(panel,regexField,gbc);
165         resetContraints(gbc);
166         addField(panel,templateField,gbc);
167         resetContraints(gbc);
168         addField(panel,matchNumberField,gbc);
169         resetContraints(gbc);
170         gbc.weighty = 1;
171         addField(panel,defaultField,gbc);
172         return panel;
173     }
174     
175     private void addField(
176         JPanel JavaDoc panel,
177         JLabeledTextField field,
178         GridBagConstraints JavaDoc gbc)
179     {
180         List JavaDoc item = field.getComponentList();
181         panel.add((Component JavaDoc)item.get(0),gbc.clone());
182         gbc.gridx++;
183         gbc.weightx = 1;;
184         panel.add((Component JavaDoc)item.get(1),gbc.clone());
185     }
186     
187     private void resetContraints(GridBagConstraints JavaDoc gbc)
188     {
189         gbc.gridx = 0;
190         gbc.gridy++;
191         gbc.weightx = 0;
192     }
193     
194     private void initConstraints(GridBagConstraints JavaDoc gbc)
195     {
196         gbc.anchor = GridBagConstraints.NORTHWEST;
197         gbc.fill = GridBagConstraints.NONE;
198         gbc.gridheight = 1;
199         gbc.gridwidth = 1;
200         gbc.gridx = 0;
201         gbc.gridy = 0;
202         gbc.weightx = 0;
203         gbc.weighty = 0;
204     }
205 }
206
Popular Tags