KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > client > LoginDialog


1 /**
2  * Copyright (c) 2005 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Gregory Lapouchnian
22  * Patrick Smith
23  * --------------------------------------------------------------------------
24  * $Id: LoginDialog.java,v 1.2 2005/07/08 14:00:45 glapouch Exp $
25  * --------------------------------------------------------------------------
26  */

27 package olstore.client;
28
29 import java.awt.Dimension;
30 import java.awt.GridLayout;
31 import java.awt.Toolkit;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34
35 import javax.swing.JButton;
36 import javax.swing.JDialog;
37 import javax.swing.JFrame;
38 import javax.swing.JLabel;
39 import javax.swing.JPasswordField;
40 import javax.swing.JTextField;
41
42 /**
43  * Login dialog to authenticate the user before completing a purchase.
44  */

45 public class LoginDialog extends JDialog implements ActionListener {
46
47     /** Username field. */
48     private JTextField username;
49
50     /** Password field. */
51     private JPasswordField password;
52
53     /** Login button. */
54     private JButton bLogin;
55
56     /** Cancel button. */
57     private JButton bCancel;
58
59     /** Length of the input fields. */
60     private static final int FIELD_LENGTH = 15;
61
62     /** */
63     private boolean loginPress = false;
64
65     /**
66      * Login dialog that accepts the username/password combination from the user
67      * which is then passed to the webservice for authetication before proceeding
68      * with the order.
69      * @param parent the parent JFrame of this dialog
70      */

71     public LoginDialog(JFrame parent) {
72         super(parent, "Enter username and password...");
73         username = new JTextField(FIELD_LENGTH);
74         password = new JPasswordField(FIELD_LENGTH);
75
76         setBackground(OlstoreSwingClient.itemColor1);
77
78         bLogin = new JButton("Login");
79         bLogin.addActionListener(this);
80         bLogin.setBackground(OlstoreSwingClient.buttonColor);
81
82         bCancel = new JButton("Cancel");
83         bCancel.addActionListener(this);
84         bCancel.setBackground(OlstoreSwingClient.buttonColor);
85
86         getContentPane().setLayout(new GridLayout(0, 2, 5, 5));
87
88         JLabel userLabel = new JLabel("Username:");
89         userLabel.setBackground(OlstoreSwingClient.itemColor1);
90         JLabel passwordLabel = new JLabel("Password:");
91         passwordLabel.setBackground(OlstoreSwingClient.itemColor1);
92         getContentPane().add(userLabel);
93         getContentPane().add(username);
94         getContentPane().add(passwordLabel);
95         getContentPane().add(password);
96         getContentPane().add(bLogin);
97         getContentPane().add(bCancel);
98
99         setModal(true);
100
101         pack();
102         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
103         setLocation((int) d.getWidth() / 2 - getWidth(), (int) d.getHeight()
104                 / 2 - getHeight());
105     }
106
107     /**
108      * Handle the login.
109      * @param e the event that triggered the listener
110      */

111     public void actionPerformed(ActionEvent e) {
112         if (e.getSource().equals(bLogin)) {
113             loginPress = true;
114         }
115         setModal(false);
116         hide();
117     }
118
119     /**
120      * Check whether the login button has been pressed.
121      * @return true if the login button has been clicked, false otherwise.
122      */

123     public boolean login() {
124         return loginPress;
125     }
126
127     /**
128      * Get the username and password.
129      * @return a string array containing the username and the password
130      */

131     public String[] getLogin() {
132         String[] result = new String[2];
133
134         result[0] = username.getText();
135         result[1] = new String(password.getPassword());
136
137         return result;
138
139     }
140 }
141
Popular Tags