001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.server;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.BorderLayout;
007import java.awt.GridBagConstraints;
008import java.awt.GridBagLayout;
009import java.awt.Insets;
010import java.net.Authenticator.RequestorType;
011import java.net.PasswordAuthentication;
012
013import javax.swing.BorderFactory;
014import javax.swing.JLabel;
015import javax.swing.JPanel;
016
017import org.openstreetmap.josm.Main;
018import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
019import org.openstreetmap.josm.gui.widgets.JosmTextField;
020import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
021import org.openstreetmap.josm.io.OsmApi;
022import org.openstreetmap.josm.io.auth.CredentialsAgent;
023import org.openstreetmap.josm.io.auth.CredentialsAgentException;
024import org.openstreetmap.josm.io.auth.CredentialsManager;
025
026/**
027 * The preferences panel for parameters necessary for the Basic Authentication Scheme.
028 * @since 2745
029 */
030public class BasicAuthenticationPreferencesPanel extends JPanel {
031
032    /** the OSM user name */
033    private final JosmTextField tfOsmUserName = new JosmTextField();
034    /** the OSM password */
035    private final JosmPasswordField tfOsmPassword = new JosmPasswordField();
036    /** a panel with further information, e.g. some warnings */
037    private JPanel decorationPanel;
038
039    /**
040     * Constructs a new {@code BasicAuthenticationPreferencesPanel}.
041     */
042    public BasicAuthenticationPreferencesPanel() {
043        build();
044    }
045
046    /**
047     * builds the UI
048     */
049    protected final void build() {
050        setLayout(new GridBagLayout());
051        setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
052        GridBagConstraints gc = new GridBagConstraints();
053
054        // -- OSM user name
055        gc.fill = GridBagConstraints.HORIZONTAL;
056        gc.anchor = GridBagConstraints.NORTHWEST;
057        gc.weightx = 0.0;
058        gc.insets = new Insets(0, 0, 3, 3);
059        add(new JLabel(tr("OSM username:")), gc);
060
061        gc.gridx = 1;
062        gc.weightx = 1.0;
063        add(tfOsmUserName, gc);
064        SelectAllOnFocusGainedDecorator.decorate(tfOsmUserName);
065        UserNameValidator valUserName = new UserNameValidator(tfOsmUserName);
066        valUserName.validate();
067
068        // -- OSM password
069        gc.gridx = 0;
070        gc.gridy = 1;
071        gc.weightx = 0.0;
072        add(new JLabel(tr("OSM password:")), gc);
073
074        gc.gridx = 1;
075        gc.weightx = 1.0;
076        add(tfOsmPassword, gc);
077        SelectAllOnFocusGainedDecorator.decorate(tfOsmPassword);
078        tfOsmPassword.setToolTipText(tr("Please enter your OSM password"));
079
080        // -- an info panel with a warning message
081        gc.gridx = 0;
082        gc.gridy = 2;
083        gc.gridwidth = 2;
084        gc.weightx = 1.0;
085        gc.weighty = 1.0;
086        gc.insets = new Insets(5, 0, 0, 0);
087        gc.fill = GridBagConstraints.BOTH;
088        decorationPanel = new JPanel(new BorderLayout());
089        add(decorationPanel, gc);
090    }
091
092    /**
093     * Inits contents from preferences.
094     */
095    public void initFromPreferences() {
096        CredentialsAgent cm = CredentialsManager.getInstance();
097        try {
098            decorationPanel.removeAll();
099            decorationPanel.add(cm.getPreferencesDecorationPanel(), BorderLayout.CENTER);
100            PasswordAuthentication pa = cm.lookup(RequestorType.SERVER, OsmApi.getOsmApi().getHost());
101            if (pa == null) {
102                tfOsmUserName.setText("");
103                tfOsmPassword.setText("");
104            } else {
105                tfOsmUserName.setText(pa.getUserName() == null ? "" : pa.getUserName());
106                tfOsmPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword()));
107            }
108        } catch (CredentialsAgentException e) {
109            Main.error(e);
110            Main.warn(tr("Failed to retrieve OSM credentials from credential manager."));
111            Main.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
112            tfOsmUserName.setText("");
113            tfOsmPassword.setText("");
114        }
115    }
116
117    /**
118     * Saves contents to preferences.
119     */
120    public void saveToPreferences() {
121        CredentialsAgent cm = CredentialsManager.getInstance();
122        try {
123            PasswordAuthentication pa = new PasswordAuthentication(
124                    tfOsmUserName.getText().trim(),
125                    tfOsmPassword.getPassword()
126            );
127            cm.store(RequestorType.SERVER, OsmApi.getOsmApi().getHost(), pa);
128        } catch (CredentialsAgentException e) {
129            Main.error(e);
130            Main.warn(tr("Failed to save OSM credentials to credential manager."));
131            Main.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
132        }
133    }
134
135    /**
136     * Clears the password field.
137     */
138    public void clearPassword() {
139        tfOsmPassword.setText("");
140    }
141}