source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java@ 10420

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

sonar - squid:S1166 - Exception handlers should preserve the original exceptions

  • Property svn:eol-style set to native
File size: 13.7 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.Color;
8import java.awt.FlowLayout;
9import java.awt.Font;
10import java.awt.GridBagConstraints;
11import java.awt.GridBagLayout;
12import java.awt.Insets;
13import java.awt.event.ActionEvent;
14import java.awt.event.ItemEvent;
15import java.awt.event.ItemListener;
16import java.beans.PropertyChangeEvent;
17import java.beans.PropertyChangeListener;
18
19import javax.swing.AbstractAction;
20import javax.swing.BorderFactory;
21import javax.swing.JCheckBox;
22import javax.swing.JLabel;
23import javax.swing.JPanel;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.data.oauth.OAuthParameters;
27import org.openstreetmap.josm.data.oauth.OAuthToken;
28import org.openstreetmap.josm.gui.SideButton;
29import org.openstreetmap.josm.gui.oauth.AdvancedOAuthPropertiesPanel;
30import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
31import org.openstreetmap.josm.gui.oauth.TestAccessTokenTask;
32import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
33import org.openstreetmap.josm.gui.widgets.JosmTextField;
34import org.openstreetmap.josm.io.OsmApi;
35import org.openstreetmap.josm.io.auth.CredentialsManager;
36import org.openstreetmap.josm.tools.ImageProvider;
37import org.openstreetmap.josm.tools.UserCancelException;
38
39/**
40 * The preferences panel for the OAuth preferences. This just a summary panel
41 * showing the current Access Token Key and Access Token Secret, if the
42 * user already has an Access Token.
43 *
44 * For initial authorisation see {@link OAuthAuthorizationWizard}.
45 * @since 2745
46 */
47public class OAuthAuthenticationPreferencesPanel extends JPanel implements PropertyChangeListener {
48 private final JCheckBox cbShowAdvancedParameters = new JCheckBox();
49 private final JCheckBox cbSaveToPreferences = new JCheckBox(tr("Save to preferences"));
50 private final JPanel pnlAuthorisationMessage = new JPanel(new BorderLayout());
51 private final NotYetAuthorisedPanel pnlNotYetAuthorised = new NotYetAuthorisedPanel();
52 private final AdvancedOAuthPropertiesPanel pnlAdvancedProperties = new AdvancedOAuthPropertiesPanel();
53 private final AlreadyAuthorisedPanel pnlAlreadyAuthorised = new AlreadyAuthorisedPanel();
54 private String apiUrl;
55
56 /**
57 * Create the panel
58 */
59 public OAuthAuthenticationPreferencesPanel() {
60 build();
61 refreshView();
62 }
63
64 /**
65 * Builds the panel for entering the advanced OAuth parameters
66 *
67 * @return panel with advanced settings
68 */
69 protected JPanel buildAdvancedPropertiesPanel() {
70 JPanel pnl = new JPanel(new GridBagLayout());
71 GridBagConstraints gc = new GridBagConstraints();
72
73 gc.anchor = GridBagConstraints.NORTHWEST;
74 gc.fill = GridBagConstraints.HORIZONTAL;
75 gc.weightx = 0.0;
76 gc.insets = new Insets(0, 0, 0, 3);
77 pnl.add(cbShowAdvancedParameters, gc);
78 cbShowAdvancedParameters.setSelected(false);
79 cbShowAdvancedParameters.addItemListener(
80 new ItemListener() {
81 @Override
82 public void itemStateChanged(ItemEvent evt) {
83 pnlAdvancedProperties.setVisible(evt.getStateChange() == ItemEvent.SELECTED);
84 }
85 }
86 );
87
88 gc.gridx = 1;
89 gc.weightx = 1.0;
90 JMultilineLabel lbl = new JMultilineLabel(tr("Display Advanced OAuth Parameters"));
91 lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
92 pnl.add(lbl, gc);
93
94 gc.gridy = 1;
95 gc.gridx = 1;
96 gc.insets = new Insets(3, 0, 3, 0);
97 gc.fill = GridBagConstraints.BOTH;
98 gc.weightx = 1.0;
99 gc.weighty = 1.0;
100 pnl.add(pnlAdvancedProperties, gc);
101 pnlAdvancedProperties.initFromPreferences(Main.pref);
102 pnlAdvancedProperties.setBorder(
103 BorderFactory.createCompoundBorder(
104 BorderFactory.createLineBorder(Color.GRAY, 1),
105 BorderFactory.createEmptyBorder(3, 3, 3, 3)
106 )
107 );
108 pnlAdvancedProperties.setVisible(false);
109 return pnl;
110 }
111
112 /**
113 * builds the UI
114 */
115 protected final void build() {
116 setLayout(new GridBagLayout());
117 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
118 GridBagConstraints gc = new GridBagConstraints();
119
120 // the panel for the OAuth parameters. pnlAuthorisationMessage is an
121 // empty panel. It is going to be filled later, depending on the
122 // current OAuth state in JOSM.
123 gc.fill = GridBagConstraints.BOTH;
124 gc.anchor = GridBagConstraints.NORTHWEST;
125 gc.weighty = 1.0;
126 gc.weightx = 1.0;
127 gc.insets = new Insets(10, 0, 0, 0);
128 add(pnlAuthorisationMessage, gc);
129 }
130
131 protected void refreshView() {
132 pnlAuthorisationMessage.removeAll();
133 if (OAuthAccessTokenHolder.getInstance().containsAccessToken()) {
134 pnlAuthorisationMessage.add(pnlAlreadyAuthorised, BorderLayout.CENTER);
135 pnlAlreadyAuthorised.refreshView();
136 pnlAlreadyAuthorised.revalidate();
137 } else {
138 pnlAuthorisationMessage.add(pnlNotYetAuthorised, BorderLayout.CENTER);
139 pnlNotYetAuthorised.revalidate();
140 }
141 repaint();
142 }
143
144 /**
145 * Sets the URL of the OSM API for which this panel is currently displaying OAuth properties.
146 *
147 * @param apiUrl the api URL
148 */
149 public void setApiUrl(String apiUrl) {
150 this.apiUrl = apiUrl;
151 pnlAdvancedProperties.setApiUrl(apiUrl);
152 }
153
154 /**
155 * Initializes the panel from preferences
156 */
157 public void initFromPreferences() {
158 setApiUrl(OsmApi.getOsmApi().getServerUrl().trim());
159 refreshView();
160 }
161
162 /**
163 * Saves the current values to preferences
164 */
165 public void saveToPreferences() {
166 OAuthAccessTokenHolder.getInstance().setSaveToPreferences(cbSaveToPreferences.isSelected());
167 OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance());
168 pnlAdvancedProperties.rememberPreferences(Main.pref);
169 }
170
171 /**
172 * The preferences panel displayed if there is currently no Access Token available.
173 * This means that the user didn't run through the OAuth authorisation procedure yet.
174 *
175 */
176 private class NotYetAuthorisedPanel extends JPanel {
177 /**
178 * Constructs a new {@code NotYetAuthorisedPanel}.
179 */
180 NotYetAuthorisedPanel() {
181 build();
182 }
183
184 protected void build() {
185 setLayout(new GridBagLayout());
186 GridBagConstraints gc = new GridBagConstraints();
187
188 // A message explaining that the user isn't authorised yet
189 gc.anchor = GridBagConstraints.NORTHWEST;
190 gc.insets = new Insets(0, 0, 3, 0);
191 gc.fill = GridBagConstraints.HORIZONTAL;
192 gc.weightx = 1.0;
193 JMultilineLabel lbl = new JMultilineLabel(
194 tr("You do not have an Access Token yet to access the OSM server using OAuth. Please authorize first."));
195 add(lbl, gc);
196 lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
197
198 // Action for authorising now
199 gc.gridy = 1;
200 gc.fill = GridBagConstraints.NONE;
201 gc.weightx = 0.0;
202 add(new SideButton(new AuthoriseNowAction()), gc);
203
204 // filler - grab remaining space
205 gc.gridy = 2;
206 gc.fill = GridBagConstraints.BOTH;
207 gc.weightx = 1.0;
208 gc.weighty = 1.0;
209 add(new JPanel(), gc);
210 }
211 }
212
213 /**
214 * The preferences panel displayed if there is currently an AccessToken available.
215 *
216 */
217 private class AlreadyAuthorisedPanel extends JPanel {
218 private final JosmTextField tfAccessTokenKey = new JosmTextField();
219 private final JosmTextField tfAccessTokenSecret = new JosmTextField();
220
221 /**
222 * Constructs a new {@code AlreadyAuthorisedPanel}.
223 */
224 AlreadyAuthorisedPanel() {
225 build();
226 refreshView();
227 }
228
229 protected void build() {
230 setLayout(new GridBagLayout());
231 GridBagConstraints gc = new GridBagConstraints();
232 gc.anchor = GridBagConstraints.NORTHWEST;
233 gc.insets = new Insets(0, 0, 3, 3);
234 gc.fill = GridBagConstraints.HORIZONTAL;
235 gc.weightx = 1.0;
236 gc.gridwidth = 2;
237 JMultilineLabel lbl = new JMultilineLabel(tr("You already have an Access Token to access the OSM server using OAuth."));
238 add(lbl, gc);
239 lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
240
241 // -- access token key
242 gc.gridy = 1;
243 gc.gridx = 0;
244 gc.gridwidth = 1;
245 gc.weightx = 0.0;
246 add(new JLabel(tr("Access Token Key:")), gc);
247
248 gc.gridx = 1;
249 gc.weightx = 1.0;
250 add(tfAccessTokenKey, gc);
251 tfAccessTokenKey.setEditable(false);
252
253 // -- access token secret
254 gc.gridy = 2;
255 gc.gridx = 0;
256 gc.gridwidth = 1;
257 gc.weightx = 0.0;
258 add(new JLabel(tr("Access Token Secret:")), gc);
259
260 gc.gridx = 1;
261 gc.weightx = 1.0;
262 add(tfAccessTokenSecret, gc);
263 tfAccessTokenSecret.setEditable(false);
264
265 // -- access token secret
266 gc.gridy = 3;
267 gc.gridx = 0;
268 gc.gridwidth = 2;
269 gc.weightx = 1.0;
270 add(cbSaveToPreferences, gc);
271 cbSaveToPreferences.setSelected(OAuthAccessTokenHolder.getInstance().isSaveToPreferences());
272
273 // -- action buttons
274 JPanel btns = new JPanel(new FlowLayout(FlowLayout.LEFT));
275 btns.add(new SideButton(new RenewAuthorisationAction()));
276 btns.add(new SideButton(new TestAuthorisationAction()));
277 gc.gridy = 4;
278 gc.gridx = 0;
279 gc.gridwidth = 2;
280 gc.weightx = 1.0;
281 add(btns, gc);
282
283 // the panel with the advanced options
284 gc.gridy = 5;
285 gc.gridx = 0;
286 gc.gridwidth = 2;
287 gc.weightx = 1.0;
288 add(buildAdvancedPropertiesPanel(), gc);
289
290 // filler - grab the remaining space
291 gc.gridy = 6;
292 gc.fill = GridBagConstraints.BOTH;
293 gc.weightx = 1.0;
294 gc.weighty = 1.0;
295 add(new JPanel(), gc);
296 }
297
298 protected final void refreshView() {
299 String v = OAuthAccessTokenHolder.getInstance().getAccessTokenKey();
300 tfAccessTokenKey.setText(v == null ? "" : v);
301 v = OAuthAccessTokenHolder.getInstance().getAccessTokenSecret();
302 tfAccessTokenSecret.setText(v == null ? "" : v);
303 cbSaveToPreferences.setSelected(OAuthAccessTokenHolder.getInstance().isSaveToPreferences());
304 }
305 }
306
307 /**
308 * Action to authorise the current user
309 */
310 private class AuthoriseNowAction extends AbstractAction {
311 AuthoriseNowAction() {
312 putValue(NAME, tr("Authorize now"));
313 putValue(SHORT_DESCRIPTION, tr("Click to step through the OAuth authorization process"));
314 putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth-small"));
315 }
316
317 @Override
318 public void actionPerformed(ActionEvent arg0) {
319 OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(
320 OAuthAuthenticationPreferencesPanel.this,
321 apiUrl,
322 Main.worker);
323 try {
324 wizard.showDialog();
325 } catch (UserCancelException ignore) {
326 Main.trace(ignore);
327 return;
328 }
329 pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters());
330 refreshView();
331 }
332 }
333
334 /**
335 * Launches the OAuthAuthorisationWizard to generate a new Access Token
336 */
337 private class RenewAuthorisationAction extends AuthoriseNowAction {
338 /**
339 * Constructs a new {@code RenewAuthorisationAction}.
340 */
341 RenewAuthorisationAction() {
342 putValue(NAME, tr("New Access Token"));
343 putValue(SHORT_DESCRIPTION, tr("Click to step through the OAuth authorization process and generate a new Access Token"));
344 putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth-small"));
345 }
346 }
347
348 /**
349 * Runs a test whether we can access the OSM server with the current Access Token
350 */
351 private class TestAuthorisationAction extends AbstractAction {
352 /**
353 * Constructs a new {@code TestAuthorisationAction}.
354 */
355 TestAuthorisationAction() {
356 putValue(NAME, tr("Test Access Token"));
357 putValue(SHORT_DESCRIPTION, tr("Click test access to the OSM server with the current access token"));
358 putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth-small"));
359
360 }
361
362 @Override
363 public void actionPerformed(ActionEvent evt) {
364 OAuthToken token = OAuthAccessTokenHolder.getInstance().getAccessToken();
365 OAuthParameters parameters = OAuthParameters.createFromPreferences(Main.pref);
366 TestAccessTokenTask task = new TestAccessTokenTask(
367 OAuthAuthenticationPreferencesPanel.this,
368 apiUrl,
369 parameters,
370 token
371 );
372 Main.worker.submit(task);
373 }
374 }
375
376 @Override
377 public void propertyChange(PropertyChangeEvent evt) {
378 if (!evt.getPropertyName().equals(OsmApiUrlInputPanel.API_URL_PROP))
379 return;
380 setApiUrl((String) evt.getNewValue());
381 }
382}
Note: See TracBrowser for help on using the repository browser.