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

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

sonar, javadoc

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