source: josm/trunk/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java@ 8426

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

Accessibility - global use of JLabel.setLabelFor() + various fixes (javadoc, code style)

  • Property svn:eol-style set to native
File size: 13.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.oauth;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.Dimension;
9import java.awt.FlowLayout;
10import java.awt.Font;
11import java.awt.GridBagConstraints;
12import java.awt.GridBagLayout;
13import java.awt.Insets;
14import java.awt.event.ActionEvent;
15import java.awt.event.ComponentEvent;
16import java.awt.event.ComponentListener;
17import java.awt.event.ItemEvent;
18import java.awt.event.ItemListener;
19import java.awt.event.KeyEvent;
20import java.awt.event.WindowAdapter;
21import java.awt.event.WindowEvent;
22import java.beans.PropertyChangeEvent;
23import java.beans.PropertyChangeListener;
24
25import javax.swing.AbstractAction;
26import javax.swing.BorderFactory;
27import javax.swing.JComponent;
28import javax.swing.JDialog;
29import javax.swing.JLabel;
30import javax.swing.JOptionPane;
31import javax.swing.JPanel;
32import javax.swing.JScrollPane;
33import javax.swing.KeyStroke;
34import javax.swing.UIManager;
35import javax.swing.event.HyperlinkEvent;
36import javax.swing.event.HyperlinkListener;
37
38import org.openstreetmap.josm.Main;
39import org.openstreetmap.josm.data.CustomConfigurator;
40import org.openstreetmap.josm.data.Preferences;
41import org.openstreetmap.josm.data.oauth.OAuthParameters;
42import org.openstreetmap.josm.data.oauth.OAuthToken;
43import org.openstreetmap.josm.gui.SideButton;
44import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
45import org.openstreetmap.josm.gui.help.HelpUtil;
46import org.openstreetmap.josm.gui.util.GuiHelper;
47import org.openstreetmap.josm.gui.widgets.HtmlPanel;
48import org.openstreetmap.josm.tools.CheckParameterUtil;
49import org.openstreetmap.josm.tools.ImageProvider;
50import org.openstreetmap.josm.tools.OpenBrowser;
51import org.openstreetmap.josm.tools.WindowGeometry;
52
53/**
54 * This wizard walks the user to the necessary steps to retrieve an OAuth Access Token which
55 * allows JOSM to access the OSM API on the users behalf.
56 *
57 */
58public class OAuthAuthorizationWizard extends JDialog {
59 private boolean canceled;
60 private final String apiUrl;
61
62 private AuthorizationProcedureComboBox cbAuthorisationProcedure;
63 private FullyAutomaticAuthorizationUI pnlFullyAutomaticAuthorisationUI;
64 private SemiAutomaticAuthorizationUI pnlSemiAutomaticAuthorisationUI;
65 private ManualAuthorizationUI pnlManualAuthorisationUI;
66 private JScrollPane spAuthorisationProcedureUI;
67
68 /**
69 * Builds the row with the action buttons
70 *
71 * @return panel with buttons
72 */
73 protected JPanel buildButtonRow(){
74 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
75
76 AcceptAccessTokenAction actAcceptAccessToken = new AcceptAccessTokenAction();
77 pnlFullyAutomaticAuthorisationUI.addPropertyChangeListener(actAcceptAccessToken);
78 pnlSemiAutomaticAuthorisationUI.addPropertyChangeListener(actAcceptAccessToken);
79 pnlManualAuthorisationUI.addPropertyChangeListener(actAcceptAccessToken);
80
81 pnl.add(new SideButton(actAcceptAccessToken));
82 pnl.add(new SideButton(new CancelAction()));
83 pnl.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Dialog/OAuthAuthorisationWizard"))));
84
85 return pnl;
86 }
87
88 /**
89 * Builds the panel with general information in the header
90 *
91 * @return panel woth information display
92 */
93 protected JPanel buildHeaderInfoPanel() {
94 JPanel pnl = new JPanel(new GridBagLayout());
95 pnl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
96 GridBagConstraints gc = new GridBagConstraints();
97
98 // the oauth logo in the header
99 gc.anchor = GridBagConstraints.NORTHWEST;
100 gc.fill = GridBagConstraints.HORIZONTAL;
101 gc.weightx = 1.0;
102 gc.gridwidth = 2;
103 JLabel lbl = new JLabel();
104 lbl.setIcon(ImageProvider.get("oauth", "oauth-logo"));
105 lbl.setOpaque(true);
106 pnl.add(lbl, gc);
107
108 // OAuth in a nutshell ...
109 gc.gridy = 1;
110 gc.insets = new Insets(5,0,0,5);
111 HtmlPanel pnlMessage = new HtmlPanel();
112 pnlMessage.setText("<html><body>"
113 + tr("With OAuth you grant JOSM the right to upload map data and GPS tracks "
114 + "on your behalf (<a href=\"{0}\">more info...</a>).", "http://oauth.net/")
115 + "</body></html>"
116 );
117 pnlMessage.getEditorPane().addHyperlinkListener(new ExternalBrowserLauncher());
118 pnl.add(pnlMessage, gc);
119
120 // the authorisation procedure
121 gc.gridy = 2;
122 gc.gridwidth = 1;
123 gc.weightx = 0.0;
124 lbl = new JLabel(tr("Please select an authorization procedure: "));
125 lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
126 pnl.add(lbl,gc);
127
128 gc.gridx = 1;
129 gc.gridwidth = 1;
130 gc.weightx = 1.0;
131 pnl.add(cbAuthorisationProcedure = new AuthorizationProcedureComboBox(),gc);
132 cbAuthorisationProcedure.addItemListener(new AuthorisationProcedureChangeListener());
133 lbl.setLabelFor(cbAuthorisationProcedure);
134 return pnl;
135 }
136
137 /**
138 * Refreshes the view of the authorisation panel, depending on the authorisation procedure
139 * currently selected
140 */
141 protected void refreshAuthorisationProcedurePanel() {
142 AuthorizationProcedure procedure = (AuthorizationProcedure)cbAuthorisationProcedure.getSelectedItem();
143 switch(procedure) {
144 case FULLY_AUTOMATIC:
145 spAuthorisationProcedureUI.getViewport().setView(pnlFullyAutomaticAuthorisationUI);
146 pnlFullyAutomaticAuthorisationUI.revalidate();
147 break;
148 case SEMI_AUTOMATIC:
149 spAuthorisationProcedureUI.getViewport().setView(pnlSemiAutomaticAuthorisationUI);
150 pnlSemiAutomaticAuthorisationUI.revalidate();
151 break;
152 case MANUALLY:
153 spAuthorisationProcedureUI.getViewport().setView(pnlManualAuthorisationUI);
154 pnlManualAuthorisationUI.revalidate();
155 break;
156 }
157 validate();
158 repaint();
159 }
160
161 /**
162 * builds the UI
163 */
164 protected final void build() {
165 getContentPane().setLayout(new BorderLayout());
166 getContentPane().add(buildHeaderInfoPanel(), BorderLayout.NORTH);
167
168 setTitle(tr("Get an Access Token for ''{0}''", apiUrl));
169
170 pnlFullyAutomaticAuthorisationUI = new FullyAutomaticAuthorizationUI(apiUrl);
171 pnlSemiAutomaticAuthorisationUI = new SemiAutomaticAuthorizationUI(apiUrl);
172 pnlManualAuthorisationUI = new ManualAuthorizationUI(apiUrl);
173
174 spAuthorisationProcedureUI = GuiHelper.embedInVerticalScrollPane(new JPanel());
175 spAuthorisationProcedureUI.getVerticalScrollBar().addComponentListener(
176 new ComponentListener() {
177 @Override
178 public void componentShown(ComponentEvent e) {
179 spAuthorisationProcedureUI.setBorder(UIManager.getBorder("ScrollPane.border"));
180 }
181
182 @Override
183 public void componentHidden(ComponentEvent e) {
184 spAuthorisationProcedureUI.setBorder(null);
185 }
186
187 @Override
188 public void componentResized(ComponentEvent e) {}
189 @Override
190 public void componentMoved(ComponentEvent e) {}
191 }
192 );
193 getContentPane().add(spAuthorisationProcedureUI, BorderLayout.CENTER);
194 getContentPane().add(buildButtonRow(), BorderLayout.SOUTH);
195
196 addWindowListener(new WindowEventHandler());
197 getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
198 getRootPane().getActionMap().put("cancel", new CancelAction());
199
200 refreshAuthorisationProcedurePanel();
201
202 HelpUtil.setHelpContext(getRootPane(), HelpUtil.ht("/Dialog/OAuthAuthorisationWizard"));
203 }
204
205 /**
206 * Creates the wizard.
207 *
208 * @param apiUrl the API URL. Must not be null.
209 * @throws IllegalArgumentException if apiUrl is null
210 */
211 public OAuthAuthorizationWizard(String apiUrl) {
212 this(Main.parent, apiUrl);
213 }
214
215 /**
216 * Creates the wizard.
217 *
218 * @param parent the component relative to which the dialog is displayed
219 * @param apiUrl the API URL. Must not be null.
220 * @throws IllegalArgumentException if apiUrl is null
221 */
222 public OAuthAuthorizationWizard(Component parent, String apiUrl) {
223 super(JOptionPane.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
224 CheckParameterUtil.ensureParameterNotNull(apiUrl, "apiUrl");
225 this.apiUrl = apiUrl;
226 build();
227 }
228
229 /**
230 * Replies true if the dialog was canceled
231 *
232 * @return true if the dialog was canceled
233 */
234 public boolean isCanceled() {
235 return canceled;
236 }
237
238 protected AbstractAuthorizationUI getCurrentAuthorisationUI() {
239 switch((AuthorizationProcedure)cbAuthorisationProcedure.getSelectedItem()) {
240 case FULLY_AUTOMATIC: return pnlFullyAutomaticAuthorisationUI;
241 case MANUALLY: return pnlManualAuthorisationUI;
242 case SEMI_AUTOMATIC: return pnlSemiAutomaticAuthorisationUI;
243 default: return null;
244 }
245 }
246
247 /**
248 * Replies the Access Token entered using the wizard
249 *
250 * @return the access token. May be null if the wizard was canceled.
251 */
252 public OAuthToken getAccessToken() {
253 return getCurrentAuthorisationUI().getAccessToken();
254 }
255
256 /**
257 * Replies the current OAuth parameters.
258 *
259 * @return the current OAuth parameters.
260 */
261 public OAuthParameters getOAuthParameters() {
262 return getCurrentAuthorisationUI().getOAuthParameters();
263 }
264
265 /**
266 * Replies true if the currently selected Access Token shall be saved to
267 * the preferences.
268 *
269 * @return true if the currently selected Access Token shall be saved to
270 * the preferences
271 */
272 public boolean isSaveAccessTokenToPreferences() {
273 return getCurrentAuthorisationUI().isSaveAccessTokenToPreferences();
274 }
275
276 /**
277 * Initializes the dialog with values from the preferences
278 *
279 */
280 public void initFromPreferences() {
281 // Copy current JOSM preferences to update API url with the one used in this wizard
282 Preferences copyPref = CustomConfigurator.clonePreferences(Main.pref);
283 copyPref.put("osm-server.url", apiUrl);
284 pnlFullyAutomaticAuthorisationUI.initFromPreferences(copyPref);
285 pnlSemiAutomaticAuthorisationUI.initFromPreferences(copyPref);
286 pnlManualAuthorisationUI.initFromPreferences(copyPref);
287 }
288
289 @Override
290 public void setVisible(boolean visible) {
291 if (visible) {
292 new WindowGeometry(
293 getClass().getName() + ".geometry",
294 WindowGeometry.centerInWindow(
295 Main.parent,
296 new Dimension(450,540)
297 )
298 ).applySafe(this);
299 initFromPreferences();
300 } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
301 new WindowGeometry(this).remember(getClass().getName() + ".geometry");
302 }
303 super.setVisible(visible);
304 }
305
306 protected void setCanceled(boolean canceled) {
307 this.canceled = canceled;
308 }
309
310 class AuthorisationProcedureChangeListener implements ItemListener {
311 @Override
312 public void itemStateChanged(ItemEvent arg0) {
313 refreshAuthorisationProcedurePanel();
314 }
315 }
316
317 class CancelAction extends AbstractAction {
318 public CancelAction() {
319 putValue(NAME, tr("Cancel"));
320 putValue(SMALL_ICON, ImageProvider.get("cancel"));
321 putValue(SHORT_DESCRIPTION, tr("Close the dialog and cancel authorization"));
322 }
323
324 public void cancel() {
325 setCanceled(true);
326 setVisible(false);
327 }
328
329 @Override
330 public void actionPerformed(ActionEvent evt) {
331 cancel();
332 }
333 }
334
335 class AcceptAccessTokenAction extends AbstractAction implements PropertyChangeListener {
336 private transient OAuthToken token;
337
338 public AcceptAccessTokenAction() {
339 putValue(NAME, tr("Accept Access Token"));
340 putValue(SMALL_ICON, ImageProvider.get("ok"));
341 putValue(SHORT_DESCRIPTION, tr("Close the dialog and accept the Access Token"));
342 updateEnabledState(null);
343 }
344
345 @Override
346 public void actionPerformed(ActionEvent evt) {
347 setCanceled(false);
348 setVisible(false);
349 }
350
351 public final void updateEnabledState(OAuthToken token) {
352 setEnabled(token != null);
353 }
354
355 @Override
356 public void propertyChange(PropertyChangeEvent evt) {
357 if (!evt.getPropertyName().equals(AbstractAuthorizationUI.ACCESS_TOKEN_PROP))
358 return;
359 token = (OAuthToken)evt.getNewValue();
360 updateEnabledState(token);
361 }
362 }
363
364 class WindowEventHandler extends WindowAdapter {
365 @Override
366 public void windowClosing(WindowEvent arg0) {
367 new CancelAction().cancel();
368 }
369 }
370
371 static class ExternalBrowserLauncher implements HyperlinkListener {
372 @Override
373 public void hyperlinkUpdate(HyperlinkEvent e) {
374 if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
375 OpenBrowser.displayUrl(e.getDescription());
376 }
377 }
378 }
379}
Note: See TracBrowser for help on using the repository browser.