KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chatserver > client > LoginDialog


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package chatserver.client;
16
17 import java.awt.*;
18 import java.awt.event.*;
19 import javax.swing.*;
20 import javax.swing.event.*;
21 import javax.swing.border.*;
22 import java.io.IOException JavaDoc;
23
24 /**
25  * Login Dialog GUI
26  * @author Akshathkumar Shetty
27  */

28 public class LoginDialog extends JDialog {
29     
30     private JPanel topPanel;
31     private JPanel ipPanel;
32     private JPanel authPanel;
33     private JPanel buttonPanel;
34     
35     private JLabel productName;
36     private JLabel ipLabel;
37     private JTextField ipField;
38     private JLabel portLabel;
39     private JTextField portField;
40     private JLabel loginLabel;
41     private JTextField loginField;
42     private JLabel passwordLabel;
43     private JPasswordField passwordField;
44     private JButton loginButton;
45     private JButton cancelButton;
46     
47     private String JavaDoc statusTxt1 = "<html><font style=\"font-size:15pt;color:#535353\"><b>";
48     private String JavaDoc statusTxt2 = "</b></font>";
49     private GridBagConstraints gbc;
50     
51     //for storing the values
52
private String JavaDoc values[] = new String JavaDoc[4];
53     private boolean isOk = false;
54     
55     public LoginDialog(Frame parent, String JavaDoc args[]) {
56         super(parent, "Please Login");
57         gbc = new GridBagConstraints();
58         productName = new JLabel(statusTxt1+
59                 "Chat Login"+statusTxt2,JLabel.CENTER);
60         
61         ipLabel = new JLabel("IP Address");
62         if(args!=null && args.length>=2)
63             ipField = new JTextField(args[1]);
64         else
65             ipField = new JTextField("127.0.0.1");
66         portLabel = new JLabel("Port");
67         if(args!=null && args.length>=3)
68             ipField = new JTextField(args[2]);
69         else
70             portField = new JTextField("7412");
71         
72         loginLabel = new JLabel("Login");
73         loginField = new JTextField("user1");
74         passwordLabel = new JLabel("Password");
75         passwordField = new JPasswordField("user1");
76         
77         loginButton = new JButton("Login");
78         loginButton.setMnemonic('L');
79         cancelButton = new JButton("Cancel");
80         cancelButton.setMnemonic('C');
81         cancelButton.addActionListener( new ActionListener() {
82             public void actionPerformed(ActionEvent e) {
83                 hide();
84             }
85         });
86         
87         //--- Action
88
ipField.addActionListener(new ActionListener() {
89             public void actionPerformed(ActionEvent e) {
90                 portField.requestFocus();
91             }
92         });
93         portField.addActionListener(new ActionListener() {
94             public void actionPerformed(ActionEvent e) {
95                 loginField.requestFocus();
96             }
97         });
98         loginField.addActionListener(new ActionListener() {
99             public void actionPerformed(ActionEvent e) {
100                 passwordField.requestFocus();
101             }
102         });
103         
104         ActionListener loginAl = new ActionListener() {
105             public void actionPerformed(ActionEvent e) {
106                 isOk = false;
107                 if(ipField.getText().equals("")) {
108                     showError("Blank IP Address");
109                     return;
110                 }
111                 if(portField.getText().equals("")) {
112                     showError("Blank Port Number");
113                     return;
114                 } else {
115                     try {
116                         Integer.parseInt(portField.getText());
117                     } catch(Exception JavaDoc ex) {
118                         showError("Bad Port Number.");
119                         return;
120                     }
121                 }
122                 if(loginField.getText().equals("")) {
123                     showError("Blank Login");
124                     return;
125                 }
126                 char p[] = passwordField.getPassword();
127                 if(p==null || p.length==0) {
128                     showError("Blank password");
129                     return;
130                 }
131                 p = null;
132                 isOk = true;
133                 hide();
134             }
135         };
136         
137         loginButton.addActionListener(loginAl);
138         passwordField.addActionListener(loginAl);
139         
140         cancelButton.addActionListener( new ActionListener() {
141             public void actionPerformed(ActionEvent e) {
142                 isOk = false;
143                 hide();
144             }
145         });
146         //---- Action
147

148         Container cp = getContentPane();
149         
150         //--- Top Panel
151
topPanel = new JPanel();
152         topPanel.setLayout(new GridBagLayout());
153         gbc.insets = new Insets( 2, 2, 2, 2 );
154         gbc.weighty = 0.0;
155         gbc.weightx = 0.0;
156         gbc.gridx = 0;
157         gbc.gridy = 0;
158         gbc.gridheight = 1;
159         gbc.gridwidth = 1;
160         gbc.anchor = GridBagConstraints.CENTER;
161         gbc.fill = GridBagConstraints.NONE;
162         topPanel.add(productName, gbc);
163         
164         //-- IP Panel
165
ipPanel = new JPanel();
166         ipPanel.setLayout(new GridBagLayout());
167         gbc.gridx = 0;
168         gbc.gridy = 0;
169         gbc.weightx = 0.0;
170         gbc.weighty = 0.0;
171         gbc.gridheight = 1;
172         gbc.gridwidth = 1;
173         gbc.anchor = GridBagConstraints.WEST;
174         gbc.fill = GridBagConstraints.NONE;
175         ipPanel.add(ipLabel, gbc);
176         
177         gbc.gridx = 1;
178         gbc.gridy = 0;
179         gbc.weightx = 1.0;
180         gbc.fill = GridBagConstraints.BOTH;
181         ipPanel.add(ipField, gbc);
182         
183         gbc.gridx = 0;
184         gbc.gridy = 1;
185         gbc.weightx = 0.0;
186         gbc.fill = GridBagConstraints.NONE;
187         ipPanel.add(portLabel, gbc);
188         
189         gbc.gridx = 1;
190         gbc.gridy = 1;
191         gbc.weightx = 1.0;
192         gbc.fill = GridBagConstraints.BOTH;
193         ipPanel.add(portField, gbc);
194         ipPanel.setBorder(BorderFactory.createTitledBorder(
195                 new EtchedBorder(),"Location"));
196         
197         //-- Login Panel
198
authPanel = new JPanel();
199         authPanel.setLayout(new GridBagLayout());
200         gbc.gridx = 0;
201         gbc.gridy = 0;
202         gbc.weightx = 0.0;
203         gbc.weighty = 0.0;
204         gbc.gridheight = 1;
205         gbc.gridwidth = 1;
206         gbc.anchor = GridBagConstraints.WEST;
207         gbc.fill = GridBagConstraints.NONE;
208         authPanel.add(loginLabel, gbc);
209         
210         gbc.gridx = 1;
211         gbc.gridy = 0;
212         gbc.weightx = 1.0;
213         gbc.fill = GridBagConstraints.BOTH;
214         authPanel.add(loginField, gbc);
215         
216         gbc.gridx = 0;
217         gbc.gridy = 1;
218         gbc.weightx = 0.0;
219         gbc.fill = GridBagConstraints.NONE;
220         authPanel.add(passwordLabel, gbc);
221         
222         gbc.gridx = 1;
223         gbc.gridy = 1;
224         gbc.weightx = 1.0;
225         gbc.fill = GridBagConstraints.BOTH;
226         authPanel.add(passwordField, gbc);
227         authPanel.setBorder(BorderFactory.createTitledBorder(
228                 new EtchedBorder(),"Authentication"));
229         
230         //-- buttonPanel
231
buttonPanel = new JPanel();
232         buttonPanel.setLayout(new GridBagLayout());
233         gbc.gridx = 0;
234         gbc.gridy = 0;
235         gbc.weightx = 0.0;
236         gbc.weighty = 0.0;
237         gbc.gridheight = 1;
238         gbc.gridwidth = 1;
239         gbc.anchor = GridBagConstraints.CENTER;
240         gbc.fill = GridBagConstraints.NONE;
241         buttonPanel.add(loginButton, gbc);
242         
243         gbc.gridx = 1;
244         buttonPanel.add(cancelButton, gbc);
245         
246         cp.setLayout(new GridBagLayout());
247         gbc.gridx = 0;
248         gbc.gridy = 0;
249         gbc.weightx = 1.0;
250         gbc.weighty = 1.0;
251         gbc.gridheight = 1;
252         gbc.gridwidth = 1;
253         gbc.anchor = GridBagConstraints.CENTER;
254         gbc.fill = GridBagConstraints.HORIZONTAL;
255         cp.add(topPanel,gbc);
256         gbc.fill = GridBagConstraints.BOTH;
257         gbc.gridy = 1;
258         cp.add(ipPanel,gbc);
259         gbc.fill = GridBagConstraints.BOTH;
260         gbc.gridy = 2;
261         cp.add(authPanel,gbc);
262         gbc.fill = GridBagConstraints.HORIZONTAL;
263         gbc.gridy = 3;
264         cp.add(buttonPanel,gbc);
265         pack();
266         setSize(240,250);
267         setResizable(false);
268         setModal(true);
269         centerWindow(this);
270     }
271     
272     private void showError(String JavaDoc msg) {
273         JOptionPane.showMessageDialog(LoginDialog.this,
274                 msg, "Error", JOptionPane.ERROR_MESSAGE);
275     }
276     
277     public String JavaDoc[] getValues(){
278         values[0] = ipField.getText();
279         values[1] = portField.getText();
280         values[2] = loginField.getText();
281         values[3] = new String JavaDoc(passwordField.getPassword());
282         return values;
283     }
284     
285     public boolean isOk(){
286         return isOk;
287     }
288
289      public void clearStatus(){
290         isOk = false;
291     }
292     
293     public static void centerWindow(Window window) {
294         Dimension dim = window.getToolkit().getScreenSize();
295         window.setLocation(dim.width/2 - window.getWidth()/2,
296                 dim.height/2 - window.getHeight()/2);
297     }
298 }
299
Popular Tags