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

Last change on this file since 2745 was 2745, checked in by Gubaer, 14 years ago

Partial commit due to #4137. Comment to follow in another commit.

File size: 6.3 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.CredentialsManagerFactory;
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
44 /**
45 * builds the UI
46 */
47 protected void build() {
48 setLayout(new GridBagLayout());
49 GridBagConstraints gc = new GridBagConstraints();
50
51 AuthenticationMethodChangeListener authChangeListener = new AuthenticationMethodChangeListener();
52
53 // -- radio button for basic authentication
54 gc.anchor = GridBagConstraints.NORTHWEST;
55 gc.fill = GridBagConstraints.HORIZONTAL;
56 gc.weightx = 0.0;
57 gc.insets = new Insets(0,0,0, 3);
58 add(rbBasicAuthentication = new JRadioButton(), gc);
59 rbBasicAuthentication.setText(tr("Use Basic Authentication"));
60 rbBasicAuthentication.setToolTipText(tr("Select to use HTTP basic authentication with your OSM username and password"));
61 rbBasicAuthentication.addItemListener(authChangeListener);
62
63 //-- radio button for OAuth
64 gc.gridx = 1;
65 gc.weightx = 1.0;
66 add(rbOAuth = new JRadioButton(), gc);
67 rbOAuth.setText(tr("Use OAuth"));
68 rbOAuth.setToolTipText(tr("Select to use OAuth as authentication mechanism"));
69 rbOAuth.addItemListener(authChangeListener);
70
71 //-- radio button for OAuth
72 ButtonGroup bg = new ButtonGroup();
73 bg.add(rbBasicAuthentication);
74 bg.add(rbOAuth);
75
76 //-- add the panel which will hld the authentication parameters
77 gc.gridx = 0;
78 gc.gridy = 1;
79 gc.gridwidth = 2;
80 gc.fill = GridBagConstraints.BOTH;
81 gc.weightx = 1.0;
82 gc.weighty = 1.0;
83 add(pnlAuthenticationParameteters = new JPanel(), gc);
84 pnlAuthenticationParameteters.setLayout(new BorderLayout());
85
86 //-- the two panel for authentication parameters
87 pnlBasicAuthPreferences = new BasicAuthenticationPreferencesPanel();
88 pnlOAuthPreferences = new OAuthAuthenticationPreferencesPanel();
89
90 rbBasicAuthentication.setSelected(true);
91 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);
92 }
93
94 public AuthenticationPreferencesPanel() {
95 build();
96 HelpUtil.setHelpContext(this, HelpUtil.ht("/Preferences/Connection#AuthenticationSettings"));
97 }
98
99 public void initFromPreferences() {
100 String authMethod = Main.pref.get("osm-server.auth-method", "basic");
101 if (authMethod.equals("basic")) {
102 rbBasicAuthentication.setSelected(true);
103 } else if (authMethod.equals("oauth")) {
104 rbOAuth.setSelected(true);
105 } else {
106 System.err.println(tr("Warning: Unsupported value in preference ''{0}'', got {1}''. Using authentication method ''Basic Authentication''.", "osm-server.auth-method", authMethod));
107 rbBasicAuthentication.setSelected(true);
108 }
109 pnlBasicAuthPreferences.initFromPreferences();
110 pnlOAuthPreferences.initFromPreferences();
111 }
112
113 public void saveToPreferences() {
114 // save the authentication method
115 String authMethod;
116 if (rbBasicAuthentication.isSelected()) {
117 authMethod = "basic";
118 } else {
119 authMethod = "oauth";
120 }
121 Main.pref.put("osm-server.auth-method", authMethod);
122 if (authMethod.equals("basic")) {
123 // save username and password and clear the OAuth token
124 pnlBasicAuthPreferences.saveToPreferences();
125 OAuthAccessTokenHolder.getInstance().clear();
126 OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManagerFactory.getCredentialManager());
127 } else if (authMethod.equals("oauth")) {
128 // clear the password in the preferences
129 pnlBasicAuthPreferences.clearPassword();
130 pnlBasicAuthPreferences.saveToPreferences();
131 pnlOAuthPreferences.saveToPreferences();
132 }
133 }
134
135 /**
136 * Listens to changes in the authentication method
137 */
138 class AuthenticationMethodChangeListener implements ItemListener {
139 public void itemStateChanged(ItemEvent e) {
140 if (rbBasicAuthentication.isSelected()) {
141 pnlAuthenticationParameteters.removeAll();
142 pnlAuthenticationParameteters.add(pnlBasicAuthPreferences, BorderLayout.CENTER);
143 pnlBasicAuthPreferences.revalidate();
144 } else {
145 pnlAuthenticationParameteters.removeAll();
146 pnlAuthenticationParameteters.add(pnlOAuthPreferences, BorderLayout.CENTER);
147 pnlOAuthPreferences.revalidate();
148 }
149 repaint();
150 }
151 }
152
153 public void propertyChange(PropertyChangeEvent evt) {
154 if (pnlOAuthPreferences != null) {
155 pnlOAuthPreferences.propertyChange(evt);
156 }
157 }
158}
Note: See TracBrowser for help on using the repository browser.