Example usage for javax.swing JPasswordField requestFocusInWindow

List of usage examples for javax.swing JPasswordField requestFocusInWindow

Introduction

In this page you can find the example usage for javax.swing JPasswordField requestFocusInWindow.

Prototype

public boolean requestFocusInWindow() 

Source Link

Document

Requests that this Component gets the input focus.

Usage

From source file:com.gs.obevo.util.inputreader.DialogInputReader.java

@Override
public String readPassword(String promptMessage) {
    final JPasswordField jpf = new JPasswordField();
    JOptionPane jop = new JOptionPane(jpf, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
    JDialog dialog = jop.createDialog(promptMessage);
    dialog.addComponentListener(new ComponentAdapter() {
        @Override//from   w ww. j a  v a 2  s  .  com
        public void componentShown(ComponentEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    jpf.requestFocusInWindow();
                }
            });
        }
    });
    dialog.setVisible(true);
    int result = (Integer) jop.getValue();
    dialog.dispose();
    String password = null;
    if (result == JOptionPane.OK_OPTION) {
        password = new String(jpf.getPassword());
    }
    if (StringUtils.isEmpty(password)) {
        return null;
    } else {
        return password;
    }
}

From source file:com._17od.upm.gui.DatabaseActions.java

/**
 * Prompt the user to enter a password/*from  ww  w  . j  a va  2  s  .  c  o m*/
 * @return The password entered by the user or null of this hit escape/cancel
 */
private char[] askUserForPassword(String message) {
    char[] password = null;

    final JPasswordField masterPassword = new JPasswordField("");
    JOptionPane pane = new JOptionPane(new Object[] { message, masterPassword }, JOptionPane.QUESTION_MESSAGE,
            JOptionPane.OK_CANCEL_OPTION);
    JDialog dialog = pane.createDialog(mainWindow, Translator.translate("masterPassword"));
    dialog.addWindowFocusListener(new WindowAdapter() {
        public void windowGainedFocus(WindowEvent e) {
            masterPassword.requestFocusInWindow();
        }
    });
    dialog.show();

    if (pane.getValue() != null && pane.getValue().equals(new Integer(JOptionPane.OK_OPTION))) {
        password = masterPassword.getPassword();
    }

    return password;
}

From source file:com._17od.upm.gui.DatabaseActions.java

/**
 * This method asks the user for the name of a new database and then creates
 * it. If the file already exists then the user is asked if they'd like to
 * overwrite it.//from ww  w. j  a  v a 2  s  . c o  m
 * @throws CryptoException 
 * @throws IOException 
 */
