001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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.config.gui;
020:
021: import java.awt.BorderLayout;
022:
023: import javax.swing.JLabel;
024: import javax.swing.JPanel;
025: import javax.swing.JPasswordField;
026: import javax.swing.JTextField;
027:
028: import org.apache.jmeter.config.ConfigTestElement;
029: import org.apache.jmeter.gui.util.VerticalPanel;
030: import org.apache.jmeter.testelement.TestElement;
031: import org.apache.jmeter.testelement.property.StringProperty;
032: import org.apache.jmeter.util.JMeterUtils;
033:
034: /**
035: * A GUI component allowing the user to enter a username and password for a
036: * login.
037: *
038: */
039: public class LoginConfigGui extends AbstractConfigGui {
040: /** Field allowing the user to enter a username. */
041: private JTextField username = new JTextField(15);
042:
043: /** Field allowing the user to enter a password. */
044: private JPasswordField password = new JPasswordField(15);
045:
046: /**
047: * Boolean indicating whether or not this component should display its name.
048: * If true, this is a standalone component. If false, this component is
049: * intended to be used as a subpanel for another component.
050: */
051: private boolean displayName = true;
052:
053: /**
054: * Create a new LoginConfigGui as a standalone component.
055: */
056: public LoginConfigGui() {
057: this (true);
058: }
059:
060: /**
061: * Create a new LoginConfigGui as either a standalone or an embedded
062: * component.
063: *
064: * @param displayName
065: * indicates whether or not this component should display its
066: * name. If true, this is a standalone component. If false, this
067: * component is intended to be used as a subpanel for another
068: * component.
069: */
070: public LoginConfigGui(boolean displayName) {
071: this .displayName = displayName;
072: init();
073: }
074:
075: public String getLabelResource() {
076: return "login_config_element"; // $NON-NLS-1$
077: }
078:
079: /**
080: * A newly created component can be initialized with the contents of a Test
081: * Element object by calling this method. The component is responsible for
082: * querying the Test Element object for the relevant information to display
083: * in its GUI.
084: *
085: * @param element
086: * the TestElement to configure
087: */
088: public void configure(TestElement element) {
089: super .configure(element);
090: username.setText(element
091: .getPropertyAsString(ConfigTestElement.USERNAME));
092: password.setText(element
093: .getPropertyAsString(ConfigTestElement.PASSWORD));
094: }
095:
096: /* Implements JMeterGUIComponent.createTestElement() */
097: public TestElement createTestElement() {
098: ConfigTestElement element = new ConfigTestElement();
099: modifyTestElement(element);
100: return element;
101: }
102:
103: /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
104: public void modifyTestElement(TestElement element) {
105: configureTestElement(element);
106: element.setProperty(new StringProperty(
107: ConfigTestElement.USERNAME, username.getText()));
108:
109: String passwordString = new String(password.getPassword());
110: element.setProperty(new StringProperty(
111: ConfigTestElement.PASSWORD, passwordString));
112: }
113:
114: /**
115: * Implements JMeterGUIComponent.clearGui
116: */
117: public void clearGui() {
118: super .clearGui();
119:
120: username.setText(""); //$NON-NLS-1$
121: password.setText(""); //$NON-NLS-1$
122: }
123:
124: /**
125: * Initialize the components and layout of this component.
126: */
127: private void init() {
128: setLayout(new BorderLayout(0, 5));
129:
130: if (displayName) {
131: setBorder(makeBorder());
132: add(makeTitlePanel(), BorderLayout.NORTH);
133: }
134:
135: VerticalPanel mainPanel = new VerticalPanel();
136: mainPanel.add(createUsernamePanel());
137: mainPanel.add(createPasswordPanel());
138: add(mainPanel, BorderLayout.CENTER);
139: }
140:
141: /**
142: * Create a panel containing the username field and corresponding label.
143: *
144: * @return a GUI panel containing the username field
145: */
146: private JPanel createUsernamePanel() {
147: JPanel panel = new JPanel(new BorderLayout(5, 0));
148: JLabel label = new JLabel(JMeterUtils.getResString("username")); // $NON-NLS-1$
149: label.setLabelFor(username);
150: panel.add(label, BorderLayout.WEST);
151: panel.add(username, BorderLayout.CENTER);
152: return panel;
153: }
154:
155: /**
156: * Create a panel containing the password field and corresponding label.
157: *
158: * @return a GUI panel containing the password field
159: */
160: private JPanel createPasswordPanel() {
161: JPanel panel = new JPanel(new BorderLayout(5, 0));
162: JLabel label = new JLabel(JMeterUtils.getResString("password")); // $NON-NLS-1$
163: label.setLabelFor(password);
164: panel.add(label, BorderLayout.WEST);
165: panel.add(password, BorderLayout.CENTER);
166: return panel;
167: }
168: }
|