KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > PasswordDialog


1 /*
2  * A password dialog box.
3  * Copyright (C) 2001, 2002 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18
19 package com.Ostermiller.util;
20
21 import javax.swing.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.util.ResourceBundle JavaDoc;
25 import java.util.Locale JavaDoc;
26
27 /**
28  * A modal dialog that asks the user for a user name and password.
29  * More information about this class is available from <a target="_top" HREF=
30  * "http://ostermiller.org/utils/PasswordDialog.html">ostermiller.org</a>.
31  *
32  * <code>
33  * <pre>
34  * PasswordDialog p = new PasswordDialog(null, "Test");
35  * if(p.showDialog()){
36  * System.out.println("Name: " + p.getName());
37  * System.out.println("Pass: " + p.getPass());
38  * } else {
39  * System.out.println("User selected cancel");
40  * }
41  * </pre>
42  * </code>
43  *
44  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
45  * @since ostermillerutils 1.00.00
46  */

47 public class PasswordDialog extends JDialog {
48
49     /**
50      * Locale specific strings displayed to the user.
51      *
52      * @since ostermillerutils 1.00.00
53      */

54     protected ResourceBundle JavaDoc labels;
55
56     /**
57      * Set the locale used for getting localized
58      * strings.
59      *
60      * @param locale Locale used to for i18n.
61      *
62      * @since ostermillerutils 1.00.00
63      */

64     public void setLocale(Locale JavaDoc locale){
65         labels = ResourceBundle.getBundle("com.Ostermiller.util.PasswordDialog", locale);
66     }
67
68     /**
69      * Where the name is typed.
70      *
71      * @since ostermillerutils 1.00.00
72      */

73     protected JTextField name;
74     /**
75      * Where the password is typed.
76      *
77      * @since ostermillerutils 1.00.00
78      */

79     protected JPasswordField pass;
80     /**
81      * The OK button.
82      *
83      * @since ostermillerutils 1.00.00
84      */

85     protected JButton okButton;
86     /**
87      * The cancel button.
88      *
89      * @since ostermillerutils 1.00.00
90      */

91     protected JButton cancelButton;
92     /**
93      * The label for the field in which the name is typed.
94      *
95      * @since ostermillerutils 1.00.00
96      */

97     protected JLabel nameLabel;
98     /**
99      * The label for the field in which the password is typed.
100      *
101      * @since ostermillerutils 1.00.00
102      */

103     protected JLabel passLabel;
104
105     /**
106      * Set the name that appears as the default
107      * An empty string will be used if this in not specified
108      * before the dialog is displayed.
109      *
110      * @param name default name to be displayed.
111      *
112      * @since ostermillerutils 1.00.00
113      */

114     public void setName(String JavaDoc name){
115         this.name.setText(name);
116     }
117
118     /**
119      * Set the password that appears as the default
120      * An empty string will be used if this in not specified
121      * before the dialog is displayed.
122      *
123      * @param pass default password to be displayed.
124      *
125      * @since ostermillerutils 1.00.00
126      */

127     public void setPass(String JavaDoc pass){
128         this.pass.setText(pass);
129     }
130
131     /**
132      * Set the label on the OK button.
133      * The default is a localized string.
134      *
135      * @param ok label for the ok button.
136      *
137      * @since ostermillerutils 1.00.00
138      */

139     public void setOKText(String JavaDoc ok){
140         this.okButton.setText(ok);
141         pack();
142     }
143
144     /**
145      * Set the label on the cancel button.
146      * The default is a localized string.
147      *
148      * @param cancel label for the cancel button.
149      *
150      * @since ostermillerutils 1.00.00
151      */

152     public void setCancelText(String JavaDoc cancel){
153         this.cancelButton.setText(cancel);
154         pack();
155     }
156
157     /**
158      * Set the label for the field in which the name is entered.
159      * The default is a localized string.
160      *
161      * @param name label for the name field.
162      *
163      * @since ostermillerutils 1.00.00
164      */

165     public void setNameLabel(String JavaDoc name){
166         this.nameLabel.setText(name);
167         pack();
168     }
169
170     /**
171      * Set the label for the field in which the password is entered.
172      * The default is a localized string.
173      *
174      * @param pass label for the password field.
175      *
176      * @since ostermillerutils 1.00.00
177      */

178     public void setPassLabel(String JavaDoc pass){
179         this.passLabel.setText(pass);
180         pack();
181     }
182
183     /**
184      * Get the name that was entered into the dialog before
185      * the dialog was closed.
186      *
187      * @return the name from the name field.
188      *
189      * @since ostermillerutils 1.00.00
190      */

191     public String JavaDoc getName(){
192         return name.getText();
193     }
194
195     /**
196      * Get the password that was entered into the dialog before
197      * the dialog was closed.
198      *
199      * @return the password from the password field.
200      *
201      * @since ostermillerutils 1.00.00
202      */

203     public String JavaDoc getPass(){
204         return new String JavaDoc(pass.getPassword());
205     }
206
207     /**
208      * Finds out if user used the OK button or an equivalent action
209      * to close the dialog.
210      * Pressing enter in the password field may be the same as
211      * 'OK' but closing the dialog and pressing the cancel button
212      * are not.
213      *
214      * @return true if the the user hit OK, false if the user canceled.
215      *
216      * @since ostermillerutils 1.00.00
217      */

218     public boolean okPressed(){
219         return pressed_OK;
220     }
221
222     /**
223      * update this variable when the user makes an action
224      *
225      * @since ostermillerutils 1.00.00
226      */

227     private boolean pressed_OK = false;
228
229     /**
230      * Create this dialog with the given parent and title.
231      *
232      * @param parent window from which this dialog is launched
233      * @param title the title for the dialog box window
234      *
235      * @since ostermillerutils 1.00.00
236      */

237     public PasswordDialog(Frame parent, String JavaDoc title) {
238
239         super(parent, title, true);
240
241         setLocale(Locale.getDefault());
242
243         if (title==null){
244             setTitle(labels.getString("dialog.title"));
245         }
246         if (parent != null){
247             setLocationRelativeTo(parent);
248         }
249         // super calls dialogInit, so we don't need to do it again.
250
}
251
252     /**
253      * Create this dialog with the given parent and the default title.
254      *
255      * @param parent window from which this dialog is launched
256      *
257      * @since ostermillerutils 1.00.00
258      */

259     public PasswordDialog(Frame parent) {
260         this(parent, null);
261     }
262
263     /**
264      * Create this dialog with the default title.
265      *
266      * @since ostermillerutils 1.00.00
267      */

268     public PasswordDialog() {
269         this(null, null);
270     }
271
272     /**
273      * Called by constructors to initialize the dialog.
274      *
275      * @since ostermillerutils 1.00.00
276      */

277     protected void dialogInit(){
278
279         if (labels == null){
280             setLocale(Locale.getDefault());
281         }
282
283         name = new JTextField("", 20);
284         pass = new JPasswordField("", 20);
285         okButton = new JButton(labels.getString("dialog.ok"));
286         cancelButton = new JButton(labels.getString("dialog.cancel"));
287         nameLabel = new JLabel(labels.getString("dialog.name") + " ");
288         passLabel = new JLabel(labels.getString("dialog.pass") + " ");
289
290         super.dialogInit();
291
292         KeyListener keyListener = (new KeyAdapter() {
293             public void keyPressed(KeyEvent e){
294                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE ||
295                         (e.getSource() == cancelButton
296                         && e.getKeyCode() == KeyEvent.VK_ENTER)){
297                     pressed_OK = false;
298                     PasswordDialog.this.setVisible(false);
299                 }
300                 if (e.getSource() == okButton &&
301                         e.getKeyCode() == KeyEvent.VK_ENTER){
302                     pressed_OK = true;
303                     PasswordDialog.this.setVisible(false);
304                 }
305             }
306         });
307         addKeyListener(keyListener);
308
309         ActionListener actionListener = new ActionListener() {
310             public void actionPerformed(ActionEvent e){
311                 Object JavaDoc source = e.getSource();
312                 if (source == name){
313                     // the user pressed enter in the name field.
314
name.transferFocus();
315                 } else {
316                     // other actions close the dialog.
317
pressed_OK = (source == pass || source == okButton);
318                     PasswordDialog.this.setVisible(false);
319                 }
320             }
321         };
322
323         GridBagLayout gridbag = new GridBagLayout();
324         GridBagConstraints c = new GridBagConstraints();
325         c.insets.top = 5;
326         c.insets.bottom = 5;
327         JPanel pane = new JPanel(gridbag);
328         pane.setBorder(BorderFactory.createEmptyBorder(10, 20, 5, 20));
329         JLabel label;
330
331         c.anchor = GridBagConstraints.EAST;
332         gridbag.setConstraints(nameLabel, c);
333         pane.add(nameLabel);
334
335         gridbag.setConstraints(name, c);
336         name.addActionListener(actionListener);
337         name.addKeyListener(keyListener);
338         pane.add(name);
339
340         c.gridy = 1;
341         gridbag.setConstraints(passLabel, c);
342         pane.add(passLabel);
343
344         gridbag.setConstraints(pass, c);
345         pass.addActionListener(actionListener);
346         pass.addKeyListener(keyListener);
347         pane.add(pass);
348
349         c.gridy = 2;
350         c.gridwidth = GridBagConstraints.REMAINDER;
351         c.anchor = GridBagConstraints.CENTER;
352         JPanel panel = new JPanel();
353         okButton.addActionListener(actionListener);
354         okButton.addKeyListener(keyListener);
355         panel.add(okButton);
356         cancelButton.addActionListener(actionListener);
357         cancelButton.addKeyListener(keyListener);
358         panel.add(cancelButton);
359         gridbag.setConstraints(panel, c);
360         pane.add(panel);
361
362         getContentPane().add(pane);
363
364         pack();
365     }
366
367     /**
368      * Shows the dialog and returns true if the user pressed ok.
369      *
370      * @return true if the the user hit OK, false if the user canceled.
371      *
372      * @since ostermillerutils 1.00.00
373      */

374     public boolean showDialog(){
375         setVisible(true);
376         return okPressed();
377     }
378
379     /**
380      * A simple example to show how this might be used.
381      * If there are arguments passed to this program, the first
382      * is treated as the default name, the second as the default password
383      *
384      * @param args command line arguments: name and password (optional)
385      *
386      * @since ostermillerutils 1.00.00
387      */

388     private static void main(String JavaDoc[] args){
389         PasswordDialog p = new PasswordDialog();
390         if(args.length > 0){
391             p.setName(args[0]);
392         }
393         if(args.length > 1){
394             p.setPass(args[1]);
395         }
396         if(p.showDialog()){
397             System.out.println("Name: " + p.getName());
398             System.out.println("Pass: " + p.getPass());
399         } else {
400             System.out.println("User selected cancel");
401         }
402         p.dispose();
403         p = null;
404         System.exit(0);
405     }
406 }
407
Popular Tags