source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/server/BasicAuthenticationPreferencesPanel.java@ 12620

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.server;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.Insets;
10import java.net.Authenticator.RequestorType;
11import java.net.PasswordAuthentication;
12
13import javax.swing.BorderFactory;
14import javax.swing.JLabel;
15import javax.swing.JPanel;
16
17import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
18import org.openstreetmap.josm.gui.widgets.JosmTextField;
19import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
20import org.openstreetmap.josm.io.OsmApi;
21import org.openstreetmap.josm.io.auth.CredentialsAgent;
22import org.openstreetmap.josm.io.auth.CredentialsAgentException;
23import org.openstreetmap.josm.io.auth.CredentialsManager;
24import org.openstreetmap.josm.tools.Logging;
25
26/**
27 * The preferences panel for parameters necessary for the Basic Authentication Scheme.
28 * @since 2745
29 */
30public 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}
Note: See TracBrowser for help on using the repository browser.