📄 jxloginpanel.java
字号:
/* * $Id: JXLoginPanel.java,v 1.3 2005/06/27 08:26:58 kleopatra Exp $ * * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. */package org.jdesktop.swingx;import java.awt.BorderLayout;import java.awt.Cursor;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.io.IOException;import java.util.Enumeration;import java.util.ResourceBundle;import javax.swing.ComboBoxModel;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JLabel;import javax.swing.JPasswordField;import javax.swing.JTextField;import javax.swing.UIManager;import org.jdesktop.swingx.auth.*;/** * <p>JXLoginPanel is a JPanel that implements a Login dialog with * support for saving passwords supplied for future use in a secure * manner. It is intended to work with <strong>LoginService</strong> * and <strong>PasswordStore</strong> to implement the * authentication.</p> * * <p> In order to perform the authentication, <strong>JXLoginPanel</strong> * calls the <code>authenticate</code> method of the <strong>LoginService * </strong>. In order to perform the persistence of the password, * <strong>JXLoginPanel</strong> calls the put method of the * <strong>PasswordStore</strong> object that is supplied. If * the <strong>PasswordStore</strong> is <code>null</code>, then the password * is not saved. Similarly, if a <strong>PasswordStore</strong> is * supplied and the password is null, then the <strong>PasswordStore</strong> * will be queried for the password using the <code>get</code> method. * * Changes by Shai: * Clarified the save mode a little bit including hiding the save checkbox when there * is no password store. * Changed the class to derive from JXImagePanel to make customization easier (need to * check my ImagePanel which has some technical advantages). * Removed the static keyword from the ok/cancel buttons since this can cause an issue * with more than one login dialogs (yes its an unlikely situation but documenting this * sort of behavior or dealing with one bug resulting from this can be a real pain!). * Allowed the name field to be represented as a text field when there is no password store. * Rewrote the layout code to mostly work with a single container. * Removed additional dialogs for progress and error messages and incorporated their * functionality into the main dialog. * Allowed for an IOException with a message to be thrown by the login code. This message * is displayed to the user when the login is stopped. * Removed repetetive code and moved it to a static block. * i18n converted some of the strings that were not localized. * * @author Bino George * @author Shai Almog * @author rbair */public class JXLoginPanel extends JXImagePanel { /** * Comment for <code>serialVersionUID</code> */ private static final long serialVersionUID = 3544949969896288564L; /** * The JXLoginPanel can attempt to save certain user information such as * the username, password, or both to their respective stores. * This type specifies what type of save should be performed. */ public static enum SaveMode { NONE, USER_NAME, PASSWORD, BOTH}; /** * Used as a prefix when pulling data out of UIManager for i18n */ private static String CLASS_NAME; /** * This is the area in which the name field is placed. That way it can toggle on the fly * between text field and a combo box depending on the situation. */ private JXPanel namePanel; /** * If a UserNameStore is used, then this combo box is presented allowing the user * to select a previous login name, or type in a new login name */ private JComboBox nameCombo; /** * If a UserNameStore is not used, then this text field is presented allowing the user * to simply enter their user name */ private JTextField nameField; /** * The password field presented allowing the user to enter their password */ private JPasswordField passwordField; /** * Check box presented if a PasswordStore is used, allowing the user to decide whether to * save their password */ private JCheckBox saveCB; /** * The LoginService to use. This must be specified for the login dialog to operate. * If no LoginService is defined, a default login service is used that simply * allows all users access. This is useful for demos or prototypes where a proper login * server is not available. */ private LoginService loginService; /** * Optional: a PasswordStore to use for storing and retrieving passwords for a specific * user. */ private PasswordStore passwordStore; /** * Optional: a UserNameStore to use for storing user names and retrieving them */ private UserNameStore userNameStore; /** * Whether to save password or username or both */ private SaveMode saveMode; /** * Listens to save events */ private SaveListener saveListener; /** * Tracks the cursor at the time that authentication was started, and restores to that * cursor after authentication ends, or is cancelled; */ private Cursor oldCursor; /** * Creates a default JXLoginPanel instance */ static { // Popuplate UIDefaults with the localizable Strings we will use // in the Login panel. CLASS_NAME = JXLoginPanel.class.getCanonicalName(); String lookup; ResourceBundle res = ResourceBundle.getBundle("org.jdesktop.swingx.auth.resources.resources"); Enumeration<String> keys = res.getKeys(); while (keys.hasMoreElements()) { String key = keys.nextElement(); lookup = CLASS_NAME + "." + key; if (UIManager.getString(lookup) == null) { UIManager.put(lookup, res.getString(key)); } } } public JXLoginPanel() { this(null); } public JXLoginPanel(LoginService service) { this(service, null, null); } public JXLoginPanel(LoginService service, PasswordStore passwordStore, UserNameStore userStore) { this.loginService = service == null ? new NullLoginService() : service; this.passwordStore = passwordStore == null ? new NullPasswordStore() : passwordStore; this.userNameStore = userStore == null ? new DefaultUserNameStore() : userStore; //initialize the save mode if (passwordStore != null && userStore != null) { saveMode = SaveMode.BOTH; } else if (passwordStore != null) { saveMode = SaveMode.PASSWORD; } else if (userStore != null) { saveMode = SaveMode.USER_NAME; } else { saveMode = SaveMode.NONE; } saveListener = new SaveListener(); this.loginService.addLoginListener(saveListener); setLayout(new BorderLayout()); add(createLoginPanel(), BorderLayout.CENTER); } // if (username != null) {// userNameStore.addUsername(username);// } private JXPanel createNamePanel() { JXPanel namePanel = new JXPanel(new BorderLayout()); nameField = new JTextField("", 15); nameField.setEditable(true); nameCombo = new JComboBox(new ComboBoxModel() { private Object selectedItem; public void setSelectedItem(Object anItem) { selectedItem = anItem; } public Object getSelectedItem() { return selectedItem; } public Object getElementAt(int index) { return userNameStore.getUserNames()[index]; } public int getSize() { return userNameStore.getUserNames().length; } public void removeListDataListener(javax.swing.event.ListDataListener l) { //TODO } public void addListDataListener(javax.swing.event.ListDataListener l) { //TODO } }); nameCombo.setEditable(true); if (saveMode == SaveMode.NONE) { namePanel.add(nameField, BorderLayout.CENTER); } else { namePanel.add(nameCombo, BorderLayout.CENTER); } return namePanel; } private JXPanel createLoginPanel() { JXPanel loginPanel = new JXPanel(); JLabel nameLabel = new JLabel(UIManager.getString(CLASS_NAME + ".nameString")); namePanel = createNamePanel(); JLabel passwordLabel = new JLabel(UIManager.getString(CLASS_NAME + ".passwordString")); passwordField = new JPasswordField("", 15); saveCB = new JCheckBox(UIManager.getString(CLASS_NAME + ".rememberPasswordString")); saveCB.setVisible(saveMode == SaveMode.PASSWORD || saveMode == SaveMode.BOTH); loginPanel.setLayout(new GridBagLayout()); GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = GridBagConstraints.WEST; gridBagConstraints.insets = new Insets(0, 0, 5, 11); loginPanel.add(nameLabel, gridBagConstraints); gridBagConstraints = new GridBagConstraints();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -