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

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

Various stuff:

  • see #9414: remove old DeprecatedTags test
  • refactor some classes in gui.preferences package
  • improve javadoc
  • Property svn:eol-style set to native
File size: 4.8 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.Main;
18import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
19import org.openstreetmap.josm.gui.widgets.JosmTextField;
20import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
21import org.openstreetmap.josm.io.OsmApi;
22import org.openstreetmap.josm.io.auth.CredentialsAgent;
23import org.openstreetmap.josm.io.auth.CredentialsAgentException;
24import org.openstreetmap.josm.io.auth.CredentialsManager;
25
26/**
27 * The preferences panel for parameters necessary for the Basic Authentication
28 * Scheme.
29 *
30 */
31public class BasicAuthenticationPreferencesPanel extends JPanel {
32
33 /** the OSM user name */
34 private JosmTextField tfOsmUserName;
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 UserNameValidator 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 /**
87 * Constructs a new {@code BasicAuthenticationPreferencesPanel}.
88 */
89 public BasicAuthenticationPreferencesPanel() {
90 build();
91 }
92
93 /**
94 * Inits contents from preferences.
95 */
96 public void initFromPreferences() {
97 CredentialsAgent cm = CredentialsManager.getInstance();
98 try {
99 decorationPanel.removeAll();
100 decorationPanel.add(cm.getPreferencesDecorationPanel(), BorderLayout.CENTER);
101 PasswordAuthentication pa = cm.lookup(RequestorType.SERVER, OsmApi.getOsmApi().getHost());
102 if (pa == null) {
103 tfOsmUserName.setText("");
104 tfOsmPassword.setText("");
105 } else {
106 tfOsmUserName.setText(pa.getUserName() == null? "" : pa.getUserName());
107 tfOsmPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword()));
108 }
109 } catch(CredentialsAgentException e) {
110 e.printStackTrace();
111 Main.warn(tr("Failed to retrieve OSM credentials from credential manager."));
112 Main.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
113 tfOsmUserName.setText("");
114 tfOsmPassword.setText("");
115 }
116 }
117
118 /**
119 * Saves contents to preferences.
120 */
121 public void saveToPreferences() {
122 CredentialsAgent cm = CredentialsManager.getInstance();
123 try {
124 PasswordAuthentication pa = new PasswordAuthentication(
125 tfOsmUserName.getText().trim(),
126 tfOsmPassword.getPassword()
127 );
128 cm.store(RequestorType.SERVER, OsmApi.getOsmApi().getHost(), pa);
129 } catch (CredentialsAgentException e) {
130 e.printStackTrace();
131 Main.warn(tr("Failed to save OSM credentials to credential manager."));
132 Main.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
133 }
134 }
135
136 /**
137 * Clears the password field.
138 */
139 public void clearPassword() {
140 tfOsmPassword.setText("");
141 }
142}
Note: See TracBrowser for help on using the repository browser.