1 18 19 package org.apache.jmeter.modifiers.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.GridLayout ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 import javax.swing.Box ; 29 import javax.swing.JButton ; 30 import javax.swing.JCheckBox ; 31 import javax.swing.JLabel ; 32 import javax.swing.JOptionPane ; 33 import javax.swing.JPanel ; 34 import javax.swing.JScrollPane ; 35 import javax.swing.JTable ; 36 import javax.swing.ListSelectionModel ; 37 import javax.swing.table.TableCellEditor ; 38 39 import org.apache.jmeter.gui.util.PowerTableModel; 40 import org.apache.jmeter.gui.util.VerticalPanel; 41 import org.apache.jmeter.modifiers.UserParameters; 42 import org.apache.jmeter.processor.gui.AbstractPreProcessorGui; 43 import org.apache.jmeter.testelement.TestElement; 44 import org.apache.jmeter.testelement.property.CollectionProperty; 45 import org.apache.jmeter.testelement.property.PropertyIterator; 46 import org.apache.jmeter.util.JMeterUtils; 47 import org.apache.jorphan.logging.LoggingManager; 48 import org.apache.log.Logger; 49 50 53 public class UserParametersGui extends AbstractPreProcessorGui 54 { 55 private static Logger log = LoggingManager.getLoggerForClass(); 56 private String THREAD_COLUMNS = JMeterUtils.getResString("user"); 57 58 private JTable paramTable; 59 private PowerTableModel tableModel; 60 private int numUserColumns = 1; 61 private JButton addParameterButton, 62 addUserButton, 63 deleteRowButton, 64 deleteColumnButton; 65 private JCheckBox perIterationCheck; 66 67 public UserParametersGui() 68 { 69 super(); 70 init(); 71 } 72 73 public String getLabelResource() 74 { 75 return "user_parameters_title"; 76 } 77 78 public void configure(TestElement el) 79 { 80 initTableModel(); 81 paramTable.setModel(tableModel); 82 UserParameters params = (UserParameters) el; 83 CollectionProperty names = params.getNames(); 84 CollectionProperty threadValues = params.getThreadLists(); 85 tableModel.setColumnData(0, (List ) names.getObjectValue()); 86 PropertyIterator iter = threadValues.iterator(); 87 if (iter.hasNext()) 88 { 89 tableModel.setColumnData(1, (List ) iter.next().getObjectValue()); 90 } 91 int count = 2; 92 while (iter.hasNext()) 93 { 94 String colName = THREAD_COLUMNS + "_" + count; 95 tableModel.addNewColumn(colName, String .class); 96 tableModel.setColumnData( 97 count, 98 (List ) iter.next().getObjectValue()); 99 count++; 100 } 101 perIterationCheck.setSelected(params.isPerIteration()); 102 super.configure(el); 103 } 104 105 108 public TestElement createTestElement() 109 { 110 UserParameters params = new UserParameters(); 111 modifyTestElement(params); 112 return params; 113 } 114 115 119 public void modifyTestElement(TestElement params) 120 { 121 if (paramTable.isEditing()) 122 { 123 paramTable.getCellEditor().stopCellEditing(); 124 } 125 ((UserParameters) params).setNames( 126 new CollectionProperty( 127 UserParameters.NAMES, 128 tableModel.getColumnData(JMeterUtils.getResString("name")))); 129 CollectionProperty threadLists = 130 new CollectionProperty( 131 UserParameters.THREAD_VALUES, 132 new ArrayList ()); 133 log.debug("making threadlists from gui = " + threadLists); 134 for (int x = 1; x < tableModel.getColumnCount(); x++) 135 { 136 threadLists.addItem( 137 tableModel.getColumnData(THREAD_COLUMNS + "_" + x)); 138 log.debug( 139 "Adding column to threadlist: " 140 + tableModel.getColumnData(THREAD_COLUMNS + "_" + x)); 141 log.debug("Threadlists now = " + threadLists); 142 } 143 log.debug("In the end, threadlists = " + threadLists); 144 ((UserParameters) params).setThreadLists(threadLists); 145 ((UserParameters) params).setPerIteration( 146 perIterationCheck.isSelected()); 147 super.configureTestElement(params); 148 } 149 150 private void init() 151 { 152 setBorder(makeBorder()); 153 setLayout(new BorderLayout ()); 154 JPanel vertPanel = new VerticalPanel(); 155 vertPanel.add(makeTitlePanel()); 156 157 perIterationCheck = 158 new JCheckBox (JMeterUtils.getResString("update_per_iter"), true); 159 Box perIterationPanel = Box.createHorizontalBox(); 160 perIterationPanel.add(perIterationCheck); 161 perIterationPanel.add(Box.createHorizontalGlue()); 162 vertPanel.add(perIterationPanel); 163 add(vertPanel, BorderLayout.NORTH); 164 165 add(makeParameterPanel(), BorderLayout.CENTER); 166 } 167 168 private JPanel makeParameterPanel() 169 { 170 JLabel tableLabel = 171 new JLabel (JMeterUtils.getResString("user_parameters_table")); 172 initTableModel(); 173 paramTable = new JTable (tableModel); 174 paramTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 177 180 JPanel paramPanel = new JPanel (new BorderLayout ()); 181 paramPanel.add(tableLabel, BorderLayout.NORTH); 182 JScrollPane scroll = new JScrollPane (paramTable); 183 scroll.setPreferredSize(scroll.getMinimumSize()); 184 paramPanel.add(scroll, BorderLayout.CENTER); 185 paramPanel.add(makeButtonPanel(), BorderLayout.SOUTH); 186 return paramPanel; 187 } 188 189 protected void initTableModel() 190 { 191 tableModel = 192 new PowerTableModel( 193 new String [] { 194 JMeterUtils.getResString("name"), 195 THREAD_COLUMNS + "_" + numUserColumns }, 196 new Class [] { String .class, String .class }); 197 } 198 199 private JPanel makeButtonPanel() 200 { 201 JPanel buttonPanel = new JPanel (); 202 buttonPanel.setLayout(new GridLayout (2, 2)); 203 addParameterButton = 204 new JButton (JMeterUtils.getResString("add_parameter")); 205 addUserButton = new JButton (JMeterUtils.getResString("add_user")); 206 deleteRowButton = 207 new JButton (JMeterUtils.getResString("delete_parameter")); 208 deleteColumnButton = 209 new JButton (JMeterUtils.getResString("delete_user")); 210 buttonPanel.add(addParameterButton); 211 buttonPanel.add(deleteRowButton); 212 buttonPanel.add(addUserButton); 213 buttonPanel.add(deleteColumnButton); 214 addParameterButton.addActionListener(new AddParamAction()); 215 addUserButton.addActionListener(new AddUserAction()); 216 deleteRowButton.addActionListener(new DeleteRowAction()); 217 deleteColumnButton.addActionListener(new DeleteColumnAction()); 218 return buttonPanel; 219 } 220 221 private class AddParamAction implements ActionListener 222 { 223 public void actionPerformed(ActionEvent e) 224 { 225 if (paramTable.isEditing()) 226 { 227 TableCellEditor cellEditor = 228 paramTable.getCellEditor( 229 paramTable.getEditingRow(), 230 paramTable.getEditingColumn()); 231 cellEditor.stopCellEditing(); 232 } 233 234 tableModel.addNewRow(); 235 tableModel.fireTableDataChanged(); 236 237 deleteRowButton.setEnabled(true); 239 240 int rowToSelect = tableModel.getRowCount() - 1; 242 paramTable.setRowSelectionInterval(rowToSelect, rowToSelect); 243 } 244 } 245 246 private class AddUserAction implements ActionListener 247 { 248 public void actionPerformed(ActionEvent e) 249 { 250 251 if (paramTable.isEditing()) 252 { 253 TableCellEditor cellEditor = 254 paramTable.getCellEditor( 255 paramTable.getEditingRow(), 256 paramTable.getEditingColumn()); 257 cellEditor.stopCellEditing(); 258 } 259 260 tableModel.addNewColumn( 261 THREAD_COLUMNS + "_" + tableModel.getColumnCount(), 262 String .class); 263 tableModel.fireTableDataChanged(); 264 265 deleteColumnButton.setEnabled(true); 267 268 int colToSelect = tableModel.getColumnCount() - 1; 270 paramTable.setColumnSelectionInterval(colToSelect, colToSelect); 271 } 272 } 273 274 private class DeleteRowAction implements ActionListener 275 { 276 public void actionPerformed(ActionEvent e) 277 { 278 if (paramTable.isEditing()) 279 { 280 TableCellEditor cellEditor = 281 paramTable.getCellEditor( 282 paramTable.getEditingRow(), 283 paramTable.getEditingColumn()); 284 cellEditor.cancelCellEditing(); 285 } 286 287 int rowSelected = paramTable.getSelectedRow(); 288 if (rowSelected >= 0) 289 { 290 tableModel.removeRow(rowSelected); 291 tableModel.fireTableDataChanged(); 292 293 if (tableModel.getRowCount() == 0) 295 { 296 deleteRowButton.setEnabled(false); 297 } 298 299 else 302 { 303 int rowToSelect = rowSelected; 304 305 if (rowSelected >= tableModel.getRowCount()) 306 { 307 rowToSelect = rowSelected - 1; 308 } 309 310 paramTable.setRowSelectionInterval( 311 rowToSelect, 312 rowToSelect); 313 } 314 } 315 } 316 } 317 318 private class DeleteColumnAction implements ActionListener 319 { 320 public void actionPerformed(ActionEvent e) 321 { 322 if (paramTable.isEditing()) 323 { 324 TableCellEditor cellEditor = 325 paramTable.getCellEditor( 326 paramTable.getEditingRow(), 327 paramTable.getEditingColumn()); 328 cellEditor.cancelCellEditing(); 329 } 330 331 int colSelected = paramTable.getSelectedColumn(); 332 if (colSelected == 0 || colSelected == 1) 333 { 334 JOptionPane.showMessageDialog( 335 null, 336 JMeterUtils.getResString("column_delete_disallowed"), 337 "Error", 338 JOptionPane.ERROR_MESSAGE); 339 return; 340 } 341 if (colSelected >= 0) 342 { 343 tableModel.removeColumn(colSelected); 344 tableModel.fireTableDataChanged(); 345 346 if (tableModel.getColumnCount() == 0) 348 { 349 deleteColumnButton.setEnabled(false); 350 } 351 352 else 355 { 356 357 if (colSelected >= tableModel.getColumnCount()) 358 { 359 colSelected = colSelected - 1; 360 } 361 362 paramTable.setColumnSelectionInterval( 363 colSelected, 364 colSelected); 365 } 366 } 367 } 368 } 369 } 370 | Popular Tags |