KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > gui > CookiePanel


1 // $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 $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.gui;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import javax.swing.BorderFactory JavaDoc;
29 import javax.swing.JButton JavaDoc;
30 import javax.swing.JCheckBox JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.JScrollPane JavaDoc;
33 import javax.swing.JTable JavaDoc;
34 import javax.swing.ListSelectionModel JavaDoc;
35 import javax.swing.table.TableCellEditor JavaDoc;
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 /**
51  * Allows the user to specify if she needs cookie services, and give parameters
52  * for this service.
53  *
54  * @version $Revision: 1.18.2.1 $
55  */

56 public class CookiePanel extends AbstractConfigGui implements ActionListener JavaDoc
57 {
58     transient private static Logger log = LoggingManager.getLoggerForClass();
59
60     private static final String JavaDoc ADD_COMMAND = "Add";
61     private static final String JavaDoc DELETE_COMMAND = "Delete";
62     private static final String JavaDoc LOAD_COMMAND = "Load";
63     private static final String JavaDoc SAVE_COMMAND = "Save";
64
65     private JTable JavaDoc cookieTable;
66     private PowerTableModel tableModel;
67     private JCheckBox JavaDoc clearEachIteration;
68     private static final String JavaDoc clearEachIterationLabel =
69         "clear_cookies_per_iter";
70
71     private static final String JavaDoc[] 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 JavaDoc[] columnClasses =
82         {
83             String JavaDoc.class,
84             String JavaDoc.class,
85             String JavaDoc.class,
86             String JavaDoc.class,
87             Boolean JavaDoc.class,
88             Long JavaDoc.class,
89             };
90
91     private JButton JavaDoc addButton;
92     private JButton JavaDoc deleteButton;
93     private JButton JavaDoc loadButton;
94     private JButton JavaDoc saveButton;
95
96     /**
97      * Default constructor.
98      */

99     public CookiePanel()
100     {
101         tableModel = new PowerTableModel(columnNames, columnClasses);
102         clearEachIteration =
103             new JCheckBox JavaDoc(
104                 JMeterUtils.getResString(clearEachIterationLabel),
105                 false);
106         init();
107     }
108
109     public String JavaDoc getLabelResource()
110     {
111         return "cookie_manager_title";
112     }
113
114     public void actionPerformed(ActionEvent JavaDoc e)
115     {
116         String JavaDoc 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 JavaDoc 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
159                         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 JavaDoc 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;
195             cookieTable.setRowSelectionInterval(rowToSelect, rowToSelect);
196         }
197         else if (action.equals("Load"))
198         {
199             try
200             {
201                 File JavaDoc 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 JavaDoc ex)
218             {
219                 log.error("", ex);
220             }
221             catch (NullPointerException JavaDoc err)
222             {
223             }
224         }
225         else if (action.equals("Save"))
226         {
227             try
228             {
229                 File JavaDoc tmp =
230                     FileDialoger.promptToSaveFile(null).getSelectedFile();
231                 if (tmp != null)
232                 {
233                     ((CookieManager) createTestElement()).save(
234                         tmp.getAbsolutePath());
235                 }
236             }
237             catch (IOException JavaDoc ex)
238             {
239                 log.error("", ex);
240             }
241             catch (NullPointerException JavaDoc err)
242             {
243             }
244         }
245     }
246
247     private void addCookieToTable(Cookie cookie)
248     {
249         tableModel.addRow(
250             new Object JavaDoc[] {
251                 cookie.getName(),
252                 cookie.getValue(),
253                 cookie.getDomain(),
254                 cookie.getPath(),
255                 JOrphanUtils.valueOf(cookie.getSecure()),
256                 new Long JavaDoc(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 JavaDoc[] rowData)
285     {
286         Cookie cookie =
287             new Cookie(
288                 (String JavaDoc) rowData[0],
289                 (String JavaDoc) rowData[1],
290                 (String JavaDoc) rowData[2],
291                 (String JavaDoc) rowData[3],
292                 ((Boolean JavaDoc) rowData[4]).booleanValue(),
293                 ((Long JavaDoc) 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 JavaDoc());
328         setBorder(makeBorder());
329         JPanel JavaDoc northPanel = new JPanel JavaDoc();
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 JavaDoc createCookieTablePanel()
338     {
339         // create the JTable that holds one cookie per row
340
cookieTable = new JTable JavaDoc(tableModel);
341         cookieTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
342         cookieTable.setPreferredScrollableViewportSize(new Dimension JavaDoc(100, 70));
343
344         JPanel JavaDoc buttonPanel = createButtonPanel();
345
346         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc(0, 5));
347         panel.setBorder(
348             BorderFactory.createTitledBorder(
349                 BorderFactory.createEtchedBorder(),
350                 JMeterUtils.getResString("cookies_stored")));
351
352         panel.add(new JScrollPane JavaDoc(cookieTable), BorderLayout.CENTER);
353         panel.add(buttonPanel, BorderLayout.SOUTH);
354         return panel;
355     }
356
357     private JButton JavaDoc createButton(
358         String JavaDoc resName,
359         char mnemonic,
360         String JavaDoc command,
361         boolean enabled)
362     {
363         JButton JavaDoc button = new JButton JavaDoc(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 JavaDoc 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 JavaDoc buttonPanel = new JPanel JavaDoc();
381         buttonPanel.add(addButton);
382         buttonPanel.add(deleteButton);
383         buttonPanel.add(loadButton);
384         buttonPanel.add(saveButton);
385         return buttonPanel;
386     }
387 }
388
Popular Tags