1 18 19 package org.apache.jmeter.control.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 25 import javax.swing.Box ; 26 import javax.swing.JCheckBox ; 27 import javax.swing.JLabel ; 28 import javax.swing.JPanel ; 29 import javax.swing.JTextField ; 30 31 import org.apache.jmeter.control.LoopController; 32 import org.apache.jmeter.gui.util.FocusRequester; 33 import org.apache.jmeter.testelement.TestElement; 34 import org.apache.jmeter.util.JMeterUtils; 35 36 43 44 public class LoopControlPanel 45 extends AbstractControllerGui 46 implements ActionListener 47 { 48 52 private JCheckBox infinite; 53 54 58 private JTextField loops; 59 60 61 66 private boolean displayName = true; 67 68 69 private static final String INFINITE = "Infinite Field"; 70 71 72 private static final String LOOPS = "Loops Field"; 73 74 77 public LoopControlPanel() 78 { 79 this(true); 80 } 81 82 91 public LoopControlPanel(boolean displayName) 92 { 93 this.displayName = displayName; 94 init(); 95 setState(1); 96 } 97 98 106 public void configure(TestElement element) 107 { 108 super.configure(element); 109 if (element instanceof LoopController) 110 { 111 setState(((LoopController) element).getLoopString()); 112 } 113 else 114 { 115 setState(1); 116 } 117 } 118 119 120 public TestElement createTestElement() 121 { 122 LoopController lc = new LoopController(); 123 modifyTestElement(lc); 124 return lc; 125 } 126 127 128 public void modifyTestElement(TestElement lc) 129 { 130 configureTestElement(lc); 131 if (lc instanceof LoopController) 132 { 133 if (loops.getText().length() > 0) 134 { 135 ((LoopController) lc).setLoops(loops.getText()); 136 } 137 else 138 { 139 ((LoopController) lc).setLoops(-1); 140 } 141 } 142 } 143 144 150 public void actionPerformed(ActionEvent event) 151 { 152 if (infinite.isSelected()) 153 { 154 loops.setText(""); 155 loops.setEnabled(false); 156 } 157 else 158 { 159 loops.setEnabled(true); 160 new FocusRequester(loops); 161 } 162 } 163 164 public String getLabelResource() 165 { 166 return "loop_controller_title"; 167 } 168 169 172 private void init() 173 { 174 180 if (displayName) 182 { 183 setLayout(new BorderLayout (0, 5)); 184 setBorder(makeBorder()); 185 add(makeTitlePanel(), BorderLayout.NORTH); 186 187 JPanel mainPanel = new JPanel (new BorderLayout ()); 188 mainPanel.add(createLoopCountPanel(), BorderLayout.NORTH); 189 add(mainPanel, BorderLayout.CENTER); 190 } 191 else 192 { 193 setLayout(new BorderLayout ()); 195 add(createLoopCountPanel(), BorderLayout.NORTH); 196 } 197 } 198 199 205 private JPanel createLoopCountPanel() 206 { 207 JPanel loopPanel = new JPanel (new BorderLayout (5, 0)); 208 209 JLabel loopsLabel = 211 new JLabel (JMeterUtils.getResString("iterator_num")); 212 loopPanel.add(loopsLabel, BorderLayout.WEST); 213 214 JPanel loopSubPanel = new JPanel (new BorderLayout (5, 0)); 215 216 loops = new JTextField ("1", 5); 218 loops.setName(LOOPS); 219 loopsLabel.setLabelFor(loops); 220 loopSubPanel.add(loops, BorderLayout.CENTER); 221 222 infinite = new JCheckBox (JMeterUtils.getResString("infinite")); 224 infinite.setActionCommand(INFINITE); 225 infinite.addActionListener(this); 226 loopSubPanel.add(infinite, BorderLayout.WEST); 227 228 loopPanel.add(loopSubPanel,BorderLayout.CENTER); 229 230 loopPanel.add( 231 Box.createHorizontalStrut( 232 loopsLabel.getPreferredSize().width 233 + loops.getPreferredSize().width 234 + infinite.getPreferredSize().width), 235 BorderLayout.NORTH); 236 237 return loopPanel; 238 } 239 240 249 private void setState(String loopCount) 250 { 251 if (loopCount.startsWith("-")) 252 { 253 setState(-1); 254 } 255 else 256 { 257 loops.setText(loopCount); 258 infinite.setSelected(false); 259 loops.setEnabled(true); 260 } 261 } 262 263 270 private void setState(int loopCount) 271 { 272 if (loopCount <= -1) 273 { 274 infinite.setSelected(true); 275 loops.setEnabled(false); 276 loops.setText(""); 277 } 278 else 279 { 280 infinite.setSelected(false); 281 loops.setEnabled(true); 282 loops.setText("" + loopCount); 283 } 284 } 285 } 286 | Popular Tags |