1 18 19 package org.apache.jmeter.protocol.http.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.io.File ; 27 import java.io.IOException ; 28 29 import javax.swing.BorderFactory ; 30 import javax.swing.DefaultCellEditor ; 31 import javax.swing.JButton ; 32 import javax.swing.JPanel ; 33 import javax.swing.JPasswordField ; 34 import javax.swing.JScrollPane ; 35 import javax.swing.JTable ; 36 import javax.swing.ListSelectionModel ; 37 import javax.swing.border.Border ; 38 import javax.swing.border.EmptyBorder ; 39 import javax.swing.table.AbstractTableModel ; 40 import javax.swing.table.TableCellEditor ; 41 import javax.swing.table.TableCellRenderer ; 42 import javax.swing.table.TableColumn ; 43 44 import org.apache.jmeter.config.gui.AbstractConfigGui; 45 import org.apache.jmeter.gui.util.FileDialoger; 46 import org.apache.jmeter.protocol.http.control.AuthManager; 47 import org.apache.jmeter.protocol.http.control.Authorization; 48 import org.apache.jmeter.testelement.TestElement; 49 import org.apache.jmeter.util.JMeterUtils; 50 import org.apache.jorphan.logging.LoggingManager; 51 import org.apache.log.Logger; 52 53 60 public class AuthPanel extends AbstractConfigGui implements ActionListener 61 { 62 transient private static Logger log = LoggingManager.getLoggerForClass(); 63 64 private static final String ADD_COMMAND = "Add"; 65 private static final String DELETE_COMMAND = "Delete"; 66 private static final String LOAD_COMMAND = "Load"; 67 private static final String SAVE_COMMAND = "Save"; 68 69 private InnerTableModel tableModel; 70 71 74 private JTable authTable; 75 76 private JButton addButton; 77 private JButton deleteButton; 78 private JButton loadButton; 79 private JButton saveButton; 80 81 84 public AuthPanel() 85 { 86 tableModel = new InnerTableModel(); 87 init(); 88 } 89 90 public TestElement createTestElement() 91 { 92 AuthManager authMan = tableModel.manager; 93 configureTestElement(authMan); 94 return (TestElement) authMan.clone(); 95 } 96 97 101 public void modifyTestElement(TestElement el) 102 { 103 if (authTable.isEditing()) 104 { 105 authTable.getCellEditor().stopCellEditing(); 106 } 107 el.clear(); 108 el.addTestElement((TestElement) tableModel.manager.clone()); 109 configureTestElement(el); 110 } 111 112 public void configure(TestElement el) 113 { 114 super.configure(el); 115 tableModel.manager.clear(); 116 tableModel.manager.addTestElement((AuthManager) el.clone()); 117 } 118 119 public String getLabelResource() 120 { 121 return "auth_manager_title"; 122 } 123 124 127 public void init() 128 { 129 setLayout(new BorderLayout ()); 130 setBorder(makeBorder()); 131 132 add(makeTitlePanel(), BorderLayout.NORTH); 133 add(createAuthTablePanel(), BorderLayout.CENTER); 134 } 135 136 public void actionPerformed(ActionEvent e) 137 { 138 String action = e.getActionCommand(); 139 140 if (action.equals(DELETE_COMMAND)) 141 { 142 if (tableModel.getRowCount() > 0) 143 { 144 if (authTable.isEditing()) 147 { 148 TableCellEditor cellEditor = 149 authTable.getCellEditor( 150 authTable.getEditingRow(), 151 authTable.getEditingColumn()); 152 cellEditor.cancelCellEditing(); 153 } 154 155 int rowSelected = authTable.getSelectedRow(); 156 157 if (rowSelected != -1) 158 { 159 tableModel.removeRow(rowSelected); 160 tableModel.fireTableDataChanged(); 161 162 if (tableModel.getRowCount() == 0) 165 { 166 deleteButton.setEnabled(false); 167 saveButton.setEnabled(false); 168 } 169 170 else 173 { 174 int rowToSelect = rowSelected; 175 176 if (rowSelected >= tableModel.getRowCount()) 177 { 178 rowToSelect = rowSelected - 1; 179 } 180 181 authTable.setRowSelectionInterval( 182 rowToSelect, 183 rowToSelect); 184 } 185 } 186 } 187 } 188 else if (action.equals(ADD_COMMAND)) 189 { 190 if (authTable.isEditing()) 193 { 194 TableCellEditor cellEditor = 195 authTable.getCellEditor( 196 authTable.getEditingRow(), 197 authTable.getEditingColumn()); 198 cellEditor.stopCellEditing(); 199 } 200 201 tableModel.addNewRow(); 202 tableModel.fireTableDataChanged(); 203 204 if (!deleteButton.isEnabled()) 207 { 208 deleteButton.setEnabled(true); 209 } 210 if (!saveButton.isEnabled()) 211 { 212 saveButton.setEnabled(true); 213 } 214 215 int rowToSelect = tableModel.getRowCount() - 1; 217 authTable.setRowSelectionInterval(rowToSelect, rowToSelect); 218 } 219 else if (action.equals(LOAD_COMMAND)) 220 { 221 try 222 { 223 File tmp = FileDialoger.promptToOpenFile().getSelectedFile(); 224 if (tmp != null) 225 { 226 tableModel.manager.addFile(tmp.getAbsolutePath()); 227 tableModel.fireTableDataChanged(); 228 229 if (tableModel.getRowCount() > 0) 230 { 231 deleteButton.setEnabled(true); 232 saveButton.setEnabled(true); 233 } 234 } 235 } 236 catch (IOException ex) 237 { 238 log.error("", ex); 239 } 240 catch (NullPointerException err) 241 { 242 } 243 } 244 else if (action.equals(SAVE_COMMAND)) 245 { 246 try 247 { 248 File tmp = 249 FileDialoger.promptToSaveFile(null).getSelectedFile(); 250 if (tmp != null) 251 { 252 tableModel.manager.save(tmp.getAbsolutePath()); 253 } 254 } 255 catch (IOException ex) 256 { 257 log.error("", ex); 258 } 259 catch (NullPointerException err) 260 { 261 } 262 } 263 } 264 265 public JPanel createAuthTablePanel() 266 { 267 authTable = new JTable (tableModel); 269 authTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 270 authTable.setPreferredScrollableViewportSize(new Dimension (100, 70)); 271 272 TableColumn passwordColumn = authTable.getColumnModel().getColumn(2); 273 passwordColumn.setCellEditor( 274 new DefaultCellEditor (new JPasswordField ())); 275 passwordColumn.setCellRenderer(new PasswordCellRenderer()); 276 277 JPanel panel = new JPanel (new BorderLayout (0, 5)); 278 panel.setBorder( 279 BorderFactory.createTitledBorder( 280 BorderFactory.createEtchedBorder(), 281 JMeterUtils.getResString("auths_stored"))); 282 panel.add(new JScrollPane (authTable)); 283 panel.add(createButtonPanel(), BorderLayout.SOUTH); 284 return panel; 285 } 286 287 private JButton createButton( 288 String resName, 289 char mnemonic, 290 String command, 291 boolean enabled) 292 { 293 JButton button = new JButton (JMeterUtils.getResString(resName)); 294 button.setMnemonic(mnemonic); 295 button.setActionCommand(command); 296 button.setEnabled(enabled); 297 button.addActionListener(this); 298 return button; 299 } 300 301 private JPanel createButtonPanel() 302 { 303 boolean tableEmpty = (tableModel.getRowCount() == 0); 304 305 addButton = createButton("add", 'A', ADD_COMMAND, true); 306 deleteButton = createButton("delete", 'D', DELETE_COMMAND, !tableEmpty); 307 loadButton = createButton("load", 'L', LOAD_COMMAND, true); 308 saveButton = createButton("save", 'S', SAVE_COMMAND, !tableEmpty); 309 310 JPanel buttonPanel = new JPanel (); 312 buttonPanel.add(addButton); 313 buttonPanel.add(deleteButton); 314 buttonPanel.add(loadButton); 315 buttonPanel.add(saveButton); 316 return buttonPanel; 317 } 318 319 322 private class InnerTableModel extends AbstractTableModel 323 { 324 AuthManager manager; 325 326 public InnerTableModel(AuthManager man) 327 { 328 manager = man; 329 } 330 331 public InnerTableModel() 332 { 333 manager = new AuthManager(); 334 } 335 336 public void removeRow(int row) 337 { 338 manager.remove(row); 339 } 340 341 public void addNewRow() 342 { 343 manager.addAuth(); 344 } 345 346 public boolean isCellEditable(int row, int column) 347 { 348 return true; 350 } 351 352 public Class getColumnClass(int column) 353 { 354 return getValueAt(0, column).getClass(); 355 } 356 357 360 public int getRowCount() 361 { 362 return manager.getAuthObjects().size(); 363 } 364 365 368 public int getColumnCount() 369 { 370 return manager.getColumnCount(); 371 } 372 373 376 public String getColumnName(int column) 377 { 378 return manager.getColumnName(column); 379 } 380 381 384 public Object getValueAt(int row, int column) 385 { 386 Authorization auth = manager.getAuthObjectAt(row); 387 388 if (column == 0) 389 { 390 return auth.getURL(); 391 } 392 else if (column == 1) 393 { 394 return auth.getUser(); 395 } 396 else if (column == 2) 397 { 398 return auth.getPass(); 399 } 400 return null; 401 } 402 403 public void setValueAt(Object value, int row, int column) 404 { 405 Authorization auth = manager.getAuthObjectAt(row); 406 log.debug("Setting auth value: " + value); 407 if (column == 0) 408 { 409 auth.setURL((String ) value); 410 } 411 else if (column == 1) 412 { 413 auth.setUser((String ) value); 414 } 415 else if (column == 2) 416 { 417 auth.setPass((String ) value); 418 } 419 } 420 } 421 422 425 private class PasswordCellRenderer 426 extends JPasswordField 427 implements TableCellRenderer 428 { 429 private Border myBorder; 430 431 public PasswordCellRenderer() 432 { 433 super(); 434 myBorder = new EmptyBorder (1, 2, 1, 2); 435 setOpaque(true); 436 setBorder(myBorder); 437 } 438 439 public Component getTableCellRendererComponent( 440 JTable table, 441 Object value, 442 boolean isSelected, 443 boolean hasFocus, 444 int row, 445 int column) 446 { 447 setText((String ) value); 448 449 setBackground( 450 isSelected 451 && !hasFocus 452 ? table.getSelectionBackground() 453 : table.getBackground()); 454 setForeground( 455 isSelected 456 && !hasFocus 457 ? table.getSelectionForeground() 458 : table.getForeground()); 459 460 setFont(table.getFont()); 461 462 return this; 463 } 464 } 465 } 466 | Popular Tags |