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

Last change on this file since 7509 was 7509, checked in by stoecker, 10 years ago

remove tabs

  • Property svn:eol-style set to native
File size: 7.0 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 final 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 initFromPreferences();
109 HelpUtil.setHelpContext(this, HelpUtil.ht("/Preferences/Connection#AuthenticationSettings"));
110 }
111
112 /**
113 * Initializes the panel from preferences
114 */
115 public final void initFromPreferences() {
116 String authMethod = Main.pref.get("osm-server.auth-method", "basic");
117 if ("basic".equals(authMethod)) {
118 rbBasicAuthentication.setSelected(true);
119 } else if ("oauth".equals(authMethod)) {
120 rbOAuth.setSelected(true);
121 } else {
122 Main.warn(tr("Unsupported value in preference ''{0}'', got ''{1}''. Using authentication method ''Basic Authentication''.", "osm-server.auth-method", authMethod));
123 rbBasicAuthentication.setSelected(true);
124 }
125 pnlBasicAuthPreferences.initFromPreferences();
126 pnlOAuthPreferences.initFromPreferences();
127 pnlMessagesPreferences.initFromPreferences();
128 }
129
130 /**
131 * Saves the current values to preferences
132 */
133 public final void saveToPreferences() {
134 // save the authentication method
135 String authMethod;
136 if (rbBasicAuthentication.isSelected()) {
137 authMethod = "basic";
138 } else {
139 authMethod = "oauth";
140 }
141 Main.pref.put("osm-server.auth-method", authMethod);
142 if ("basic".equals(authMethod)) {
143 // save username and password and clear the OAuth token
144 pnlBasicAuthPreferences.saveToPreferences();
145 OAuthAccessTokenHolder.getInstance().clear();
146 OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance());
147 } else if ("oauth".equals(authMethod)) {
148 // clear the password in the preferences
149 pnlBasicAuthPreferences.clearPassword();
150 pnlBasicAuthPreferences.saveToPreferences();
151 pnlOAuthPreferences.saveToPreferences();
152 }
153 // save message notifications preferences. To be done after authentication preferences.
154 pnlMessagesPreferences.saveToPreferences();
155 }
156
157 /**
158 * Listens to changes in the authentication method
159 */
160 class AuthenticationMethodChangeListener implements ItemListener {
161 @Override
162 public void itemStateChanged(ItemEvent e) {
163 if (rbBasicAuthentication.isSelected()) {
164 pnlAuthenticationParameteters.removeAll();
165 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);
166 pnlBasicAuthPreferences.revalidate();
167 } else {
168 pnlAuthenticationParameteters.removeAll();
169 pnlAuthenticationParameteters.add(pnlOAuthPreferences, BorderLayout.CENTER);
170 pnlOAuthPreferences.revalidate();
171 }
172 repaint();
173 }
174 }
175
176 @Override
177 public void propertyChange(PropertyChangeEvent evt) {
178 if (pnlOAuthPreferences != null) {
179 pnlOAuthPreferences.propertyChange(evt);
180 }
181 }
182}
Note: See TracBrowser for help on using the repository browser.