Search in sources :

Example 1 with CredentialItem

use of org.eclipse.jgit.transport.CredentialItem in project bndtools by bndtools.

the class GitCredentialsProvider method updateCredentialItems.

private static void updateCredentialItems(JComponent[] components) {
    for (JComponent component : components) {
        CredentialItem item = (CredentialItem) component.getClientProperty(CRED_ITEM);
        if (item == null) {
            continue;
        }
        if (item instanceof CredentialItem.Username) {
            JTextField field = (JTextField) component;
            ((CredentialItem.Username) item).setValue(field.getText());
            continue;
        }
        if (item instanceof CredentialItem.Password) {
            JPasswordField field = (JPasswordField) component;
            ((CredentialItem.Password) item).setValue(field.getPassword());
            continue;
        }
        if (item instanceof CredentialItem.StringType) {
            if (item.isValueSecure()) {
                JPasswordField field = (JPasswordField) component;
                ((CredentialItem.StringType) item).setValue(new String(field.getPassword()));
                continue;
            }
            JTextField field = (JTextField) component;
            ((CredentialItem.Username) item).setValue(field.getText());
            continue;
        }
        if (item instanceof CredentialItem.YesNoType) {
            JCheckBox field = (JCheckBox) component;
            ((CredentialItem.YesNoType) item).setValue(field.isSelected());
            continue;
        }
    }
}
Also used : JCheckBox(javax.swing.JCheckBox) JPasswordField(javax.swing.JPasswordField) JComponent(javax.swing.JComponent) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) JTextField(javax.swing.JTextField)

Example 2 with CredentialItem

use of org.eclipse.jgit.transport.CredentialItem in project bndtools by bndtools.

the class GitCredentialsProvider method get.

@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
    Mapping mapping = repo.findMapping(uri.toString());
    if (mapping != null) {
        for (CredentialItem item : items) {
            if (item instanceof CredentialItem.Username) {
                ((CredentialItem.Username) item).setValue(mapping.user);
                continue;
            }
            if (item instanceof CredentialItem.Password) {
                ((CredentialItem.Password) item).setValue(mapping.pass);
                continue;
            }
            // Usually Passphrase
            if (item instanceof CredentialItem.StringType && item.isValueSecure()) {
                ((CredentialItem.StringType) item).setValue(new String(mapping.pass));
                continue;
            }
        }
        return true;
    }
    if (isInteractive()) {
        JComponent[] inputs = getSwingUI(items);
        int result = JOptionPane.showConfirmDialog(null, inputs, "Enter credentials for " + repo.getName(), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result != JOptionPane.OK_OPTION) {
            return false;
        }
        updateCredentialItems(inputs);
        return true;
    }
    return false;
}
Also used : JComponent(javax.swing.JComponent) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) Mapping(bndtools.bndplugins.repo.git.GitOBRRepo.Mapping)

Example 3 with CredentialItem

use of org.eclipse.jgit.transport.CredentialItem in project bndtools by bndtools.

the class GitCredentialsProvider method getSwingUI.

private static JComponent[] getSwingUI(CredentialItem... items) {
    List<JComponent> components = new ArrayList<JComponent>();
    for (CredentialItem item : items) {
        if (item instanceof CredentialItem.Username) {
            components.add(new JLabel(item.getPromptText()));
            JTextField field = new JTextField();
            field.putClientProperty(CRED_ITEM, item);
            components.add(field);
            continue;
        }
        if (item instanceof CredentialItem.Password) {
            components.add(new JLabel(item.getPromptText()));
            JTextField field = new JPasswordField();
            field.putClientProperty(CRED_ITEM, item);
            components.add(field);
            continue;
        }
        if (item instanceof CredentialItem.StringType) {
            components.add(new JLabel(item.getPromptText()));
            JTextField field;
            if (item.isValueSecure()) {
                field = new JPasswordField();
            } else {
                field = new JTextField();
            }
            field.putClientProperty(CRED_ITEM, item);
            components.add(field);
            continue;
        }
        if (item instanceof CredentialItem.InformationalMessage) {
            components.add(new JLabel(item.getPromptText()));
            continue;
        }
        if (item instanceof CredentialItem.YesNoType) {
            JCheckBox field = new JCheckBox(item.getPromptText(), ((CredentialItem.YesNoType) item).getValue());
            field.putClientProperty(CRED_ITEM, item);
            components.add(field);
            continue;
        }
    }
    return components.toArray(new JComponent[0]);
}
Also used : ArrayList(java.util.ArrayList) JComponent(javax.swing.JComponent) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) JCheckBox(javax.swing.JCheckBox) JPasswordField(javax.swing.JPasswordField)

Aggregations

JComponent (javax.swing.JComponent)3 UnsupportedCredentialItem (org.eclipse.jgit.errors.UnsupportedCredentialItem)3 CredentialItem (org.eclipse.jgit.transport.CredentialItem)3 JCheckBox (javax.swing.JCheckBox)2 JPasswordField (javax.swing.JPasswordField)2 JTextField (javax.swing.JTextField)2 Mapping (bndtools.bndplugins.repo.git.GitOBRRepo.Mapping)1 ArrayList (java.util.ArrayList)1 JLabel (javax.swing.JLabel)1