1 18 19 package org.apache.jmeter.config.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Component ; 23 import java.awt.FlowLayout ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 29 import javax.swing.BorderFactory ; 30 import javax.swing.Box ; 31 import javax.swing.JButton ; 32 import javax.swing.JLabel ; 33 import javax.swing.JPanel ; 34 import javax.swing.JTable ; 35 import javax.swing.ListSelectionModel ; 36 import javax.swing.table.TableCellEditor ; 37 38 import junit.framework.TestCase; 39 40 import org.apache.jmeter.config.Argument; 41 import org.apache.jmeter.config.Arguments; 42 import org.apache.jmeter.testelement.TestElement; 43 import org.apache.jmeter.testelement.property.PropertyIterator; 44 import org.apache.jmeter.util.JMeterUtils; 45 import org.apache.jorphan.gui.ObjectTableModel; 46 47 54 public class ArgumentsPanel 55 extends AbstractConfigGui 56 implements ActionListener 57 { 58 59 60 private JLabel tableLabel; 61 62 63 private transient JTable table; 64 65 66 protected transient ObjectTableModel tableModel; 67 68 69 private JButton add; 70 71 72 private JButton delete; 73 74 78 private boolean standalone = true; 79 80 81 private static final String ADD = "add"; 82 83 84 private static final String DELETE = "delete"; 85 86 public static final String [] COLUMN_NAMES = 87 { 88 JMeterUtils.getResString("name"), 89 JMeterUtils.getResString("value"), 90 JMeterUtils.getResString("metadata") 91 }; 92 93 96 public ArgumentsPanel() 97 { 98 tableLabel= new JLabel (JMeterUtils.getResString("user_defined_variables")); 99 standalone = true; 100 init(); 101 } 102 103 109 public ArgumentsPanel(String label) 110 { 111 tableLabel = new JLabel (label); 112 standalone = false; 113 init(); 114 } 115 116 123 public Collection getMenuCategories() 124 { 125 if (standalone) 126 { 127 return super.getMenuCategories(); 128 } 129 else 130 { 131 return null; 132 } 133 } 134 135 public String getLabelResource() 136 { 137 return "user_defined_variables"; 138 } 139 140 141 public TestElement createTestElement() 142 { 143 Arguments args = new Arguments(); 144 modifyTestElement(args); 145 return args; 146 } 147 148 149 public void modifyTestElement(TestElement args) 150 { 151 stopTableEditing(); 152 Iterator modelData = tableModel.iterator(); 153 Arguments arguments = null; 154 if (args instanceof Arguments) 155 { 156 arguments = (Arguments) args; 157 arguments.clear(); 158 while (modelData.hasNext()) 159 { 160 Argument arg = (Argument) modelData.next(); 161 arg.setMetaData("="); 162 arguments.addArgument(arg); 163 } 164 } 165 this.configureTestElement(args); 166 } 167 168 176 public void configure(TestElement el) 177 { 178 super.configure(el); 179 if (el instanceof Arguments) 180 { 181 tableModel.clearData(); 182 PropertyIterator iter = ((Arguments) el).iterator(); 183 while (iter.hasNext()) 184 { 185 Argument arg = (Argument) iter.next().getObjectValue(); 186 tableModel.addRow(arg); 187 } 188 } 189 checkDeleteStatus(); 190 } 191 192 197 protected JTable getTable() 198 { 199 return table; 200 } 201 202 207 protected JLabel getTableLabel() 208 { 209 return tableLabel; 210 } 211 212 217 protected JButton getDeleteButton() 218 { 219 return delete; 220 } 221 222 227 protected JButton getAddButton() 228 { 229 return add; 230 } 231 232 236 protected void checkDeleteStatus() 237 { 238 if (tableModel.getRowCount() == 0) 240 { 241 delete.setEnabled(false); 242 } 243 else 244 { 245 delete.setEnabled(true); 246 } 247 } 248 249 253 public void clear() 254 { 255 stopTableEditing(); 256 tableModel.clearData(); 257 } 258 259 265 public void actionPerformed(ActionEvent e) 266 { 267 String action = e.getActionCommand(); 268 if (action.equals(DELETE)) 269 { 270 deleteArgument(); 271 } 272 else if (action.equals(ADD)) 273 { 274 addArgument(); 275 } 276 } 277 278 281 protected void deleteArgument() 282 { 283 if (table.isEditing()) 286 { 287 TableCellEditor cellEditor = table.getCellEditor( 288 table.getEditingRow(), 289 table.getEditingColumn()); 290 cellEditor.cancelCellEditing(); 291 } 292 293 int rowSelected = table.getSelectedRow(); 294 if (rowSelected >= 0) 295 { 296 tableModel.removeRow(rowSelected); 297 tableModel.fireTableDataChanged(); 298 299 if (tableModel.getRowCount() == 0) 301 { 302 delete.setEnabled(false); 303 } 304 305 else 308 { 309 int rowToSelect = rowSelected; 310 311 if (rowSelected >= tableModel.getRowCount()) 312 { 313 rowToSelect = rowSelected - 1; 314 } 315 316 table.setRowSelectionInterval(rowToSelect, rowToSelect); 317 } 318 } 319 } 320 321 324 protected void addArgument() 325 { 326 stopTableEditing(); 329 330 tableModel.addRow(makeNewArgument()); 331 332 delete.setEnabled(true); 334 335 int rowToSelect = tableModel.getRowCount() - 1; 337 table.setRowSelectionInterval(rowToSelect, rowToSelect); 338 } 339 340 344 protected Object makeNewArgument() 345 { 346 return new Argument("", ""); 347 } 348 349 353 protected void stopTableEditing() 354 { 355 if (table.isEditing()) 356 { 357 TableCellEditor cellEditor = 358 table.getCellEditor( 359 table.getEditingRow(), 360 table.getEditingColumn()); 361 cellEditor.stopCellEditing(); 362 } 363 } 364 365 368 protected void initializeTableModel() 369 { 370 tableModel = 371 new ObjectTableModel( 372 new String [] { 373 COLUMN_NAMES[0], 374 COLUMN_NAMES[1] }, 375 new String [] { "name", "value" }, 376 new Class [] { String .class, String .class }, 377 new Class [] { String .class, String .class }, 378 new Argument()); 379 } 380 381 385 protected void sizeColumns(JTable table) 386 { 387 } 388 389 394 private Component makeMainPanel() 395 { 396 initializeTableModel(); 397 table = new JTable (tableModel); 398 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 399 return makeScrollPane(table); 400 } 401 402 407 protected Component makeLabelPanel() 408 { 409 JPanel labelPanel = new JPanel (new FlowLayout (FlowLayout.CENTER)); 410 labelPanel.add(tableLabel); 411 return labelPanel; 412 } 413 414 419 private JPanel makeButtonPanel() 420 { 421 add = new JButton (JMeterUtils.getResString("add")); 422 add.setActionCommand(ADD); 423 add.setEnabled(true); 424 425 delete = new JButton (JMeterUtils.getResString("delete")); 426 delete.setActionCommand(DELETE); 427 428 checkDeleteStatus(); 429 430 JPanel buttonPanel = new JPanel (); 431 buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10)); 432 add.addActionListener(this); 433 delete.addActionListener(this); 434 buttonPanel.add(add); 435 buttonPanel.add(delete); 436 return buttonPanel; 437 } 438 439 442 private void init() 443 { 444 JPanel p= this; 445 446 if (standalone) 447 { 448 setLayout(new BorderLayout (0,5)); 449 setBorder(makeBorder()); 450 add(makeTitlePanel(), BorderLayout.NORTH); 451 p= new JPanel (); 452 } 453 454 p.setLayout(new BorderLayout ()); 455 456 p.add(makeLabelPanel(), BorderLayout.NORTH); 457 p.add(makeMainPanel(), BorderLayout.CENTER); 458 p.add(Box.createVerticalStrut(70), BorderLayout.WEST); 460 p.add(makeButtonPanel(), BorderLayout.SOUTH); 461 462 if (standalone) 463 { 464 add(p, BorderLayout.CENTER); 465 } 466 467 table.revalidate(); 468 sizeColumns(table); 469 } 470 471 472 475 public static class Test extends TestCase 476 { 477 481 public Test(String name) 482 { 483 super(name); 484 } 485 486 492 public void testArgumentCreation() throws Exception 493 { 494 ArgumentsPanel gui = new ArgumentsPanel(); 495 gui.tableModel.addRow(new Argument()); 496 gui.tableModel.setValueAt("howdy", 0, 0); 497 gui.tableModel.addRow(new Argument()); 498 gui.tableModel.setValueAt("doody", 0, 1); 499 500 assertEquals( 501 "=", 502 ((Argument) ((Arguments) gui.createTestElement()) 503 .getArguments() 504 .get(0) 505 .getObjectValue()) 506 .getMetaData()); 507 } 508 } 509 } 510 | Popular Tags |