e.g. Calendar Search Help
You must enter a value before pressing Search
jmeter

Class: org.apache.jmeter.protocol.http.gui.CookiePanel   ©

 OK to copy?
001 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/gui/CookiePanel.java,v 1.18.2.1 2004/06/12 16:44:34 mstover1 Exp $
002 /*
003  * Copyright 2001-2004 The Apache Software Foundation.
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *   http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  * 
017 */
018 
019 package org.apache.jmeter.protocol.http.gui;
020 
021 import java.awt.BorderLayout;
022 import java.awt.Dimension;
023 import java.awt.event.ActionEvent;
024 import java.awt.event.ActionListener;
025 import java.io.File;
026 import java.io.IOException;
027 
028 import javax.swing.BorderFactory;
029 import javax.swing.JButton;
030 import javax.swing.JCheckBox;
031 import javax.swing.JPanel;
032 import javax.swing.JScrollPane;
033 import javax.swing.JTable;
034 import javax.swing.ListSelectionModel;
035 import javax.swing.table.TableCellEditor;
036 
037 import org.apache.jmeter.config.gui.AbstractConfigGui;
038 import org.apache.jmeter.gui.util.FileDialoger;
039 import org.apache.jmeter.gui.util.PowerTableModel;
040 import org.apache.jmeter.protocol.http.control.Cookie;
041 import org.apache.jmeter.protocol.http.control.CookieManager;
042 import org.apache.jmeter.testelement.TestElement;
043 import org.apache.jmeter.testelement.property.PropertyIterator;
044 import org.apache.jmeter.util.JMeterUtils;
045 import org.apache.jorphan.gui.layout.VerticalLayout;
046 import org.apache.jorphan.logging.LoggingManager;
047 import org.apache.jorphan.util.JOrphanUtils;
048 import org.apache.log.Logger;
049 
050 /**
051  * Allows the user to specify if she needs cookie services, and give parameters
052  * for this service.
053  *
054  * @version   $Revision: 1.18.2.1 $
055  */
056 public class CookiePanel extends AbstractConfigGui implements ActionListener
057 {
058     transient private static Logger log = LoggingManager.getLoggerForClass();
059 
060     private static final String ADD_COMMAND = "Add";
061     private static final String DELETE_COMMAND = "Delete";
062     private static final String LOAD_COMMAND = "Load";
063     private static final String SAVE_COMMAND = "Save";
064 
065     private JTable cookieTable;
066     private PowerTableModel tableModel;
067     private JCheckBox clearEachIteration;
068     private static final String clearEachIterationLabel =
069         "clear_cookies_per_iter";
070 
071     private static final String[] columnNames =
072         {
073             JMeterUtils.getResString("name"),
074             JMeterUtils.getResString("value"),
075             JMeterUtils.getResString("domain"),
076             JMeterUtils.getResString("path"),
077             JMeterUtils.getResString("secure"),
078             JMeterUtils.getResString("expiration"),
079             };
080 
081     private static final Class[] columnClasses =
082         {
083             String.class,
084             String.class,
085             String.class,
086             String.class,
087             Boolean.class,
088             Long.class,
089             };
090 
091     private JButton addButton;
092     private JButton deleteButton;
093     private JButton loadButton;
094     private JButton saveButton;
095 
096     /**
097      * Default constructor.
098      */
099     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 a table cell is being edited, we must cancel the editing
123                 // before deleting the row.
124                 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                     // Disable the DELETE and SAVE buttons if no rows remaining
141                     // after delete.
142                     if (tableModel.getRowCount() == 0)
143                     {
144                         deleteButton.setEnabled(false);
145                         saveButton.setEnabled(false);
146                     }
147 
148                     // Table still contains one or more rows, so highlight
149                     // (select) the appropriate one.
150                     else
151                     {
152                         int rowToSelect = rowSelected;
153 
154                         if (rowSelected >= tableModel.getRowCount())
155                         {
156                             rowToSelect = rowSelected - 1;
157                         }
158 
Rate159                         cookieTable.setRowSelectionInterval(
160                             rowToSelect,
161                             rowToSelect);
162                     }
163                 }
164             }
165         }
166         else if (action.equals("Add"))
167         {
168             // If a table cell is being edited, we should accept the current
169             // value and stop the editing before adding a new row.
170             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             // Enable the DELETE and SAVE buttons if they are currently
183             // disabled.
184             if (!deleteButton.isEnabled())
185             {
186                 deleteButton.setEnabled(true);
187             }
188             if (!saveButton.isEnabled())
189             {
190                 saveButton.setEnabled(true);
191             }
192 
193             // Highlight (select) the appropriate row.
194             int rowToSelect = tableModel.getRowCount() - 1;
Rate195             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     /**
260      * Modifies a given TestElement to mirror the data in the gui components.
261      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
262      */
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     /**
323      * Shows the main cookie configuration panel.
324      */
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         // create the JTable that holds one cookie per row
340         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 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
159 0% of 0
195 0% of 0