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.PasswordAuthentication;
|
---|
11 | import java.net.Authenticator.RequestorType;
|
---|
12 |
|
---|
13 | import javax.swing.BorderFactory;
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.JPasswordField;
|
---|
17 | import javax.swing.JTextField;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
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.io.auth.JosmPreferencesCredentialAgent;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * The preferences panel for parameters necessary for the Basic Authentication
|
---|
28 | * Scheme.
|
---|
29 | *
|
---|
30 | */
|
---|
31 | public class BasicAuthenticationPreferencesPanel extends JPanel {
|
---|
32 |
|
---|
33 | /** the OSM user name */
|
---|
34 | private JTextField tfOsmUserName;
|
---|
35 | private UserNameValidator valUserName;
|
---|
36 | /** the OSM password */
|
---|
37 | private JPasswordField tfOsmPassword;
|
---|
38 | /** a panel with further information, e.g. some warnings */
|
---|
39 | private JPanel decorationPanel;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * builds the UI
|
---|
43 | */
|
---|
44 | protected void build() {
|
---|
45 | setLayout(new GridBagLayout());
|
---|
46 | setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
|
---|
47 | GridBagConstraints gc = new GridBagConstraints();
|
---|
48 |
|
---|
49 | // -- OSM user name
|
---|
50 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
51 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
52 | gc.weightx = 0.0;
|
---|
53 | gc.insets = new Insets(0,0,3,3);
|
---|
54 | add(new JLabel(tr("OSM username:")), gc);
|
---|
55 |
|
---|
56 | gc.gridx = 1;
|
---|
57 | gc.weightx = 1.0;
|
---|
58 | add(tfOsmUserName = new JTextField(), gc);
|
---|
59 | SelectAllOnFocusGainedDecorator.decorate(tfOsmUserName);
|
---|
60 | valUserName = new UserNameValidator(tfOsmUserName);
|
---|
61 | valUserName.validate();
|
---|
62 |
|
---|
63 | // -- OSM password
|
---|
64 | gc.gridx = 0;
|
---|
65 | gc.gridy = 1;
|
---|
66 | gc.weightx = 0.0;
|
---|
67 | add(new JLabel(tr("OSM password:")), gc);
|
---|
68 |
|
---|
69 | gc.gridx = 1;
|
---|
70 | gc.weightx = 1.0;
|
---|
71 | add(tfOsmPassword = new JPasswordField(), gc);
|
---|
72 | SelectAllOnFocusGainedDecorator.decorate(tfOsmPassword);
|
---|
73 | tfOsmPassword.setToolTipText(tr("Please enter your OSM password"));
|
---|
74 |
|
---|
75 | // -- an info panel with a warning message
|
---|
76 | gc.gridx = 0;
|
---|
77 | gc.gridy = 2;
|
---|
78 | gc.gridwidth = 2;
|
---|
79 | gc.weightx = 1.0;
|
---|
80 | gc.weighty = 1.0;
|
---|
81 | gc.insets = new Insets(5,0,0,0);
|
---|
82 | gc.fill = GridBagConstraints.BOTH;
|
---|
83 | decorationPanel = new JPanel(new BorderLayout());
|
---|
84 | add(decorationPanel, gc);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public BasicAuthenticationPreferencesPanel() {
|
---|
88 | build();
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void initFromPreferences() {
|
---|
92 | CredentialsAgent cm = CredentialsManager.getInstance();
|
---|
93 | try {
|
---|
94 | decorationPanel.removeAll();
|
---|
95 | decorationPanel.add(cm.getPreferencesDecorationPanel(), BorderLayout.CENTER);
|
---|
96 | PasswordAuthentication pa = cm.lookup(RequestorType.SERVER);
|
---|
97 | if (pa == null) {
|
---|
98 | tfOsmUserName.setText("");
|
---|
99 | tfOsmPassword.setText("");
|
---|
100 | } else {
|
---|
101 | tfOsmUserName.setText(pa.getUserName() == null? "" : pa.getUserName());
|
---|
102 | tfOsmPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword()));
|
---|
103 | }
|
---|
104 | } catch(CredentialsAgentException e) {
|
---|
105 | e.printStackTrace();
|
---|
106 | System.err.println(tr("Warning: failed to retrieve OSM credentials from credential manager."));
|
---|
107 | System.err.println(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
|
---|
108 | tfOsmUserName.setText("");
|
---|
109 | tfOsmPassword.setText("");
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | public void saveToPreferences() {
|
---|
114 | CredentialsAgent cm = CredentialsManager.getInstance();
|
---|
115 | try {
|
---|
116 | PasswordAuthentication pa = new PasswordAuthentication(
|
---|
117 | tfOsmUserName.getText().trim(),
|
---|
118 | tfOsmPassword.getPassword()
|
---|
119 | );
|
---|
120 | cm.store(RequestorType.SERVER, pa);
|
---|
121 | } catch(CredentialsAgentException e) {
|
---|
122 | e.printStackTrace();
|
---|
123 | System.err.println(tr("Warning: failed to save OSM credentials to credential manager."));
|
---|
124 | System.err.println(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | public void clearPassword() {
|
---|
129 | tfOsmPassword.setText("");
|
---|
130 | }
|
---|
131 | }
|
---|