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.protocol.http.modifier.gui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.awt.event.FocusEvent;
024: import java.awt.event.FocusListener;
025:
026: import javax.swing.BorderFactory;
027: import javax.swing.JLabel;
028: import javax.swing.JOptionPane;
029: import javax.swing.JPanel;
030: import javax.swing.JTextField;
031:
032: import org.apache.jmeter.gui.util.HorizontalPanel;
033: import org.apache.jmeter.processor.gui.AbstractPreProcessorGui;
034: import org.apache.jmeter.protocol.http.modifier.ParamMask;
035: import org.apache.jmeter.protocol.http.modifier.ParamModifier;
036: import org.apache.jmeter.testelement.TestElement;
037: import org.apache.jmeter.util.JMeterUtils;
038:
039: /**
040: * A swing panel to allow UI with the ParamModifier class.
041: *
042: * Created Jan 18, 2002
043: *
044: */
045: public class ParamModifierGui extends AbstractPreProcessorGui implements
046: FocusListener {
047:
048: /*
049: * These are used as GUI item names;
050: * LOWERBOUND, UPPERBOUND and INCREMENT are used in the focusLost() method
051: */
052:
053: private static final String NAME = "name";
054:
055: private static final String PREFIX = "prefix";
056:
057: private static final String LOWERBOUND = "lowerBound";
058:
059: private static final String UPPERBOUND = "upperBound";
060:
061: private static final String INCREMENT = "increment";
062:
063: private static final String SUFFIX = "suffix";
064:
065: private static final String ZERO = "0"; //$NON-NLS-1$
066:
067: private JTextField _fieldName;
068:
069: private JTextField _prefix;
070:
071: private JTextField _lowerBound;
072:
073: private JTextField _upperBound;
074:
075: private JTextField _increment;
076:
077: private JTextField _suffix;
078:
079: public ParamModifierGui() {
080: init();
081: }
082:
083: public String getLabelResource() {
084: return "html_parameter_mask"; //$NON-NLS-1$
085: }
086:
087: public void configure(TestElement el) {
088: super .configure(el);
089: ParamModifier model = (ParamModifier) el;
090: updateGui(model);
091: }
092:
093: public TestElement createTestElement() {
094: ParamModifier modifier = new ParamModifier();
095: modifyTestElement(modifier);
096: return modifier;
097: }
098:
099: /**
100: * Modifies a given TestElement to mirror the data in the gui components.
101: *
102: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
103: */
104: public void modifyTestElement(TestElement m) {
105: configureTestElement(m);
106: if (m instanceof ParamModifier) {
107: ParamModifier modifier = (ParamModifier) m;
108: ParamMask mask = modifier.getMask();
109: mask.setFieldName(_fieldName.getText());
110: mask.setPrefix(_prefix.getText());
111: mask.setLowerBound(Long.parseLong(_lowerBound.getText()));
112: mask.setIncrement(Long.parseLong(_increment.getText()));
113: mask.setUpperBound(Long.parseLong(_upperBound.getText()));
114: mask.setSuffix(_suffix.getText());
115: }
116: }
117:
118: /**
119: * Implements JMeterGUIComponent.clearGui
120: */
121: public void clearGui() {
122: super .clearGui();
123:
124: _fieldName.setText(""); //$NON-NLS-1$
125: _prefix.setText(""); //$NON-NLS-1$
126: _lowerBound.setText(ZERO);
127: _upperBound.setText("10"); //$NON-NLS-1$
128: _increment.setText("1"); //$NON-NLS-1$
129: _suffix.setText(""); //$NON-NLS-1$
130: }
131:
132: public void focusGained(FocusEvent evt) {
133: }
134:
135: public void focusLost(FocusEvent evt) {
136: String name = ((Component) evt.getSource()).getName();
137: if (evt.isTemporary()) {
138: return;
139: } else if (name.equals(LOWERBOUND)) {
140: checkTextField(evt, ZERO);
141: } else if (name.equals(UPPERBOUND)) {
142: checkTextField(evt, ZERO);
143: } else if (name.equals(INCREMENT)) {
144: checkTextField(evt, ZERO);
145: }
146: }
147:
148: protected void init() {
149: setLayout(new BorderLayout());
150: setBorder(makeBorder());
151:
152: add(makeTitlePanel(), BorderLayout.NORTH);
153: add(getParameterMaskPanel(), BorderLayout.CENTER);
154: // this.updateUI();
155: }
156:
157: private void updateGui(ParamModifier model) {
158: _fieldName.setText(model.getMask().getFieldName());
159: _prefix.setText(model.getMask().getPrefix());
160: _lowerBound.setText(Long.toString(model.getMask()
161: .getLowerBound()));
162: _upperBound.setText(Long.toString(model.getMask()
163: .getUpperBound()));
164: _increment.setText(Long
165: .toString(model.getMask().getIncrement()));
166: _suffix.setText(model.getMask().getSuffix());
167: }
168:
169: private JPanel createLabeledField(String labelResName,
170: JTextField field) {
171: JLabel label = new JLabel(JMeterUtils
172: .getResString(labelResName));
173: label.setLabelFor(field);
174:
175: JPanel panel = new JPanel(new BorderLayout());
176: panel.add(label, BorderLayout.NORTH);
177: panel.add(field, BorderLayout.CENTER);
178: return panel;
179: }
180:
181: private JPanel getParameterMaskPanel() {
182: HorizontalPanel panel = new HorizontalPanel(10,
183: HorizontalPanel.TOP_ALIGNMENT);
184: panel.setBorder(BorderFactory.createTitledBorder(BorderFactory
185: .createEtchedBorder(), JMeterUtils
186: .getResString("html_parameter_mask"))); //$NON-NLS-1$
187:
188: _fieldName = new JTextField(10);
189: _fieldName.setName(NAME);
190: panel.add(createLabeledField("name", _fieldName)); //$NON-NLS-1$ resource name
191:
192: _prefix = new JTextField(5);
193: _prefix.setName(PREFIX);
194: panel.add(createLabeledField("id_prefix", _prefix)); //$NON-NLS-1$ resource name
195:
196: _lowerBound = new JTextField(ZERO, 5);
197: _lowerBound.addFocusListener(this );
198: _lowerBound.setName(LOWERBOUND);
199: panel.add(createLabeledField("lower_bound", _lowerBound)); //$NON-NLS-1$ resource name
200:
201: _upperBound = new JTextField("10", 5);
202: _upperBound.addFocusListener(this );
203: _upperBound.setName(UPPERBOUND);
204: panel.add(createLabeledField("upper_bound", _upperBound)); //$NON-NLS-1$ resource name
205:
206: _increment = new JTextField("1", 3);
207: _increment.addFocusListener(this );
208: _increment.setName(INCREMENT);
209: panel.add(createLabeledField("increment", _increment)); //$NON-NLS-1$ resource name
210:
211: _suffix = new JTextField(5);
212: _suffix.setName(SUFFIX);
213: panel.add(createLabeledField("id_suffix", _suffix)); //$NON-NLS-1$ resource name
214:
215: JPanel mainPanel = new JPanel(new BorderLayout());
216: mainPanel.add(panel, BorderLayout.NORTH);
217: return mainPanel;
218: }
219:
220: /**
221: * Used to validate a text field that requires a <code>long</code> input.
222: * Returns the <code>long</code> if valid, else creates a pop-up error
223: * message and throws a NumberFormatException.
224: *
225: * @return the number entered in the text field
226: */
227: private long checkTextField(FocusEvent evt, String defaultValue) {
228: JTextField temp = (JTextField) evt.getSource();
229: // boolean pass = true;
230: long longVal = 0;
231:
232: try {
233: longVal = Long.parseLong(temp.getText());
234: } catch (NumberFormatException err) {
235: JOptionPane.showMessageDialog(this ,
236: "This field must have a long value!",
237: "Value Required", JOptionPane.ERROR_MESSAGE);
238: temp.setText(defaultValue);
239: temp.requestFocus();
240: }
241: return longVal;
242: }
243: }
|