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

Last change on this file since 6087 was 5886, checked in by Don-vip, 11 years ago

see #4429 - Right click menu "undo, cut, copy, paste, delete, select all" for each text component (originally based on patch by NooN)

  • Property svn:eol-style set to native
File size: 4.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.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;
24
25/**
26 * The preferences panel for parameters necessary for the Basic Authentication
27 * Scheme.
28 *
29 */
30public 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}
Note: See TracBrowser for help on using the repository browser.