Example usage for javax.swing JDialog addWindowFocusListener

List of usage examples for javax.swing JDialog addWindowFocusListener

Introduction

In this page you can find the example usage for javax.swing JDialog addWindowFocusListener.

Prototype

public synchronized void addWindowFocusListener(WindowFocusListener l) 

Source Link

Document

Adds the specified window focus listener to receive window events from this window.

Usage

From source file:Main.java

/**
 * Initialises the {@link JDialog} for the {@link JComponent}.
 * //from  w  w  w . j  a  v a2 s .  com
 * @param dialog
 * @param component
 * @param parentComponent
 */
private static void initDialog(final JDialog dialog, final JComponent component,
        final Component parentComponent) {
    dialog.setResizable(true);
    dialog.setComponentOrientation(component.getComponentOrientation());
    Container contentPane = dialog.getContentPane();

    contentPane.setLayout(new BorderLayout());
    contentPane.add(component, BorderLayout.CENTER);

    final int buttonWidth = 75;

    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(2, 4, 4, 4));

    buttonPanel.add(Box.createHorizontalGlue());

    @SuppressWarnings("serial")
    final Action closeAction = new AbstractAction("Close") {
        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    };

    final JButton button = new JButton(closeAction);
    fixWidth(button, buttonWidth);
    buttonPanel.add(button);

    contentPane.add(buttonPanel, BorderLayout.SOUTH);

    if (JDialog.isDefaultLookAndFeelDecorated()) {
        boolean supportsWindowDecorations = UIManager.getLookAndFeel().getSupportsWindowDecorations();
        if (supportsWindowDecorations) {
            dialog.setUndecorated(true);
            component.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        }
    }
    dialog.pack();
    dialog.setLocationRelativeTo(parentComponent);
    WindowAdapter adapter = new WindowAdapter() {
        //         private boolean gotFocus = false;
        public void windowClosing(WindowEvent we) {
            fireAction(we.getSource(), closeAction, "close");
        }
    };
    dialog.addWindowListener(adapter);
    dialog.addWindowFocusListener(adapter);
}

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

/**
 * Prompt the user to enter a password//from  w ww  .ja v a  2s  .  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.//w  ww .j  ava 2s  . 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 {/*  w w  w  .  j  ava2  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;
}

From source file:org.kontalk.view.View.java

void showPasswordDialog(boolean wasWrong) {
    WebPanel passPanel = new WebPanel();
    WebLabel passLabel = new WebLabel(Tr.tr("Please enter your key password:"));
    passPanel.add(passLabel, BorderLayout.NORTH);
    final WebPasswordField passField = new WebPasswordField();
    passPanel.add(passField, BorderLayout.CENTER);
    if (wasWrong) {
        WebLabel wrongLabel = new WebLabel(Tr.tr("Wrong password"));
        wrongLabel.setForeground(Color.RED);
        passPanel.add(wrongLabel, BorderLayout.SOUTH);
    }/*w  ww  . java  2 s. c o m*/
    WebOptionPane passPane = new WebOptionPane(passPanel, WebOptionPane.QUESTION_MESSAGE,
            WebOptionPane.OK_CANCEL_OPTION);
    JDialog dialog = passPane.createDialog(mMainFrame, Tr.tr("Enter password"));
    dialog.setModal(true);
    dialog.addWindowFocusListener(new WindowAdapter() {
        @Override
        public void windowGainedFocus(WindowEvent e) {
            passField.requestFocusInWindow();
        }
    });
    // blocking
    dialog.setVisible(true);

    Object value = passPane.getValue();
    if (value != null && value.equals(WebOptionPane.OK_OPTION))
        mControl.connect(passField.getPassword());
}