001: /*
002: * Copyright (C) 2001-2007 Stephen Ostermiller
003: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * See COPYING.TXT for details.
016: */
017:
018: package com.Ostermiller.util;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022: import java.util.*;
023: import javax.swing.*;
024:
025: /**
026: * A modal dialog that asks the user for a user name and password.
027: * More information about this class is available from <a target="_top" href=
028: * "http://ostermiller.org/utils/PasswordDialog.html">ostermiller.org</a>.
029: *
030: * <code>
031: * <pre>
032: * PasswordDialog p = new PasswordDialog(null, "Test");
033: * if(p.showDialog()){
034: * System.out.println("Name: " + p.getName());
035: * System.out.println("Pass: " + p.getPass());
036: * } else {
037: * System.out.println("User selected cancel");
038: * }
039: * </pre>
040: * </code>
041: *
042: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
043: * @since ostermillerutils 1.00.00
044: */
045: public class PasswordDialog extends JDialog {
046:
047: /**
048: * Serial version id
049: */
050: private static final long serialVersionUID = -832548326686122133L;
051:
052: /**
053: * Locale specific strings displayed to the user.
054: *
055: * @since ostermillerutils 1.00.00
056: */
057: protected ResourceBundle labels;
058:
059: /**
060: * Set the locale used for getting localized
061: * strings.
062: *
063: * @param locale Locale used to for i18n.
064: *
065: * @since ostermillerutils 1.00.00
066: */
067: @Override
068: public void setLocale(Locale locale) {
069: labels = ResourceBundle.getBundle(
070: "com.Ostermiller.util.PasswordDialog", locale);
071: }
072:
073: /**
074: * Where the name is typed.
075: *
076: * @since ostermillerutils 1.00.00
077: */
078: protected JTextField name;
079: /**
080: * Where the password is typed.
081: *
082: * @since ostermillerutils 1.00.00
083: */
084: protected JPasswordField pass;
085: /**
086: * The OK button.
087: *
088: * @since ostermillerutils 1.00.00
089: */
090: protected JButton okButton;
091: /**
092: * The cancel button.
093: *
094: * @since ostermillerutils 1.00.00
095: */
096: protected JButton cancelButton;
097: /**
098: * The label for the field in which the name is typed.
099: *
100: * @since ostermillerutils 1.00.00
101: */
102: protected JLabel nameLabel;
103: /**
104: * The label for the field in which the password is typed.
105: *
106: * @since ostermillerutils 1.00.00
107: */
108: protected JLabel passLabel;
109:
110: /**
111: * Set the name that appears as the default
112: * An empty string will be used if this in not specified
113: * before the dialog is displayed.
114: *
115: * @param name default name to be displayed.
116: *
117: * @since ostermillerutils 1.00.00
118: */
119: @Override
120: public void setName(String name) {
121: this .name.setText(name);
122: }
123:
124: /**
125: * Set the password that appears as the default
126: * An empty string will be used if this in not specified
127: * before the dialog is displayed.
128: *
129: * @param pass default password to be displayed.
130: *
131: * @since ostermillerutils 1.00.00
132: */
133: public void setPass(String pass) {
134: this .pass.setText(pass);
135: }
136:
137: /**
138: * Set the label on the OK button.
139: * The default is a localized string.
140: *
141: * @param ok label for the ok button.
142: *
143: * @since ostermillerutils 1.00.00
144: */
145: public void setOKText(String ok) {
146: this .okButton.setText(ok);
147: pack();
148: }
149:
150: /**
151: * Set the label on the cancel button.
152: * The default is a localized string.
153: *
154: * @param cancel label for the cancel button.
155: *
156: * @since ostermillerutils 1.00.00
157: */
158: public void setCancelText(String cancel) {
159: this .cancelButton.setText(cancel);
160: pack();
161: }
162:
163: /**
164: * Set the label for the field in which the name is entered.
165: * The default is a localized string.
166: *
167: * @param name label for the name field.
168: *
169: * @since ostermillerutils 1.00.00
170: */
171: public void setNameLabel(String name) {
172: this .nameLabel.setText(name);
173: pack();
174: }
175:
176: /**
177: * Set the label for the field in which the password is entered.
178: * The default is a localized string.
179: *
180: * @param pass label for the password field.
181: *
182: * @since ostermillerutils 1.00.00
183: */
184: public void setPassLabel(String pass) {
185: this .passLabel.setText(pass);
186: pack();
187: }
188:
189: /**
190: * Get the name that was entered into the dialog before
191: * the dialog was closed.
192: *
193: * @return the name from the name field.
194: *
195: * @since ostermillerutils 1.00.00
196: */
197: @Override
198: public String getName() {
199: return name.getText();
200: }
201:
202: /**
203: * Get the password that was entered into the dialog before
204: * the dialog was closed.
205: *
206: * @return the password from the password field.
207: *
208: * @since ostermillerutils 1.00.00
209: */
210: public String getPass() {
211: return new String(pass.getPassword());
212: }
213:
214: /**
215: * Finds out if user used the OK button or an equivalent action
216: * to close the dialog.
217: * Pressing enter in the password field may be the same as
218: * 'OK' but closing the dialog and pressing the cancel button
219: * are not.
220: *
221: * @return true if the the user hit OK, false if the user canceled.
222: *
223: * @since ostermillerutils 1.00.00
224: */
225: public boolean okPressed() {
226: return pressed_OK;
227: }
228:
229: /**
230: * update this variable when the user makes an action
231: *
232: * @since ostermillerutils 1.00.00
233: */
234: private boolean pressed_OK = false;
235:
236: /**
237: * Create this dialog with the given parent and title.
238: *
239: * @param parent window from which this dialog is launched
240: * @param title the title for the dialog box window
241: *
242: * @since ostermillerutils 1.00.00
243: */
244: public PasswordDialog(Frame parent, String title) {
245:
246: super (parent, title, true);
247:
248: setLocale(Locale.getDefault());
249:
250: if (title == null) {
251: setTitle(labels.getString("dialog.title"));
252: }
253: if (parent != null) {
254: setLocationRelativeTo(parent);
255: }
256: // super calls dialogInit, so we don't need to do it again.
257: }
258:
259: /**
260: * Create this dialog with the given parent and the default title.
261: *
262: * @param parent window from which this dialog is launched
263: *
264: * @since ostermillerutils 1.00.00
265: */
266: public PasswordDialog(Frame parent) {
267: this (parent, null);
268: }
269:
270: /**
271: * Create this dialog with the default title.
272: *
273: * @since ostermillerutils 1.00.00
274: */
275: public PasswordDialog() {
276: this (null, null);
277: }
278:
279: /**
280: * Called by constructors to initialize the dialog.
281: *
282: * @since ostermillerutils 1.00.00
283: */
284: @Override
285: protected void dialogInit() {
286:
287: if (labels == null) {
288: setLocale(Locale.getDefault());
289: }
290:
291: name = new JTextField("", 20);
292: pass = new JPasswordField("", 20);
293: okButton = new JButton(labels.getString("dialog.ok"));
294: cancelButton = new JButton(labels.getString("dialog.cancel"));
295: nameLabel = new JLabel(labels.getString("dialog.name") + " ");
296: passLabel = new JLabel(labels.getString("dialog.pass") + " ");
297:
298: super .dialogInit();
299:
300: KeyListener keyListener = (new KeyAdapter() {
301: @Override
302: public void keyPressed(KeyEvent e) {
303: if (e.getKeyCode() == KeyEvent.VK_ESCAPE
304: || (e.getSource() == cancelButton && e
305: .getKeyCode() == KeyEvent.VK_ENTER)) {
306: pressed_OK = false;
307: PasswordDialog.this .setVisible(false);
308: }
309: if (e.getSource() == okButton
310: && e.getKeyCode() == KeyEvent.VK_ENTER) {
311: pressed_OK = true;
312: PasswordDialog.this .setVisible(false);
313: }
314: }
315: });
316: addKeyListener(keyListener);
317:
318: ActionListener actionListener = new ActionListener() {
319: public void actionPerformed(ActionEvent e) {
320: Object source = e.getSource();
321: if (source == name) {
322: // the user pressed enter in the name field.
323: name.transferFocus();
324: } else {
325: // other actions close the dialog.
326: pressed_OK = (source == pass || source == okButton);
327: PasswordDialog.this .setVisible(false);
328: }
329: }
330: };
331:
332: GridBagLayout gridbag = new GridBagLayout();
333: GridBagConstraints c = new GridBagConstraints();
334: c.insets.top = 5;
335: c.insets.bottom = 5;
336: JPanel pane = new JPanel(gridbag);
337: pane.setBorder(BorderFactory.createEmptyBorder(10, 20, 5, 20));
338: c.anchor = GridBagConstraints.EAST;
339: gridbag.setConstraints(nameLabel, c);
340: pane.add(nameLabel);
341:
342: gridbag.setConstraints(name, c);
343: name.addActionListener(actionListener);
344: name.addKeyListener(keyListener);
345: pane.add(name);
346:
347: c.gridy = 1;
348: gridbag.setConstraints(passLabel, c);
349: pane.add(passLabel);
350:
351: gridbag.setConstraints(pass, c);
352: pass.addActionListener(actionListener);
353: pass.addKeyListener(keyListener);
354: pane.add(pass);
355:
356: c.gridy = 2;
357: c.gridwidth = GridBagConstraints.REMAINDER;
358: c.anchor = GridBagConstraints.CENTER;
359: JPanel panel = new JPanel();
360: okButton.addActionListener(actionListener);
361: okButton.addKeyListener(keyListener);
362: panel.add(okButton);
363: cancelButton.addActionListener(actionListener);
364: cancelButton.addKeyListener(keyListener);
365: panel.add(cancelButton);
366: gridbag.setConstraints(panel, c);
367: pane.add(panel);
368:
369: getContentPane().add(pane);
370:
371: pack();
372: }
373:
374: /**
375: * Shows the dialog and returns true if the user pressed ok.
376: *
377: * @return true if the the user hit OK, false if the user canceled.
378: *
379: * @since ostermillerutils 1.00.00
380: */
381: public boolean showDialog() {
382: setVisible(true);
383: return okPressed();
384: }
385: }
|