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

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

fix #9234 - For identified users, notifies periodically (every 5 minutes by default) of new OSM messages he/she has received. All of this is configurable in server access preferences.

  • 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 add(pnlAuthenticationParameteters = new JPanel(), gc);
86 pnlAuthenticationParameteters.setLayout(new BorderLayout());
87
88 //-- the two panels for authentication parameters
89 pnlBasicAuthPreferences = new BasicAuthenticationPreferencesPanel();
90 pnlOAuthPreferences = new OAuthAuthenticationPreferencesPanel();
91
92 rbBasicAuthentication.setSelected(true);
93 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);
94
95 //-- the panel for messages preferences
96 gc.gridy = 2;
97 gc.fill = GridBagConstraints.NONE;
98 add(pnlMessagesPreferences = new MessagesNotifierPanel(), gc);
99 }
100
101 /**
102 * Constructs a new {@code AuthenticationPreferencesPanel}.
103 */
104 public AuthenticationPreferencesPanel() {
105 build();
106 HelpUtil.setHelpContext(this, HelpUtil.ht("/Preferences/Connection#AuthenticationSettings"));
107 }
108
109 /**
110 * Initializes the panel from preferences
111 */
112 public void initFromPreferences() {
113 String authMethod = Main.pref.get("osm-server.auth-method", "basic");
114 if (authMethod.equals("basic")) {
115 rbBasicAuthentication.setSelected(true);
116 } else if (authMethod.equals("oauth")) {
117 rbOAuth.setSelected(true);
118 } else {
119 Main.warn(tr("Unsupported value in preference ''{0}'', got ''{1}''. Using authentication method ''Basic Authentication''.", "osm-server.auth-method", authMethod));
120 rbBasicAuthentication.setSelected(true);
121 }
122 pnlBasicAuthPreferences.initFromPreferences();
123 pnlOAuthPreferences.initFromPreferences();
124 pnlMessagesPreferences.initFromPreferences();
125 }
126
127 /**
128 * Saves the current values to preferences
129 */
130 public void saveToPreferences() {
131 // save the authentication method
132 String authMethod;
133 if (rbBasicAuthentication.isSelected()) {
134 authMethod = "basic";
135 } else {
136 authMethod = "oauth";
137 }
138 Main.pref.put("osm-server.auth-method", authMethod);
139 if (authMethod.equals("basic")) {
140 // save username and password and clear the OAuth token
141 pnlBasicAuthPreferences.saveToPreferences();
142 OAuthAccessTokenHolder.getInstance().clear();
143 OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance());
144 } else if (authMethod.equals("oauth")) {
145 // clear the password in the preferences
146 pnlBasicAuthPreferences.clearPassword();
147 pnlBasicAuthPreferences.saveToPreferences();
148 pnlOAuthPreferences.saveToPreferences();
149 }
150 // save message notifications preferences. To be done after authentication preferences.
151 pnlMessagesPreferences.saveToPreferences();
152 }
153
154 /**
155 * Listens to changes in the authentication method
156 */
157 class AuthenticationMethodChangeListener implements ItemListener {
158 @Override
159 public void itemStateChanged(ItemEvent e) {
160 if (rbBasicAuthentication.isSelected()) {
161 pnlAuthenticationParameteters.removeAll();
162 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);
163 pnlBasicAuthPreferences.revalidate();
164 } else {
165 pnlAuthenticationParameteters.removeAll();
166 pnlAuthenticationParameteters.add(pnlOAuthPreferences, BorderLayout.CENTER);
167 pnlOAuthPreferences.revalidate();
168 }
169 repaint();
170 }
171 }
172
173 @Override
174 public void propertyChange(PropertyChangeEvent evt) {
175 if (pnlOAuthPreferences != null) {
176 pnlOAuthPreferences.propertyChange(evt);
177 }
178 }
179}
Note: See TracBrowser for help on using the repository browser.