Skip to content

Commit

Permalink
Fixed EDT errors and added option to delete the images after upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
nokutu committed Aug 12, 2015
1 parent f91b087 commit 21f35e9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 39 deletions.
23 changes: 16 additions & 7 deletions src/org/openstreetmap/josm/plugins/mapillary/MapillaryPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

import org.apache.commons.jcs.access.CacheAccess;
import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
Expand Down Expand Up @@ -117,10 +118,9 @@ public MapillaryPlugin(PluginInformation info) {
this.importIntoSequenceAction, false, 14);
IMPORT_MENU = MainMenu.add(Main.main.menu.fileMenu, importAction, false,
14);
UPLOAD_MENU = MainMenu.add(Main.main.menu.fileMenu, uploadAction,
false, 14);
ZOOM_MENU = MainMenu.add(Main.main.menu.viewMenu, zoomAction, false,
15);
UPLOAD_MENU = MainMenu.add(Main.main.menu.fileMenu, uploadAction, false,
14);
ZOOM_MENU = MainMenu.add(Main.main.menu.viewMenu, zoomAction, false, 15);
DOWNLOAD_VIEW_MENU = MainMenu.add(Main.main.menu.fileMenu,
this.downloadViewAction, false, 14);
JOIN_MENU = MainMenu.add(Main.main.menu.dataMenu, this.joinAction, false);
Expand Down Expand Up @@ -181,9 +181,18 @@ public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
* @param value
* true to enable the JMenuItem; false to disable it.
*/
public static void setMenuEnabled(JMenuItem menu, boolean value) {
menu.setEnabled(value);
menu.getAction().setEnabled(value);
public static void setMenuEnabled(final JMenuItem menu, final boolean value) {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
setMenuEnabled(menu, value);
}
});
} else {
menu.setEnabled(value);
menu.getAction().setEnabled(value);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void actionPerformed(ActionEvent arg0) {
&& (int) pane.getValue() == JOptionPane.OK_OPTION) {
if (dialog.sequence.isSelected()) {
UploadUtils.uploadSequence(MapillaryLayer.getInstance().getData()
.getSelectedImage().getSequence());
.getSelectedImage().getSequence(), dialog.delete.isSelected());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.openstreetmap.josm.plugins.mapillary.gui;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.plugins.mapillary.MapillaryImportedImage;
import org.openstreetmap.josm.plugins.mapillary.MapillaryLayer;

Expand All @@ -19,11 +22,15 @@ public class MapillaryUploadDialog extends JPanel {
public ButtonGroup group;
/** Upload the whole sequence. */
public JRadioButton sequence;
/** Whether the images must be deleted after upload or not */
public JCheckBox delete;

/**
* Main constructor.
*/
public MapillaryUploadDialog() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

this.group = new ButtonGroup();

this.sequence = new JRadioButton("Upload selected sequence.");
Expand All @@ -32,8 +39,12 @@ public MapillaryUploadDialog() {
this.sequence.setEnabled(false);
this.group.add(this.sequence);
add(this.sequence);

this.group.setSelected(this.sequence.getModel(), true);

this.delete = new JCheckBox("Delete after uplaod");
this.delete.setSelected(Main.pref.getBoolean(
"mapillary.delete-after-upload", true));
add(this.delete);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;

import javax.swing.SwingUtilities;

import org.openstreetmap.josm.plugins.mapillary.MapillaryAbstractImage;
import org.openstreetmap.josm.plugins.mapillary.history.commands.MapillaryCommand;
import org.openstreetmap.josm.plugins.mapillary.history.commands.MapillaryExecutableCommand;
Expand Down Expand Up @@ -69,33 +71,42 @@ public void removeListener(MapillaryRecordListener lis) {
* @param command
* The command to be added.
*/
public void addCommand(MapillaryCommand command) {
if (command instanceof MapillaryExecutableCommand)
((MapillaryExecutableCommand) command).execute();
// Checks if it is a continuation of last command
if (this.position != -1) {
boolean equalSets = true;
for (MapillaryAbstractImage img : this.commandList.get(this.position).images)
if (!command.images.contains(img))
equalSets = false;
for (MapillaryAbstractImage img : command.images)
if (!this.commandList.get(this.position).images.contains(img))
equalSets = false;
if (equalSets
&& this.commandList.get(this.position).getClass() == command
.getClass()) {
this.commandList.get(this.position).sum(command);
fireRecordChanged();
return;
public void addCommand(final MapillaryCommand command) {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
addCommand(command);
}
});
} else {
if (command instanceof MapillaryExecutableCommand)
((MapillaryExecutableCommand) command).execute();
// Checks if it is a continuation of last command
if (this.position != -1) {
boolean equalSets = true;
for (MapillaryAbstractImage img : this.commandList.get(this.position).images)
if (!command.images.contains(img))
equalSets = false;
for (MapillaryAbstractImage img : command.images)
if (!this.commandList.get(this.position).images.contains(img))
equalSets = false;
if (equalSets
&& this.commandList.get(this.position).getClass() == command
.getClass()) {
this.commandList.get(this.position).sum(command);
fireRecordChanged();
return;
}
}
// Adds the command to the last position of the list.
this.commandList.add(this.position + 1, command);
this.position++;
while (this.commandList.size() > this.position + 1) {
this.commandList.remove(this.position + 1);
}
fireRecordChanged();
}
// Adds the command to the last position of the list.
this.commandList.add(this.position + 1, command);
this.position++;
while (this.commandList.size() > this.position + 1) {
this.commandList.remove(this.position + 1);
}
fireRecordChanged();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public CommandImport(List<MapillaryAbstractImage> images) {

@Override
public void execute() {
this.redo();
MapillaryLayer.getInstance().getData().add(this.images);
}

@Override
Expand All @@ -41,7 +41,7 @@ public void undo() {

@Override
public void redo() {
MapillaryLayer.getInstance().getData().add(this.images);
this.execute();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.openstreetmap.josm.plugins.mapillary.MapillaryAbstractImage;
import org.openstreetmap.josm.plugins.mapillary.MapillaryImportedImage;
import org.openstreetmap.josm.plugins.mapillary.MapillarySequence;
import org.openstreetmap.josm.plugins.mapillary.history.MapillaryRecord;
import org.openstreetmap.josm.plugins.mapillary.history.commands.CommandDelete;
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryUtils;
import org.openstreetmap.josm.plugins.mapillary.utils.PluginState;

Expand Down Expand Up @@ -140,21 +142,26 @@ public static void uploadFile(File file, HashMap<String, String> hash)
* @param sequence
* The sequence to upload. It must contain only
* {@link MapillaryImportedImage} objects.
* @param delete
* Whether the images must be deleted after upload or not.
*/
public static void uploadSequence(MapillarySequence sequence) {
Main.worker.submit(new SequenceUploadThread(sequence.getImages()));
public static void uploadSequence(MapillarySequence sequence, boolean delete) {
Main.worker.submit(new SequenceUploadThread(sequence.getImages(), delete));
}

private static class SequenceUploadThread extends Thread {
private List<MapillaryAbstractImage> images;
private UUID uuid;
private boolean delete;
ThreadPoolExecutor ex;

private SequenceUploadThread(List<MapillaryAbstractImage> images) {
private SequenceUploadThread(List<MapillaryAbstractImage> images,
boolean delete) {
this.images = images;
this.uuid = UUID.randomUUID();
this.ex = new ThreadPoolExecutor(8, 8, 25, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(15));
this.delete = delete;
}

@Override
Expand All @@ -175,6 +182,14 @@ public void run() {
}
}
this.ex.shutdown();
try {
this.ex.awaitTermination(15, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Main.error(e);
}
System.out.println(this.images.size());
if (this.delete)
MapillaryRecord.getInstance().addCommand(new CommandDelete(this.images));
}
}

Expand Down

0 comments on commit 21f35e9

Please sign in to comment.