001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util;
019:
020: import java.awt.Color;
021: import java.awt.event.WindowAdapter;
022: import java.awt.event.WindowEvent;
023: import java.beans.PropertyChangeEvent;
024: import java.beans.PropertyChangeListener;
025:
026: import javax.swing.JDialog;
027: import javax.swing.JLabel;
028: import javax.swing.JOptionPane;
029: import javax.swing.JPasswordField;
030:
031: import de.finix.contelligent.client.ContelligentClient;
032: import de.finix.contelligent.client.i18n.Resources;
033:
034: public class ChangePasswordDialog extends EscapeDialog {
035: private final String okString = Resources.getLocalString("ok");
036:
037: private final String cancelString = Resources
038: .getLocalString("cancel");
039:
040: private JLabel passwordLabel, retypePasswordLabel,
041: oldPasswordLabel;
042:
043: private JPasswordField oldPasswordField = new JPasswordField(15);
044:
045: private JPasswordField passwordField = new JPasswordField(15);
046:
047: private JPasswordField password2Field = new JPasswordField(15);
048:
049: private JOptionPane optionPane;
050:
051: private Color defaultForeground;
052:
053: private int option = JOptionPane.CANCEL_OPTION;
054:
055: private boolean askForOldPassword = false;
056:
057: private int minPasswordChars;
058:
059: public ChangePasswordDialog(String title, String passwordText,
060: String retypePasswordText, String oldPasswordText,
061: int minPasswordChars) {
062: super (ContelligentClient.getActiveFrame(), title, true);
063: RestrictedDocument document = new RestrictedDocument();
064: document.setAllowString(RestrictedDocument.PRINCIPALS);
065: oldPasswordField.setDocument(document);
066: document = new RestrictedDocument();
067: document.setAllowString(RestrictedDocument.PRINCIPALS);
068: passwordField.setDocument(document);
069: document = new RestrictedDocument();
070: document.setAllowString(RestrictedDocument.PRINCIPALS);
071: password2Field.setDocument(document);
072: passwordLabel = new JLabel(passwordText);
073: retypePasswordLabel = new JLabel(retypePasswordText);
074: defaultForeground = passwordLabel.getForeground();
075: this .minPasswordChars = minPasswordChars;
076: Object[] array;
077: if (oldPasswordText != null) {
078: askForOldPassword = true;
079: oldPasswordLabel = new JLabel(oldPasswordText);
080: array = new Object[] { oldPasswordLabel, oldPasswordField,
081: passwordLabel, passwordField, retypePasswordLabel,
082: password2Field };
083: } else {
084: array = new Object[] { passwordLabel, passwordField,
085: retypePasswordLabel, password2Field };
086: }
087: Object[] options = { okString, cancelString };
088: optionPane = new JOptionPane(array,
089: JOptionPane.QUESTION_MESSAGE,
090: JOptionPane.YES_NO_OPTION, null, options, options[0]);
091: setContentPane(optionPane);
092:
093: setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
094: addWindowListener(new WindowAdapter() {
095: public void windowClosing(WindowEvent we) {
096: /*
097: * Instead of directly closing the window, we're going to change
098: * the JOptionPane's value property.
099: */
100: option = JOptionPane.CANCEL_OPTION;
101: optionPane.setValue(new Integer(
102: JOptionPane.CLOSED_OPTION));
103: }
104: });
105:
106: optionPane
107: .addPropertyChangeListener(new PropertyChangeListener() {
108: public void propertyChange(PropertyChangeEvent e) {
109: String prop = e.getPropertyName();
110:
111: option = JOptionPane.CANCEL_OPTION;
112:
113: if (isVisible()
114: && (e.getSource() == optionPane)
115: && (prop
116: .equals(JOptionPane.VALUE_PROPERTY) || prop
117: .equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
118: Object value = optionPane.getValue();
119:
120: if (value == JOptionPane.UNINITIALIZED_VALUE) {
121: // ignore reset
122: return;
123: }
124:
125: // Reset the JOptionPane's value.
126: // If you don't do this, then if the user
127: // presses the same button next time, no
128: // property change event will be fired.
129: optionPane
130: .setValue(JOptionPane.UNINITIALIZED_VALUE);
131:
132: if (value.equals(okString)) {
133: boolean inputIsValid = checkPassword();
134: if (inputIsValid) {
135: option = JOptionPane.OK_OPTION;
136: setVisible(false);
137: } else {
138: // Reset the JOptionPane's value.
139: // If you don't do this, then if the user
140: // presses the same button next time, no
141: // property change event will be fired.
142: optionPane
143: .setValue(JOptionPane.UNINITIALIZED_VALUE);
144: }
145: } else { // user closed dialog or clicked cancel
146: setVisible(false);
147: }
148: }
149: }
150: });
151: }
152:
153: private boolean checkPassword() {
154: if (askForOldPassword
155: && String.valueOf(passwordField.getPassword()).length() == 0) {
156: JOptionPane.showMessageDialog(null, Resources
157: .getLocalString("password_missing"), Resources
158: .getLocalString("password_invalid"),
159: JOptionPane.ERROR_MESSAGE);
160: oldPasswordLabel.setForeground(Color.red);
161: return false;
162: } else if (minPasswordChars > -1
163: && String.valueOf(passwordField.getPassword()).length() < minPasswordChars) {
164: JOptionPane.showMessageDialog(null, Resources
165: .getLocalString("password_too_short",
166: new String[] { String
167: .valueOf(minPasswordChars) }),
168: Resources.getLocalString("password_invalid"),
169: JOptionPane.ERROR_MESSAGE);
170: passwordLabel.setForeground(Color.red);
171: return false;
172: } else if (String.valueOf(passwordField.getPassword()).length() == 0) {
173: JOptionPane.showMessageDialog(null, Resources
174: .getLocalString("password_missing"), Resources
175: .getLocalString("password_invalid"),
176: JOptionPane.ERROR_MESSAGE);
177: passwordLabel.setForeground(Color.red);
178: retypePasswordLabel.setForeground(defaultForeground);
179: return false;
180: } else if (!String.valueOf(passwordField.getPassword()).equals(
181: String.valueOf(password2Field.getPassword()))) {
182: JOptionPane.showMessageDialog(null, Resources
183: .getLocalString("password_mismatch"), Resources
184: .getLocalString("password_invalid"),
185: JOptionPane.ERROR_MESSAGE);
186: retypePasswordLabel.setForeground(Color.red);
187: return false;
188: }
189: passwordLabel.setForeground(defaultForeground);
190: return true;
191: }
192:
193: public String getPassword() {
194: return String.valueOf(passwordField.getPassword());
195: }
196:
197: public String getOldPassword() {
198: return String.valueOf(oldPasswordField.getPassword());
199: }
200:
201: public int getOption() {
202: return option;
203: }
204: }
|