001: package org.dbbrowser.ui.panel.connectioninformationwindow;
002:
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005: import java.awt.event.KeyAdapter;
006: import java.awt.event.KeyEvent;
007: import infrastructure.internationalization.InternationalizationManager;
008: import infrastructure.propertymanager.PropertyManager;
009: import javax.swing.ImageIcon;
010: import javax.swing.JButton;
011: import javax.swing.JDialog;
012: import javax.swing.JFrame;
013: import javax.swing.JLabel;
014: import javax.swing.JOptionPane;
015: import javax.swing.JPasswordField;
016: import org.dbbrowser.ui.UIControllerForQueries;
017:
018: /**
019: * This class presents the user with a UI which is used to enter a password
020: * @author amangat
021: *
022: */
023: public class PasswordInputWindow extends JDialog implements
024: ActionListener {
025: private static final String TITLE = InternationalizationManager
026: .getInstance().getMessage("dbbrowser-ui",
027: "dbbrowser-ui-dbbrowser-window-title-label", null);;
028:
029: private static final long serialVersionUID = UIControllerForQueries.version;
030: private static final String PASSWORD_1_INPUT_LABEL = InternationalizationManager
031: .getInstance()
032: .getMessage(
033: "dbbrowser-ui",
034: "dbbrowser-ui-known-connections-panel-type-password-label",
035: null);
036: private static final String PASSWORD_2_INPUT_LABEL = InternationalizationManager
037: .getInstance()
038: .getMessage(
039: "dbbrowser-ui",
040: "dbbrowser-ui-known-connections-panel-re-type-password-label",
041: null);
042: private static final String PASSWORDS_DONT_MATCH_ERROR_MESSAGE = InternationalizationManager
043: .getInstance()
044: .getMessage(
045: "dbbrowser-ui",
046: "dbbrowser-ui-known-connections-panel-passwords-dont-match",
047: null);
048: private static final String OK_BUTTON_LABEL = InternationalizationManager
049: .getInstance()
050: .getMessage(
051: "dbbrowser-ui",
052: "dbbrowser-ui-first-run-message-dialog-ok-button-label",
053: null);
054: private static final String OK_BUTTON_ICON_FILENAME = PropertyManager
055: .getInstance()
056: .getProperty(
057: "dbbrowser-ui-view-record-window-update-record-icon");
058:
059: private JLabel label1 = new JLabel(PASSWORD_1_INPUT_LABEL);
060: private JPasswordField passwordField1 = new JPasswordField();
061: private JLabel label2 = new JLabel(PASSWORD_2_INPUT_LABEL);
062: private JPasswordField passwordField2 = new JPasswordField();
063: private JButton okButton = new JButton(OK_BUTTON_LABEL);
064: private String password = null;
065: private boolean confirmPassword = true;
066:
067: public PasswordInputWindow(Boolean confirmPassword) {
068: this .setTitle(TITLE);
069: this .setSize(300, 200);
070: this .setLocationRelativeTo(null);
071: this .setResizable(false);
072: this .setModal(true);
073:
074: this .confirmPassword = confirmPassword.booleanValue();
075:
076: this .getContentPane().setLayout(null);
077:
078: this .label1.setBounds(30, 30, 130, 30);
079: this .passwordField1.setBounds(160, 30, 120, 30);
080: this .label2.setBounds(30, 90, 130, 30);
081: this .passwordField2.setBounds(160, 90, 120, 30);
082: this .okButton.setBounds(100, 130, 100, 30);
083:
084: this .passwordField1
085: .addKeyListener(new PasswordFieldKeyAdapter());
086: this .passwordField2
087: .addKeyListener(new PasswordFieldKeyAdapter());
088: this .okButton.addActionListener(this );
089: this .okButton.setIcon(new ImageIcon(OK_BUTTON_ICON_FILENAME));
090: this .okButton.setEnabled(false);
091:
092: this .getContentPane().add(label1);
093: this .getContentPane().add(passwordField1);
094:
095: //If confirmation is required, add second field
096: if (this .confirmPassword) {
097: this .getContentPane().add(label2);
098: this .getContentPane().add(passwordField2);
099: }
100: this .getContentPane().add(okButton);
101:
102: this .setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
103: }
104:
105: public String getPassword() {
106: return this .password;
107: }
108:
109: public void actionPerformed(ActionEvent e) {
110: String password1 = new String(this .passwordField1.getPassword());
111: String password2 = new String(this .passwordField2.getPassword());
112:
113: //if confirmation of 2 passwords required, do the check
114: if (this .confirmPassword) {
115: if (password1 != null && password2 != null
116: && (!"".equals(password1))
117: && (!"".equals(password2))
118: && password1.equals(password2)) {
119: this .password = password1;
120: this .hide();
121: this .pack();
122: } else {
123: this .okButton.setEnabled(false);
124: this .passwordField1.setText("");
125: this .passwordField2.setText("");
126: this .passwordField1.requestFocus();
127: JOptionPane.showMessageDialog(null,
128: PASSWORDS_DONT_MATCH_ERROR_MESSAGE, TITLE,
129: JOptionPane.ERROR_MESSAGE);
130: }
131: } else {
132: this .password = password1;
133: this .hide();
134: this .pack();
135: }
136: }
137:
138: private class PasswordFieldKeyAdapter extends KeyAdapter {
139: public void keyTyped(KeyEvent e) {
140: if (confirmPassword) {
141: if (passwordField1.getPassword().length > 0
142: && passwordField2.getPassword().length > 0) {
143: okButton.setEnabled(true);
144: } else {
145: okButton.setEnabled(false);
146: }
147: } else {
148: if (passwordField1.getPassword().length > 0) {
149: okButton.setEnabled(true);
150: } else {
151: okButton.setEnabled(false);
152: }
153: }
154: }
155: }
156: }
|