KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/gui/AuthPanel.java,v 1.15.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.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import javax.swing.BorderFactory JavaDoc;
30 import javax.swing.DefaultCellEditor JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33 import javax.swing.JPasswordField JavaDoc;
34 import javax.swing.JScrollPane JavaDoc;
35 import javax.swing.JTable JavaDoc;
36 import javax.swing.ListSelectionModel JavaDoc;
37 import javax.swing.border.Border JavaDoc;
38 import javax.swing.border.EmptyBorder JavaDoc;
39 import javax.swing.table.AbstractTableModel JavaDoc;
40 import javax.swing.table.TableCellEditor JavaDoc;
41 import javax.swing.table.TableCellRenderer JavaDoc;
42 import javax.swing.table.TableColumn JavaDoc;
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 /**
54  * Handles input for determining if authentication services are required for a
55  * Sampler. It also understands how to get AuthManagers for the files that the
56  * user selects.
57  *
58  * @version $Revision: 1.15.2.1 $ Last updated: $Date: 2004/06/12 16:44:34 $
59  */

60 public class AuthPanel extends AbstractConfigGui implements ActionListener JavaDoc
61 {
62     transient private static Logger log = LoggingManager.getLoggerForClass();
63
64     private static final String JavaDoc ADD_COMMAND = "Add";
65     private static final String JavaDoc DELETE_COMMAND = "Delete";
66     private static final String JavaDoc LOAD_COMMAND = "Load";
67     private static final String JavaDoc SAVE_COMMAND = "Save";
68
69     private InnerTableModel tableModel;
70
71     /**
72      * A table to show the authentication information.
73      */

74     private JTable JavaDoc authTable;
75
76     private JButton JavaDoc addButton;
77     private JButton JavaDoc deleteButton;
78     private JButton JavaDoc loadButton;
79     private JButton JavaDoc saveButton;
80
81     /**
82      * Default Constructor.
83      */

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     /**
98      * Modifies a given TestElement to mirror the data in the gui components.
99      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
100      */

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 JavaDoc getLabelResource()
120     {
121         return "auth_manager_title";
122     }
123
124     /**
125      * Shows the main authentication panel for this object.
126      */

127     public void init()
128     {
129         setLayout(new BorderLayout JavaDoc());
130         setBorder(makeBorder());
131
132         add(makeTitlePanel(), BorderLayout.NORTH);
133         add(createAuthTablePanel(), BorderLayout.CENTER);
134     }
135
136     public void actionPerformed(ActionEvent JavaDoc e)
137     {
138         String JavaDoc action = e.getActionCommand();
139
140         if (action.equals(DELETE_COMMAND))
141         {
142             if (tableModel.getRowCount() > 0)
143             {
144                 // If a table cell is being edited, we must cancel the editing
145
// before deleting the row.
146
if (authTable.isEditing())
147                 {
148                     TableCellEditor JavaDoc 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                     // Disable the DELETE and SAVE buttons if no rows remaining
163
// after delete.
164
if (tableModel.getRowCount() == 0)
165                     {
166                         deleteButton.setEnabled(false);
167                         saveButton.setEnabled(false);
168                     }
169
170                     // Table still contains one or more rows, so highlight
171
// (select) the appropriate one.
172
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 a table cell is being edited, we should accept the current
191
// value and stop the editing before adding a new row.
192
if (authTable.isEditing())
193             {
194                 TableCellEditor JavaDoc cellEditor =
195                     authTable.getCellEditor(
196                         authTable.getEditingRow(),
197                         authTable.getEditingColumn());
198                 cellEditor.stopCellEditing();
199             }
200
201             tableModel.addNewRow();
202             tableModel.fireTableDataChanged();
203
204             // Enable the DELETE and SAVE buttons if they are currently
205
// disabled.
206
if (!deleteButton.isEnabled())
207             {
208                 deleteButton.setEnabled(true);
209             }
210             if (!saveButton.isEnabled())
211             {
212                 saveButton.setEnabled(true);
213             }
214
215             // Highlight (select) the appropriate row.
216
int rowToSelect = tableModel.getRowCount() - 1;
217             authTable.setRowSelectionInterval(rowToSelect, rowToSelect);
218         }
219         else if (action.equals(LOAD_COMMAND))
220         {
221             try
222             {
223                 File JavaDoc 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 JavaDoc ex)
237             {
238                 log.error("", ex);
239             }
240             catch (NullPointerException JavaDoc err)
241             {
242             }
243         }
244         else if (action.equals(SAVE_COMMAND))
245         {
246             try
247             {
248                 File JavaDoc tmp =
249                     FileDialoger.promptToSaveFile(null).getSelectedFile();
250                 if (tmp != null)
251                 {
252                     tableModel.manager.save(tmp.getAbsolutePath());
253                 }
254             }
255             catch (IOException JavaDoc ex)
256             {
257                 log.error("", ex);
258             }
259             catch (NullPointerException JavaDoc err)
260             {
261             }
262         }
263     }
264
265     public JPanel JavaDoc createAuthTablePanel()
266     {
267         // create the JTable that holds auth per row
268
authTable = new JTable JavaDoc(tableModel);
269         authTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
270         authTable.setPreferredScrollableViewportSize(new Dimension JavaDoc(100, 70));
271
272         TableColumn JavaDoc passwordColumn = authTable.getColumnModel().getColumn(2);
273         passwordColumn.setCellEditor(
274             new DefaultCellEditor JavaDoc(new JPasswordField JavaDoc()));
275         passwordColumn.setCellRenderer(new PasswordCellRenderer());
276
277         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc(0, 5));
278         panel.setBorder(
279             BorderFactory.createTitledBorder(
280                 BorderFactory.createEtchedBorder(),
281                 JMeterUtils.getResString("auths_stored")));
282         panel.add(new JScrollPane JavaDoc(authTable));
283         panel.add(createButtonPanel(), BorderLayout.SOUTH);
284         return panel;
285     }
286
287     private JButton JavaDoc createButton(
288         String JavaDoc resName,
289         char mnemonic,
290         String JavaDoc command,
291         boolean enabled)
292     {
293         JButton JavaDoc button = new JButton JavaDoc(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 JavaDoc 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         // Button Panel
311
JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
312         buttonPanel.add(addButton);
313         buttonPanel.add(deleteButton);
314         buttonPanel.add(loadButton);
315         buttonPanel.add(saveButton);
316         return buttonPanel;
317     }
318
319     /**
320      * @version $Revision: 1.15.2.1 $
321      */

322     private class InnerTableModel extends AbstractTableModel JavaDoc
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             // all table cells are editable
349
return true;
350         }
351
352         public Class JavaDoc getColumnClass(int column)
353         {
354             return getValueAt(0, column).getClass();
355         }
356
357         /**
358          * Required by table model interface.
359          */

360         public int getRowCount()
361         {
362             return manager.getAuthObjects().size();
363         }
364
365         /**
366          * Required by table model interface.
367          */

368         public int getColumnCount()
369         {
370             return manager.getColumnCount();
371         }
372
373         /**
374          * Required by table model interface.
375          */

376         public String JavaDoc getColumnName(int column)
377         {
378             return manager.getColumnName(column);
379         }
380
381         /**
382          * Required by table model interface.
383          */

384         public Object JavaDoc 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 JavaDoc 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 JavaDoc) value);
410             }
411             else if (column == 1)
412             {
413                 auth.setUser((String JavaDoc) value);
414             }
415             else if (column == 2)
416             {
417                 auth.setPass((String JavaDoc) value);
418             }
419         }
420     }
421
422     /**
423      * @version $Revision: 1.15.2.1 $
424      */

425     private class PasswordCellRenderer
426         extends JPasswordField JavaDoc
427         implements TableCellRenderer JavaDoc
428     {
429         private Border JavaDoc myBorder;
430
431         public PasswordCellRenderer()
432         {
433             super();
434             myBorder = new EmptyBorder JavaDoc(1, 2, 1, 2);
435             setOpaque(true);
436             setBorder(myBorder);
437         }
438
439         public Component JavaDoc getTableCellRendererComponent(
440             JTable JavaDoc table,
441             Object JavaDoc value,
442             boolean isSelected,
443             boolean hasFocus,
444             int row,
445             int column)
446         {
447             setText((String JavaDoc) 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