Back to index

wims  3.65+svn20090927
PasswordDialog.java
Go to the documentation of this file.
00001 /*
00002  * @(#)PasswordDialog.java
00003  *
00004  * $Id: PasswordDialog.java,v 1.4 2001/05/27 22:28:58 huaz Exp $
00005  *
00006  * Created on November 16, 2000, 12:00 AM
00007  */
00008 package SharpTools;
00009 import javax.swing.*;
00010 import java.awt.*;
00011 import java.awt.event.*;
00012 import java.util.*;
00024 public class PasswordDialog extends SharpDialog {
00025     
00026     private JFrame frame;
00027     private String typedText = null;
00028     private String password;
00029     final private JPasswordField textField;
00030     final private JPasswordField confirmField;   
00031     final ImageIcon passwordIcon=new ImageIcon(getClass().getResource("/images/password32.gif"));
00032     //final private static ImageIcon passwordIcon = SharpTools.getImageIcon("password32.gif");
00033     
00034     // password is null if this is Set Password...
00035     // it's a password if we need to verify
00036     public PasswordDialog(JFrame aFrame, String pass) {
00037         
00038        super(aFrame, "Password", true);
00039 
00040        frame = aFrame;
00041        password = pass;
00042         
00043        //various properties of the dialog labels and text fields 
00044         final String msgString1 = SharpTools.translation.getProperty("Enter_password")+" : ";
00045         final String msgString2 = SharpTools.translation.getProperty("Confirm_Password")+" : ";
00046         textField = new JPasswordField(10);
00047        confirmField = new JPasswordField(10);    
00048        
00049        Object[] input = {msgString1, textField, msgString2, confirmField};
00050        Object[] verify = {msgString1, textField};
00051        Object[] array = password == null ? input : verify;
00052 
00053         setOptionPane(array, 
00054                     JOptionPane.QUESTION_MESSAGE,
00055                     JOptionPane.OK_CANCEL_OPTION,
00056                     passwordIcon); 
00057     }
00058     
00059     /*
00060      * @return password string
00061      */
00062     public String getValidatedText() {
00063         return typedText;
00064     }
00065     
00066     //compares to arrays of characters
00067     private static boolean fieldsAreTheSame(char[] input1, char[] input2) {
00068         
00069         if (input1.length != input2.length)
00070             return false;
00071         for (int i = 0;  i < input1.length; i ++)
00072             if (input1[i] != input2[i])
00073                 return false;
00074         return true;
00075     }
00076 
00077     protected boolean onOK() {
00078        typedText = new String(textField.getPassword());
00079        
00080        if (password != null) {
00081            // verify password
00082            if (!typedText.equals(password)) {
00083               SharpOptionPane.showMessageDialog
00084                   (PasswordDialog.this,
00085                    SharpTools.translation.getProperty("Password_Incorrect")+" !" , SharpTools.translation.getProperty("Password"),
00086                    JOptionPane.ERROR_MESSAGE);
00087               textField.setText(null);
00088               textField.requestFocus();
00089               return false;
00090            }
00091            else
00092               return true;
00093        }
00094        
00095        //validate password
00096        if (fieldsAreTheSame(textField.getPassword(),
00097                           confirmField.getPassword())) {
00098            //compare the dialogs
00099            return true;
00100        } else { 
00101            // passwords dont match
00102            
00103            textField.selectAll();
00104            SharpOptionPane.showMessageDialog
00105               (this,
00106                SharpTools.translation.getProperty("Passwords_do_not_match_Please_try_again"),
00107                SharpTools.translation.getProperty("Password"),
00108                JOptionPane.ERROR_MESSAGE,
00109                null);
00110            
00111            typedText = null;
00112                   
00113            textField.setText(null);
00114            confirmField.setText(null);
00115            textField.requestFocus();
00116            return false;
00117        }
00118     }
00119     
00120     protected boolean onCancel() {
00121        typedText = null;
00122        return true;
00123     }
00124     
00125     protected void onOpen() {
00126        // set the initial focus to textField
00127        textField.requestFocus();
00128     }
00129 }