/* * LoginPrompt.java * * Copyright (c) 2003-2004 Digi International * This program and the information contained in it is confidential and * proprietary to Digi International and may not be used, copied, or re- * produced without the prior written permission of Digi International. * */ package com.digi.config.ui; import com.digi.config.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import java.awt.*; import java.awt.event.*; /** * Shows a login prompt dialog. */ public class LoginPrompt { Component parent; // parent component of dialog String user; String password; String realm; String host; // UI controls JTextField userEdit; JTextField pwdEdit; /** * Constructor * @param parent parent component of dialog (if null then a default frame is used). */ public LoginPrompt(Component parent) { this.parent = parent; this.user = ""; this.password = ""; this.realm = ""; this.host = ""; } /** * Show the login prompt dialog. * @return JOptionPane.OK_OPTION if user specified a user and password and clicked OK. * JOptionPane.CANCEL_OPTION or JOptionPane.CLOSE_OPTION if user clicked cancel or close. */ public int showLoginPrompt() { JOptionPane promptPane = new JOptionPane(); // Create the panel that will hold the server controls GridContentPanel content = new GridContentPanel(1); content.setOpaque(false); WindowAdapter winListener = new WindowAdapter() { public void windowOpened(WindowEvent e) { // Determine initial focus (user if no default user, otherwise password). if (user.length() == 0) { userEdit.requestFocus(); } else { pwdEdit.requestFocus(); } // Since this listener is no longer needed, remove it e.getWindow().removeWindowListener(this); } }; content.addTextLine("LoginHeading"); if (host.length() > 0) { JLabel hostLbl = content.addJLabel("LoginHostLbl", null); hostLbl.setText(host); } if (realm.length() > 0) { JLabel realmLbl = content.addJLabel("LoginRealmLbl", null); realmLbl.setText(realm); } userEdit = content.addJTextField("LoginUserLbl", null, 150, null); userEdit.setText(user); pwdEdit = content.addJPasswordField("LoginPasswordLbl", null, 150, null); if (user.length() == 0) { userEdit.requestFocus(); } else { pwdEdit.requestFocus(); } // String userLbl = ConfigResource.getUiRbString("LoginUserLbl"); // JTextField userEdit = new JTextField(user); // String pwdLbl = ConfigResource.getUiRbString("LoginPasswordLbl"); // JTextField pwdEdit = new JPasswordField(); // promptPane.setMessage(new Object[] {userLbl, userEdit, pwdLbl, pwdEdit}); promptPane.setMessage(content); promptPane.setMessageType(JOptionPane.PLAIN_MESSAGE); promptPane.setOptionType(JOptionPane.OK_CANCEL_OPTION); JDialog dlg = promptPane.createDialog(parent, ConfigResource.getUiRbString("LoginTitle")); dlg.addWindowListener(winListener); // Listen for open window to set proper initial focus dlg.show(); int response = JOptionPane.CLOSED_OPTION; Object value = promptPane.getValue(); if (value != null && (value instanceof Integer)) { response = ((Integer)value).intValue(); } if (response == JOptionPane.OK_OPTION) { user = userEdit.getText(); password = pwdEdit.getText(); } return response; } /** * Set the initial user name shown when the prompt is displayed. * @param user The user name. */ public void setUserName(String user) { if (user != null) { this.user = user; } else { this.user = ""; } } /** * Set the realm requesting authentication. * @param realm The security realm name. */ public void setRealm(String realm) { if (realm != null) { this.realm = realm; } else { this.realm = ""; } } /** * Set the host system requesting authentication. * @param host The host system. */ public void setHost(String host) { if (host != null) { this.host = host; } else { this.host = ""; } } /** * Get the user name entered in the login prompt (if showLoginPrompt returned OK_OPTION). * @return user name */ public String getUserName() { return user; } /** * Get the password entered in the login prompt (if showLoginPrompt returned OK_OPTION). */ public String getPassword() { return password; } /** * Default constructor - private */ private LoginPrompt() { } }