Changeset 31808 in osm
- Timestamp:
- 2015-12-10T22:12:00+01:00 (9 years ago)
- 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 5 5 6 6 import java.awt.FlowLayout; 7 import java.awt.GridBagConstraints; 8 import java.awt.GridBagLayout; 7 9 import java.awt.event.ActionEvent; 8 10 import java.io.IOException; 9 11 import java.net.URL; 10 12 13 import javax.json.Json; 11 14 import javax.swing.AbstractAction; 15 import javax.swing.BorderFactory; 16 import javax.swing.Box; 12 17 import javax.swing.JButton; 13 18 import javax.swing.JCheckBox; … … 15 20 import javax.swing.JLabel; 16 21 import javax.swing.JPanel; 22 import javax.swing.JScrollPane; 23 import javax.swing.SwingUtilities; 17 24 18 25 import org.openstreetmap.josm.Main; … … 22 29 import org.openstreetmap.josm.plugins.mapillary.MapillaryPlugin; 23 30 import org.openstreetmap.josm.plugins.mapillary.io.download.MapillaryDownloader; 31 import org.openstreetmap.josm.plugins.mapillary.oauth.MapillaryLoginListener; 24 32 import org.openstreetmap.josm.plugins.mapillary.oauth.MapillaryUser; 25 33 import org.openstreetmap.josm.plugins.mapillary.oauth.OAuthPortListener; 26 34 import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryUtils; 35 import org.openstreetmap.josm.tools.GBC; 36 import org.openstreetmap.josm.tools.I18n; 27 37 28 38 /** … … 32 42 * 33 43 */ 34 public class MapillaryPreferenceSetting implements SubPreferenceSetting {44 public class MapillaryPreferenceSetting implements SubPreferenceSetting, MapillaryLoginListener { 35 45 36 46 private JCheckBox reverseButtons = new JCheckBox( … … 47 57 tr("Move to picture''s location with next/previous buttons")); 48 58 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(); 49 64 50 65 @Override … … 78 93 panel.add(this.format24); 79 94 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 92 103 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")); 93 141 } 94 142 … … 126 174 */ 127 175 public class LoginAction extends AbstractAction { 128 129 176 private static final long serialVersionUID = -3908477563072057344L; 177 final transient MapillaryLoginListener callback; 178 179 public LoginAction(MapillaryLoginListener loginCallback) { 180 this.callback = loginCallback; 181 } 130 182 131 183 @Override 132 184 public void actionPerformed(ActionEvent arg0) { 133 OAuthPortListener portListener = new OAuthPortListener( );185 OAuthPortListener portListener = new OAuthPortListener(callback); 134 186 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"; 136 188 try { 137 189 MapillaryUtils.browse(new URL(url)); … … 156 208 MapillaryUser.reset(); 157 209 Main.pref.put("mapillary.access-token", null); 158 MapillaryPreferenceSetting.this.login.setText("Login");210 onLogout(); 159 211 } 160 212 } -
applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListener.java
r31807 r31808 29 29 I18n.tr("Login successful, return to JOSM.") 30 30 ); 31 private MapillaryLoginListener callback; 31 32 33 public OAuthPortListener(MapillaryLoginListener loginCallback) { 34 this.callback = loginCallback; 35 } 32 36 33 37 @Override … … 70 74 if (Main.main != null) { 71 75 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 } 73 81 } 74 82 } catch (BindException e) { -
applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListenerTest.java
r31807 r31808 25 25 @Test 26 26 public void responseTest() { 27 OAuthPortListener t = new OAuthPortListener( );27 OAuthPortListener t = new OAuthPortListener(null); 28 28 t.start(); 29 29 try {
Note:
See TracChangeset
for help on using the changeset viewer.