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 |
|
---|
25 | /**
|
---|
26 | * The preferences panel for parameters necessary for the Basic Authentication
|
---|
27 | * Scheme.
|
---|
28 | *
|
---|
29 | */
|
---|
30 | public class BasicAuthenticationPreferencesPanel extends JPanel {
|
---|
31 |
|
---|
32 | /** the OSM user name */
|
---|
33 | private JosmTextField tfOsmUserName;
|
---|
34 | private UserNameValidator valUserName;
|
---|
35 | /** the OSM password */
|
---|
36 | private JosmPasswordField tfOsmPassword;
|
---|
37 | /** a panel with further information, e.g. some warnings */
|
---|
38 | private JPanel decorationPanel;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * builds the UI
|
---|
42 | */
|
---|
43 | protected void build() {
|
---|
44 | setLayout(new GridBagLayout());
|
---|
45 | setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
|
---|
46 | GridBagConstraints gc = new GridBagConstraints();
|
---|
47 |
|
---|
48 | // -- OSM user name
|
---|
49 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
50 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
51 | gc.weightx = 0.0;
|
---|
52 | gc.insets = new Insets(0,0,3,3);
|
---|
53 | add(new JLabel(tr("OSM username:")), gc);
|
---|
54 |
|
---|
55 | gc.gridx = 1;
|
---|
56 | gc.weightx = 1.0;
|
---|
57 | add(tfOsmUserName = new JosmTextField(), gc);
|
---|
58 | SelectAllOnFocusGainedDecorator.decorate(tfOsmUserName);
|
---|
59 | valUserName = new UserNameValidator(tfOsmUserName);
|
---|
60 | valUserName.validate();
|
---|
61 |
|
---|
62 | // -- OSM password
|
---|
63 | gc.gridx = 0;
|
---|
64 | gc.gridy = 1;
|
---|
65 | gc.weightx = 0.0;
|
---|
66 | add(new JLabel(tr("OSM password:")), gc);
|
---|
67 |
|
---|
68 | gc.gridx = 1;
|
---|
69 | gc.weightx = 1.0;
|
---|
70 | add(tfOsmPassword = new JosmPasswordField(), gc);
|
---|
71 | SelectAllOnFocusGainedDecorator.decorate(tfOsmPassword);
|
---|
72 | tfOsmPassword.setToolTipText(tr("Please enter your OSM password"));
|
---|
73 |
|
---|
74 | // -- an info panel with a warning message
|
---|
75 | gc.gridx = 0;
|
---|
76 | gc.gridy = 2;
|
---|
77 | gc.gridwidth = 2;
|
---|
78 | gc.weightx = 1.0;
|
---|
79 | gc.weighty = 1.0;
|
---|
80 | gc.insets = new Insets(5,0,0,0);
|
---|
81 | gc.fill = GridBagConstraints.BOTH;
|
---|
82 | decorationPanel = new JPanel(new BorderLayout());
|
---|
83 | add(decorationPanel, gc);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public BasicAuthenticationPreferencesPanel() {
|
---|
87 | build();
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void initFromPreferences() {
|
---|
91 | CredentialsAgent cm = CredentialsManager.getInstance();
|
---|
92 | try {
|
---|
93 | decorationPanel.removeAll();
|
---|
94 | decorationPanel.add(cm.getPreferencesDecorationPanel(), BorderLayout.CENTER);
|
---|
95 | PasswordAuthentication pa = cm.lookup(RequestorType.SERVER, OsmApi.getOsmApi().getHost());
|
---|
96 | if (pa == null) {
|
---|
97 | tfOsmUserName.setText("");
|
---|
98 | tfOsmPassword.setText("");
|
---|
99 | } 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 | System.err.println(tr("Warning: failed to retrieve OSM credentials from credential manager."));
|
---|
106 | System.err.println(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 | System.err.println(tr("Warning: failed to save OSM credentials to credential manager."));
|
---|
123 | System.err.println(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | public void clearPassword() {
|
---|
128 | tfOsmPassword.setText("");
|
---|
129 | }
|
---|
130 | }
|
---|