public void newDatabase() throws IOException, CryptoException {

    File newDatabaseFile = getSaveAsFile(Translator.translate("newPasswordDatabase"));
    if (newDatabaseFile == null) {
        return;
    }

    final JPasswordField masterPassword = new JPasswordField("");
    boolean passwordsMatch = false;
    do {

        //Get a new master password for this database from the user
        JPasswordField confirmedMasterPassword = new JPasswordField("");
        JOptionPane pane = new JOptionPane(
                new Object[] { Translator.translate("enterMasterPassword"), masterPassword,
                        Translator.translate("confirmation"), confirmedMasterPassword },
                JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
        JDialog dialog = pane.createDialog(mainWindow, Translator.translate("masterPassword"));
        dialog.addWindowFocusListener(new WindowAdapter() {
            public void windowGainedFocus(WindowEvent e) {
                masterPassword.requestFocusInWindow();
            }
        });
        dialog.show();

        if (pane.getValue().equals(new Integer(JOptionPane.OK_OPTION))) {
            if (!Arrays.equals(masterPassword.getPassword(), confirmedMasterPassword.getPassword())) {
                JOptionPane.showMessageDialog(mainWindow, Translator.translate("passwordsDontMatch"));
            } else {
                passwordsMatch = true;
            }
        } else {
            return;
        }

    } while (passwordsMatch == false);

    if (newDatabaseFile.exists()) {
        newDatabaseFile.delete();
    }

    database = new PasswordDatabase(newDatabaseFile);
    dbPers = new PasswordDatabasePersistence(masterPassword.getPassword());
    saveDatabase();
    accountNames = new ArrayList();
    doOpenDatabaseActions();

    // If a "Database to Load on Startup" hasn't been set yet then ask the
    // user if they'd like to open this database on startup.
    if (Preferences.get(Preferences.ApplicationOptions.DB_TO_LOAD_ON_STARTUP) == null
            || Preferences.get(Preferences.ApplicationOptions.DB_TO_LOAD_ON_STARTUP).equals("")) {
        int option = JOptionPane.showConfirmDialog(mainWindow,
                Translator.translate("setNewLoadOnStartupDatabase"),
                Translator.translate("newPasswordDatabase"), JOptionPane.YES_NO_OPTION);
        if (option == JOptionPane.YES_OPTION) {
            Preferences.set(Preferences.ApplicationOptions.DB_TO_LOAD_ON_STARTUP,
                    newDatabaseFile.getAbsolutePath());
            Preferences.save();
        }
    }
}

From source file:com._17od.upm.gui.DatabaseActions.java

public void changeMasterPassword() throws IOException, ProblemReadingDatabaseFile, CryptoException,
        PasswordDatabaseException, TransportException {

    if (getLatestVersionOfDatabase()) {
        //The first task is to get the current master password
        boolean passwordCorrect = false;
        boolean okClicked = true;
        do {//from  w  w w. j  a  va2  s  . c  o  m
            char[] password = askUserForPassword(Translator.translate("enterDatabasePassword"));
            if (password == null) {
                okClicked = false;
            } else {
                try {
                    dbPers.load(database.getDatabaseFile(), password);
                    passwordCorrect = true;
                } catch (InvalidPasswordException e) {
                    JOptionPane.showMessageDialog(mainWindow, Translator.translate("incorrectPassword"));
                }
            }
        } while (!passwordCorrect && okClicked);

        //If the master password was entered correctly then the next step is to get the new master password
        if (passwordCorrect == true) {

            final JPasswordField masterPassword = new JPasswordField("");
            boolean passwordsMatch = false;
            Object buttonClicked;

            //Ask the user for the new master password
            //This loop will continue until the two passwords entered match or until the user hits the cancel button
            do {

                JPasswordField confirmedMasterPassword = new JPasswordField("");
                JOptionPane pane = new JOptionPane(
                        new Object[] { Translator.translate("enterNewMasterPassword"), masterPassword,
                                Translator.translate("confirmation"), confirmedMasterPassword },
                        JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
                JDialog dialog = pane.createDialog(mainWindow, Translator.translate("changeMasterPassword"));
                dialog.addWindowFocusListener(new WindowAdapter() {
                    public void windowGainedFocus(WindowEvent e) {
                        masterPassword.requestFocusInWindow();
                    }
                });
                dialog.show();

                buttonClicked = pane.getValue();
                if (buttonClicked.equals(new Integer(JOptionPane.OK_OPTION))) {
                    if (!Arrays.equals(masterPassword.getPassword(), confirmedMasterPassword.getPassword())) {
                        JOptionPane.showMessageDialog(mainWindow, Translator.translate("passwordsDontMatch"));
                    } else {
                        passwordsMatch = true;
                    }
                }

            } while (buttonClicked.equals(new Integer(JOptionPane.OK_OPTION)) && !passwordsMatch);

            //If the user clicked OK and the passwords match then change the database password
            if (buttonClicked.equals(new Integer(JOptionPane.OK_OPTION)) && passwordsMatch) {
                this.dbPers.getEncryptionService().initCipher(masterPassword.getPassword());
                saveDatabase();
            }

        }
    }

}

From source file:org.executequery.gui.browser.SSHTunnelConnectionPanel.java

public boolean canConnect() {

    if (useSshCheckbox.isSelected()) {

        if (!hasValue(userNameField)) {

            GUIUtilities//from w  ww.ja v  a2  s. c o  m
                    .displayErrorMessage("You have selected SSH Tunnel but have not provided an SSH user name");
            return false;
        }

        if (!hasValue(portField)) {

            GUIUtilities.displayErrorMessage("You have selected SSH Tunnel but have not provided an SSH port");
            return false;
        }

        if (!hasValue(passwordField)) {

            final JPasswordField field = WidgetFactory.createPasswordField();

            JOptionPane optionPane = new JOptionPane(field, JOptionPane.QUESTION_MESSAGE,
                    JOptionPane.OK_CANCEL_OPTION);
            JDialog dialog = optionPane.createDialog("Enter SSH password");

            dialog.addWindowFocusListener(new WindowAdapter() {
                @Override
                public void windowGainedFocus(WindowEvent e) {
                    field.requestFocusInWindow();
                }
            });

            dialog.pack();
            dialog.setLocation(GUIUtilities.getLocationForDialog(dialog.getSize()));
            dialog.setVisible(true);
            dialog.dispose();

            int result = Integer.parseInt(optionPane.getValue().toString());
            if (result == JOptionPane.OK_OPTION) {

                String password = MiscUtils.charsToString(field.getPassword());
                if (StringUtils.isNotBlank(password)) {

                    passwordField.setText(password);
                    return true;

                } else {

                    GUIUtilities.displayErrorMessage(
                            "You have selected SSH Tunnel but have not provided an SSH password");

                    // send back here and force them to select cancel if they want to bail

                    return canConnect();
                }

            }
            return false;
        }

    }

    return true;
}