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

Last change on this file since 9353 was 9353, checked in by simon04, 9 years ago

Refactoring: introduce OsmApi#getServerUrl

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