1 18 19 package org.apache.jmeter.config.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 26 import javax.swing.Box ; 27 import javax.swing.JButton ; 28 import javax.swing.JPanel ; 29 import javax.swing.JTable ; 30 import javax.swing.ListSelectionModel ; 31 import javax.swing.table.TableCellEditor ; 32 33 import org.apache.jmeter.config.ConfigTestElement; 34 import org.apache.jmeter.gui.util.PowerTableModel; 35 import org.apache.jmeter.testelement.TestElement; 36 import org.apache.jmeter.testelement.property.JMeterProperty; 37 import org.apache.jmeter.testelement.property.PropertyIterator; 38 import org.apache.jmeter.testelement.property.StringProperty; 39 import org.apache.jmeter.util.JMeterUtils; 40 import org.apache.jorphan.collections.Data; 41 42 47 public class SimpleConfigGui extends AbstractConfigGui implements 48 ActionListener 49 { 50 51 52 55 private JTable table; 56 57 58 private PowerTableModel tableModel; 59 60 61 private JButton add; 62 63 64 private JButton delete; 65 66 67 private static final String ADD = "add"; 68 69 70 private static final String DELETE = "delete"; 71 72 77 private boolean displayName = true; 78 79 80 public static final String [] COLUMN_NAMES = { 81 JMeterUtils.getResString("name"), JMeterUtils.getResString("value"), 82 JMeterUtils.getResString("metadata")}; 83 84 87 public SimpleConfigGui() 88 { 89 this(true); 90 } 91 92 102 public SimpleConfigGui(boolean displayName) 103 { 104 this.displayName = displayName; 105 init(); 106 } 107 108 public String getLabelResource() 109 { 110 return "simple_config_element"; 111 } 112 113 125 public void configure(TestElement el) 126 { 127 super.configure(el); 128 tableModel.clearData(); 129 PropertyIterator iter = el.propertyIterator(); 130 while (iter.hasNext()) 131 { 132 JMeterProperty prop = iter.next(); 133 tableModel 134 .addRow(new Object [] { prop.getName(), prop.getStringValue()}); 135 } 136 checkDeleteStatus(); 137 } 138 139 140 public TestElement createTestElement() 141 { 142 TestElement el = new ConfigTestElement(); 143 modifyTestElement(el); 144 return el; 145 } 146 147 154 public void modifyTestElement(TestElement el) 155 { 156 if (table.isEditing()) 157 { 158 table.getCellEditor().stopCellEditing(); 159 } 160 Data model = tableModel.getData(); 161 model.reset(); 162 while (model.next()) 163 { 164 el.setProperty(new StringProperty((String ) model 165 .getColumnValue(COLUMN_NAMES[0]), (String ) model 166 .getColumnValue(COLUMN_NAMES[1]))); 167 } 168 super.configureTestElement(el); 169 } 170 171 174 private void init() 175 { 176 setLayout(new BorderLayout (0, 10)); 177 178 if (displayName) 179 { 180 setBorder(makeBorder()); 181 add(makeTitlePanel(), BorderLayout.NORTH); 182 } 183 184 add(createTablePanel(), BorderLayout.CENTER); 185 add(Box.createVerticalStrut(70), BorderLayout.WEST); 187 add(createButtonPanel(), BorderLayout.SOUTH); 188 } 189 190 197 public void actionPerformed(ActionEvent e) 198 { 199 String action = e.getActionCommand(); 200 if (action.equals(DELETE)) 201 { 202 deleteArgument(); 203 } 204 else if (action.equals(ADD)) 205 { 206 addArgument(); 207 } 208 } 209 210 215 private Component createTablePanel() 216 { 217 tableModel = new PowerTableModel(new String [] { COLUMN_NAMES[0], 218 COLUMN_NAMES[1]}, new Class [] { String .class, String .class}); 219 220 table = new JTable (tableModel); 221 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 222 return makeScrollPane(table); 223 } 224 225 230 private JPanel createButtonPanel() 231 { 232 add = new JButton (JMeterUtils.getResString("add")); 233 add.setActionCommand(ADD); 234 add.addActionListener(this); 235 add.setEnabled(true); 236 237 delete = new JButton (JMeterUtils.getResString("delete")); 238 delete.setActionCommand(DELETE); 239 delete.addActionListener(this); 240 241 checkDeleteStatus(); 242 243 JPanel buttonPanel = new JPanel (); 244 buttonPanel.add(add); 245 buttonPanel.add(delete); 246 return buttonPanel; 247 } 248 249 253 protected void checkDeleteStatus() 254 { 255 if (tableModel.getRowCount() == 0) 257 { 258 delete.setEnabled(false); 259 } 260 else 261 { 262 delete.setEnabled(true); 263 } 264 } 265 266 269 protected void addArgument() 270 { 271 stopTableEditing(); 274 275 tableModel.addNewRow(); 276 tableModel.fireTableDataChanged(); 277 278 delete.setEnabled(true); 280 281 int rowToSelect = tableModel.getRowCount() - 1; 283 table.setRowSelectionInterval(rowToSelect, rowToSelect); 284 } 285 286 290 protected void stopTableEditing() 291 { 292 if (table.isEditing()) 293 { 294 TableCellEditor cellEditor = table.getCellEditor( 295 table.getEditingRow(), table.getEditingColumn()); 296 cellEditor.stopCellEditing(); 297 } 298 } 299 300 303 protected void deleteArgument() 304 { 305 if (table.isEditing()) 308 { 309 TableCellEditor cellEditor = table.getCellEditor( 310 table.getEditingRow(), table.getEditingColumn()); 311 cellEditor.cancelCellEditing(); 312 } 313 314 int rowSelected = table.getSelectedRow(); 315 316 if (rowSelected >= 0) 317 { 318 319 tableModel.removeRow(rowSelected); 322 tableModel.fireTableDataChanged(); 323 324 if (tableModel.getRowCount() == 0) 326 { 327 delete.setEnabled(false); 328 } 329 else 330 { 331 int rowToSelect = rowSelected; 334 335 if (rowSelected >= tableModel.getRowCount()) 336 { 337 rowToSelect = rowSelected - 1; 338 } 339 340 table.setRowSelectionInterval(rowToSelect, rowToSelect); 341 } 342 } 343 } 344 } 345 | Popular Tags |