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

Last change on this file since 12846 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

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