KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > net > qsadmin > gui > 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 org.quickserver.net.qsadmin.gui;
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 import org.quickserver.util.TextFile;
25 import org.quickserver.swing.JFrameUtilities;
26
27 /**
28  * Login Dialog
29  * QuickServer Admin GUI - QSAdminGUI
30  * @author Akshathkumar Shetty
31  */

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

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