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

Last change on this file since 3083 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • Property svn:eol-style set to native
File size: 5.6 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.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.awt.Insets;
9import java.net.PasswordAuthentication;
10import java.net.Authenticator.RequestorType;
11
12import javax.swing.BorderFactory;
13import javax.swing.JLabel;
14import javax.swing.JPanel;
15import javax.swing.JPasswordField;
16import javax.swing.JTextField;
17import javax.swing.text.html.HTMLEditorKit;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.gui.widgets.HtmlPanel;
21import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
22import org.openstreetmap.josm.io.auth.CredentialsManager;
23import org.openstreetmap.josm.io.auth.CredentialsManagerException;
24import org.openstreetmap.josm.io.auth.CredentialsManagerFactory;
25import org.openstreetmap.josm.io.auth.JosmPreferencesCredentialManager;
26
27/**
28 * The preferences panel for parameters necessary for the Basic Authentication
29 * Scheme.
30 *
31 */
32public class BasicAuthenticationPreferencesPanel extends JPanel {
33
34 /** the OSM user name */
35 private JTextField tfOsmUserName;
36 private UserNameValidator valUserName;
37 /** the OSM password */
38 private JPasswordField tfOsmPassword;
39
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 HtmlPanel pnlMessage = new HtmlPanel();
84 HTMLEditorKit kit = (HTMLEditorKit)pnlMessage.getEditorPane().getEditorKit();
85 kit.getStyleSheet().addRule(".warning-body {background-color:rgb(253,255,221);padding: 10pt; border-color:rgb(128,128,128);border-style: solid;border-width: 1px;}");
86 pnlMessage.setText(
87 tr(
88 "<html><body>"
89 + "<p class=\"warning-body\">"
90 + "<strong>Warning:</strong> The password is stored in plain text in the JOSM preferences file. "
91 + "Furthermore, it is transferred <strong>unencrypted</strong> in every request sent to the OSM server. "
92 + "<strong>Do not use a valuable password.</strong>"
93 + "</p>"
94 + "</body></html>"
95 )
96 );
97 add(pnlMessage, gc);
98 }
99
100 public BasicAuthenticationPreferencesPanel() {
101 build();
102 }
103
104 public void initFromPreferences() {
105 CredentialsManager cm = CredentialsManagerFactory.getCredentialManager();
106 try {
107 PasswordAuthentication pa = cm.lookup(RequestorType.SERVER);
108 if (pa == null) {
109 tfOsmUserName.setText("");
110 tfOsmPassword.setText("");
111 } else {
112 tfOsmUserName.setText(pa.getUserName() == null? "" : pa.getUserName());
113 tfOsmPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword()));
114 }
115 } catch(CredentialsManagerException e) {
116 e.printStackTrace();
117 System.err.println(tr("Warning: failed to retrieve OSM credentials from credential manager."));
118 System.err.println(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
119 tfOsmUserName.setText("");
120 tfOsmPassword.setText("");
121 }
122 }
123
124 public void saveToPreferences() {
125 CredentialsManager cm = CredentialsManagerFactory.getCredentialManager();
126 try {
127 PasswordAuthentication pa = new PasswordAuthentication(
128 tfOsmUserName.getText().trim(),
129 tfOsmPassword.getPassword()
130 );
131 cm.store(RequestorType.SERVER, pa);
132 // always save the username to the preferences if it isn't already saved there
133 // by the credential manager
134 if (! (cm instanceof JosmPreferencesCredentialManager)) {
135 Main.pref.put("osm-server.username", tfOsmUserName.getText().trim());
136 }
137 } catch(CredentialsManagerException e) {
138 e.printStackTrace();
139 System.err.println(tr("Warning: failed to save OSM credentials to credential manager."));
140 System.err.println(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
141 }
142 }
143
144 public void clearPassword() {
145 tfOsmPassword.setText("");
146 }
147}
Note: See TracBrowser for help on using the repository browser.