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

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

Sonar/Findbugs - fix of recent violations

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