1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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 21 import java.awt.BorderLayout; 22 23 import javax.swing.JLabel; 24 import javax.swing.JPanel; 25 import javax.swing.JPasswordField; 26 import javax.swing.JTextField; 27 28 import org.apache.jmeter.config.ConfigTestElement; 29 import org.apache.jmeter.gui.util.VerticalPanel; 30 import org.apache.jmeter.testelement.TestElement; 31 import org.apache.jmeter.testelement.property.StringProperty; 32 import org.apache.jmeter.util.JMeterUtils; 33 34 /** 35 * A GUI component allowing the user to enter a username and password for a 36 * login. 37 * 38 */ 39 public class LoginConfigGui extends AbstractConfigGui { 40 /** Field allowing the user to enter a username. */ 41 private JTextField username = new JTextField(15); 42 43 /** Field allowing the user to enter a password. */ 44 private JPasswordField password = new JPasswordField(15); 45 46 /** 47 * Boolean indicating whether or not this component should display its name. 48 * If true, this is a standalone component. If false, this component is 49 * intended to be used as a subpanel for another component. 50 */ 51 private boolean displayName = true; 52 53 /** 54 * Create a new LoginConfigGui as a standalone component. 55 */ 56 public LoginConfigGui() { 57 this(true); 58 } 59 60 /** 61 * Create a new LoginConfigGui as either a standalone or an embedded 62 * component. 63 * 64 * @param displayName 65 * indicates whether or not this component should display its 66 * name. If true, this is a standalone component. If false, this 67 * component is intended to be used as a subpanel for another 68 * component. 69 */ 70 public LoginConfigGui(boolean displayName) { 71 this.displayName = displayName; 72 init(); 73 } 74 75 public String getLabelResource() { 76 return "login_config_element"; // $NON-NLS-1$ 77 } 78 79 /** 80 * A newly created component can be initialized with the contents of a Test 81 * Element object by calling this method. The component is responsible for 82 * querying the Test Element object for the relevant information to display 83 * in its GUI. 84 * 85 * @param element 86 * the TestElement to configure 87 */ 88 public void configure(TestElement element) { 89 super.configure(element); 90 username.setText(element.getPropertyAsString(ConfigTestElement.USERNAME)); 91 password.setText(element.getPropertyAsString(ConfigTestElement.PASSWORD)); 92 } 93 94 /* Implements JMeterGUIComponent.createTestElement() */ 95 public TestElement createTestElement() { 96 ConfigTestElement element = new ConfigTestElement(); 97 modifyTestElement(element); 98 return element; 99 } 100 101 /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */ 102 public void modifyTestElement(TestElement element) { 103 configureTestElement(element); 104 element.setProperty(new StringProperty(ConfigTestElement.USERNAME, username.getText())); 105 106 String passwordString = new String(password.getPassword()); 107 element.setProperty(new StringProperty(ConfigTestElement.PASSWORD, passwordString)); 108 } 109 /** 110 * Implements JMeterGUIComponent.clearGui 111 */ 112 public void clearGui() { 113 super.clearGui(); 114 115 username.setText(""); //$NON-NLS-1$ 116 password.setText(""); //$NON-NLS-1$ 117 } 118 119 /** 120 * Initialize the components and layout of this component. 121 */ 122 private void init() { 123 setLayout(new BorderLayout(0, 5)); 124 125 if (displayName) { 126 setBorder(makeBorder()); 127 add(makeTitlePanel(), BorderLayout.NORTH); 128 } 129 130 VerticalPanel mainPanel = new VerticalPanel(); 131 mainPanel.add(createUsernamePanel()); 132 mainPanel.add(createPasswordPanel()); 133 add(mainPanel, BorderLayout.CENTER); 134 } 135 136 /** 137 * Create a panel containing the username field and corresponding label. 138 * 139 * @return a GUI panel containing the username field 140 */ 141 private JPanel createUsernamePanel() { 142 JPanel panel = new JPanel(new BorderLayout(5, 0)); 143 JLabel label = new JLabel(JMeterUtils.getResString("username")); // $NON-NLS-1$ 144 label.setLabelFor(username); 145 panel.add(label, BorderLayout.WEST); 146 panel.add(username, BorderLayout.CENTER); 147 return panel; 148 } 149 150 /** 151 * Create a panel containing the password field and corresponding label. 152 * 153 * @return a GUI panel containing the password field 154 */ 155 private JPanel createPasswordPanel() { 156 JPanel panel = new JPanel(new BorderLayout(5, 0)); 157 JLabel label = new JLabel(JMeterUtils.getResString("password")); // $NON-NLS-1$ 158 label.setLabelFor(password); 159 panel.add(label, BorderLayout.WEST); 160 panel.add(password, BorderLayout.CENTER); 161 return panel; 162 } 163 }