Add upload hook to add changeset tags

* Also remove default keys from preferences

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2019-10-16 17:38:58 -06:00
rodzic 44de005f16
commit 93246042a9
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
6 zmienionych plików z 88 dodań i 5 usunięć

Wyświetl plik

@ -202,7 +202,8 @@ publish to / GitLab.com packages:
name: GitLab.com / JOSM-MapWithAI packages name: GitLab.com / JOSM-MapWithAI packages
url: https://gitlab.com/gokaart/JOSM_MapWithAI/-/packages url: https://gitlab.com/gokaart/JOSM_MapWithAI/-/packages
script: script:
- ./gradlew publishPluginPublicationToGitlabRepository publishAllPublicationsToGitlabRepository releaseToGitlab - ./gradlew publishAllPublicationsToGitlabRepository
- ./gradlew releaseToGitlab
dependencies: dependencies:
- build - build
only: only:

Wyświetl plik

@ -14,6 +14,7 @@ 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.actions.UploadAction;
import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.MainMenu; import org.openstreetmap.josm.gui.MainMenu;
import org.openstreetmap.josm.gui.preferences.PreferenceSetting; import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
@ -26,6 +27,7 @@ import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIMoveAction; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIMoveAction;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIRemoteControl; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIRemoteControl;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIUploadHook;
import org.openstreetmap.josm.tools.Destroyable; import org.openstreetmap.josm.tools.Destroyable;
import org.openstreetmap.josm.tools.Logging; import org.openstreetmap.josm.tools.Logging;
@ -34,6 +36,8 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
public static final String NAME = "MapWithAI"; public static final String NAME = "MapWithAI";
private static String versionInfo; private static String versionInfo;
private final MapWithAIUploadHook uploadHook;
private final PreferenceSetting preferenceSetting; private final PreferenceSetting preferenceSetting;
private static final Map<Class<? extends JosmAction>, Boolean> MENU_ENTRIES = new LinkedHashMap<>(); private static final Map<Class<? extends JosmAction>, Boolean> MENU_ENTRIES = new LinkedHashMap<>();
@ -47,6 +51,7 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
super(info); super(info);
preferenceSetting = new MapWithAIPreferences(); preferenceSetting = new MapWithAIPreferences();
uploadHook = new MapWithAIUploadHook(info);
final JMenu dataMenu = MainApplication.getMenu().dataMenu; final JMenu dataMenu = MainApplication.getMenu().dataMenu;
for (final Entry<Class<? extends JosmAction>, Boolean> entry : MENU_ENTRIES.entrySet()) { for (final Entry<Class<? extends JosmAction>, Boolean> entry : MENU_ENTRIES.entrySet()) {
@ -70,6 +75,8 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
MapWithAIDataUtils.addMapWithAIPaintStyles(); MapWithAIDataUtils.addMapWithAIPaintStyles();
UploadAction.registerUploadHook(uploadHook);
setVersionInfo(info.localversion); setVersionInfo(info.localversion);
RequestProcessor.addRequestHandlerClass("mapwithai", MapWithAIRemoteControl.class); RequestProcessor.addRequestHandlerClass("mapwithai", MapWithAIRemoteControl.class);
} }
@ -90,6 +97,11 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
versionInfo = newVersionInfo; versionInfo = newVersionInfo;
} }
/**
* This is so that if JOSM ever decides to support updating plugins without
* restarting, I don't have to do anything (hopefully -- I might have to change
* the interface and method). Not currently used... (October 16, 2019)
*/
@Override @Override
public void destroy() { public void destroy() {
final JMenu dataMenu = MainApplication.getMenu().dataMenu; final JMenu dataMenu = MainApplication.getMenu().dataMenu;
@ -101,6 +113,7 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
dataMenu.remove(action.getValue()); dataMenu.remove(action.getValue());
} }
} }
UploadAction.unregisterUploadHook(uploadHook);
MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).stream() MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).stream()
.forEach(layer -> MainApplication.getLayerManager().removeLayer(layer)); .forEach(layer -> MainApplication.getLayerManager().removeLayer(layer));

