1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.server;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
10 | import java.net.Authenticator.RequestorType;
|
---|
11 | import java.net.PasswordAuthentication;
|
---|
12 |
|
---|
13 | import javax.swing.BorderFactory;
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
|
---|
18 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
19 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
20 | import org.openstreetmap.josm.io.OsmApi;
|
---|
21 | import org.openstreetmap.josm.io.auth.CredentialsAgent;
|
---|
22 | import org.openstreetmap.josm.io.auth.CredentialsAgentException;
|
---|
23 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
24 | import org.openstreetmap.josm.tools.Logging;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * The preferences panel for parameters necessary for the Basic Authentication Scheme.
|
---|
28 | * @since 2745
|
---|
29 | */
|
---|
30 | public class BasicAuthenticationPreferencesPanel extends JPanel {
|
---|
31 |
|
---|
32 | /** the OSM user name */
|
---|
33 | private final JosmTextField tfOsmUserName = new JosmTextField();
|
---|
34 | /** the OSM password */
|
---|
35 | private final JosmPasswordField tfOsmPassword = new JosmPasswordField();
|
---|
36 | /** a panel with further information, e.g. some warnings */
|
---|
37 | private JPanel decorationPanel;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Constructs a new {@code BasicAuthenticationPreferencesPanel}.
|
---|
41 | */
|
---|
42 | public BasicAuthenticationPreferencesPanel() {
|
---|
43 | build();
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * builds the UI
|
---|
48 | */
|
---|
49 | protected final void build() {
|
---|
50 | setLayout(new GridBagLayout());
|
---|
51 | setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
|
---|
52 | GridBagConstraints gc = new GridBagConstraints();
|
---|
53 |
|
---|
54 | // -- OSM user name
|
---|
55 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
56 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
57 | gc.weightx = 0.0;
|
---|
58 | gc.insets = new Insets(0, 0, 3, 3);
|
---|
59 | add(new JLabel(tr("OSM username:")), gc);
|
---|
60 |
|
---|
61 | gc.gridx = 1;
|
---|
62 | gc.weightx = 1.0;
|
---|
63 | add(tfOsmUserName, gc);
|
---|
64 | SelectAllOnFocusGainedDecorator.decorate(tfOsmUserName);
|
---|
65 | UserNameValidator valUserName = new UserNameValidator(tfOsmUserName);
|
---|
66 | valUserName.validate();
|
---|
67 |
|
---|
68 | // -- OSM password
|
---|
69 | gc.gridx = 0;
|
---|
70 | gc.gridy = 1;
|
---|
71 | gc.weightx = 0.0;
|
---|
72 | add(new JLabel(tr("OSM password:")), gc);
|
---|
73 |
|
---|
74 | gc.gridx = 1;
|
---|
75 | gc.weightx = 1.0;
|
---|
76 | add(tfOsmPassword, gc);
|
---|
77 | SelectAllOnFocusGainedDecorator.decorate(tfOsmPassword);
|
---|
78 | tfOsmPassword.setToolTipText(tr("Please enter your OSM password"));
|
---|
79 |
|
---|
80 | // -- an info panel with a warning message
|
---|
81 | gc.gridx = 0;
|
---|
82 | gc.gridy = 2;
|
---|
83 | gc.gridwidth = 2;
|
---|
84 | gc.weightx = 1.0;
|
---|
85 | gc.weighty = 1.0;
|
---|
86 | gc.insets = new Insets(5, 0, 0, 0);
|
---|
87 | gc.fill = GridBagConstraints.BOTH;
|
---|
88 | decorationPanel = new JPanel(new BorderLayout());
|
---|
89 | add(decorationPanel, gc);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Inits contents from preferences.
|
---|
94 | */
|
---|
95 | public void initFromPreferences() {
|
---|
96 | CredentialsAgent cm = CredentialsManager.getInstance();
|
---|
97 | try {
|
---|
98 | decorationPanel.removeAll();
|
---|
99 | 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 | Logging.error(e);
|
---|
110 | Logging.warn(tr("Failed to retrieve OSM credentials from credential manager."));
|
---|
111 | Logging.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 | Logging.error(e);
|
---|
130 | Logging.warn(tr("Failed to save OSM credentials to credential manager."));
|
---|
131 | Logging.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 | }
|
---|