📄 dialogauthentication.java
字号:
/*** $Id: DialogAuthentication.java,v 1.4 2001/05/07 12:37:21 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998-2000 by Timothy Gerard Endres** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE. */package org.icemail.mail;import java.awt.Container;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.net.InetAddress;import javax.swing.JButton;import javax.swing.JPanel;import javax.swing.JLabel;import javax.swing.JDialog;import javax.swing.JPasswordField;import javax.swing.JTextField;import javax.swing.JMenu;import javax.swing.border.EmptyBorder;import javax.mail.Authenticator;import javax.mail.PasswordAuthentication;import org.icemail.util.AWTUtilities; import org.icemail.util.ComponentFactory; import org.icemail.util.JFCUtilities; /** * This class represents an object that knows how to obtain authentication for * network connections from a user via a dialog. * * @see javax.mail.Authenticator */public class DialogAuthentication extends Authenticator{ private AuthDialog dialog_; /** * Default constructor. */ public DialogAuthentication() { super(); } /** * Called when password authentication is needed. * <p> * Implementation of javax.mail.Authenticator.getDialogUserName() * * @return authentication collected from user input, or null if none is provided */ protected PasswordAuthentication getPasswordAuthentication() { dialog_ = this.new AuthDialog(); dialog_.pack(); Point loc = AWTUtilities.computeDialogLocation( dialog_ ); dialog_.setLocation( loc.x, loc.y ); dialog_.show(); PasswordAuthentication result = dialog_.getAuthentication(); return result; } private String getDialogPrompt() { // given a prompt? String prompt = getRequestingPrompt(); if ( prompt == null ) prompt = "Login to:"; // protocol String protocol = getRequestingProtocol(); if ( protocol == null ) protocol = "Unknown"; // get the host String host = null; InetAddress inet = getRequestingSite(); if ( inet != null ) host = inet.getHostName(); if ( host == null ) host = "Unknown"; // port String port = ""; int portnum = getRequestingPort(); if ( portnum != -1 ) port = "Port # " + portnum; // Build the info string Object[] xargs = new Object[4]; xargs[0] = prompt; xargs[1] = host; xargs[2] = port; xargs[3] = protocol; String result = ICEMail.getBundle().getFormatString( "Authentication.Prompt", xargs ); return result; } private String getUserName() { return super.getDefaultUserName(); }//............................................................ private class AuthDialog extends JDialog implements ActionListener { private JTextField userField; private JPasswordField passField; private PasswordAuthentication authentication; public AuthDialog() { super(); this.setModal( true ); this.setTitle( ICEMail.getBundle().getString( "Authentication.Title" ) ); this.authentication = null; this.establishContents(); } public PasswordAuthentication getAuthentication() { return this.authentication; } public void actionPerformed( ActionEvent event ) { String command = event.getActionCommand(); if ( event.getSource() == this.userField ) { command = "NOOP"; this.passField.requestFocus(); } else if ( event.getSource() == this.passField ) { command = "OK"; } if ( command.equals( "OK" ) ) { this.authentication = new PasswordAuthentication( this.userField.getText(), new String( this.passField.getPassword() ) ); this.dispose(); } else if ( command.equals( "CANCEL" ) ) { this.authentication = null; this.dispose(); } } public void addNotify() { super.addNotify(); if ( this.userField.getText().length() < 1 ) this.userField.requestFocus(); else this.passField.requestFocus(); } public void establishContents() { int row = 0; Container contentPane = this.getContentPane(); contentPane.setLayout( new GridBagLayout() ); JPanel content = new JPanel(); content.setLayout( new GridBagLayout() ); content.setBorder( new EmptyBorder( 5, 5, 5, 5 ) ); contentPane.add( content ); String prompt = getDialogPrompt(); // prompt JLabel xlabel = new JLabel( prompt ); xlabel.setHorizontalAlignment( JLabel.CENTER ); xlabel.setBorder( new EmptyBorder( 2, 0, 4, 0 ) ); AWTUtilities.constrain( content, xlabel, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 0, row++, 2, 1, 1.0, 0.0 ); // information about the person we want back xlabel = ComponentFactory.getLabel( ICEMail.getBundle(), "Authentication.Username" ); AWTUtilities.constrain( content, xlabel, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, row, 1, 1, 0.0, 0.0 ); String defUser = getUserName(); this.userField = new JTextField( defUser, 20 ); this.userField.addActionListener( this ); AWTUtilities.constrain( content, this.userField, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 1, row++, 1, 1, 1.0, 0.0 ); xlabel = ComponentFactory.getLabel( ICEMail.getBundle(), "Authentication.Password" ); AWTUtilities.constrain( content, xlabel, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, row, 1, 1, 0.0, 0.0 ); this.passField = new JPasswordField( 20 ); this.passField.setEchoChar( '*' ); this.passField.addActionListener( this ); AWTUtilities.constrain( content, this.passField, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 1, row++, 1, 1, 1.0, 0.0 ); JPanel btnPanel = new JPanel(); btnPanel.setLayout( new GridLayout( 1, 2, 25, 10 ) ); btnPanel.setBorder( JFCUtilities.StandardBorder ); AWTUtilities.constrain( content, btnPanel, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 0, row++, 2, 1, 1.0, 0.0 ); JButton xbutton; xbutton = ComponentFactory.getButton( ICEMail.getBundle(), "Authentication.OK", this, null ); btnPanel.add( xbutton ); xbutton = ComponentFactory.getButton( ICEMail.getBundle(), "Authentication.Cancel", this, null ); btnPanel.add( xbutton ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -