source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java@ 12931

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

see #14602 - Override digit group separator to be consistent across languages with ISO 80000-1 + checkstyle fixes

  • Property svn:eol-style set to native
File size: 7.2 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.awt.event.ItemEvent;
11import java.awt.event.ItemListener;
12import java.beans.PropertyChangeEvent;
13import java.beans.PropertyChangeListener;
14
15import javax.swing.ButtonGroup;
16import javax.swing.JPanel;
17import javax.swing.JRadioButton;
18import javax.swing.JSeparator;
19
20import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder;
21import org.openstreetmap.josm.gui.help.HelpUtil;
22import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
23import org.openstreetmap.josm.io.OsmApi;
24import org.openstreetmap.josm.io.auth.CredentialsManager;
25import org.openstreetmap.josm.spi.preferences.Config;
26import org.openstreetmap.josm.tools.Logging;
27
28/**
29 * This is the preference panel for the authentication method and the authentication parameters.
30 * @since 2745
31 */
32public class AuthenticationPreferencesPanel extends VerticallyScrollablePanel implements PropertyChangeListener {
33
34 /** indicates whether we use basic authentication */
35 private final JRadioButton rbBasicAuthentication = new JRadioButton();
36 /** indicates whether we use OAuth as authentication scheme */
37 private final JRadioButton rbOAuth = new JRadioButton();
38 /** the panel which contains the authentication parameters for the respective authentication scheme */
39 private final JPanel pnlAuthenticationParameteters = new JPanel(new BorderLayout());
40 /** the panel for the basic authentication parameters */
41 private BasicAuthenticationPreferencesPanel pnlBasicAuthPreferences;
42 /** the panel for the OAuth authentication parameters */
43 private OAuthAuthenticationPreferencesPanel pnlOAuthPreferences;
44 /** the panel for messages notifier preferences */
45 private FeaturesPanel pnlFeaturesPreferences;
46
47 /**
48 * Constructs a new {@code AuthenticationPreferencesPanel}.
49 */
50 public AuthenticationPreferencesPanel() {
51 build();
52 initFromPreferences();
53 HelpUtil.setHelpContext(this, HelpUtil.ht("/Preferences/Connection#AuthenticationSettings"));
54 }
55
56 /**
57 * builds the UI
58 */
59 protected final void build() {
60 setLayout(new GridBagLayout());
61 GridBagConstraints gc = new GridBagConstraints();
62
63 AuthenticationMethodChangeListener authChangeListener = new AuthenticationMethodChangeListener();
64
65 // -- radio button for basic authentication
66 gc.anchor = GridBagConstraints.NORTHWEST;
67 gc.fill = GridBagConstraints.HORIZONTAL;
68 gc.gridx = 1;
69 gc.weightx = 1.0;
70 gc.insets = new Insets(0, 0, 0, 3);
71 add(rbBasicAuthentication, gc);
72 rbBasicAuthentication.setText(tr("Use Basic Authentication"));
73 rbBasicAuthentication.setToolTipText(tr("Select to use HTTP basic authentication with your OSM username and password"));
74 rbBasicAuthentication.addItemListener(authChangeListener);
75
76 //-- radio button for OAuth
77 gc.gridx = 0;
78 gc.weightx = 0.0;
79 add(rbOAuth, gc);
80 rbOAuth.setText(tr("Use OAuth"));
81 rbOAuth.setToolTipText(tr("Select to use OAuth as authentication mechanism"));
82 rbOAuth.addItemListener(authChangeListener);
83
84 //-- radio button for OAuth
85 ButtonGroup bg = new ButtonGroup();
86 bg.add(rbBasicAuthentication);
87 bg.add(rbOAuth);
88
89 //-- add the panel which will hold the authentication parameters
90 gc.gridx = 0;
91 gc.gridy = 1;
92 gc.gridwidth = 2;
93 gc.fill = GridBagConstraints.BOTH;
94 gc.weightx = 1.0;
95 gc.weighty = 1.0;
96 add(pnlAuthenticationParameteters, gc);
97
98 //-- the two panels for authentication parameters
99 pnlBasicAuthPreferences = new BasicAuthenticationPreferencesPanel();
100 pnlOAuthPreferences = new OAuthAuthenticationPreferencesPanel();
101
102 rbBasicAuthentication.setSelected(true);
103 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);
104
105 gc.gridy = 2;
106 add(new JSeparator(), gc);
107
108 //-- the panel for API feature preferences
109 gc.gridy = 3;
110 gc.fill = GridBagConstraints.NONE;
111 pnlFeaturesPreferences = new FeaturesPanel();
112 add(pnlFeaturesPreferences, gc);
113 }
114
115 /**
116 * Initializes the panel from preferences
117 */
118 public final void initFromPreferences() {
119 final String authMethod = OsmApi.getAuthMethod();
120 if ("basic".equals(authMethod)) {
121 rbBasicAuthentication.setSelected(true);
122 } else if ("oauth".equals(authMethod)) {
123 rbOAuth.setSelected(true);
124 } else {
125 Logging.warn(tr("Unsupported value in preference ''{0}'', got ''{1}''. Using authentication method ''Basic Authentication''.",
126 "osm-server.auth-method", authMethod));
127 rbBasicAuthentication.setSelected(true);
128 }
129 pnlBasicAuthPreferences.initFromPreferences();
130 pnlOAuthPreferences.initFromPreferences();
131 pnlFeaturesPreferences.initFromPreferences();
132 }
133
134 /**
135 * Saves the current values to the preferences
136 */
137 public final void saveToPreferences() {
138 // save the authentication method
139 String authMethod;
140 if (rbBasicAuthentication.isSelected()) {
141 authMethod = "basic";
142 } else {
143 authMethod = "oauth";
144 }
145 Config.getPref().put("osm-server.auth-method", authMethod);
146 if ("basic".equals(authMethod)) {
147 // save username and password and clear the OAuth token
148 pnlBasicAuthPreferences.saveToPreferences();
149 OAuthAccessTokenHolder.getInstance().clear();
150 OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance());
151 } else if ("oauth".equals(authMethod)) {
152 // clear the password in the preferences
153 pnlBasicAuthPreferences.clearPassword();
154 pnlBasicAuthPreferences.saveToPreferences();
155 pnlOAuthPreferences.saveToPreferences();
156 }
157 // save message notifications preferences. To be done after authentication preferences.
158 pnlFeaturesPreferences.saveToPreferences();
159 }
160
161 /**
162 * Listens to changes in the authentication method
163 */
164 class AuthenticationMethodChangeListener implements ItemListener {
165 @Override
166 public void itemStateChanged(ItemEvent e) {
167 if (rbBasicAuthentication.isSelected()) {
168 pnlAuthenticationParameteters.removeAll();
169 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);
170 pnlBasicAuthPreferences.revalidate();
171 } else {
172 pnlAuthenticationParameteters.removeAll();
173 pnlAuthenticationParameteters.add(pnlOAuthPreferences, BorderLayout.CENTER);
174 pnlOAuthPreferences.revalidate();
175 }
176 repaint();
177 }
178 }
179
180 @Override
181 public void propertyChange(PropertyChangeEvent evt) {
182 if (pnlOAuthPreferences != null) {
183 pnlOAuthPreferences.propertyChange(evt);
184 }
185 }
186}
Note: See TracBrowser for help on using the repository browser.