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

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

 OK to copy?
001 // $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 $
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.Component;
023 import java.awt.Dimension;
024 import java.awt.event.ActionEvent;
025 import java.awt.event.ActionListener;
026 import java.io.File;
027 import java.io.IOException;
028 
029 import javax.swing.BorderFactory;
030 import javax.swing.DefaultCellEditor;
031 import javax.swing.JButton;
032 import javax.swing.JPanel;
033 import javax.swing.JPasswordField;
034 import javax.swing.JScrollPane;
035 import javax.swing.JTable;
036 import javax.swing.ListSelectionModel;
037 import javax.swing.border.Border;
038 import javax.swing.border.EmptyBorder;
039 import javax.swing.table.AbstractTableModel;
040 import javax.swing.table.TableCellEditor;
041 import javax.swing.table.TableCellRenderer;
042 import javax.swing.table.TableColumn;
043 
044 import org.apache.jmeter.config.gui.AbstractConfigGui;
045 import org.apache.jmeter.gui.util.FileDialoger;
046 import org.apache.jmeter.protocol.http.control.AuthManager;
047 import org.apache.jmeter.protocol.http.control.Authorization;
048 import org.apache.jmeter.testelement.TestElement;
049 import org.apache.jmeter.util.JMeterUtils;
050 import org.apache.jorphan.logging.LoggingManager;
051 import org.apache.log.Logger;
052 
053 /**
054  * Handles input for determining if authentication services are required for a
055  * Sampler. It also understands how to get AuthManagers for the files that the
056  * user selects.
057  *
058  * @version   $Revision: 1.15.2.1 $  Last updated: $Date: 2004/06/12 16:44:34 $
059  */
060 public class AuthPanel extends AbstractConfigGui implements ActionListener
061 {
062     transient private static Logger log = LoggingManager.getLoggerForClass();
063 
064     private static final String ADD_COMMAND = "Add";
065     private static final String DELETE_COMMAND = "Delete";
066     private static final String LOAD_COMMAND = "Load";
067     private static final String SAVE_COMMAND = "Save";
068 
069     private InnerTableModel tableModel;
070 
071     /**
072      * A table to show the authentication information.
073      */
074     private JTable authTable;
075 
076     private JButton addButton;
077     private JButton deleteButton;
078     private JButton loadButton;
079     private JButton saveButton;
080 
081     /**
082      * Default Constructor.
083      */
084     public AuthPanel()
085     {
086         tableModel = new InnerTableModel();
087         init();
088     }
089 
090     public TestElement createTestElement()
091     {
092         AuthManager authMan = tableModel.manager;
093         configureTestElement(authMan);
094         return (TestElement) authMan.clone();
095     }
096 
097     /**
098      * Modifies a given TestElement to mirror the data in the gui components.
099      * @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 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());
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 a table cell is being edited, we must cancel the editing
145                 // before deleting the row.
146                 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                     // 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 
Rate181                         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 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;
Rate217             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         // create the JTable that holds auth per row
268         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         // Button Panel
311         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     /**
320      * @version   $Revision: 1.15.2.1 $
321      */
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             // all table cells are editable
349             return true;
350         }
351 
352         public Class 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 getColumnName(int column)
377         {
378             return manager.getColumnName(column);
379         }
380 
381         /**
382          * Required by table model interface.
383          */
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     /**
423      * @version   $Revision: 1.15.2.1 $
424      */
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 }

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