Search in sources :

Example 61 with JPasswordField

use of javax.swing.JPasswordField in project n2a by frothga.

the class ConnectionInfo method promptKeyboardInteractive.

public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo) {
    panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    gbc.weightx = 1.0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.gridx = 0;
    panel.add(new JLabel(instruction), gbc);
    gbc.gridy++;
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    JTextField[] texts = new JTextField[prompt.length];
    for (int i = 0; i < prompt.length; i++) {
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.weightx = 1;
        panel.add(new JLabel(prompt[i]), gbc);
        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weighty = 1;
        if (echo[i]) {
            texts[i] = new JTextField(20);
        } else {
            texts[i] = new JPasswordField(20);
        }
        panel.add(texts[i], gbc);
        gbc.gridy++;
    }
    if (JOptionPane.showConfirmDialog(null, panel, destination + ": " + name, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
        String[] response = new String[prompt.length];
        for (int i = 0; i < prompt.length; i++) {
            response[i] = texts[i].getText();
        }
        return response;
    }
    // cancel
    return null;
}
Also used : JPanel(javax.swing.JPanel) GridBagLayout(java.awt.GridBagLayout) JPasswordField(javax.swing.JPasswordField) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField)

Example 62 with JPasswordField

use of javax.swing.JPasswordField 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)

Example 63 with JPasswordField

use of javax.swing.JPasswordField in project Spark by igniterealtime.

the class PasswordDialog method getPassword.

/**
 * Prompt and return input.
 *
 * @param title       the title of the dialog.
 * @param description the dialog description.
 * @param icon        the icon to use.
 * @param parent      the parent to use.
 * @return the user input.
 */
