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

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

refactoring - global simplification of use of setLayout method - simply pass layout to JPanel constructor

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