001: package jimm.datavision.gui.sql;
002:
003: import jimm.datavision.gui.EditFieldLayout;
004: import jimm.datavision.gui.FocusSetter;
005: import jimm.util.I18N;
006: import java.awt.BorderLayout;
007: import java.awt.Frame;
008: import java.awt.event.ActionListener;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.WindowAdapter;
011: import java.awt.event.WindowEvent;
012: import javax.swing.*;
013:
014: /**
015: * A modal dialog used to ask the user for a database password.
016: *
017: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
018: */
019: public class DbPasswordDialog extends JDialog implements ActionListener {
020:
021: protected static final int FIELD_COLUMNS = 20;
022:
023: protected String username;
024: protected String password;
025: protected JTextField usernameField;
026: protected JPasswordField passwordField;
027:
028: /**
029: * Constructor.
030: *
031: * @param parent frame with which this dialog should be associated
032: * @param dbName database name
033: * @param userName database user name
034: */
035: public DbPasswordDialog(Frame parent, String dbName, String userName) {
036: super (parent, I18N.get("DbPasswordDialog.title"), true); // Modal
037: username = userName == null ? "" : userName;
038: buildWindow(dbName);
039: pack();
040: setVisible(true);
041: }
042:
043: /**
044: * Returns username (or <code>null</code> if user hit Cancel).
045: *
046: * @return username (or <code>null</code> if user cancelled)
047: */
048: public String getUserName() {
049: return username;
050: }
051:
052: /**
053: * Returns password (or <code>null</code> if user hit Cancel).
054: *
055: * @return password (or <code>null</code> if user cancelled)
056: */
057: public String getPassword() {
058: return password;
059: }
060:
061: protected void buildWindow(String dbName) {
062: getContentPane().setLayout(new BorderLayout());
063:
064: EditFieldLayout efl = new EditFieldLayout();
065: efl.addLabel(I18N.get("DbPasswordDialog.database"), dbName);
066: efl.setBorder(20);
067: usernameField = efl.addTextField(I18N
068: .get("DbPasswordDialog.user_name"), username,
069: FIELD_COLUMNS);
070: passwordField = efl.addPasswordField(I18N
071: .get("DbPasswordDialog.password"), FIELD_COLUMNS);
072:
073: JPanel buttonPanel = new JPanel();
074: JButton button;
075:
076: buttonPanel.add(button = new JButton(I18N.get("GUI.ok")));
077: button.addActionListener(this );
078: button.setDefaultCapable(true);
079: getRootPane().setDefaultButton(button);
080:
081: buttonPanel.add(button = new JButton(I18N.get("GUI.cancel")));
082: button.addActionListener(this );
083:
084: getContentPane().add(efl.getPanel(), BorderLayout.CENTER);
085: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
086:
087: addWindowListener(new WindowAdapter() {
088: public void windowClosing(WindowEvent e) {
089: dispose();
090: }
091: });
092:
093: new FocusSetter(passwordField);
094: }
095:
096: /**
097: * Handles the buttons.
098: *
099: * @param e action event
100: */
101: public void actionPerformed(ActionEvent e) {
102: String cmd = e.getActionCommand();
103: if (I18N.get("GUI.ok").equals(cmd)) {
104: username = usernameField.getText();
105: password = new String(passwordField.getPassword());
106: dispose();
107: } else if (I18N.get("GUI.cancel").equals(cmd)) {
108: dispose();
109: }
110: }
111:
112: }
|