Skip to content

Commit 7e318d6

Browse files
author
nokutu
committed
Code improvements and fixed a bug caused by trying to upload a picture without being logged in.
git-svn-id: http://svn.openstreetmap.org/applications/editors/josm/plugins/mapillary@31514 b9d5c4c9-76e1-0310-9c85-f3177eceb1e4
1 parent a08fa54 commit 7e318d6

File tree

8 files changed

+51
-87
lines changed

8 files changed

+51
-87
lines changed

src/org/openstreetmap/josm/plugins/mapillary/MapillaryAbstractImage.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.text.SimpleDateFormat;
44
import java.util.Calendar;
55
import java.util.Date;
6-
import java.util.concurrent.locks.Lock;
7-
import java.util.concurrent.locks.ReentrantLock;
86

97
import org.openstreetmap.josm.Main;
108
import org.openstreetmap.josm.data.coor.LatLon;
@@ -18,13 +16,6 @@
1816
*/
1917
public abstract class MapillaryAbstractImage {
2018

21-
/**
22-
* Lock that locks {@link MapillaryAbstractImage#next()} and
23-
* {@link MapillaryAbstractImage#previous()} methods. Used when downloading
24-
* images to prevent concurrency problems.
25-
*/
26-
public static final Lock LOCK = new ReentrantLock();
27-
2819
/** The time the image was captured, in Epoch format. */
2920
private long capturedAt;
3021
/** Sequence of pictures containing this object. */
@@ -204,14 +195,10 @@ public void move(double x, double y) {
204195
* @return The following MapillaryImage, or null if there is none.
205196
*/
206197
public MapillaryAbstractImage next() {
207-
LOCK.lock();
208-
try {
209-
if (this.getSequence() == null) {
198+
synchronized (this.getClass()) {
199+
if (this.getSequence() == null)
210200
return null;
211-
}
212201
return this.getSequence().next(this);
213-
} finally {
214-
LOCK.unlock();
215202
}
216203
}
217204

@@ -222,16 +209,11 @@ public MapillaryAbstractImage next() {
222209
* @return The previous MapillaryImage, or null if there is none.
223210
*/
224211
public MapillaryAbstractImage previous() {
225-
LOCK.lock();
226-
try {
227-
if (this.getSequence() == null) {
212+
synchronized (this.getClass()) {
213+
if (this.getSequence() == null)
228214
return null;
229-
}
230215
return this.getSequence().previous(this);
231-
} finally {
232-
LOCK.unlock();
233216
}
234-
235217
}
236218

237219
/**

src/org/openstreetmap/josm/plugins/mapillary/actions/MapillaryUploadAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public void actionPerformed(ActionEvent arg0) {
5252
dlg.setVisible(true);
5353

5454
if (pane.getValue() != null
55-
&& (int) pane.getValue() == JOptionPane.OK_OPTION) {
55+
&& (int) pane.getValue() == JOptionPane.OK_OPTION
56+
&& dialog.delete != null) {
5657
if (dialog.sequence.isSelected()) {
5758
UploadUtils.uploadSequence(MapillaryLayer.getInstance().getData()
5859
.getSelectedImage().getSequence(), dialog.delete.isSelected());

src/org/openstreetmap/josm/plugins/mapillary/actions/WalkThread.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.openstreetmap.josm.plugins.mapillary.actions;
22

33
import java.awt.image.BufferedImage;
4-
import java.util.concurrent.locks.Lock;
5-
import java.util.concurrent.locks.ReentrantLock;
64

75
import javax.swing.SwingUtilities;
86

@@ -23,7 +21,6 @@
2321
public class WalkThread extends Thread implements MapillaryDataListener {
2422
private final int interval;
2523
private final MapillaryData data;
26-
private final Lock lock;
2724
private boolean end = false;
2825
private final boolean waitForFullQuality;
2926
private final boolean followSelected;
@@ -52,7 +49,6 @@ public WalkThread(int interval, boolean waitForPicture,
5249
this.goForward = goForward;
5350
this.data = MapillaryLayer.getInstance().getData();
5451
this.data.addListener(this);
55-
this.lock = new ReentrantLock();
5652
}
5753

5854
@Override
@@ -109,14 +105,11 @@ public void run() {
109105
}
110106
this.lastImage = MapillaryMainDialog.getInstance().mapillaryImageDisplay
111107
.getImage();
112-
this.lock.lock();
113-
try {
108+
synchronized (this) {
114109
if (this.goForward)
115110
this.data.selectNext(this.followSelected);
116111
else
117112
this.data.selectPrevious(this.followSelected);
118-
} finally {
119-
this.lock.unlock();
120113
}
121114
} catch (InterruptedException e) {
122115
return;
@@ -129,14 +122,8 @@ public void run() {
129122
}
130123

131124
@Override
132-
public void interrupt() {
133-
this.lock.lock();
134-
try {
135-
super.interrupt();
136-
} catch (Exception e) {
137-
} finally {
138-
this.lock.unlock();
139-
}
125+
public synchronized void interrupt() {
126+
super.interrupt();
140127
}
141128

142129
@Override

src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillarySequenceDownloadThread.java

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import java.util.ArrayList;
88
import java.util.List;
99
import java.util.concurrent.ExecutorService;
10-
import java.util.concurrent.locks.Lock;
11-
import java.util.concurrent.locks.ReentrantLock;
1210

1311
import javax.json.Json;
1412
import javax.json.JsonArray;
@@ -32,9 +30,6 @@
3230
public class MapillarySequenceDownloadThread extends Thread {
3331
private static final String URL = MapillaryDownloader.BASE_URL + "search/s/";
3432

35-
/** Lock to prevent multiple downloads to be imported at the same time. */
36-
private static final Lock LOCK = new ReentrantLock();
37-
3833
private final String queryString;
3934
private final ExecutorService ex;
4035
private final MapillaryLayer layer;
@@ -57,8 +52,8 @@ public MapillarySequenceDownloadThread(ExecutorService ex, String queryString) {
5752
public void run() {
5853
try {
5954
BufferedReader br;
60-
br = new BufferedReader(new InputStreamReader(
61-
new URL(URL + this.queryString).openStream(), "UTF-8"));
55+
br = new BufferedReader(new InputStreamReader(new URL(URL
56+
+ this.queryString).openStream(), "UTF-8"));
6257
JsonObject jsonall = Json.createReader(br).readObject();
6358

6459
if (!jsonall.getBoolean("more") && !this.ex.isShutdown())
@@ -94,29 +89,25 @@ public void run() {
9489
if (!isInside(img))
9590
finalImages.remove(img);
9691
}
97-
98-
LOCK.lock();
99-
MapillaryAbstractImage.LOCK.lock();
100-
try {
101-
for (MapillaryImage img : finalImages) {
102-
if (this.layer.getData().getImages().contains(img)) {
103-
// The image in finalImages is substituted by the one in the
104-
// database, as they represent the same picture.
105-
img = (MapillaryImage) this.layer.getData().getImages()
106-
.get(this.layer.getData().getImages().indexOf(img));
107-
sequence.add(img);
108-
((MapillaryImage) this.layer.getData().getImages()
109-
.get(this.layer.getData().getImages().indexOf(img)))
110-
.setSequence(sequence);
111-
finalImages.set(finalImages.indexOf(img), img);
112-
} else {
113-
img.setSequence(sequence);
114-
sequence.add(img);
92+
synchronized (this.getClass()) {
93+
synchronized (MapillaryAbstractImage.class) {
94+
for (MapillaryImage img : finalImages) {
95+
if (this.layer.getData().getImages().contains(img)) {
96+
// The image in finalImages is substituted by the one in the
97+
// database, as they represent the same picture.
98+
img = (MapillaryImage) this.layer.getData().getImages()
99+
.get(this.layer.getData().getImages().indexOf(img));
100+
sequence.add(img);
101+
((MapillaryImage) this.layer.getData().getImages()
102+
.get(this.layer.getData().getImages().indexOf(img)))
103+
.setSequence(sequence);
104+
finalImages.set(finalImages.indexOf(img), img);
105+
} else {
106+
img.setSequence(sequence);
107+
sequence.add(img);
108+
}
115109
}
116110
}
117-
} finally {
118-
MapillaryAbstractImage.LOCK.unlock();
119-
LOCK.unlock();
120111
}
121112

122113
this.layer.getData().add(

src/org/openstreetmap/josm/plugins/mapillary/gui/MapillaryPreferenceSetting.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void addGui(PreferenceTabbedPane gui) {
8282
panel.add(this.format24);
8383
panel.add(this.moveTo);
8484
this.login = new JButton(new LoginAction());
85-
if (Main.pref.get("mapillary.access-token") == null)
85+
if (MapillaryUser.getUsername() == null)
8686
this.login.setText("Login");
8787
else
8888
this.login.setText("Logged as: " + MapillaryUser.getUsername()

src/org/openstreetmap/josm/plugins/mapillary/gui/MapillaryUploadDialog.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import javax.swing.BoxLayout;
66
import javax.swing.ButtonGroup;
77
import javax.swing.JCheckBox;
8+
import javax.swing.JLabel;
89
import javax.swing.JPanel;
910
import javax.swing.JRadioButton;
1011

1112
import org.openstreetmap.josm.Main;
1213
import org.openstreetmap.josm.plugins.mapillary.MapillaryImportedImage;
1314
import org.openstreetmap.josm.plugins.mapillary.MapillaryLayer;
15+
import org.openstreetmap.josm.plugins.mapillary.oauth.MapillaryUser;
1416

1517
/**
1618
* @author nokutu
@@ -32,21 +34,23 @@ public class MapillaryUploadDialog extends JPanel {
3234
*/
3335
public MapillaryUploadDialog() {
3436
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
35-
36-
this.group = new ButtonGroup();
37-
38-
this.sequence = new JRadioButton(tr("Upload selected sequence"));
39-
if (MapillaryLayer.getInstance().getData().getSelectedImage() == null
40-
|| !(MapillaryLayer.getInstance().getData().getSelectedImage() instanceof MapillaryImportedImage))
41-
this.sequence.setEnabled(false);
42-
this.group.add(this.sequence);
43-
add(this.sequence);
44-
this.group.setSelected(this.sequence.getModel(), true);
45-
46-
this.delete = new JCheckBox(tr("Delete after upload"));
47-
this.delete.setSelected(Main.pref.getBoolean(
48-
"mapillary.delete-after-upload", true));
49-
add(this.delete);
50-
37+
if (MapillaryUser.getUsername() != null) {
38+
this.group = new ButtonGroup();
39+
40+
this.sequence = new JRadioButton(tr("Upload selected sequence"));
41+
if (MapillaryLayer.getInstance().getData().getSelectedImage() == null
42+
|| !(MapillaryLayer.getInstance().getData().getSelectedImage() instanceof MapillaryImportedImage))
43+
this.sequence.setEnabled(false);
44+
this.group.add(this.sequence);
45+
add(this.sequence);
46+
this.group.setSelected(this.sequence.getModel(), true);
47+
48+
this.delete = new JCheckBox(tr("Delete after upload"));
49+
this.delete.setSelected(Main.pref.getBoolean(
50+
"mapillary.delete-after-upload", true));
51+
add(this.delete);
52+
} else {
53+
this.add(new JLabel("Go to setting and log in to Mapillary before uploading."));
54+
}
5155
}
5256
}

src/org/openstreetmap/josm/plugins/mapillary/oauth/MapillaryUser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public static HashMap<String, String> getSecrets() {
6060
} catch (IOException e) {
6161
Main.error(e);
6262
isTokenValid = false;
63-
isTokenValid = false;
6463
}
6564
hash.put("images_hash", images_hash);
6665
if (images_policy == null)

src/org/openstreetmap/josm/plugins/mapillary/traffico/TrafficoSignElement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public TrafficoSignElement(char glyph, Color c) {
1919
* @return the color
2020
*/
2121
public Color getColor() {
22-
return color;
22+
return this.color;
2323
}
2424

2525
/**
2626
* @return the glyph
2727
*/
2828
public char getGlyph() {
29-
return glyph;
29+
return this.glyph;
3030
}
3131
}

0 commit comments

Comments
 (0)