This allows for continuous download of areas

Downloading data from OSM will trigger a download of MapWithAI data, if
continous download is enabled (per MapWithAI layer basis, don't
currently want to create another pref key).

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2020-01-13 10:42:53 -07:00
rodzic 17959a4020
commit 4b0a07bb2f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
7 zmienionych plików z 122 dodań i 8 usunięć

Wyświetl plik

@ -112,7 +112,6 @@ compile against min JOSM:
- ./gradlew compileJava_minJosm - ./gradlew compileJava_minJosm
dependencies: dependencies:
- assemble - assemble
allow_failure: true
compile against latest JOSM: compile against latest JOSM:
stage: test stage: test

Wyświetl plik

@ -127,6 +127,7 @@ jacocoTestCoverageVerification {
josm { josm {
manifest { manifest {
oldVersionDownloadLink 15542, "v1.0.3", new URL("https://gokaart.gitlab.io/JOSM_MapWithAI/dist/v1.0.3/mapwithai.jar")
oldVersionDownloadLink 15233, "v0.2.9", new URL("https://gokaart.gitlab.io/JOSM_MapWithAI/dist/v0.2.9/mapwithai.jar") oldVersionDownloadLink 15233, "v0.2.9", new URL("https://gokaart.gitlab.io/JOSM_MapWithAI/dist/v0.2.9/mapwithai.jar")
} }
i18n { i18n {

Wyświetl plik

@ -1,9 +1,9 @@
# The minimum JOSM version this plugin is compatible with (can be any numeric version # The minimum JOSM version this plugin is compatible with (can be any numeric version
plugin.main.version = 15542 plugin.main.version = 15609
# The JOSM version this plugin is currently compiled against # The JOSM version this plugin is currently compiled against
# Please make sure this version is available at https://josm.openstreetmap.de/download # Please make sure this version is available at https://josm.openstreetmap.de/download
# The special values "latest" and "tested" are also possible here, but not recommended. # The special values "latest" and "tested" are also possible here, but not recommended.
plugin.compile.version = 15603 plugin.compile.version = 15609
plugin.canloadatruntime = true plugin.canloadatruntime = true
plugin.author = Taylor Smock <incoming+gokaart/JOSM_MapWithAI@incoming.gitlab.com> plugin.author = Taylor Smock <incoming+gokaart/JOSM_MapWithAI@incoming.gitlab.com>
plugin.class = org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin plugin.class = org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin

Wyświetl plik

@ -17,7 +17,6 @@ import javax.swing.JMenu;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import org.openstreetmap.josm.actions.JosmAction; import org.openstreetmap.josm.actions.JosmAction;
import org.openstreetmap.josm.data.Version;
import org.openstreetmap.josm.data.validation.OsmValidator; import org.openstreetmap.josm.data.validation.OsmValidator;
import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.MainMenu; import org.openstreetmap.josm.gui.MainMenu;
@ -27,6 +26,7 @@ import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
import org.openstreetmap.josm.io.remotecontrol.RequestProcessor; import org.openstreetmap.josm.io.remotecontrol.RequestProcessor;
import org.openstreetmap.josm.plugins.Plugin; import org.openstreetmap.josm.plugins.Plugin;
import org.openstreetmap.josm.plugins.PluginInformation; import org.openstreetmap.josm.plugins.PluginInformation;
import org.openstreetmap.josm.plugins.mapwithai.backend.DownloadListener;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIAction; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIAction;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer;
@ -160,8 +160,7 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
destroyables.forEach(Destroyable::destroy); destroyables.forEach(Destroyable::destroy);
DownloadDialog.removeDownloadSource(mapWithAIDownloadReader); DownloadDialog.removeDownloadSource(mapWithAIDownloadReader);
if (Version.getInstance().getVersion() >= 15603) {
OsmValidator.removeTest(RoutingIslandsTest.class); OsmValidator.removeTest(RoutingIslandsTest.class);
} DownloadListener.destroyAll();
} }
} }

Wyświetl plik

@ -0,0 +1,65 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.backend;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.stream.Collectors;
import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.DataSourceChangeEvent;
import org.openstreetmap.josm.data.osm.DataSourceListener;
import org.openstreetmap.josm.data.osm.event.DataSourceAddedEvent;
import org.openstreetmap.josm.tools.Destroyable;
/**
* This class listens for download events, and then gets new data
*
* @author Taylor Smock
*
*/
public final class DownloadListener implements DataSourceListener, Destroyable {
private WeakReference<DataSet> ds;
private static final Collection<DownloadListener> LISTENERS = new HashSet<>();
public DownloadListener(DataSet dataSet) {
ds = new WeakReference<>(Objects.requireNonNull(dataSet, "DataSet cannot be null"));
ds.get().addDataSourceListener(this);
LISTENERS.add(this);
}
@Override
public void dataSourceChange(DataSourceChangeEvent event) {
if (event instanceof DataSourceAddedEvent) {
MapWithAILayer layer = MapWithAIDataUtils.getLayer(false);
if (layer == null) {
destroy();
return;
}
if (layer.downloadContinuous()) {
MapWithAIDataUtils.getMapWithAIData(layer, event.getAdded().stream().map(ev -> ev.bounds)
.map(Bounds::toBBox).collect(Collectors.toList()));
}
}
}
@Override
public void destroy() {
if (ds.get() != null) {
// Should be added, so no exception should be thrown
ds.get().removeDataSourceListener(this);
ds.clear();
LISTENERS.remove(this);
}
}
/**
* Destroy all download listeners for MapWithAI
*/
public static void destroyAll() {
LISTENERS.forEach(DownloadListener::destroy);
}
}

Wyświetl plik

@ -47,6 +47,7 @@ public class MapWithAIAction extends JosmAction {
.collect(Collectors.toList()); .collect(Collectors.toList());
final OsmDataLayer layer = getOsmLayer(osmLayers); final OsmDataLayer layer = getOsmLayer(osmLayers);
if ((layer != null) && MapWithAIDataUtils.getMapWithAIData(MapWithAIDataUtils.getLayer(true), layer)) { if ((layer != null) && MapWithAIDataUtils.getMapWithAIData(MapWithAIDataUtils.getLayer(true), layer)) {
new DownloadListener(layer.getDataSet());
final Notification notification = createMessageDialog(); final Notification notification = createMessageDialog();
if (notification != null) { if (notification != null) {
notification.show(); notification.show();

Wyświetl plik

@ -3,7 +3,10 @@ package org.openstreetmap.josm.plugins.mapwithai.backend;
import static org.openstreetmap.josm.tools.I18n.tr; import static org.openstreetmap.josm.tools.I18n.tr;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -11,8 +14,10 @@ import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.swing.AbstractAction;
import javax.swing.Action; import javax.swing.Action;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
@ -25,6 +30,7 @@ import org.openstreetmap.josm.data.osm.DownloadPolicy;
import org.openstreetmap.josm.data.osm.OsmPrimitive; import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.data.osm.UploadPolicy; import org.openstreetmap.josm.data.osm.UploadPolicy;
import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.layer.Layer;
import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent; import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener; import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
import org.openstreetmap.josm.gui.layer.OsmDataLayer; import org.openstreetmap.josm.gui.layer.OsmDataLayer;
@ -43,6 +49,7 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
private Integer maximumAddition; private Integer maximumAddition;
private String url; private String url;
private Boolean switchLayers; private Boolean switchLayers;
private boolean continuousDownload;
private final Lock lock; private final Lock lock;
/** /**
@ -114,7 +121,12 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
public Action[] getMenuEntries() { public Action[] getMenuEntries() {
final List<Action> actions = Arrays.asList(super.getMenuEntries()).stream() final List<Action> actions = Arrays.asList(super.getMenuEntries()).stream()
.filter(action -> !(action instanceof LayerSaveAction) && !(action instanceof LayerSaveAsAction)) .filter(action -> !(action instanceof LayerSaveAction) && !(action instanceof LayerSaveAsAction))
.collect(Collectors.toList()); .collect(Collectors.toCollection(ArrayList::new));
if (actions.isEmpty()) {
actions.add(new ContinuousDownloadAction(this));
} else {
actions.add(actions.size() - 2, new ContinuousDownloadAction(this));
}
return actions.toArray(new Action[0]); return actions.toArray(new Action[0]);
} }
@ -205,4 +217,41 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
SwingUtilities.invokeLater(() -> getDataSet().setSelected(selection)); SwingUtilities.invokeLater(() -> getDataSet().setSelected(selection));
} }
} }
/**
* @return {@code true} indicates that we should attempt to keep it in sync with
* the data layer(s)
*/
public boolean downloadContinuous() {
return continuousDownload;
}
private class ContinuousDownloadAction extends AbstractAction implements LayerAction {
private static final long serialVersionUID = -3528632887550700527L;
private final MapWithAILayer layer;
public ContinuousDownloadAction(MapWithAILayer layer) {
super(tr("Continuous download"));
new ImageProvider("download").getResource().attachImageIcon(this, true);
this.layer = layer;
}
@Override
public void actionPerformed(ActionEvent e) {
layer.continuousDownload = !layer.continuousDownload;
}
@Override
public boolean supportLayers(List<Layer> layers) {
return layers.stream().allMatch(MapWithAILayer.class::isInstance);
}
@Override
public Component createMenuComponent() {
JCheckBoxMenuItem item = new JCheckBoxMenuItem(this);
item.setSelected(layer.continuousDownload);
return item;
}
}
} }