Search in sources :

Example 26 with FocusAdapter

use of java.awt.event.FocusAdapter in project processdash by dtuma.

the class HttpAuthenticator method getPasswordAuthentication.

@Override
protected PasswordAuthentication getPasswordAuthentication() {
    // only handle "server" auth requests (no proxy support for now)
    if (getRequestorType() != RequestorType.SERVER)
        return null;
    // only handle Data Bridge requests (no support for non-PDES servers)
    if (getRequestingURL().toString().indexOf("/DataBridge/") == -1 && getRequestingURL().toString().indexOf("/api/v1/") == -1)
        return null;
    // find out what state we are presently in.
    determineCurrentState();
    // if we're in a failure state, return no auth data.
    if (state == State.Failed || state == State.Cancelled)
        return null;
    // possibly supply credentials that were stored in the keyring
    if (state == State.Keyring) {
        char[] password = getPasswordFromKeyring(lastUsername);
        if (password != null)
            return new PasswordAuthentication(lastUsername, password);
        else if (lastUsername != null && lastPassword != null)
            return new PasswordAuthentication(lastUsername, mask(lastPassword));
        else
            state = State.UserEntry1;
    }
    // create user interface components to prompt for username and password
    JTextField username = new JTextField(2);
    JPasswordField password = new JPasswordField(2);
    FocusAdapter selectAll = new FocusAdapter() {

        public void focusGained(FocusEvent e) {
            if (e.getComponent() instanceof JTextField) {
                ((JTextField) e.getComponent()).selectAll();
            }
        }
    };
    username.addFocusListener(selectAll);
    password.addFocusListener(selectAll);
    JComponent focus = username;
    if (StringUtils.hasValue(lastUsername)) {
        username.setText(lastUsername);
        focus = password;
    }
    JLabel usernameLabel = new JLabel(resources.getString("Username"), SwingConstants.RIGHT);
    JLabel passwordLabel = new JLabel(resources.getString("Password"), SwingConstants.RIGHT);
    Dimension d = usernameLabel.getPreferredSize();
    d.width = Math.max(d.width, passwordLabel.getPreferredSize().width);
    usernameLabel.setPreferredSize(d);
    passwordLabel.setPreferredSize(d);
    // if "remember me" support is enabled, create a checkbox
    JCheckBox rememberMe = null;
    if (rememberMeDays > 0) {
        rememberMe = new JCheckBox(resources.getString("Remember_Me.Prompt"));
        rememberMe.setToolTipText(resources.format("Remember_Me.Tooltip_FMT", rememberMeDays));
        Font f = rememberMe.getFont();
        f = f.deriveFont(f.getSize2D() * 0.8f);
        rememberMe.setFont(f);
    }
    // prompt the user for credentials
    final String title = (StringUtils.hasValue(this.title) ? this.title : resources.getString("Title"));
    String[] promptLines = resources.formatStrings("Prompt_FMT", getRequestingPrompt());
    Object[] prompt = new Object[promptLines.length];
    System.arraycopy(promptLines, 0, prompt, 0, prompt.length);
    // add a tooltip to the last line of the prompt, indicating the URL
    JLabel promptLabel = new JLabel(promptLines[prompt.length - 1]);
    promptLabel.setToolTipText(getEffectiveURL());
    prompt[prompt.length - 1] = promptLabel;
    final Object[] message = new Object[] { prompt, BoxUtils.vbox(5), BoxUtils.hbox(15, usernameLabel, 5, username), BoxUtils.hbox(15, passwordLabel, 5, password), BoxUtils.hbox(BoxUtils.GLUE, rememberMe), new JOptionPaneTweaker.GrabFocus(focus) };
    final int[] userChoice = new int[1];
    try {
        Runnable r = new Runnable() {

            public void run() {
                userChoice[0] = JOptionPane.showConfirmDialog(parentComponent, message, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            }
        };
        if (SwingUtilities.isEventDispatchThread())
            r.run();
        else
            SwingUtilities.invokeAndWait(r);
    } catch (Exception e) {
    }
    // record metadata about this password request
    lastUrl = getEffectiveURL();
    lastTimestamp = System.currentTimeMillis();
    lastUsername = username.getText().trim();
    prefs.put(prefsKey(LAST_USERNAME), lastUsername);
    if (userChoice[0] == JOptionPane.OK_OPTION) {
        // if the user entered credentials, return them.
        if (rememberMe != null && rememberMe.isSelected())
            savePasswordToKeyring(lastUsername, password.getPassword());
        lastPassword = mask(password.getPassword());
        return new PasswordAuthentication(lastUsername, password.getPassword());
    } else {
        // if the user cancelled the operation, abort.
        state = State.Cancelled;
        return null;
    }
}
Also used : FocusAdapter(java.awt.event.FocusAdapter) JComponent(javax.swing.JComponent) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) JTextField(javax.swing.JTextField) FocusEvent(java.awt.event.FocusEvent) Font(java.awt.Font) JCheckBox(javax.swing.JCheckBox) JPasswordField(javax.swing.JPasswordField) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

FocusAdapter (java.awt.event.FocusAdapter)26 FocusEvent (java.awt.event.FocusEvent)26 Dimension (java.awt.Dimension)6 KeyAdapter (java.awt.event.KeyAdapter)5 KeyEvent (java.awt.event.KeyEvent)5 JLabel (javax.swing.JLabel)5 JTextField (javax.swing.JTextField)5 FocusListener (java.awt.event.FocusListener)3 JButton (javax.swing.JButton)3 Container (java.awt.Container)2 FlowLayout (java.awt.FlowLayout)2 MouseAdapter (java.awt.event.MouseAdapter)2 MouseEvent (java.awt.event.MouseEvent)2 JComponent (javax.swing.JComponent)2 JPanel (javax.swing.JPanel)2 JPasswordField (javax.swing.JPasswordField)2 JScrollPane (javax.swing.JScrollPane)2 Border (javax.swing.border.Border)2 DocumentEvent (javax.swing.event.DocumentEvent)2 DocumentListener (javax.swing.event.DocumentListener)2