public String getPassword(String title, String description, Icon icon, Component parent) {
    passwordField = new JPasswordField();
    passwordField.setText(password);
    TitlePanel titlePanel = new TitlePanel(title, description, icon, true);
    // Construct main panel w/ layout.
    final JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(titlePanel, BorderLayout.NORTH);
    final JPanel passwordPanel = new JPanel(new GridBagLayout());
    JLabel passwordLabel = new JLabel(Res.getString("label.enter.password") + ":");
    passwordPanel.add(passwordLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
    passwordPanel.add(passwordField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    // user should be able to close this dialog (with an option to save room's password)
    passwordPanel.add(_savePasswordBox, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    ResourceUtils.resButton(_savePasswordBox, Res.getString("checkbox.save.password"));
    final Object[] options = { Res.getString("ok"), Res.getString("cancel") };
    optionPane = new JOptionPane(passwordPanel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);
    mainPanel.add(optionPane, BorderLayout.CENTER);
    // Lets make sure that the dialog is modal. Cannot risk people
    // losing this dialog.
    JOptionPane p = new JOptionPane();
    dialog = p.createDialog(parent, title);
    dialog.setModal(true);
    dialog.pack();
    dialog.setSize(width, height);
    dialog.setContentPane(mainPanel);
    dialog.setLocationRelativeTo(parent);
    optionPane.addPropertyChangeListener(this);
    // Add Key Listener to Send Field
    passwordField.addKeyListener(new KeyAdapter() {

        public void keyPressed(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_TAB) {
                optionPane.requestFocus();
            } else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                dialog.dispose();
            }
        }
    });
    passwordField.requestFocus();
    dialog.setVisible(true);
    return stringValue;
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) KeyAdapter(java.awt.event.KeyAdapter) JLabel(javax.swing.JLabel) JOptionPane(javax.swing.JOptionPane) KeyEvent(java.awt.event.KeyEvent) BorderLayout(java.awt.BorderLayout) JPasswordField(javax.swing.JPasswordField)

Example 64 with JPasswordField

use of javax.swing.JPasswordField in project Spark by igniterealtime.

the class DataFormUI method buildUI.

private void buildUI(Form form) {
    // Add default answers to the form to submit
    for (final FormField field : form.getFields()) {
        String variable = field.getVariable();
        String label = field.getLabel();
        FormField.Type type = field.getType();
        List<String> valueList = field.getValues();
        if (type.equals(FormField.Type.bool)) {
            String o = valueList.get(0);
            boolean isSelected = o.equals("1");
            JCheckBox box = new JCheckBox(label);
            box.setSelected(isSelected);
            addField(label, box, variable);
        } else if (type.equals(FormField.Type.text_single) || type.equals(FormField.Type.jid_single)) {
            String v = "";
            if (valueList.size() > 0) {
                v = valueList.get(0);
            }
            addField(label, new JTextField(v), variable);
        } else if (type.equals(FormField.Type.text_multi) || type.equals(FormField.Type.jid_multi)) {
            StringBuilder buf = new StringBuilder();
            for (FormField.Option option : field.getOptions()) {
                buf.append(option);
            }
            addField(label, new JTextArea(buf.toString()), variable);
        } else if (type.equals(FormField.Type.text_private)) {
            addField(label, new JPasswordField(), variable);
        } else if (type.equals(FormField.Type.list_single)) {
            JComboBox box = new JComboBox();
            for (final FormField.Option option : field.getOptions()) {
                box.addItem(option);
            }
            if (valueList.size() > 0) {
                String defaultValue = valueList.get(0);
                box.setSelectedItem(defaultValue);
            }
            addField(label, box, variable);
        } else if (type.equals(FormField.Type.list_multi)) {
            CheckBoxList checkBoxList = new CheckBoxList();
            for (final String value : field.getValues()) {
                checkBoxList.addCheckBox(new JCheckBox(value), value);
            }
            addField(label, checkBoxList, variable);
        }
    }
}
Also used : JTextArea(javax.swing.JTextArea) JComboBox(javax.swing.JComboBox) JTextField(javax.swing.JTextField) JCheckBox(javax.swing.JCheckBox) JPasswordField(javax.swing.JPasswordField) CheckBoxList(org.jivesoftware.spark.component.CheckBoxList) FormField(org.jivesoftware.smackx.xdata.FormField)

Example 65 with JPasswordField

use of javax.swing.JPasswordField in project Spark by igniterealtime.

the class WorkgroupDataForm method buildUI.

private void buildUI(Form form) {
    // Add default answers to the form to submit
    for (final FormField field : form.getFields()) {
        String variable = field.getVariable();
        if (field.isRequired()) {
            requiredList.add(variable);
        }
        if (presetVariables.containsKey(variable)) {
            continue;
        }
        String label = field.getLabel();
        FormField.Type type = field.getType();
        List valueList = new ArrayList();
        for (String value : field.getValues()) {
            valueList.add(value);
        }
        if (type.equals(FormField.Type.bool)) {
            String o = (String) valueList.get(0);
            boolean isSelected = o.equals("1");
            JCheckBox box = new JCheckBox(label);
            box.setSelected(isSelected);
            addField(label, box, variable);
        } else if (type.equals(FormField.Type.text_single) || type.equals(FormField.Type.jid_single)) {
            String v = "";
            if (valueList.size() > 0) {
                v = (String) valueList.get(0);
            }
            addField(label, new JTextField(v), variable);
        } else if (type.equals(FormField.Type.text_multi) || type.equals(FormField.Type.jid_multi)) {
            StringBuffer buf = new StringBuffer();
            for (final FormField.Option option : field.getOptions()) {
                buf.append(option);
            }
            addField(label, new JTextArea(buf.toString()), variable);
        } else if (type.equals(FormField.Type.text_private)) {
            addField(label, new JPasswordField(), variable);
        } else if (type.equals(FormField.Type.list_single)) {
            JComboBox box = new JComboBox();
            for (final FormField.Option option : field.getOptions()) {
                String value = option.getValue();
                box.addItem(option);
            }
            if (valueList.size() > 0) {
                String defaultValue = (String) valueList.get(0);
                box.setSelectedItem(defaultValue);
            }
            addField(label, box, variable);
        } else if (type.equals(FormField.Type.list_multi)) {
            CheckBoxList checkBoxList = new CheckBoxList();
            for (final String value : field.getValues()) {
                checkBoxList.addCheckBox(new JCheckBox(value), value);
            }
            addField(label, checkBoxList, variable);
        }
    }
}
Also used : JTextArea(javax.swing.JTextArea) JComboBox(javax.swing.JComboBox) ArrayList(java.util.ArrayList) JTextField(javax.swing.JTextField) JCheckBox(javax.swing.JCheckBox) JPasswordField(javax.swing.JPasswordField) CheckBoxList(org.jivesoftware.spark.component.CheckBoxList) ArrayList(java.util.ArrayList) List(java.util.List) CheckBoxList(org.jivesoftware.spark.component.CheckBoxList) FormField(org.jivesoftware.smackx.xdata.FormField)

Aggregations

JPasswordField (javax.swing.JPasswordField)100 JLabel (javax.swing.JLabel)60 JPanel (javax.swing.JPanel)47 JTextField (javax.swing.JTextField)44 BorderLayout (java.awt.BorderLayout)30 JButton (javax.swing.JButton)24 JCheckBox (javax.swing.JCheckBox)23 GridBagLayout (java.awt.GridBagLayout)22 GridBagConstraints (java.awt.GridBagConstraints)19 ActionEvent (java.awt.event.ActionEvent)19 ActionListener (java.awt.event.ActionListener)19 Insets (java.awt.Insets)18 Dimension (java.awt.Dimension)15 WindowAdapter (java.awt.event.WindowAdapter)12 WindowEvent (java.awt.event.WindowEvent)12 EmptyBorder (javax.swing.border.EmptyBorder)12 EtchedBorder (javax.swing.border.EtchedBorder)12 AbstractAction (javax.swing.AbstractAction)11 CompoundBorder (javax.swing.border.CompoundBorder)11 ItemEvent (java.awt.event.ItemEvent)7