KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > config > gui > LoginConfigGui


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/config/gui/LoginConfigGui.java,v 1.9 2004/03/05 01:33:48 sebb 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.config.gui;
20 import java.awt.BorderLayout JavaDoc;
21
22 import javax.swing.JLabel JavaDoc;
23 import javax.swing.JPanel JavaDoc;
24 import javax.swing.JPasswordField JavaDoc;
25 import javax.swing.JTextField JavaDoc;
26
27 import org.apache.jmeter.config.ConfigTestElement;
28 import org.apache.jmeter.gui.util.VerticalPanel;
29 import org.apache.jmeter.testelement.TestElement;
30 import org.apache.jmeter.testelement.property.StringProperty;
31 import org.apache.jmeter.util.JMeterUtils;
32
33 /**
34  * A GUI component allowing the user to enter a username and password for a
35  * login.
36  *
37  * @version $Revision: 1.9 $ on $Date: 2004/03/05 01:33:48 $
38  */

39 public class LoginConfigGui extends AbstractConfigGui
40 {
41     /** Field allowing the user to enter a username. */
42     private JTextField JavaDoc username = new JTextField JavaDoc(15);
43     
44     /** Field allowing the user to enter a password. */
45     private JPasswordField JavaDoc password = new JPasswordField JavaDoc(15);
46     
47     /**
48      * Boolean indicating whether or not this component should display its
49      * name. If true, this is a standalone component. If false, this component
50      * is intended to be used as a subpanel for another component.
51      */

52     private boolean displayName = true;
53
54     /**
55      * Create a new LoginConfigGui as a standalone component.
56      */

57     public LoginConfigGui()
58     {
59         this(true);
60     }
61
62     /**
63      * Create a new LoginConfigGui as either a standalone or an embedded
64      * component.
65      *
66      * @param displayName indicates whether or not this component should
67      * display its name. If true, this is a standalone
68      * component. If false, this component is intended
69      * to be used as a subpanel for another component.
70      */

71     public LoginConfigGui(boolean displayName)
72     {
73         this.displayName = displayName;
74         init();
75     }
76
77     public String JavaDoc getLabelResource()
78     {
79         return "login_config_element";
80     }
81
82     /**
83      * A newly created component can be initialized with the contents of
84      * a Test Element object by calling this method. The component is
85      * responsible for querying the Test Element object for the
86      * relevant information to display in its GUI.
87      *
88      * @param element the TestElement to configure
89      */

90     public void configure(TestElement element)
91     {
92         super.configure(element);
93         username.setText(
94             element.getPropertyAsString(ConfigTestElement.USERNAME));
95         password.setText(
96             element.getPropertyAsString(ConfigTestElement.PASSWORD));
97     }
98
99     /* Implements JMeterGUIComponent.createTestElement() */
100     public TestElement createTestElement()
101     {
102         ConfigTestElement element = new ConfigTestElement();
103         modifyTestElement(element);
104         return element;
105     }
106
107     /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
108     public void modifyTestElement(TestElement element)
109     {
110         configureTestElement(element);
111         element.setProperty(
112             new StringProperty(ConfigTestElement.USERNAME, username.getText()));
113         
114         String JavaDoc passwordString = new String JavaDoc(password.getPassword());
115         element.setProperty(
116             new StringProperty(ConfigTestElement.PASSWORD, passwordString));
117     }
118
119     /**
120      * Initialize the components and layout of this component.
121      */

122     private void init()
123     {
124         setLayout(new BorderLayout JavaDoc(0, 5));
125
126         if (displayName)
127         {
128             setBorder(makeBorder());
129             add(makeTitlePanel(), BorderLayout.NORTH);
130         }
131
132         VerticalPanel mainPanel = new VerticalPanel();
133         mainPanel.add(createUsernamePanel());
134         mainPanel.add(createPasswordPanel());
135         add(mainPanel, BorderLayout.CENTER);
136     }
137
138     /**
139      * Create a panel containing the username field and corresponding label.
140      *
141      * @return a GUI panel containing the username field
142      */

143     private JPanel JavaDoc createUsernamePanel()
144     {
145         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
146         JLabel JavaDoc label = new JLabel JavaDoc(JMeterUtils.getResString("username"));
147         label.setLabelFor(username);
148         panel.add(label, BorderLayout.WEST);
149         panel.add(username, BorderLayout.CENTER);
150         return panel;
151     }
152
153     /**
154      * Create a panel containing the password field and corresponding label.
155      *
156      * @return a GUI panel containing the password field
157      */

158     private JPanel JavaDoc createPasswordPanel()
159     {
160         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
161         JLabel JavaDoc label = new JLabel JavaDoc(JMeterUtils.getResString("password"));
162         label.setLabelFor(password);
163         panel.add(label, BorderLayout.WEST);
164         panel.add(password, BorderLayout.CENTER);
165         return panel;
166     }
167 }
168
Popular Tags