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

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

fix many checkstyle violations

  • Property svn:eol-style set to native
File size: 7.1 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.gui.help.HelpUtil;
22import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
23import org.openstreetmap.josm.io.auth.CredentialsManager;
24
25/**
26 * This is the preference panel for the authentication method and the authentication
27 * parameters.
28 *
29 */
30public class AuthenticationPreferencesPanel extends VerticallyScrollablePanel implements PropertyChangeListener{
31
32 /** indicates whether we use basic authentication */
33 private JRadioButton rbBasicAuthentication;
34 /** indicates whether we use OAuth as authentication scheme */
35 private JRadioButton rbOAuth;
36 /** the panel which contains the authentication parameters for the respective
37 * authentication scheme
38 */
39 private JPanel pnlAuthenticationParameteters;
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 * builds the UI
49 */
50 protected final void build() {
51 setLayout(new GridBagLayout());
52 GridBagConstraints gc = new GridBagConstraints();
53
54 AuthenticationMethodChangeListener authChangeListener = new AuthenticationMethodChangeListener();
55
56 // -- radio button for basic authentication
57 gc.anchor = GridBagConstraints.NORTHWEST;
58 gc.fill = GridBagConstraints.HORIZONTAL;
59 gc.weightx = 0.0;
60 gc.insets = new Insets(0,0,0, 3);
61 add(rbBasicAuthentication = new JRadioButton(), gc);
62 rbBasicAuthentication.setText(tr("Use Basic Authentication"));
63 rbBasicAuthentication.setToolTipText(tr("Select to use HTTP basic authentication with your OSM username and password"));
64 rbBasicAuthentication.addItemListener(authChangeListener);
65
66 //-- radio button for OAuth
67 gc.gridx = 1;
68 gc.weightx = 1.0;
69 add(rbOAuth = new JRadioButton(), gc);
70 rbOAuth.setText(tr("Use OAuth"));
71 rbOAuth.setToolTipText(tr("Select to use OAuth as authentication mechanism"));
72 rbOAuth.addItemListener(authChangeListener);
73
74 //-- radio button for OAuth
75 ButtonGroup bg = new ButtonGroup();
76 bg.add(rbBasicAuthentication);
77 bg.add(rbOAuth);
78
79 //-- add the panel which will hold the authentication parameters
80 gc.gridx = 0;
81 gc.gridy = 1;
82 gc.gridwidth = 2;
83 gc.fill = GridBagConstraints.BOTH;
84 gc.weightx = 1.0;
85 gc.weighty = 1.0;
86 pnlAuthenticationParameteters = new JPanel();
87 add(pnlAuthenticationParameteters, gc);
88 pnlAuthenticationParameteters.setLayout(new BorderLayout());
89
90 //-- the two panels for authentication parameters
91 pnlBasicAuthPreferences = new BasicAuthenticationPreferencesPanel();
92 pnlOAuthPreferences = new OAuthAuthenticationPreferencesPanel();
93
94 rbBasicAuthentication.setSelected(true);
95 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);
96
97 gc.gridy = 2;
98 add(new JSeparator(), gc);
99
100 //-- the panel for API feature preferences
101 gc.gridy = 3;
102 gc.fill = GridBagConstraints.NONE;
103 pnlFeaturesPreferences = new FeaturesPanel();
104 add(pnlFeaturesPreferences, gc);
105 }
106
107 /**
108 * Constructs a new {@code AuthenticationPreferencesPanel}.
109 */
110 public AuthenticationPreferencesPanel() {
111 build();
112 initFromPreferences();
113 HelpUtil.setHelpContext(this, HelpUtil.ht("/Preferences/Connection#AuthenticationSettings"));
114 }
115
116 /**
117 * Initializes the panel from preferences
118 */
119 public final void initFromPreferences() {
120 String authMethod = Main.pref.get("osm-server.auth-method", "basic");
121 if ("basic".equals(authMethod)) {
122 rbBasicAuthentication.setSelected(true);
123 } else if ("oauth".equals(authMethod)) {
124 rbOAuth.setSelected(true);
125 } else {
126 Main.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 Main.pref.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.