Wyświetl plik

@ -50,6 +50,7 @@ import org.openstreetmap.josm.tools.Logging;
public final class MapWithAIDataUtils { public final class MapWithAIDataUtils {
public static final String DEFAULT_MAPWITHAI_API = "https://www.facebook.com/maps/ml_roads?conflate_with_osm=true&theme=ml_road_vector&collaborator=josm&token=ASb3N5o9HbX8QWn8G_NtHIRQaYv3nuG2r7_f3vnGld3KhZNCxg57IsaQyssIaEw5rfRNsPpMwg4TsnrSJtIJms5m&hash=ASawRla3rBcwEjY4HIY&result_type=road_building_vector_xml&bbox={bbox}"; public static final String DEFAULT_MAPWITHAI_API = "https://www.facebook.com/maps/ml_roads?conflate_with_osm=true&theme=ml_road_vector&collaborator=josm&token=ASb3N5o9HbX8QWn8G_NtHIRQaYv3nuG2r7_f3vnGld3KhZNCxg57IsaQyssIaEw5rfRNsPpMwg4TsnrSJtIJms5m&hash=ASawRla3rBcwEjY4HIY&result_type=road_building_vector_xml&bbox={bbox}";
public static final int MAXIMUM_SIDE_DIMENSIONS = 1000; // 1 km public static final int MAXIMUM_SIDE_DIMENSIONS = 1000; // 1 km
private static final int DEFAULT_MAXIMUM_ADDITION = 5;
static final Object LAYER_LOCK = new Object(); static final Object LAYER_LOCK = new Object();
@ -293,6 +294,9 @@ public final class MapWithAIDataUtils {
urls.add(url); urls.add(url);
setMapWithAIURLs(urls); setMapWithAIURLs(urls);
} }
if (DEFAULT_MAPWITHAI_API.equals(url)) {
url = "";
}
Config.getPref().put(MapWithAIPlugin.NAME.concat(".current_api"), url); Config.getPref().put(MapWithAIPlugin.NAME.concat(".current_api"), url);
} else if (layer != null) { } else if (layer != null) {
layer.setMapWithAIUrl(url); layer.setMapWithAIUrl(url);
@ -345,7 +349,11 @@ public final class MapWithAIDataUtils {
public static void setSwitchLayers(boolean selected, boolean permanent) { public static void setSwitchLayers(boolean selected, boolean permanent) {
final MapWithAILayer layer = getLayer(false); final MapWithAILayer layer = getLayer(false);
if (permanent) { if (permanent) {
Config.getPref().putBoolean(MapWithAIPlugin.NAME.concat(".autoswitchlayers"), selected); if (!selected) {
Config.getPref().putBoolean(MapWithAIPlugin.NAME.concat(".autoswitchlayers"), selected);
} else {
Config.getPref().put(MapWithAIPlugin.NAME.concat(".autoswitchlayers"), null);
}
} else if (layer != null) { } else if (layer != null) {
layer.setSwitchLayers(selected); layer.setSwitchLayers(selected);
} }
@ -370,13 +378,18 @@ public final class MapWithAIDataUtils {
*/ */
public static int getMaximumAddition() { public static int getMaximumAddition() {
final MapWithAILayer mapWithAILayer = MapWithAIDataUtils.getLayer(false); final MapWithAILayer mapWithAILayer = MapWithAIDataUtils.getLayer(false);
Integer defaultReturn = Config.getPref().getInt(MapWithAIPlugin.NAME.concat(".maximumselection"), 5); Integer defaultReturn = Config.getPref().getInt(MapWithAIPlugin.NAME.concat(".maximumselection"),
getDefaultMaximumAddition());
if (mapWithAILayer != null && mapWithAILayer.getMaximumAddition() != null) { if (mapWithAILayer != null && mapWithAILayer.getMaximumAddition() != null) {
defaultReturn = mapWithAILayer.getMaximumAddition(); defaultReturn = mapWithAILayer.getMaximumAddition();
} }
return defaultReturn; return defaultReturn;
} }
public static int getDefaultMaximumAddition() {
return DEFAULT_MAXIMUM_ADDITION;
}
/** /**
* Set the maximum number of objects that can be added at one time. * Set the maximum number of objects that can be added at one time.
* *
@ -388,7 +401,11 @@ public final class MapWithAIDataUtils {
public static void setMaximumAddition(int max, boolean permanent) { public static void setMaximumAddition(int max, boolean permanent) {
final MapWithAILayer mapWithAILayer = getLayer(false); final MapWithAILayer mapWithAILayer = getLayer(false);
if (permanent) { if (permanent) {
Config.getPref().putInt(MapWithAIPlugin.NAME.concat(".maximumselection"), max); if (getDefaultMaximumAddition() != max) {
Config.getPref().putInt(MapWithAIPlugin.NAME.concat(".maximumselection"), max);
} else {
Config.getPref().put(MapWithAIPlugin.NAME.concat(".maximumselection"), null);
}
} else if (mapWithAILayer != null) { } else if (mapWithAILayer != null) {
mapWithAILayer.setMaximumAddition(max); mapWithAILayer.setMaximumAddition(max);
} }

Wyświetl plik

@ -0,0 +1,48 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.backend;
import java.util.Map;
import org.openstreetmap.josm.actions.upload.UploadHook;
import org.openstreetmap.josm.data.UndoRedoHandler;
import org.openstreetmap.josm.plugins.PluginInformation;
import org.openstreetmap.josm.plugins.mapwithai.commands.MapWithAIAddCommand;
/**
* @author Taylor Smock
*
*/
public class MapWithAIUploadHook implements UploadHook {
private final String version;
public MapWithAIUploadHook(PluginInformation info) {
version = info.localversion;
}
@Override
public void modifyChangesetTags(Map<String, String> tags) {
final Integer addedObjects = getAddedObjects();
if (addedObjects != 0) {
tags.put("mapwithai", addedObjects.toString());
final StringBuilder sb = new StringBuilder();
sb.append("version=").append(version);
if (MapWithAIDataUtils.getMaximumAddition() != MapWithAIDataUtils.getDefaultMaximumAddition()) {
sb.append(";maxadd=").append(MapWithAIDataUtils.getMaximumAddition());
}
if (!MapWithAIDataUtils.getMapWithAIUrl().equalsIgnoreCase(MapWithAIDataUtils.DEFAULT_MAPWITHAI_API)) {
sb.append(";url=").append(MapWithAIDataUtils.getMapWithAIUrl());
}
if (DetectTaskingManagerUtils.hasTaskingManagerLayer()) {
sb.append(";task=").append(DetectTaskingManagerUtils.getTaskingManagerBBox().toStringCSV(","));
}
tags.put("mapwithai:options", sb.toString());
}
}
private static int getAddedObjects() {
return UndoRedoHandler.getInstance().getUndoCommands().parallelStream()
.filter(object -> object instanceof MapWithAIAddCommand).map(object -> (MapWithAIAddCommand) object)
.mapToInt(MapWithAIAddCommand::getAddedObjects).sum();
}
}

Wyświetl plik

@ -131,4 +131,8 @@ public class MapWithAIAddCommand extends Command implements Runnable {
Collection<OsmPrimitive> added) { Collection<OsmPrimitive> added) {
modified.addAll(primitives); modified.addAll(primitives);
} }
public int getAddedObjects() {
return primitives.size();
}
} }

Wyświetl plik

@ -24,7 +24,7 @@ public class MapWithAIAvailabilityTest {
@Rule @Rule
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
public JOSMTestRules test = new JOSMTestRules(); public JOSMTestRules test = new JOSMTestRules().projection();
@Rule @Rule
public WireMockRule wireMockRule = new WireMockRule(options().usingFilesUnderDirectory("test/resources/wiremock")); public WireMockRule wireMockRule = new WireMockRule(options().usingFilesUnderDirectory("test/resources/wiremock"));