1 import javax.swing.*;
2 import javax.swing.border.EmptyBorder;
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.*;
6
7
8 /**
9 * Title: Login Panel
10 * Description: A simple yet complete login/logout panel with user callbacks
11 * for approving login attempts and getting notification of logouts.
12 * Copyright: Copyright (c) 2004
13 * Company: Superliminal Software
14 * @author Melinda Green
15 * @version 1.0
16 */
17
18 public class LoginPanel extends JPanel {
19 public final static String
20 LOG_IN = "Login",
21 LOG_OUT = "Logout";
22 protected JButton logButt;
23 public JButton getLogButton() { return logButt; }
24 private final static int DEFAULT_PSWD_CHARS = 10;
25 private JTextField nameField = new JTextField(DEFAULT_PSWD_CHARS);
26 public String getUserName() { return nameField.getText(); }
27
28 /**
29 * override this method to return true if approved, false otherwise.
30 * default is true.
31 */
32 public boolean approveLogin(String uname, String pswd) {
33 return true;
34 }
35
36 /**
37 * override this method to learn about logout events.
38 */
39 public void loggedOut(String uname) {
40 }
41
42 public LoginPanel() {
43 this(false);
44 }
45
46 public LoginPanel(final boolean clearPasswords) {
47 this(clearPasswords, true, null, null);
48 }
49
50 /**
51 * @param clearPasswords if true, clears password field on successful login.
52 * @param initial_user optional default text to load into the 'user' type-in.
53 * @param initial_password optional default text to load into the 'password' type-in.
54 */
55 public LoginPanel(final boolean clearPasswords, final boolean displayFailures, String initial_user, String initial_password) {
56 final JPasswordField pswdField = new JPasswordField(DEFAULT_PSWD_CHARS);
57 logButt = new JButton(LOG_IN);
58 KeyListener quickLogin = new KeyAdapter() {
59 public void keyTyped(KeyEvent ke) {
60 if(ke.getKeyChar() == KeyEvent.VK_ENTER) {
61 logButt.doClick();
62 logButt.requestFocus();
63 }
64 }
65 };
66 nameField.setText(initial_user);
67 pswdField.setText(initial_password);
68 logButt.setName(LOG_IN);
69 nameField.addKeyListener(quickLogin);
70 pswdField.addKeyListener(quickLogin);
71 // create the grid
72 JPanel grid = new JPanel(new GridLayout(2, 2));
73 grid.setBackground(new Color(255,255,255));
74 grid.add(new JLabel("User Name"));
75 grid.add(nameField);
76 grid.add(new JLabel("Password"));
77 grid.add(pswdField);
78
79 // create login button row
80 JPanel row = new JPanel();
81 row.setBorder(new EmptyBorder(5, 0, 5, 0));
82 row.setOpaque(false);
83 row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));
84 row.add(logButt);
85 logButt.setBackground(new Color(220,220,220));
86
87 logButt.addActionListener(new ActionListener() {
88 public void actionPerformed(ActionEvent ae) {
89 if(logButt.getText().equals(LOG_IN)) {
90 // seek login approval from derived class
91 if(approveLogin(nameField.getText(), new String(pswdField.getPassword()))) {
92 // note: must set logout text *before* clearing password
93 // otherwise component dependancy handler will disable the
94 // login button w/out password text before later setting logout text
95 // this closes bug #2336
96 logButt.setText(LOG_OUT);
97 if(clearPasswords)
98 pswdField.setText(null);
99 nameField.setEnabled(false);
100 pswdField.setEnabled(false);
101 fireLoginEvent(nameField.getText(), true);
102 }
103 else
104 if(displayFailures)
105 JOptionPane.showMessageDialog(LoginPanel.this, "Login Denied", "Login Error", JOptionPane.ERROR_MESSAGE);
106 }
107 else {
108 logButt.setText(LOG_IN);
109 loggedOut(nameField.getText());
110 nameField.setEnabled(true);
111 pswdField.setEnabled(true);
112 fireLoginEvent(nameField.getText(), false);
113 }
114 }
115 });
116
117 // implement component dependancies
118 // new ComponentDependencyHandler(nameField, pswdField) {
119 // public void dependencyNotification() {
120 // String
121 // logtext = logButt.getText(),
122 // nametext = nameField.getText(),
123 // pswdtext = String.copyValueOf(pswdField.getPassword());
124 //boolean newstate = logtext.equalsIgnoreCase(LOG_OUT) ||
125 // (nameField.getText() != null && nametext.length() > 0 // has login text?
126 // && pswdtext.length() > 0); // has password text?
127 //logButt.setEnabled(newstate);
128 // }
129 //};
130
131 // construct final layout
132 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
133 add(grid);
134 add(row);
135 }
136
137 public interface LoginListener {
138 void loggedIn(String uname);
139 void loggedOut(String uname);
140 }
141 public static class LoginAdapter implements LoginListener {
142 public void loggedIn(String uname){}
143 public void loggedOut(String uname){}
144 }
145 private Vector loginListeners = new Vector();
146 public void addLoginListener(LoginListener ll) { loginListeners.add(ll); }
147 public void removeLoginListener(LoginListener ll) { loginListeners.remove(ll); }
148 protected void fireLoginEvent(String uname, boolean in) {
149 for(Enumeration e=loginListeners.elements(); e.hasMoreElements(); ) {
150 LoginListener ll = (LoginListener)e.nextElement();
151 if(in)
152 ll.loggedIn(uname);
153 else
154 ll.loggedOut(uname);
155 }
156 }
157
158 /**
159 * simple example test program for LoginPanel class
160 */
161 public static void main(String[] args) {
162 final String NOT_LOGGED_IN = "LoginPanel Test - Currently Logged Out";
163 final JFrame frame = new JFrame(NOT_LOGGED_IN);
164 frame.getContentPane().add(new LoginPanel() {
165 public boolean approveLogin(String uname, String pswd) {
166 // this is where to make the server call to approve or reject login attempt
167 frame.setTitle("LoginPanel Test - Currently logged in as " + uname);
168 return true;
169 }
170 public void loggedOut(String uname) {
171 frame.setTitle(NOT_LOGGED_IN);
172 }
173 });
174 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
175 frame.pack();
176 frame.setSize(500, frame.getHeight());
177 frame.setVisible(true);
178 }
179 }
180
181