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
028 * Scheme.
029 *
030 */
031public class BasicAuthenticationPreferencesPanel extends JPanel {
032
033    /** the OSM user name */
034    private JosmTextField tfOsmUserName;
035    /** the OSM password */
036    private JosmPasswordField tfOsmPassword;
037    /** a panel with further information, e.g. some warnings */
038    private JPanel decorationPanel;
039
040    /**
041     * builds the UI
042     */
043    protected void build() {
044        setLayout(new GridBagLayout());
045        setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
046        GridBagConstraints gc = new GridBagConstraints();
047
048        // -- OSM user name
049        gc.fill = GridBagConstraints.HORIZONTAL;
050        gc.anchor = GridBagConstraints.NORTHWEST;
051        gc.weightx = 0.0;
052        gc.insets = new Insets(0,0,3,3);
053        add(new JLabel(tr("OSM username:")), gc);
054
055        gc.gridx = 1;
056        gc.weightx = 1.0;
057        add(tfOsmUserName = new JosmTextField(), gc);
058        SelectAllOnFocusGainedDecorator.decorate(tfOsmUserName);
059        UserNameValidator valUserName = new UserNameValidator(tfOsmUserName);
060        valUserName.validate();
061
062        // -- OSM password
063        gc.gridx = 0;
064        gc.gridy = 1;
065        gc.weightx = 0.0;
066        add(new JLabel(tr("OSM password:")), gc);
067
068        gc.gridx = 1;
069        gc.weightx = 1.0;
070        add(tfOsmPassword = new JosmPasswordField(), gc);
071        SelectAllOnFocusGainedDecorator.decorate(tfOsmPassword);
072        tfOsmPassword.setToolTipText(tr("Please enter your OSM password"));
073
074        // -- an info panel with a warning message
075        gc.gridx = 0;
076        gc.gridy = 2;
077        gc.gridwidth = 2;
078        gc.weightx = 1.0;
079        gc.weighty = 1.0;
080        gc.insets = new Insets(5,0,0,0);
081        gc.fill = GridBagConstraints.BOTH;
082        decorationPanel = new JPanel(new BorderLayout());
083        add(decorationPanel, gc);
084    }
085
086    public BasicAuthenticationPreferencesPanel() {
087        build();
088    }
089
090    public void initFromPreferences() {
091        CredentialsAgent cm = CredentialsManager.getInstance();
092        try {
093            decorationPanel.removeAll();
094            decorationPanel.add(cm.getPreferencesDecorationPanel(), BorderLayout.CENTER);
095            PasswordAuthentication pa = cm.lookup(RequestorType.SERVER, OsmApi.getOsmApi().getHost());
096            if (pa == null) {
097                tfOsmUserName.setText("");
098                tfOsmPassword.setText("");
099            } else {
100                tfOsmUserName.setText(pa.getUserName() == null? "" : pa.getUserName());
101                tfOsmPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword()));
102            }
103        } catch(CredentialsAgentException e) {
104            e.printStackTrace();
105            Main.warn(tr("Failed to retrieve OSM credentials from credential manager."));
106            Main.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
107            tfOsmUserName.setText("");
108            tfOsmPassword.setText("");
109        }
110    }
111
112    public void saveToPreferences() {
113        CredentialsAgent cm = CredentialsManager.getInstance();
114        try {
115            PasswordAuthentication pa = new PasswordAuthentication(
116                    tfOsmUserName.getText().trim(),
117                    tfOsmPassword.getPassword()
118            );
119            cm.store(RequestorType.SERVER, OsmApi.getOsmApi().getHost(), pa);
120        } catch (CredentialsAgentException e) {
121            e.printStackTrace();
122            Main.warn(tr("Failed to save OSM credentials to credential manager."));
123            Main.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
124        }
125    }
126
127    public void clearPassword() {
128        tfOsmPassword.setText("");
129    }
130}