KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > sql > DbPasswordDialog


1 package jimm.datavision.gui.sql;
2 import jimm.datavision.gui.EditFieldLayout;
3 import jimm.datavision.gui.FocusSetter;
4 import jimm.util.I18N;
5 import java.awt.BorderLayout JavaDoc;
6 import java.awt.Frame JavaDoc;
7 import java.awt.event.ActionListener JavaDoc;
8 import java.awt.event.ActionEvent JavaDoc;
9 import java.awt.event.WindowAdapter JavaDoc;
10 import java.awt.event.WindowEvent JavaDoc;
11 import javax.swing.*;
12
13 /**
14  * A modal dialog used to ask the user for a database password.
15  *
16  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
17  */

18 public class DbPasswordDialog extends JDialog implements ActionListener JavaDoc {
19
20 protected static final int FIELD_COLUMNS = 20;
21
22 protected String JavaDoc username;
23 protected String JavaDoc password;
24 protected JTextField usernameField;
25 protected JPasswordField passwordField;
26
27 /**
28  * Constructor.
29  *
30  * @param parent frame with which this dialog should be associated
31  * @param dbName database name
32  * @param userName database user name
33  */

34 public DbPasswordDialog(Frame JavaDoc parent, String JavaDoc dbName, String JavaDoc userName) {
35     super(parent, I18N.get("DbPasswordDialog.title"), true); // Modal
36
username = userName == null ? "" : userName;
37     buildWindow(dbName);
38     pack();
39     setVisible(true);
40 }
41
42 /**
43  * Returns username (or <code>null</code> if user hit Cancel).
44  *
45  * @return username (or <code>null</code> if user cancelled)
46  */

47 public String JavaDoc getUserName() { return username; }
48
49 /**
50  * Returns password (or <code>null</code> if user hit Cancel).
51  *
52  * @return password (or <code>null</code> if user cancelled)
53  */

54 public String JavaDoc getPassword() { return password; }
55
56 protected void buildWindow(String JavaDoc dbName) {
57     getContentPane().setLayout(new BorderLayout JavaDoc());
58
59     EditFieldLayout efl = new EditFieldLayout();
60     efl.addLabel(I18N.get("DbPasswordDialog.database"), dbName);
61     efl.setBorder(20);
62     usernameField = efl.addTextField(I18N.get("DbPasswordDialog.user_name"),
63                      username, FIELD_COLUMNS);
64     passwordField = efl.addPasswordField(I18N.get("DbPasswordDialog.password"),
65                      FIELD_COLUMNS);
66
67     JPanel buttonPanel = new JPanel();
68     JButton button;
69
70     buttonPanel.add(button = new JButton(I18N.get("GUI.ok")));
71     button.addActionListener(this);
72     button.setDefaultCapable(true);
73     getRootPane().setDefaultButton(button);
74
75     buttonPanel.add(button = new JButton(I18N.get("GUI.cancel")));
76     button.addActionListener(this);
77
78     getContentPane().add(efl.getPanel(), BorderLayout.CENTER);
79     getContentPane().add(buttonPanel, BorderLayout.SOUTH);
80
81     addWindowListener(new WindowAdapter JavaDoc() {
82     public void windowClosing(WindowEvent JavaDoc e) {
83         dispose();
84     }
85     });
86
87     new FocusSetter(passwordField);
88 }
89
90 /**
91  * Handles the buttons.
92  *
93  * @param e action event
94  */

95 public void actionPerformed(ActionEvent JavaDoc e) {
96     String JavaDoc cmd = e.getActionCommand();
97     if (I18N.get("GUI.ok").equals(cmd)) {
98     username = usernameField.getText();
99     password = new String JavaDoc(passwordField.getPassword());
100     dispose();
101     }
102     else if (I18N.get("GUI.cancel").equals(cmd)) {
103     dispose();
104     }
105 }
106
107 }
108
Popular Tags