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