KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > modifier > gui > ParamModifierGui


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/modifier/gui/ParamModifierGui.java,v 1.13 2004/03/05 01:38:17 sebb Exp $
2
/*
3  * Copyright 2002-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.http.modifier.gui;
20 import java.awt.BorderLayout JavaDoc;
21 import java.awt.Component JavaDoc;
22 import java.awt.event.FocusEvent JavaDoc;
23 import java.awt.event.FocusListener JavaDoc;
24
25 import javax.swing.BorderFactory JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JOptionPane JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.JTextField JavaDoc;
30
31 import org.apache.jmeter.gui.util.HorizontalPanel;
32 import org.apache.jmeter.processor.gui.AbstractPreProcessorGui;
33 import org.apache.jmeter.protocol.http.modifier.ParamMask;
34 import org.apache.jmeter.protocol.http.modifier.ParamModifier;
35 import org.apache.jmeter.testelement.TestElement;
36 import org.apache.jmeter.util.JMeterUtils;
37
38 /**
39  * A swing panel to allow UI with the ParamModifier class.
40  *
41  * Created Jan 18, 2002
42  * @version $Revision: 1.13 $ Last updated: $Date: 2004/03/05 01:38:17 $
43  */

44 public class ParamModifierGui
45     extends AbstractPreProcessorGui
46     implements FocusListener JavaDoc
47 {
48     private static final String JavaDoc NAME = "name";
49     private static final String JavaDoc PREFIX = "prefix";
50     private static final String JavaDoc LOWERBOUND = "lowerBound";
51     private static final String JavaDoc UPPERBOUND = "upperBound";
52     private static final String JavaDoc INCREMENT = "increment";
53     private static final String JavaDoc SUFFIX = "suffix";
54
55     private JTextField JavaDoc _fieldName;
56     private JTextField JavaDoc _prefix;
57     private JTextField JavaDoc _lowerBound;
58     private JTextField JavaDoc _upperBound;
59     private JTextField JavaDoc _increment;
60     private JTextField JavaDoc _suffix;
61
62     public ParamModifierGui()
63     {
64         init();
65     }
66
67     public String JavaDoc getLabelResource()
68     {
69         return "html_parameter_mask";
70     }
71
72     public void configure(TestElement el)
73     {
74         super.configure(el);
75         ParamModifier model = (ParamModifier) el;
76         updateGui(model);
77     }
78
79     public TestElement createTestElement()
80     {
81         ParamModifier modifier = new ParamModifier();
82         modifyTestElement(modifier);
83         return modifier;
84     }
85
86     /**
87      * Modifies a given TestElement to mirror the data in the gui components.
88      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
89      */

90     public void modifyTestElement(TestElement m)
91     {
92         configureTestElement(m);
93         if (m instanceof ParamModifier)
94         {
95             ParamModifier modifier = (ParamModifier) m;
96             ParamMask mask = modifier.getMask();
97             mask.setFieldName(_fieldName.getText());
98             mask.setPrefix(_prefix.getText());
99             mask.setLowerBound(Long.parseLong(_lowerBound.getText()));
100             mask.setIncrement(Long.parseLong(_increment.getText()));
101             mask.setUpperBound(Long.parseLong(_upperBound.getText()));
102             mask.setSuffix(_suffix.getText());
103         }
104     }
105
106     public void focusGained(FocusEvent JavaDoc evt)
107     {}
108
109     public void focusLost(FocusEvent JavaDoc evt)
110     {
111         String JavaDoc name = ((Component JavaDoc) evt.getSource()).getName();
112         if (evt.isTemporary())
113         {
114             return;
115         }
116         else if (name.equals(LOWERBOUND))
117         {
118             checkTextField(evt, "0");
119         }
120         else if (name.equals(UPPERBOUND))
121         {
122             checkTextField(evt, "0");
123         }
124         else if (name.equals(INCREMENT))
125         {
126             checkTextField(evt, "0");
127         }
128     }
129
130     protected void init()
131     {
132         setLayout(new BorderLayout JavaDoc());
133         setBorder(makeBorder());
134         
135         add(makeTitlePanel(), BorderLayout.NORTH);
136         add(getParameterMaskPanel(), BorderLayout.CENTER);
137         // this.updateUI();
138
}
139
140     private void updateGui(ParamModifier model)
141     {
142         _fieldName.setText(model.getMask().getFieldName());
143         _prefix.setText(model.getMask().getPrefix());
144         _lowerBound.setText(Long.toString(model.getMask().getLowerBound()));
145         _upperBound.setText(Long.toString(model.getMask().getUpperBound()));
146         _increment.setText(Long.toString(model.getMask().getIncrement()));
147         _suffix.setText(model.getMask().getSuffix());
148     }
149
150     private JPanel JavaDoc createLabeledField(String JavaDoc labelResName, JTextField JavaDoc field)
151     {
152         JLabel JavaDoc label = new JLabel JavaDoc(JMeterUtils.getResString(labelResName));
153         label.setLabelFor(field);
154         
155         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
156         panel.add(label, BorderLayout.NORTH);
157         panel.add(field, BorderLayout.CENTER);
158         return panel;
159     }
160     
161     private JPanel JavaDoc getParameterMaskPanel()
162     {
163         HorizontalPanel panel =
164             new HorizontalPanel(10, HorizontalPanel.TOP_ALIGNMENT);
165         panel.setBorder(
166             BorderFactory.createTitledBorder(
167                 BorderFactory.createEtchedBorder(),
168                 JMeterUtils.getResString("HTML Parameter Mask")));
169
170         _fieldName = new JTextField JavaDoc(10);
171         _fieldName.setName(NAME);
172         panel.add(createLabeledField("Name", _fieldName));
173
174         _prefix = new JTextField JavaDoc(5);
175         _prefix.setName(PREFIX);
176         panel.add(createLabeledField("ID Prefix", _prefix));
177
178         _lowerBound = new JTextField JavaDoc("0", 5);
179         _lowerBound.addFocusListener(this);
180         _lowerBound.setName(LOWERBOUND);
181         panel.add(createLabeledField("Lower Bound", _lowerBound));
182         
183         _upperBound = new JTextField JavaDoc("10", 5);
184         _upperBound.addFocusListener(this);
185         _upperBound.setName(UPPERBOUND);
186         panel.add(createLabeledField("Upper Bound", _upperBound));
187         
188         _increment = new JTextField JavaDoc("1", 3);
189         _increment.addFocusListener(this);
190         _increment.setName(INCREMENT);
191         panel.add(createLabeledField("Increment", _increment));
192         
193         _suffix = new JTextField JavaDoc(5);
194         _suffix.setName(SUFFIX);
195         panel.add(createLabeledField("ID Suffix", _suffix));
196
197         JPanel JavaDoc mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
198         mainPanel.add(panel, BorderLayout.NORTH);
199         return mainPanel;
200     }
201
202     /**
203      * Used to validate a text field that requires a <code>long</code> input.
204      * Returns the <code>long</code> if valid, else creates a pop-up error
205      * message and throws a NumberFormatException.
206      *
207      * @return the number entered in the text field
208      */

209     private long checkTextField(FocusEvent JavaDoc evt, String JavaDoc defaultValue)
210     {
211         JTextField JavaDoc temp = (JTextField JavaDoc) evt.getSource();
212         //boolean pass = true;
213
long longVal = 0;
214
215         try
216         {
217             longVal = Long.parseLong(temp.getText());
218         }
219         catch (NumberFormatException JavaDoc err)
220         {
221             JOptionPane.showMessageDialog(
222                 this,
223                 "This field must have a long value!",
224                 "Value Required",
225                 JOptionPane.ERROR_MESSAGE);
226             temp.setText(defaultValue);
227             temp.requestFocus();
228         }
229         return longVal;
230     }
231 }
232
Popular Tags