1 18 19 package org.apache.jmeter.protocol.http.modifier.gui; 20 import java.awt.BorderLayout ; 21 import java.awt.Component ; 22 import java.awt.event.FocusEvent ; 23 import java.awt.event.FocusListener ; 24 25 import javax.swing.BorderFactory ; 26 import javax.swing.JLabel ; 27 import javax.swing.JOptionPane ; 28 import javax.swing.JPanel ; 29 import javax.swing.JTextField ; 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 44 public class ParamModifierGui 45 extends AbstractPreProcessorGui 46 implements FocusListener 47 { 48 private static final String NAME = "name"; 49 private static final String PREFIX = "prefix"; 50 private static final String LOWERBOUND = "lowerBound"; 51 private static final String UPPERBOUND = "upperBound"; 52 private static final String INCREMENT = "increment"; 53 private static final String SUFFIX = "suffix"; 54 55 private JTextField _fieldName; 56 private JTextField _prefix; 57 private JTextField _lowerBound; 58 private JTextField _upperBound; 59 private JTextField _increment; 60 private JTextField _suffix; 61 62 public ParamModifierGui() 63 { 64 init(); 65 } 66 67 public String 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 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 evt) 107 {} 108 109 public void focusLost(FocusEvent evt) 110 { 111 String name = ((Component ) 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 ()); 133 setBorder(makeBorder()); 134 135 add(makeTitlePanel(), BorderLayout.NORTH); 136 add(getParameterMaskPanel(), BorderLayout.CENTER); 137 } 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 createLabeledField(String labelResName, JTextField field) 151 { 152 JLabel label = new JLabel (JMeterUtils.getResString(labelResName)); 153 label.setLabelFor(field); 154 155 JPanel panel = new JPanel (new BorderLayout ()); 156 panel.add(label, BorderLayout.NORTH); 157 panel.add(field, BorderLayout.CENTER); 158 return panel; 159 } 160 161 private JPanel 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 (10); 171 _fieldName.setName(NAME); 172 panel.add(createLabeledField("Name", _fieldName)); 173 174 _prefix = new JTextField (5); 175 _prefix.setName(PREFIX); 176 panel.add(createLabeledField("ID Prefix", _prefix)); 177 178 _lowerBound = new JTextField ("0", 5); 179 _lowerBound.addFocusListener(this); 180 _lowerBound.setName(LOWERBOUND); 181 panel.add(createLabeledField("Lower Bound", _lowerBound)); 182 183 _upperBound = new JTextField ("10", 5); 184 _upperBound.addFocusListener(this); 185 _upperBound.setName(UPPERBOUND); 186 panel.add(createLabeledField("Upper Bound", _upperBound)); 187 188 _increment = new JTextField ("1", 3); 189 _increment.addFocusListener(this); 190 _increment.setName(INCREMENT); 191 panel.add(createLabeledField("Increment", _increment)); 192 193 _suffix = new JTextField (5); 194 _suffix.setName(SUFFIX); 195 panel.add(createLabeledField("ID Suffix", _suffix)); 196 197 JPanel mainPanel = new JPanel (new BorderLayout ()); 198 mainPanel.add(panel, BorderLayout.NORTH); 199 return mainPanel; 200 } 201 202 209 private long checkTextField(FocusEvent evt, String defaultValue) 210 { 211 JTextField temp = (JTextField ) evt.getSource(); 212 long longVal = 0; 214 215 try 216 { 217 longVal = Long.parseLong(temp.getText()); 218 } 219 catch (NumberFormatException 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 |