Changeset 31808 in osm


Ignore:
Timestamp:
2015-12-10T22:12:00+01:00 (9 years ago)
Author:
floscher
Message:

[mapillary] Fix #11855 by moving networking for OAuth in separate thread

Location:
applications/editors/josm/plugins/mapillary
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/gui/MapillaryPreferenceSetting.java

    r31799 r31808  
    55
    66import java.awt.FlowLayout;
     7import java.awt.GridBagConstraints;
     8import java.awt.GridBagLayout;
    79import java.awt.event.ActionEvent;
    810import java.io.IOException;
    911import java.net.URL;
    1012
     13import javax.json.Json;
    1114import javax.swing.AbstractAction;
     15import javax.swing.BorderFactory;
     16import javax.swing.Box;
    1217import javax.swing.JButton;
    1318import javax.swing.JCheckBox;
     
    1520import javax.swing.JLabel;
    1621import javax.swing.JPanel;
     22import javax.swing.JScrollPane;
     23import javax.swing.SwingUtilities;
    1724
    1825import org.openstreetmap.josm.Main;
     
    2229import org.openstreetmap.josm.plugins.mapillary.MapillaryPlugin;
    2330import org.openstreetmap.josm.plugins.mapillary.io.download.MapillaryDownloader;
     31import org.openstreetmap.josm.plugins.mapillary.oauth.MapillaryLoginListener;
    2432import org.openstreetmap.josm.plugins.mapillary.oauth.MapillaryUser;
    2533import org.openstreetmap.josm.plugins.mapillary.oauth.OAuthPortListener;
    2634import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryUtils;
     35import org.openstreetmap.josm.tools.GBC;
     36import org.openstreetmap.josm.tools.I18n;
    2737
    2838/**
     
    3242 *
    3343 */
    34 public class MapillaryPreferenceSetting implements SubPreferenceSetting {
     44public class MapillaryPreferenceSetting implements SubPreferenceSetting, MapillaryLoginListener {
    3545
    3646  private JCheckBox reverseButtons = new JCheckBox(
     
    4757      tr("Move to picture''s location with next/previous buttons"));
    4858  private JButton login;
     59
     60  private JButton loginButton = new JButton(new LoginAction(this));
     61  private JButton logoutButton = new JButton(new LogoutAction());
     62  private JLabel loginLabel = new JLabel();
     63  private JPanel loginPanel = new JPanel();
    4964
    5065  @Override
     
    7893    panel.add(this.format24);
    7994    panel.add(this.moveTo);
    80     this.login = new JButton(new LoginAction());
    81     if (MapillaryUser.getUsername() == null)
    82       this.login.setText("Login");
    83     else
    84       this.login.setText("Logged as: " + MapillaryUser.getUsername()
    85           + ". Click to relogin.");
    86     panel.add(this.login);
    87     if (MapillaryUser.getUsername() != null) {
    88       JButton logout = new JButton(new LogoutAction());
    89       logout.setText("Logout");
    90       panel.add(logout);
    91     }
     95
     96    loginPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
     97    loginPanel.add(loginButton, 0);
     98    loginPanel.add(loginLabel, 1);
     99    onLogout();
     100    panel.add(loginPanel, GBC.eol());
     101    panel.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
     102
    92103    gui.getDisplayPreference().addSubTab(this, "Mapillary", panel);
     104
     105    new Thread(new Runnable() {
     106      @Override
     107      public void run() {
     108        String username = MapillaryUser.getUsername();
     109        if (username != null) {
     110          SwingUtilities.invokeLater(new Runnable() {
     111            @Override
     112            public void run() {
     113              onLogin(MapillaryUser.getUsername());
     114            }
     115          });
     116        }
     117      }
     118    }).start();
     119  }
     120
     121  /**
     122   * Should be called whenever the user logs into a mapillary account.
     123   * This updates the GUI to reflect the login status.
     124   * @param username the username that the user is now logged in with
     125   */
     126  public void onLogin(final String username) {
     127    loginPanel.add(logoutButton, 1);
     128    loginLabel.setText(I18n.tr("You are logged in as ''{0}''.", username));
     129    loginButton.setText(I18n.tr("Re-Login"));
     130    logoutButton.setText(I18n.tr("Logout"));
     131  }
     132
     133  /**
     134   * Should be called whenever the user logs out of a mapillary account.
     135   * This updates the GUI to reflect the login status.
     136   */
     137  public void onLogout() {
     138    loginPanel.remove(logoutButton);
     139    loginLabel.setText(I18n.tr("You are currently not logged in."));
     140    loginButton.setText(I18n.tr("Login"));
    93141  }
    94142
     
    126174   */
    127175  public class LoginAction extends AbstractAction {
    128 
    129176    private static final long serialVersionUID = -3908477563072057344L;
     177    final transient MapillaryLoginListener callback;
     178
     179    public LoginAction(MapillaryLoginListener loginCallback) {
     180      this.callback = loginCallback;
     181    }
    130182
    131183    @Override
    132184    public void actionPerformed(ActionEvent arg0) {
    133       OAuthPortListener portListener = new OAuthPortListener();
     185      OAuthPortListener portListener = new OAuthPortListener(callback);
    134186      portListener.start();
    135       String url = "http://www.mapillary.com/connect?redirect_uri=http:%2F%2Flocalhost:8763%2F&client_id="+MapillaryPlugin.CLIENT_ID+"&response_type=token&scope=user:read%20public:upload%20public:write";
     187      String url = "http://www.mapillary.com/connect?redirect_uri=http:%2F%2Flocalhost:"+OAuthPortListener.PORT+"%2F&client_id="+MapillaryPlugin.CLIENT_ID+"&response_type=token&scope=user:read%20public:upload%20public:write";
    136188      try {
    137189        MapillaryUtils.browse(new URL(url));
     
    156208      MapillaryUser.reset();
    157209      Main.pref.put("mapillary.access-token", null);
    158       MapillaryPreferenceSetting.this.login.setText("Login");
     210      onLogout();
    159211    }
    160212  }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListener.java

    r31807 r31808  
    2929      I18n.tr("Login successful, return to JOSM.")
    3030  );
     31  private MapillaryLoginListener callback;
    3132
     33  public OAuthPortListener(MapillaryLoginListener loginCallback) {
     34    this.callback = loginCallback;
     35  }
    3236
    3337  @Override
     
    7074      if (Main.main != null) {
    7175        Main.pref.put("mapillary.access-token", accessToken);
    72         Main.info("The username is: " + MapillaryUser.getUsername());
     76        String username = MapillaryUser.getUsername();
     77        Main.info("The username is: " + username);
     78        if (callback != null) {
     79          callback.onLogin(username);
     80        }
    7381      }
    7482    } catch (BindException e) {
  • applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListenerTest.java

    r31807 r31808  
    2525  @Test
    2626  public void responseTest() {
    27     OAuthPortListener t = new OAuthPortListener();
     27    OAuthPortListener t = new OAuthPortListener(null);
    2828    t.start();
    2929    try {
Note: See TracChangeset for help on using the changeset viewer.