1 18 19 package org.apache.jmeter.protocol.http.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Dimension ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.io.File ; 26 import java.io.IOException ; 27 28 import javax.swing.BorderFactory ; 29 import javax.swing.JButton ; 30 import javax.swing.JCheckBox ; 31 import javax.swing.JPanel ; 32 import javax.swing.JScrollPane ; 33 import javax.swing.JTable ; 34 import javax.swing.ListSelectionModel ; 35 import javax.swing.table.TableCellEditor ; 36 37 import org.apache.jmeter.config.gui.AbstractConfigGui; 38 import org.apache.jmeter.gui.util.FileDialoger; 39 import org.apache.jmeter.gui.util.PowerTableModel; 40 import org.apache.jmeter.protocol.http.control.Cookie; 41 import org.apache.jmeter.protocol.http.control.CookieManager; 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.layout.VerticalLayout; 46 import org.apache.jorphan.logging.LoggingManager; 47 import org.apache.jorphan.util.JOrphanUtils; 48 import org.apache.log.Logger; 49 50 56 public class CookiePanel extends AbstractConfigGui implements ActionListener 57 { 58 transient private static Logger log = LoggingManager.getLoggerForClass(); 59 60 private static final String ADD_COMMAND = "Add"; 61 private static final String DELETE_COMMAND = "Delete"; 62 private static final String LOAD_COMMAND = "Load"; 63 private static final String SAVE_COMMAND = "Save"; 64 65 private JTable cookieTable; 66 private PowerTableModel tableModel; 67 private JCheckBox clearEachIteration; 68 private static final String clearEachIterationLabel = 69 "clear_cookies_per_iter"; 70 71 private static final String [] columnNames = 72 { 73 JMeterUtils.getResString("name"), 74 JMeterUtils.getResString("value"), 75 JMeterUtils.getResString("domain"), 76 JMeterUtils.getResString("path"), 77 JMeterUtils.getResString("secure"), 78 JMeterUtils.getResString("expiration"), 79 }; 80 81 private static final Class [] columnClasses = 82 { 83 String .class, 84 String .class, 85 String .class, 86 String .class, 87 Boolean .class, 88 Long .class, 89 }; 90 91 private JButton addButton; 92 private JButton deleteButton; 93 private JButton loadButton; 94 private JButton saveButton; 95 96 99 public CookiePanel() 100 { 101 tableModel = new PowerTableModel(columnNames, columnClasses); 102 clearEachIteration = 103 new JCheckBox ( 104 JMeterUtils.getResString(clearEachIterationLabel), 105 false); 106 init(); 107 } 108 109 public String getLabelResource() 110 { 111 return "cookie_manager_title"; 112 } 113 114 public void actionPerformed(ActionEvent e) 115 { 116 String action = e.getActionCommand(); 117 118 if (action.equals("Delete")) 119 { 120 if (tableModel.getRowCount() > 0) 121 { 122 if (cookieTable.isEditing()) 125 { 126 TableCellEditor cellEditor = 127 cookieTable.getCellEditor( 128 cookieTable.getEditingRow(), 129 cookieTable.getEditingColumn()); 130 cellEditor.cancelCellEditing(); 131 } 132 133 int rowSelected = cookieTable.getSelectedRow(); 134 135 if (rowSelected != -1) 136 { 137 tableModel.removeRow(rowSelected); 138 tableModel.fireTableDataChanged(); 139 140 if (tableModel.getRowCount() == 0) 143 { 144 deleteButton.setEnabled(false); 145 saveButton.setEnabled(false); 146 } 147 148 else 151 { 152 int rowToSelect = rowSelected; 153 154 if (rowSelected >= tableModel.getRowCount()) 155 { 156 rowToSelect = rowSelected - 1; 157 } 158 159 cookieTable.setRowSelectionInterval( 160 rowToSelect, 161 rowToSelect); 162 } 163 } 164 } 165 } 166 else if (action.equals("Add")) 167 { 168 if (cookieTable.isEditing()) 171 { 172 TableCellEditor cellEditor = 173 cookieTable.getCellEditor( 174 cookieTable.getEditingRow(), 175 cookieTable.getEditingColumn()); 176 cellEditor.stopCellEditing(); 177 } 178 179 tableModel.addNewRow(); 180 tableModel.fireTableDataChanged(); 181 182 if (!deleteButton.isEnabled()) 185 { 186 deleteButton.setEnabled(true); 187 } 188 if (!saveButton.isEnabled()) 189 { 190 saveButton.setEnabled(true); 191 } 192 193 int rowToSelect = tableModel.getRowCount() - 1; 195 cookieTable.setRowSelectionInterval(rowToSelect, rowToSelect); 196 } 197 else if (action.equals("Load")) 198 { 199 try 200 { 201 File tmp = FileDialoger.promptToOpenFile().getSelectedFile(); 202 if (tmp != null) 203 { 204 CookieManager manager = new CookieManager(); 205 manager.addFile(tmp.getAbsolutePath()); 206 Cookie cookie = manager.get(0); 207 addCookieToTable(cookie); 208 tableModel.fireTableDataChanged(); 209 210 if (tableModel.getRowCount() > 0) 211 { 212 deleteButton.setEnabled(true); 213 saveButton.setEnabled(true); 214 } 215 } 216 } 217 catch (IOException ex) 218 { 219 log.error("", ex); 220 } 221 catch (NullPointerException err) 222 { 223 } 224 } 225 else if (action.equals("Save")) 226 { 227 try 228 { 229 File tmp = 230 FileDialoger.promptToSaveFile(null).getSelectedFile(); 231 if (tmp != null) 232 { 233 ((CookieManager) createTestElement()).save( 234 tmp.getAbsolutePath()); 235 } 236 } 237 catch (IOException ex) 238 { 239 log.error("", ex); 240 } 241 catch (NullPointerException err) 242 { 243 } 244 } 245 } 246 247 private void addCookieToTable(Cookie cookie) 248 { 249 tableModel.addRow( 250 new Object [] { 251 cookie.getName(), 252 cookie.getValue(), 253 cookie.getDomain(), 254 cookie.getPath(), 255 JOrphanUtils.valueOf(cookie.getSecure()), 256 new Long (cookie.getExpires())}); 257 } 258 259 263 public void modifyTestElement(TestElement cm) 264 { 265 if (cookieTable.isEditing()) 266 { 267 cookieTable.getCellEditor().stopCellEditing(); 268 } 269 cm.clear(); 270 configureTestElement(cm); 271 if (cm instanceof CookieManager) 272 { 273 CookieManager cookieManager = (CookieManager) cm; 274 for (int i = 0; i < tableModel.getRowCount(); i++) 275 { 276 Cookie cookie = createCookie(tableModel.getRowData(i)); 277 cookieManager.add(cookie); 278 } 279 cookieManager.setClearEachIteration( 280 clearEachIteration.isSelected()); 281 } 282 } 283 284 private Cookie createCookie(Object [] rowData) 285 { 286 Cookie cookie = 287 new Cookie( 288 (String ) rowData[0], 289 (String ) rowData[1], 290 (String ) rowData[2], 291 (String ) rowData[3], 292 ((Boolean ) rowData[4]).booleanValue(), 293 ((Long ) rowData[5]).longValue()); 294 return cookie; 295 } 296 297 private void populateTable(CookieManager manager) 298 { 299 tableModel.clearData(); 300 PropertyIterator iter = manager.getCookies().iterator(); 301 while (iter.hasNext()) 302 { 303 addCookieToTable((Cookie) iter.next().getObjectValue()); 304 } 305 } 306 307 public TestElement createTestElement() 308 { 309 CookieManager cookieManager = new CookieManager(); 310 modifyTestElement(cookieManager); 311 return cookieManager; 312 } 313 314 public void configure(TestElement el) 315 { 316 super.configure(el); 317 populateTable((CookieManager) el); 318 clearEachIteration.setSelected( 319 ((CookieManager) el).getClearEachIteration()); 320 } 321 322 325 public void init() 326 { 327 setLayout(new BorderLayout ()); 328 setBorder(makeBorder()); 329 JPanel northPanel = new JPanel (); 330 northPanel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT)); 331 northPanel.add(makeTitlePanel()); 332 northPanel.add(clearEachIteration); 333 add(northPanel, BorderLayout.NORTH); 334 add(createCookieTablePanel(), BorderLayout.CENTER); 335 } 336 337 public JPanel createCookieTablePanel() 338 { 339 cookieTable = new JTable (tableModel); 341 cookieTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 342 cookieTable.setPreferredScrollableViewportSize(new Dimension (100, 70)); 343 344 JPanel buttonPanel = createButtonPanel(); 345 346 JPanel panel = new JPanel (new BorderLayout (0, 5)); 347 panel.setBorder( 348 BorderFactory.createTitledBorder( 349 BorderFactory.createEtchedBorder(), 350 JMeterUtils.getResString("cookies_stored"))); 351 352 panel.add(new JScrollPane (cookieTable), BorderLayout.CENTER); 353 panel.add(buttonPanel, BorderLayout.SOUTH); 354 return panel; 355 } 356 357 private JButton createButton( 358 String resName, 359 char mnemonic, 360 String command, 361 boolean enabled) 362 { 363 JButton button = new JButton (JMeterUtils.getResString(resName)); 364 button.setMnemonic(mnemonic); 365 button.setActionCommand(command); 366 button.setEnabled(enabled); 367 button.addActionListener(this); 368 return button; 369 } 370 371 private JPanel createButtonPanel() 372 { 373 boolean tableEmpty = (tableModel.getRowCount() == 0); 374 375 addButton = createButton("add", 'A', ADD_COMMAND, true); 376 deleteButton = createButton("delete", 'D', DELETE_COMMAND, !tableEmpty); 377 loadButton = createButton("load", 'L', LOAD_COMMAND, true); 378 saveButton = createButton("save", 'S', SAVE_COMMAND, !tableEmpty); 379 380 JPanel buttonPanel = new JPanel (); 381 buttonPanel.add(addButton); 382 buttonPanel.add(deleteButton); 383 buttonPanel.add(loadButton); 384 buttonPanel.add(saveButton); 385 return buttonPanel; 386 } 387 } 388 | Popular Tags |