kopia lustrzana https://github.com/JOSM/MapWithAI
Switch to new MapWithAI source information panel
Signed-off-by: Taylor Smock <taylor.smock@kaart.com>pull/1/head
rodzic
dc09f38ae6
commit
65ea265f7b
|
@ -4,9 +4,6 @@ package org.openstreetmap.josm.plugins.mapwithai.backend;
|
|||
import static org.openstreetmap.josm.tools.I18n.tr;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.openstreetmap.josm.data.Bounds;
|
||||
import org.openstreetmap.josm.data.osm.DataSet;
|
||||
|
@ -14,6 +11,7 @@ import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
|||
import org.openstreetmap.josm.io.BoundingBoxDownloader;
|
||||
import org.openstreetmap.josm.io.IllegalDataException;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.tools.HttpClient;
|
||||
|
||||
public class BoundingBoxMapWithAIDownloader extends BoundingBoxDownloader {
|
||||
|
@ -21,12 +19,14 @@ public class BoundingBoxMapWithAIDownloader extends BoundingBoxDownloader {
|
|||
private final boolean crop;
|
||||
|
||||
private final Bounds downloadArea;
|
||||
private MapWithAIInfo info;
|
||||
|
||||
private static final int DEFAULT_TIMEOUT = 50_000; // 50 seconds
|
||||
|
||||
public BoundingBoxMapWithAIDownloader(Bounds downloadArea, String url, boolean crop) {
|
||||
public BoundingBoxMapWithAIDownloader(Bounds downloadArea, MapWithAIInfo info, boolean crop) {
|
||||
super(downloadArea);
|
||||
this.url = url;
|
||||
this.info = info;
|
||||
this.url = info.getUrlExpanded();
|
||||
this.crop = crop;
|
||||
this.downloadArea = downloadArea;
|
||||
}
|
||||
|
@ -40,27 +40,15 @@ public class BoundingBoxMapWithAIDownloader extends BoundingBoxDownloader {
|
|||
@Override
|
||||
protected DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
|
||||
DataSet ds = OsmReaderCustom.parseDataSet(source, progressMonitor, true);
|
||||
if (url != null) {
|
||||
GetDataRunnable.addMapWithAISourceTag(ds, getSourceTag(url));
|
||||
if (url != null && info.getUrl() != null && !info.getUrl().trim().isEmpty()) {
|
||||
GetDataRunnable.addMapWithAISourceTag(ds, getSourceTag(info));
|
||||
}
|
||||
GetDataRunnable.cleanup(ds, downloadArea);
|
||||
return ds;
|
||||
}
|
||||
|
||||
private static String getSourceTag(String url) {
|
||||
List<Map<String, String>> possible = MapWithAIPreferenceHelper.getMapWithAIUrl().stream()
|
||||
.filter(map -> url.contains(map.getOrDefault("url", null))).collect(Collectors.toList());
|
||||
Map<String, String> probable = null;
|
||||
long max = 0;
|
||||
for (Map<String, String> check : possible) {
|
||||
List<String> parameters = MapWithAIDataUtils.parseParameters(check.get("parameters"));
|
||||
long count = parameters.parallelStream().filter(url::contains).count();
|
||||
if (count > max || probable == null) {
|
||||
max = count;
|
||||
probable = check;
|
||||
}
|
||||
}
|
||||
return probable == null ? MapWithAIPlugin.NAME : probable.getOrDefault("source", MapWithAIPlugin.NAME);
|
||||
private static String getSourceTag(MapWithAIInfo info) {
|
||||
return info.getName() == null ? MapWithAIPlugin.NAME : info.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -22,7 +22,6 @@ import javax.json.stream.JsonParser;
|
|||
import org.openstreetmap.josm.data.coor.LatLon;
|
||||
import org.openstreetmap.josm.data.osm.BBox;
|
||||
import org.openstreetmap.josm.io.CachedFile;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation.DataUrl;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
import org.openstreetmap.josm.tools.Territories;
|
||||
import org.openstreetmap.josm.tools.Utils;
|
||||
|
@ -64,28 +63,6 @@ public class DataAvailability {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate preferences with the default values
|
||||
*/
|
||||
public static void populatePreferences() {
|
||||
try (CachedFile jsonFile = new CachedFile(DEFAULT_SERVER_URL);
|
||||
JsonParser jsonParser = Json.createParser(jsonFile.getContentReader());) {
|
||||
jsonFile.setMaxAge(604_800);
|
||||
jsonParser.next();
|
||||
JsonObject jsonObject = jsonParser.getObject();
|
||||
for (Entry<String, JsonValue> entry : jsonObject.entrySet()) {
|
||||
JsonObject json = entry.getValue().asJsonObject();
|
||||
JsonValue parameters = json.getJsonArray("parameters");
|
||||
boolean enabled = json.getBoolean("default", false);
|
||||
DataUrl url = new DataUrl(entry.getKey(), json.getString("url", ""), enabled,
|
||||
parameters == null ? "[]" : parameters.toString());
|
||||
MapWithAIPreferenceHelper.setMapWithAIUrl(url, enabled, true);
|
||||
}
|
||||
} catch (JsonException | IOException e) {
|
||||
Logging.debug(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the class
|
||||
*/
|
||||
|
@ -95,11 +72,7 @@ public class DataAvailability {
|
|||
jsonFile.setMaxAge(604_800);
|
||||
jsonParser.next();
|
||||
JsonObject jsonObject = jsonParser.getObject();
|
||||
boolean initializePreferences = MapWithAIPreferenceHelper.getMapWithAIUrl().isEmpty();
|
||||
for (Entry<String, JsonValue> entry : jsonObject.entrySet()) {
|
||||
if (initializePreferences) {
|
||||
populatePreferences();
|
||||
}
|
||||
Logging.debug("{0}: {1}", entry.getKey(), entry.getValue());
|
||||
if (JsonValue.ValueType.OBJECT.equals(entry.getValue().getValueType())
|
||||
&& entry.getValue().asJsonObject().containsKey("countries")) {
|
||||
|
|
|
@ -41,8 +41,8 @@ import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
|||
import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
||||
import org.openstreetmap.josm.io.OsmTransferException;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation.DataUrl;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.commands.MergeDuplicateWays;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.tools.Geometry;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
import org.openstreetmap.josm.tools.Pair;
|
||||
|
@ -431,21 +431,13 @@ public class GetDataRunnable extends RecursiveTask<DataSet> {
|
|||
*/
|
||||
private static DataSet getDataReal(BBox bbox, ProgressMonitor monitor) {
|
||||
final DataSet dataSet = new DataSet();
|
||||
final List<Map<String, String>> urlMaps = MapWithAIPreferenceHelper.getMapWithAIUrl().stream().map(TreeMap::new)
|
||||
.collect(Collectors.toList());
|
||||
if (DetectTaskingManagerUtils.hasTaskingManagerLayer()) {
|
||||
urlMaps.forEach(map -> map.put("url", map.get("url").concat("&crop_bbox={crop_bbox}")));
|
||||
}
|
||||
|
||||
urlMaps.parallelStream().filter(map -> map.containsKey("parameters")).forEach(DataUrl::addUrlParameters);
|
||||
|
||||
dataSet.setUploadPolicy(UploadPolicy.DISCOURAGED);
|
||||
|
||||
urlMaps.parallelStream().forEach(map -> {
|
||||
MapWithAILayerInfo.instance.getLayers().parallelStream().forEach(map -> {
|
||||
try {
|
||||
Bounds bound = new Bounds(bbox.getBottomRight());
|
||||
bound.extend(bbox.getTopLeft());
|
||||
BoundingBoxMapWithAIDownloader downloader = new BoundingBoxMapWithAIDownloader(bound, map.get("url"),
|
||||
BoundingBoxMapWithAIDownloader downloader = new BoundingBoxMapWithAIDownloader(bound, map,
|
||||
DetectTaskingManagerUtils.hasTaskingManagerLayer());
|
||||
dataSet.mergeFrom(downloader.parseOsm(monitor));
|
||||
} catch (OsmTransferException e1) {
|
||||
|
|
|
@ -9,14 +9,12 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.json.JsonObject;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
|
@ -39,8 +37,8 @@ import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
|||
import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
|
||||
import org.openstreetmap.josm.io.OsmTransferException;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation.DataUrl;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.commands.MapWithAIAddCommand;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
import org.openstreetmap.josm.tools.Utils;
|
||||
|
||||
|
@ -166,31 +164,28 @@ public final class MapWithAIDataUtils {
|
|||
final List<Bounds> realBounds = realBBoxes.stream()
|
||||
.flatMap(tBBox -> MapWithAIDataUtils.reduceBBoxSize(tBBox).stream())
|
||||
.map(MapWithAIDataUtils::bboxToBounds).collect(Collectors.toList());
|
||||
if (MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream()
|
||||
.anyMatch(map -> Boolean.valueOf(map.getOrDefault("enabled", "false")))) {
|
||||
if (!MapWithAILayerInfo.instance.getLayers().isEmpty()) {
|
||||
if ((realBBoxes.size() < TOO_MANY_BBOXES) || confirmBigDownload(realBBoxes)) {
|
||||
final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor();
|
||||
monitor.beginTask(tr("Downloading {0} Data", MapWithAIPlugin.NAME), realBounds.size());
|
||||
realBounds.parallelStream()
|
||||
.forEach(bound -> MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream()
|
||||
.filter(map -> map.containsKey("url")).map(MapWithAIDataUtils::getUrl)
|
||||
.filter(string -> !string.trim().isEmpty()).forEach(url -> {
|
||||
BoundingBoxMapWithAIDownloader downloader = new BoundingBoxMapWithAIDownloader(
|
||||
bound, url, DetectTaskingManagerUtils.hasTaskingManagerLayer());
|
||||
try {
|
||||
DataSet ds = downloader.parseOsm(monitor.createSubTaskMonitor(1, false));
|
||||
synchronized (MapWithAIDataUtils.class) {
|
||||
dataSet.mergeFrom(ds);
|
||||
}
|
||||
} catch (OsmTransferException e) {
|
||||
Logging.error(e);
|
||||
}
|
||||
}));
|
||||
realBounds.parallelStream().forEach(bound -> MapWithAIPreferenceHelper.getMapWithAIUrl()
|
||||
.parallelStream().filter(i -> i.getUrl() != null && !i.getUrl().trim().isEmpty()).forEach(i -> {
|
||||
BoundingBoxMapWithAIDownloader downloader = new BoundingBoxMapWithAIDownloader(bound, i,
|
||||
DetectTaskingManagerUtils.hasTaskingManagerLayer());
|
||||
try {
|
||||
DataSet ds = downloader.parseOsm(monitor.createSubTaskMonitor(1, false));
|
||||
synchronized (MapWithAIDataUtils.class) {
|
||||
dataSet.mergeFrom(ds);
|
||||
}
|
||||
} catch (OsmTransferException e) {
|
||||
Logging.error(e);
|
||||
}
|
||||
}));
|
||||
monitor.finishTask();
|
||||
monitor.close();
|
||||
}
|
||||
} else {
|
||||
final Notification noUrls = MapWithAIPreferenceHelper.getMapWithAIURLs().isEmpty()
|
||||
final Notification noUrls = MapWithAIPreferenceHelper.getMapWithAIUrl().isEmpty()
|
||||
? new Notification(tr("There are no defined URLs. To get the defaults, restart JOSM"))
|
||||
: new Notification(tr("No URLS are enabled"));
|
||||
noUrls.setDuration(Notification.TIME_DEFAULT);
|
||||
|
@ -240,33 +235,6 @@ public final class MapWithAIDataUtils {
|
|||
}
|
||||
}
|
||||
|
||||
private static String getUrl(Map<String, String> urlInformation) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (urlInformation.containsKey("url")) {
|
||||
sb.append(urlInformation.get("url"));
|
||||
if (urlInformation.containsKey("parameters")) {
|
||||
List<String> parameters = parseParameters(urlInformation.get("parameters"));
|
||||
if (!parameters.isEmpty()) {
|
||||
sb.append('&').append(String.join("&", parameters));
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a parameters string and return relevant parameters
|
||||
*
|
||||
* @param parameters A JSON string of parameters
|
||||
* @return Enabled parameters (list thereof)
|
||||
*/
|
||||
public static List<String> parseParameters(String parameters) {
|
||||
return DataUrl.readJsonStringArraySimple(parameters).parallelStream().filter(JsonObject.class::isInstance)
|
||||
.map(JsonObject.class::cast).filter(map -> map.getBoolean("enabled", false))
|
||||
.filter(map -> map.containsKey("parameter")).map(map -> map.getString("parameter"))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static Bounds bboxToBounds(BBox bbox) {
|
||||
Bounds bound = new Bounds(bbox.getBottomRight());
|
||||
bound.extend(bbox.getTopLeft());
|
||||
|
|
|
@ -37,6 +37,7 @@ import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|||
import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
||||
import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.spi.preferences.Config;
|
||||
import org.openstreetmap.josm.tools.GBC;
|
||||
import org.openstreetmap.josm.tools.ImageProvider;
|
||||
|
@ -47,7 +48,7 @@ import org.openstreetmap.josm.tools.ImageProvider;
|
|||
*/
|
||||
public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeListener {
|
||||
private Integer maximumAddition;
|
||||
private String url;
|
||||
private MapWithAIInfo url;
|
||||
private Boolean switchLayers;
|
||||
private boolean continuousDownload;
|
||||
private final Lock lock;
|
||||
|
@ -81,11 +82,11 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
|
|||
return maximumAddition;
|
||||
}
|
||||
|
||||
public void setMapWithAIUrl(String url) {
|
||||
this.url = url;
|
||||
public void setMapWithAIUrl(MapWithAIInfo info) {
|
||||
this.url = info;
|
||||
}
|
||||
|
||||
public String getMapWithAIUrl() {
|
||||
public MapWithAIInfo getMapWithAIUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
|
@ -107,7 +108,8 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
|
|||
GBC.eop().insets(15, 0, 0, 0));
|
||||
}
|
||||
if (url != null) {
|
||||
panel.add(new JLabel(tr("URL: {0}", url), SwingConstants.HORIZONTAL), GBC.eop().insets(15, 0, 0, 0));
|
||||
panel.add(new JLabel(tr("URL: {0}", url.getUrlExpanded()), SwingConstants.HORIZONTAL),
|
||||
GBC.eop().insets(15, 0, 0, 0));
|
||||
}
|
||||
if (switchLayers != null) {
|
||||
panel.add(new JLabel(tr("Switch Layers: {0}", switchLayers), SwingConstants.HORIZONTAL),
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
// License: GPL. For details, see LICENSE file.
|
||||
package org.openstreetmap.josm.plugins.mapwithai.backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.openstreetmap.josm.data.osm.Tag;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation.DataUrl;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.spi.preferences.Config;
|
||||
|
||||
public final class MapWithAIPreferenceHelper {
|
||||
|
@ -27,12 +26,6 @@ public final class MapWithAIPreferenceHelper {
|
|||
private static final String AUTOSWITCHLAYERS = MapWithAIPlugin.NAME.concat(".autoswitchlayers");
|
||||
private static final String MERGEBUILDINGADDRESSES = MapWithAIPlugin.NAME.concat(".mergebuildingaddresses");
|
||||
private static final String MAXIMUMSELECTION = MapWithAIPlugin.NAME.concat(".maximumselection");
|
||||
private static final String API_CONFIG = MapWithAIPlugin.NAME.concat(".apis");
|
||||
private static final String API_MAP_CONFIG = API_CONFIG.concat("map");
|
||||
private static final String URL_STRING = "url";
|
||||
private static final String SOURCE_STRING = "source";
|
||||
private static final String ENABLED_STRING = "enabled";
|
||||
private static final String PARAMETERS_STRING = "parameters";
|
||||
|
||||
private MapWithAIPreferenceHelper() {
|
||||
// Hide the constructor
|
||||
|
@ -51,55 +44,11 @@ public final class MapWithAIPreferenceHelper {
|
|||
* @return A list of enabled MapWithAI urls (maps have source, parameters,
|
||||
* enabled, and the url)
|
||||
*/
|
||||
public static List<Map<String, String>> getMapWithAIUrl() {
|
||||
final MapWithAILayer layer = MapWithAIDataUtils.getLayer(false);
|
||||
return (layer != null) && (layer.getMapWithAIUrl() != null)
|
||||
? getMapWithAIURLs().parallelStream().filter(map -> layer.getMapWithAIUrl().equals(map.get(URL_STRING)))
|
||||
.collect(Collectors.toList())
|
||||
: getMapWithAIURLs().stream()
|
||||
.filter(map -> Boolean.valueOf(map.getOrDefault(ENABLED_STRING, Boolean.FALSE.toString())))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the MapWithAI urls (or the default)
|
||||
*
|
||||
* @return The urls for MapWithAI endpoints (maps have source, parameters,
|
||||
* enabled, and the url)
|
||||
*/
|
||||
public static List<Map<String, String>> getMapWithAIURLs() {
|
||||
final List<Map<String, String>> returnMap = Config.getPref().getListOfMaps(API_MAP_CONFIG, new ArrayList<>())
|
||||
.stream().map(TreeMap::new).collect(Collectors.toList());
|
||||
if (MapWithAIDataUtils.getLayer(false) != null) {
|
||||
TreeMap<String, String> layerMap = new TreeMap<>();
|
||||
layerMap.put("url", MapWithAIDataUtils.getLayer(false).getMapWithAIUrl());
|
||||
if (layerMap.get("url") != null && !layerMap.get("url").trim().isEmpty() && returnMap.parallelStream()
|
||||
.noneMatch(map -> map.getOrDefault("url", "").equals(layerMap.get("url")))) {
|
||||
returnMap.add(layerMap);
|
||||
}
|
||||
}
|
||||
if (returnMap.isEmpty()) {
|
||||
final List<String> defaultAPIs = Collections.singletonList(DEFAULT_MAPWITHAI_API);
|
||||
final List<String> defaultList = Config.getPref().getList(API_CONFIG).isEmpty() ? defaultAPIs
|
||||
: Config.getPref().getList(API_CONFIG);
|
||||
returnMap.addAll(defaultList.stream().map(string -> {
|
||||
final TreeMap<String, String> map = new TreeMap<>();
|
||||
map.put(URL_STRING, string);
|
||||
return map;
|
||||
}).collect(Collectors.toList()));
|
||||
}
|
||||
returnMap.parallelStream().forEach(map -> {
|
||||
final String url = map.get(URL_STRING);
|
||||
if (DEFAULT_MAPWITHAI_API.equals(url)) {
|
||||
map.putIfAbsent(SOURCE_STRING, MapWithAIPlugin.NAME);
|
||||
map.putIfAbsent(ENABLED_STRING, "true");
|
||||
map.putIfAbsent(PARAMETERS_STRING, DEFAULT_MAPWITHAI_API_PARAMETERS);
|
||||
} else {
|
||||
map.putIfAbsent(SOURCE_STRING, url);
|
||||
map.putIfAbsent(ENABLED_STRING, Boolean.FALSE.toString());
|
||||
}
|
||||
});
|
||||
return returnMap;
|
||||
public static List<MapWithAIInfo> getMapWithAIUrl() {
|
||||
return MapWithAIDataUtils.getLayer(false) == null
|
||||
|| MapWithAIDataUtils.getLayer(false).getMapWithAIUrl() == null
|
||||
? MapWithAILayerInfo.instance.getLayers()
|
||||
: Collections.singletonList(MapWithAIDataUtils.getLayer(false).getMapWithAIUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -136,60 +85,13 @@ public final class MapWithAIPreferenceHelper {
|
|||
return returnBoolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the MapWithAI url
|
||||
*
|
||||
* @param source The source tag for the url
|
||||
* @param url The url to set as the default
|
||||
* @param enabled {@code true} if the url should be used for downloads
|
||||
* @param permanent {@code true} if we want the setting to persist between
|
||||
* sessions
|
||||
*/
|
||||
public static void setMapWithAIUrl(String source, String url, boolean enabled, boolean permanent) {
|
||||
setMapWithAIUrl(new DataUrl(source, url, permanent), enabled, permanent);
|
||||
}
|
||||
|
||||
public static void setMapWithAIUrl(DataUrl dataUrl, boolean enabled, boolean permanent) {
|
||||
final MapWithAILayer layer = MapWithAIDataUtils.getLayer(false);
|
||||
final String setUrl = dataUrl.getMap().getOrDefault("url", DEFAULT_MAPWITHAI_API);
|
||||
|
||||
if (permanent) {
|
||||
final List<Map<String, String>> urls = new ArrayList<>(getMapWithAIURLs());
|
||||
Map<String, String> addOrModifyMap = urls.parallelStream()
|
||||
.filter(map -> map.getOrDefault(URL_STRING, "").equals(setUrl)).findFirst().orElse(new TreeMap<>());
|
||||
if (addOrModifyMap.isEmpty()) {
|
||||
urls.add(addOrModifyMap);
|
||||
} else {
|
||||
urls.remove(addOrModifyMap);
|
||||
addOrModifyMap = new TreeMap<>(addOrModifyMap);
|
||||
urls.add(addOrModifyMap);
|
||||
}
|
||||
addOrModifyMap.putAll(dataUrl.getMap());
|
||||
addOrModifyMap.put(ENABLED_STRING, Boolean.toString(enabled));
|
||||
setMapWithAIURLs(urls);
|
||||
public static void setMapWithAIUrl(MapWithAIInfo info, boolean enabled, boolean permanent) {
|
||||
if (permanent && enabled) {
|
||||
MapWithAILayerInfo.instance.add(info);
|
||||
MapWithAILayerInfo.instance.save();
|
||||
} else if (enabled && MapWithAIDataUtils.getLayer(false) != null) {
|
||||
MapWithAIDataUtils.getLayer(false).setMapWithAIUrl(info);
|
||||
}
|
||||
if (layer != null && !permanent && enabled) {
|
||||
layer.setMapWithAIUrl(setUrl);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the MapWithAI urls
|
||||
*
|
||||
* @param urls A list of URLs
|
||||
* @return true if the configuration changed
|
||||
*/
|
||||
public static boolean setMapWithAIURLs(List<Map<String, String>> urls) {
|
||||
final List<Map<String, String>> setUrls = urls.isEmpty() ? new ArrayList<>() : urls;
|
||||
if (urls.isEmpty()) {
|
||||
final TreeMap<String, String> defaultAPIMap = new TreeMap<>();
|
||||
defaultAPIMap.put(URL_STRING, DEFAULT_MAPWITHAI_API);
|
||||
defaultAPIMap.put(ENABLED_STRING, Boolean.TRUE.toString());
|
||||
defaultAPIMap.put(SOURCE_STRING, MapWithAIPlugin.NAME);
|
||||
defaultAPIMap.put(PARAMETERS_STRING, DEFAULT_MAPWITHAI_API_PARAMETERS);
|
||||
setUrls.add(defaultAPIMap);
|
||||
}
|
||||
return Config.getPref().putListOfMaps(API_MAP_CONFIG, setUrls);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -277,4 +179,5 @@ public final class MapWithAIPreferenceHelper {
|
|||
: Arrays.asList(new TreeMap<>(tagsToReplace));
|
||||
Config.getPref().putListOfMaps(MapWithAIPlugin.NAME.concat(".replacementtags"), tags);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|||
import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
||||
import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
|
||||
public class MapWithAIRemoteControl extends RequestHandler.RawURLParseRequestHandler {
|
||||
|
||||
|
@ -100,7 +101,8 @@ public class MapWithAIRemoteControl extends RequestHandler.RawURLParseRequestHan
|
|||
if (url != null) {
|
||||
// TODO make option for permanent url
|
||||
String tSource = source == null ? url : source;
|
||||
MapWithAIPreferenceHelper.setMapWithAIUrl(tSource, url, true, false);
|
||||
MapWithAIInfo info = new MapWithAIInfo(tSource, url);
|
||||
MapWithAIPreferenceHelper.setMapWithAIUrl(info, true, false);
|
||||
}
|
||||
if (switchLayer != null) {
|
||||
MapWithAIPreferenceHelper.setSwitchLayers(switchLayer, false);
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
package org.openstreetmap.josm.plugins.mapwithai.backend;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.openstreetmap.josm.actions.UploadAction;
|
||||
|
@ -35,13 +36,12 @@ public class MapWithAIUploadHook implements UploadHook, Destroyable {
|
|||
if (DetectTaskingManagerUtils.hasTaskingManagerLayer()) {
|
||||
sb.append(";task=").append(DetectTaskingManagerUtils.getTaskingManagerBBox().toStringCSV(","));
|
||||
}
|
||||
if (MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream().anyMatch(
|
||||
map -> !MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API.equalsIgnoreCase(map.get("url")))) {
|
||||
sb.append(";url=")
|
||||
.append(String.join(";url=", MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream()
|
||||
.filter(map -> map.containsKey("url")).map(map -> map.get("url"))
|
||||
.map(url -> url.replace(MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API, "DEFAULT_URL"))
|
||||
.filter(url -> !"DEFAULT_URL".equals(url)).collect(Collectors.toList())));
|
||||
if (!MapWithAIPreferenceHelper.getMapWithAIUrl().isEmpty()) {
|
||||
sb.append(";url_ids=")
|
||||
.append(String.join(";url_ids=",
|
||||
MapWithAIPreferenceHelper.getMapWithAIUrl().stream()
|
||||
.map(i -> i.getId() == null ? i.getUrl() : i.getId()).filter(Objects::nonNull)
|
||||
.collect(Collectors.joining(","))));
|
||||
}
|
||||
String mapwithaiOptions = sb.toString();
|
||||
tags.put("mapwithai:options",
|
||||
|
|
|
@ -1,90 +0,0 @@
|
|||
// License: GPL. For details, see LICENSE file.
|
||||
package org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation;
|
||||
|
||||
import static org.openstreetmap.josm.tools.I18n.tr;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonArray;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.JsonReader;
|
||||
import javax.json.JsonValue;
|
||||
|
||||
public class DataUrl {
|
||||
private List<Object> dataList;
|
||||
|
||||
public DataUrl(String source, String url, Boolean enabled) {
|
||||
this(source, url, enabled, "[]");
|
||||
}
|
||||
|
||||
public DataUrl(String source, String url, Boolean enabled, String jsonString) {
|
||||
setDataList(Arrays.asList(source, url, enabled, readJsonStringArraySimple(jsonString)));
|
||||
}
|
||||
|
||||
public static DataUrl emptyData() {
|
||||
return new DataUrl(tr("Default Source"), "http://example.com", false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Naively read a json string into an array
|
||||
*
|
||||
* @param jsonString A json array (e.g. [0, {"this": "that"}])
|
||||
* @return A JsonArray
|
||||
*/
|
||||
public static JsonArray readJsonStringArraySimple(String jsonString) {
|
||||
JsonArray returnArray = JsonValue.EMPTY_JSON_ARRAY;
|
||||
try (JsonReader parser = Json.createReader(new StringReader(jsonString))) {
|
||||
returnArray = parser.readArray();
|
||||
}
|
||||
return returnArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the dataList (source, url, enabled)
|
||||
*/
|
||||
public List<Object> getDataList() {
|
||||
return dataList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dataList the dataList to set
|
||||
*/
|
||||
public void setDataList(List<Object> dataList) {
|
||||
if ((this.dataList == null) || ((dataList != null) && (dataList.size() == this.dataList.size()))) {
|
||||
this.dataList = dataList;
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
dataList = Arrays.asList("", "", false);
|
||||
}
|
||||
|
||||
public Map<String, String> getMap() {
|
||||
final Map<String, String> map = new TreeMap<>();
|
||||
map.put("source", dataList.get(0).toString());
|
||||
map.put("url", dataList.get(1).toString());
|
||||
map.put("enabled", dataList.get(2).toString());
|
||||
map.put("parameters", dataList.size() > 3 ? dataList.get(3).toString() : "[]");
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Map<String, String> addUrlParameters(Map<String, String> map) {
|
||||
if (map.containsKey("parameters") && map.containsKey("url")) {
|
||||
final JsonArray array = readJsonStringArraySimple(map.get("parameters"));
|
||||
for (final Object val : array.toArray()) {
|
||||
if (val instanceof JsonObject) {
|
||||
final JsonObject obj = (JsonObject) val;
|
||||
if (obj.getBoolean("enabled", false)) {
|
||||
map.put("url", map.get("url").concat("&").concat(obj.getString("parameter")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
|
@ -5,20 +5,28 @@ import static org.openstreetmap.josm.tools.I18n.marktr;
|
|||
import static org.openstreetmap.josm.tools.I18n.tr;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonArray;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
import org.openstreetmap.gui.jmapviewer.interfaces.Attributed;
|
||||
import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
|
||||
import org.openstreetmap.gui.jmapviewer.tilesources.TileSourceInfo;
|
||||
import org.openstreetmap.josm.data.StructUtils.StructEntry;
|
||||
import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
||||
import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryBounds;
|
||||
|
@ -29,8 +37,9 @@ import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|||
import org.openstreetmap.josm.tools.ImageProvider;
|
||||
import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
import org.openstreetmap.josm.tools.Utils;
|
||||
|
||||
public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
||||
public class MapWithAIInfo extends TileSourceInfo implements Comparable<MapWithAIInfo>, Attributed {
|
||||
/**
|
||||
* Type of MapWithAI entry
|
||||
*/
|
||||
|
@ -76,20 +85,20 @@ public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
|||
this.description = description;
|
||||
}
|
||||
|
||||
public String getCategoryString() {
|
||||
public final String getCategoryString() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public ImageIcon getIcon(ImageProvider.ImageSizes size) {
|
||||
public final String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public final ImageIcon getIcon(ImageSizes size) {
|
||||
return iconCache
|
||||
.computeIfAbsent(size, x -> Collections.synchronizedMap(new EnumMap<>(MapWithAICategory.class)))
|
||||
.computeIfAbsent(this, x -> ImageProvider.get(x.icon, size));
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public static MapWithAICategory fromString(String s) {
|
||||
for (MapWithAICategory category : MapWithAICategory.values()) {
|
||||
if (category.getCategoryString().equals(s)) {
|
||||
|
@ -104,6 +113,10 @@ public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
|||
* original name of the service entry in case of translation call, for multiple
|
||||
* languages English when possible
|
||||
*/
|
||||
/**
|
||||
* original name of the imagery entry in case of translation call, for multiple
|
||||
* languages English when possible
|
||||
*/
|
||||
private String origName;
|
||||
/** (original) language of the translated name entry */
|
||||
private String langName;
|
||||
|
@ -150,15 +163,12 @@ public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
|||
private String date;
|
||||
/** icon used in menu */
|
||||
private String icon;
|
||||
/** HTTP headers **/
|
||||
private Map<String, String> customHttpHeaders = Collections.emptyMap();
|
||||
/** category of the service */
|
||||
private MapWithAICategory category;
|
||||
/** The name of the service */
|
||||
private String name;
|
||||
/** The id of the service */
|
||||
private String id;
|
||||
/** The url of the service */
|
||||
private String url;
|
||||
private MapWithAIType type;
|
||||
private JsonArray parameters;
|
||||
|
||||
/**
|
||||
* when adding a field, also adapt the: {@link #MapWithAIPreferenceEntry
|
||||
|
@ -213,6 +223,8 @@ public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
|||
String description;
|
||||
@StructEntry
|
||||
String category;
|
||||
@StructEntry
|
||||
String parameters;
|
||||
|
||||
/**
|
||||
* Constructs a new empty {@MapWithAIPreferenceEntry}
|
||||
|
@ -242,8 +254,12 @@ public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
|||
terms_of_use_text = i.termsOfUseText;
|
||||
terms_of_use_url = i.termsOfUseURL;
|
||||
country_code = i.countryCode;
|
||||
cookies = i.cookies;
|
||||
icon = intern(i.icon);
|
||||
description = i.description;
|
||||
if (i.parameters != null) {
|
||||
parameters = i.parameters.toString();
|
||||
}
|
||||
category = i.category != null ? i.category.getCategoryString() : null;
|
||||
if (i.bounds != null) {
|
||||
bounds = i.bounds.encodeAsString(",");
|
||||
|
@ -318,7 +334,15 @@ public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
|||
CheckParameterUtil.ensureParameterNotNull(e.name, "name");
|
||||
CheckParameterUtil.ensureParameterNotNull(e.url, "url");
|
||||
description = e.description;
|
||||
cookies = e.cookies;
|
||||
eulaAcceptanceRequired = e.eula;
|
||||
if (e.parameters != null) {
|
||||
try (JsonParser parser = Json.createParser(new StringReader(e.parameters))) {
|
||||
if (parser.hasNext() && JsonParser.Event.START_ARRAY.equals(parser.next())) {
|
||||
parameters = parser.getArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
type = MapWithAIType.fromString(e.type);
|
||||
if (type == null) {
|
||||
throw new IllegalArgumentException("unknown type");
|
||||
|
@ -350,6 +374,8 @@ public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
|||
|
||||
public MapWithAIInfo(MapWithAIInfo i) {
|
||||
this(i.name, i.url, i.id);
|
||||
this.cookies = i.cookies;
|
||||
|
||||
this.origName = i.origName;
|
||||
this.langName = i.langName;
|
||||
this.defaultEntry = i.defaultEntry;
|
||||
|
@ -368,6 +394,7 @@ public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
|||
this.countryCode = i.countryCode;
|
||||
this.date = i.date;
|
||||
this.icon = intern(i.icon);
|
||||
setCustomHttpHeaders(i.customHttpHeaders);
|
||||
this.category = i.category;
|
||||
}
|
||||
|
||||
|
@ -442,61 +469,6 @@ public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
|||
return url.equals(in.url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Request name of the tile source
|
||||
*
|
||||
* @return name of the tile source
|
||||
*/
|
||||
public final String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request URL of the tile source
|
||||
*
|
||||
* @return url of the tile source
|
||||
*/
|
||||
public final String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request ID of the tile source. Id can be null. This gets the configured id as
|
||||
* is. Due to a user error, this may not be unique.
|
||||
*
|
||||
* @return id of the tile source
|
||||
*/
|
||||
public final String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the tile URL.
|
||||
*
|
||||
* @param url tile URL
|
||||
*/
|
||||
public final void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the tile name.
|
||||
*
|
||||
* @param name tile name
|
||||
*/
|
||||
public final void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the tile id.
|
||||
*
|
||||
* @param id tile id
|
||||
*/
|
||||
public final void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
private static String intern(String string) {
|
||||
return string == null ? null : string.intern();
|
||||
}
|
||||
|
@ -670,26 +642,31 @@ public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
|||
* @see #getTermsOfUseURL()
|
||||
*/
|
||||
public void setTermsOfUseURL(String text) {
|
||||
termsOfUseURL = text; // 950
|
||||
termsOfUseURL = text;
|
||||
}
|
||||
|
||||
public void clearId() {
|
||||
if (this.id != null) {
|
||||
Collection<String> newAddedIds = new TreeSet<>(Config.getPref().getList("imagery.layers.addedIds"));
|
||||
Collection<String> newAddedIds = new TreeSet<>(
|
||||
Config.getPref().getList(MapWithAILayerInfo.CONFIG_PREFIX + "addedIds"));
|
||||
newAddedIds.add(this.id);
|
||||
Config.getPref().putList("imagery.layers.addedIds", new ArrayList<>(newAddedIds));
|
||||
Config.getPref().putList(MapWithAILayerInfo.CONFIG_PREFIX + "addedIds", new ArrayList<>(newAddedIds));
|
||||
}
|
||||
setId(null);
|
||||
}
|
||||
|
||||
public String getCountryCode() {
|
||||
return countryCode;
|
||||
}
|
||||
|
||||
public MapWithAICategory getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public boolean isDefaultEntry() {
|
||||
return defaultEntry;
|
||||
}
|
||||
|
||||
public void setDefaultEntry(boolean defaultEntry) {
|
||||
this.defaultEntry = defaultEntry;
|
||||
}
|
||||
|
||||
public String getToolTipText() {
|
||||
StringBuilder res = new StringBuilder(getName());
|
||||
boolean html = false;
|
||||
|
@ -703,4 +680,55 @@ public class MapWithAIInfo implements Comparable<MapWithAIInfo>, Attributed {
|
|||
return res.toString();
|
||||
}
|
||||
|
||||
public String getCountryCode() {
|
||||
return countryCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns custom HTTP headers that should be sent with request towards imagery
|
||||
* provider
|
||||
*
|
||||
* @return headers
|
||||
*/
|
||||
public Map<String, String> getCustomHttpHeaders() {
|
||||
return customHttpHeaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets custom HTTP headers that should be sent with request towards imagery
|
||||
* provider
|
||||
*
|
||||
* @param customHttpHeaders http headers
|
||||
*/
|
||||
public void setCustomHttpHeaders(Map<String, String> customHttpHeaders) {
|
||||
this.customHttpHeaders = Utils.toUnmodifiableMap(customHttpHeaders);
|
||||
}
|
||||
|
||||
public void setParameters(JsonArray parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public JsonArray getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public List<String> getParametersString() {
|
||||
return parameters == null ? Collections.emptyList()
|
||||
: parameters.stream().filter(JsonObject.class::isInstance).map(JsonObject.class::cast)
|
||||
.filter(map -> map.getBoolean("enabled", false)).filter(map -> map.containsKey("parameter"))
|
||||
.map(map -> map.getString("parameter")).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public String getUrlExpanded() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (url != null && !url.trim().isEmpty()) {
|
||||
sb.append(url);
|
||||
List<String> parameters = getParametersString();
|
||||
if (!parameters.isEmpty()) {
|
||||
sb.append('&').append(String.join("&", parameters));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@ import java.util.Set;
|
|||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import org.openstreetmap.josm.data.StructUtils;
|
||||
import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
||||
import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
||||
|
@ -35,8 +37,6 @@ import org.openstreetmap.josm.tools.Utils;
|
|||
*/
|
||||
public class MapWithAILayerInfo {
|
||||
|
||||
/** Unique instance */
|
||||
public static final MapWithAILayerInfo instance = new MapWithAILayerInfo();
|
||||
/** List of all usable layers */
|
||||
private final List<MapWithAIInfo> layers = new ArrayList<>();
|
||||
/** List of layer ids of all usable layers */
|
||||
|
@ -48,20 +48,27 @@ public class MapWithAILayerInfo {
|
|||
/** List of all layer ids of available default layers (including mirrors) */
|
||||
static final Map<String, MapWithAIInfo> defaultLayerIds = new HashMap<>();
|
||||
|
||||
/** The prefix for configuration of the MapWithAI sources */
|
||||
public static final String CONFIG_PREFIX = "mapwithai.sources.";
|
||||
|
||||
private static final String[] DEFAULT_LAYER_SITES = {
|
||||
"https://gitlab.com/gokaart/JOSM_MapWithAI/-/raw/pages/public/json/sources.json" };
|
||||
|
||||
/** Unique instance -- MUST be after DEFAULT_LAYER_SITES */
|
||||
public static final MapWithAILayerInfo instance = new MapWithAILayerInfo();
|
||||
|
||||
/**
|
||||
* Returns the list of imagery layers sites.
|
||||
* Returns the list of source layers sites.
|
||||
*
|
||||
* @return the list of imagery layers sites
|
||||
* @return the list of source layers sites
|
||||
* @since 7434
|
||||
*/
|
||||
public static Collection<String> getImageryLayersSites() {
|
||||
return Config.getPref().getList("mapwithai.sources.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES));
|
||||
return Config.getPref().getList(CONFIG_PREFIX + "layers.sites", Arrays.asList(DEFAULT_LAYER_SITES));
|
||||
}
|
||||
|
||||
private MapWithAILayerInfo() {
|
||||
load(false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -89,8 +96,8 @@ public class MapWithAILayerInfo {
|
|||
*/
|
||||
public void load(boolean fastFail) {
|
||||
clear();
|
||||
List<MapWithAIPreferenceEntry> entries = StructUtils.getListOfStructs(Config.getPref(), "mapwithai.entries",
|
||||
null, MapWithAIPreferenceEntry.class);
|
||||
List<MapWithAIPreferenceEntry> entries = StructUtils.getListOfStructs(Config.getPref(),
|
||||
CONFIG_PREFIX + "entries", null, MapWithAIPreferenceEntry.class);
|
||||
if (entries != null) {
|
||||
for (MapWithAIPreferenceEntry prefEntry : entries) {
|
||||
try {
|
||||
|
@ -102,7 +109,7 @@ public class MapWithAILayerInfo {
|
|||
}
|
||||
Collections.sort(layers);
|
||||
}
|
||||
loadDefaults(false, null, fastFail);
|
||||
loadDefaults(false, null, fastFail, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -117,13 +124,13 @@ public class MapWithAILayerInfo {
|
|||
* in the background
|
||||
* @param fastFail whether opening HTTP connections should fail fast, see
|
||||
* {@link ImageryReader#setFastFail(boolean)}
|
||||
* @param listener A listener to call when the everything is done
|
||||
* @since 12634
|
||||
*/
|
||||
public void loadDefaults(boolean clearCache, ExecutorService worker, boolean fastFail) {
|
||||
final DefaultEntryLoader loader = new DefaultEntryLoader(clearCache, fastFail);
|
||||
public void loadDefaults(boolean clearCache, ExecutorService worker, boolean fastFail, FinishListener listener) {
|
||||
final DefaultEntryLoader loader = new DefaultEntryLoader(clearCache, fastFail, listener);
|
||||
if (worker == null) {
|
||||
loader.realRun();
|
||||
loader.finish();
|
||||
} else {
|
||||
worker.execute(loader);
|
||||
}
|
||||
|
@ -140,11 +147,13 @@ public class MapWithAILayerInfo {
|
|||
private MapWithAISourceReader reader;
|
||||
private boolean canceled;
|
||||
private boolean loadError;
|
||||
private FinishListener listener;
|
||||
|
||||
DefaultEntryLoader(boolean clearCache, boolean fastFail) {
|
||||
DefaultEntryLoader(boolean clearCache, boolean fastFail, FinishListener listener) {
|
||||
super(tr("Update default entries"));
|
||||
this.clearCache = clearCache;
|
||||
this.fastFail = fastFail;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -161,6 +170,7 @@ public class MapWithAILayerInfo {
|
|||
}
|
||||
loadSource(source);
|
||||
}
|
||||
SwingUtilities.invokeLater(this::finish);
|
||||
}
|
||||
|
||||
protected void loadSource(String source) {
|
||||
|
@ -202,6 +212,9 @@ public class MapWithAILayerInfo {
|
|||
if (!loadError && !defaultLayerIds.isEmpty()) {
|
||||
dropOldEntries();
|
||||
}
|
||||
if (listener != null) {
|
||||
listener.onFinish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -239,12 +252,50 @@ public class MapWithAILayerInfo {
|
|||
public void updateEntriesFromDefaults(boolean dropold) {
|
||||
// add new default entries to the user selection
|
||||
boolean changed = false;
|
||||
Collection<String> knownDefaults = new TreeSet<>(Config.getPref().getList("mapwithai.sources.layers.default"));
|
||||
Collection<String> knownDefaults = new TreeSet<>(Config.getPref().getList(CONFIG_PREFIX + "layers.default"));
|
||||
Collection<String> newKnownDefaults = new TreeSet<>();
|
||||
for (MapWithAIInfo def : defaultLayers) {
|
||||
if (def.isDefaultEntry()) {
|
||||
boolean isKnownDefault = false;
|
||||
for (String entry : knownDefaults) {
|
||||
if (entry.equals(def.getId())) {
|
||||
isKnownDefault = true;
|
||||
newKnownDefaults.add(entry);
|
||||
knownDefaults.remove(entry);
|
||||
break;
|
||||
} else if (isSimilar(entry, def.getUrl())) {
|
||||
isKnownDefault = true;
|
||||
if (def.getId() != null) {
|
||||
newKnownDefaults.add(def.getId());
|
||||
}
|
||||
knownDefaults.remove(entry);
|
||||
break;
|
||||
}
|
||||
}
|
||||
boolean isInUserList = false;
|
||||
if (!isKnownDefault) {
|
||||
if (def.getId() != null) {
|
||||
newKnownDefaults.add(def.getId());
|
||||
for (MapWithAIInfo i : layers) {
|
||||
if (isSimilar(def, i)) {
|
||||
isInUserList = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Logging.error("Default imagery ''{0}'' has no id. Skipping.", def.getName());
|
||||
}
|
||||
}
|
||||
if (!isKnownDefault && !isInUserList) {
|
||||
add(new MapWithAIInfo(def));
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!dropold && !knownDefaults.isEmpty()) {
|
||||
newKnownDefaults.addAll(knownDefaults);
|
||||
}
|
||||
Config.getPref().putList("imagery.layers.default", new ArrayList<>(newKnownDefaults));
|
||||
Config.getPref().putList(CONFIG_PREFIX + "layers.default", new ArrayList<>(newKnownDefaults));
|
||||
|
||||
// automatically update user entries with same id as a default entry
|
||||
for (int i = 0; i < layers.size(); i++) {
|
||||
|
@ -332,7 +383,7 @@ public class MapWithAILayerInfo {
|
|||
for (MapWithAIInfo info : layers) {
|
||||
entries.add(new MapWithAIPreferenceEntry(info));
|
||||
}
|
||||
StructUtils.putListOfStructs(Config.getPref(), "mapwithai.source.entries", entries,
|
||||
StructUtils.putListOfStructs(Config.getPref(), CONFIG_PREFIX + "entries", entries,
|
||||
MapWithAIPreferenceEntry.class);
|
||||
}
|
||||
|
||||
|
@ -403,4 +454,8 @@ public class MapWithAILayerInfo {
|
|||
public MapWithAIInfo getLayer(String id) {
|
||||
return layerIds.get(id);
|
||||
}
|
||||
|
||||
public static interface FinishListener {
|
||||
public void onFinish();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,12 +10,9 @@ import java.awt.GridBagConstraints;
|
|||
import java.awt.GridBagLayout;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.json.JsonObject;
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JOptionPane;
|
||||
|
@ -35,7 +32,7 @@ import org.openstreetmap.josm.plugins.mapwithai.backend.DetectTaskingManagerUtil
|
|||
import org.openstreetmap.josm.plugins.mapwithai.backend.DownloadMapWithAITask;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIPreferenceHelper;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation.DataUrl;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.tools.GBC;
|
||||
import org.openstreetmap.josm.tools.ImageProvider;
|
||||
|
||||
|
@ -52,29 +49,13 @@ public class MapWithAIDownloadReader implements DownloadSource<MapWithAIDownload
|
|||
DownloadMapWithAITask task = new DownloadMapWithAITask();
|
||||
task.setZoomAfterDownload(settings.zoomToData());
|
||||
data.getUrls().forEach(url -> {
|
||||
Future<?> future = task.download(new BoundingBoxMapWithAIDownloader(area, getUrl(url),
|
||||
DetectTaskingManagerUtils.hasTaskingManagerLayer()), new DownloadParams(), area, null);
|
||||
Future<?> future = task.download(
|
||||
new BoundingBoxMapWithAIDownloader(area, url, DetectTaskingManagerUtils.hasTaskingManagerLayer()),
|
||||
new DownloadParams(), area, null);
|
||||
MainApplication.worker.execute(new PostDownloadHandler(task, future, data.getErrorReporter()));
|
||||
});
|
||||
}
|
||||
|
||||
private static String getUrl(Map<String, String> urlInformation) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (urlInformation.containsKey("url")) {
|
||||
sb.append(urlInformation.get("url"));
|
||||
if (urlInformation.containsKey("parameters")) {
|
||||
List<String> parameters = DataUrl.readJsonStringArraySimple(urlInformation.get("parameters"))
|
||||
.parallelStream().filter(JsonObject.class::isInstance).map(JsonObject.class::cast)
|
||||
.filter(map -> map.getBoolean("enabled", false)).filter(map -> map.containsKey("parameter"))
|
||||
.map(map -> map.getString("parameter")).collect(Collectors.toList());
|
||||
if (!parameters.isEmpty()) {
|
||||
sb.append('&').append(String.join("&", parameters));
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return tr("Download from {0} API", MapWithAIPlugin.NAME);
|
||||
|
@ -89,15 +70,15 @@ public class MapWithAIDownloadReader implements DownloadSource<MapWithAIDownload
|
|||
* Encapsulates data that is required to perform download from MapWithAI API
|
||||
*/
|
||||
static class MapWithAIDownloadData {
|
||||
private final List<Map<String, String>> url;
|
||||
private final List<MapWithAIInfo> url;
|
||||
private final Consumer<Collection<Object>> errorReporter;
|
||||
|
||||
MapWithAIDownloadData(List<Map<String, String>> list, Consumer<Collection<Object>> errorReporter) {
|
||||
MapWithAIDownloadData(List<MapWithAIInfo> list, Consumer<Collection<Object>> errorReporter) {
|
||||
this.url = list;
|
||||
this.errorReporter = errorReporter;
|
||||
}
|
||||
|
||||
List<Map<String, String>> getUrls() {
|
||||
List<MapWithAIInfo> getUrls() {
|
||||
return url;
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.swing.Box;
|
||||
import javax.swing.JButton;
|
||||
|
@ -39,7 +38,6 @@ import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
|||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.DataAvailability;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIPreferenceHelper;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation.DataUrl;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.mapwithai.MapWithAIProvidersPanel;
|
||||
import org.openstreetmap.josm.spi.preferences.StringSetting;
|
||||
|
@ -52,9 +50,8 @@ public class MapWithAIPreferences extends DefaultTabPreferenceSetting {
|
|||
private final JCheckBox mergeBuildingAddressCheckBox;
|
||||
private final JSpinner maximumAdditionSpinner;
|
||||
private final ReplacementPreferenceTable replacementPreferenceTable;
|
||||
private final MapWithAIURLPreferenceTable mapwithaiUrlPreferenceTable;
|
||||
private final List<PrefEntry> replacementTableDisplayData;
|
||||
private final List<DataUrl> mapwithaiurlTableDisplayData;
|
||||
private MapWithAILayerInfo info;
|
||||
private static final int MAX_SELECTED_TO_EDIT = 1;
|
||||
private static final String SOURCE = "source";
|
||||
|
||||
|
@ -68,11 +65,6 @@ public class MapWithAIPreferences extends DefaultTabPreferenceSetting {
|
|||
replacementTableDisplayData = new ArrayList<>();
|
||||
fillReplacementTagDisplayData(replacementTableDisplayData);
|
||||
replacementPreferenceTable = new ReplacementPreferenceTable(replacementTableDisplayData);
|
||||
|
||||
mapwithaiurlTableDisplayData = new ArrayList<>();
|
||||
fillMapWithAIURLTableDisplayData(mapwithaiurlTableDisplayData);
|
||||
mapwithaiUrlPreferenceTable = new MapWithAIURLPreferenceTable(mapwithaiurlTableDisplayData);
|
||||
|
||||
}
|
||||
|
||||
private static void fillReplacementTagDisplayData(List<PrefEntry> list) {
|
||||
|
@ -83,16 +75,6 @@ public class MapWithAIPreferences extends DefaultTabPreferenceSetting {
|
|||
}
|
||||
}
|
||||
|
||||
private static void fillMapWithAIURLTableDisplayData(List<DataUrl> list) {
|
||||
List<Map<String, String>> entries = MapWithAIPreferenceHelper.getMapWithAIURLs();
|
||||
if (list.isEmpty()) {
|
||||
DataAvailability.populatePreferences();
|
||||
entries = MapWithAIPreferenceHelper.getMapWithAIURLs();
|
||||
}
|
||||
entries.forEach(entry -> list.add(new DataUrl(entry.get(SOURCE), entry.get("url"),
|
||||
Boolean.valueOf(entry.getOrDefault("enabled", "false")), entry.getOrDefault("parameters", "[]"))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGui(PreferenceTabbedPane gui) {
|
||||
final JPanel p = gui.createPreferenceTab(this);
|
||||
|
@ -105,50 +87,7 @@ public class MapWithAIPreferences extends DefaultTabPreferenceSetting {
|
|||
}
|
||||
|
||||
private Component getServerList(PreferenceTabbedPane gui) {
|
||||
if (false) {
|
||||
final JPanel pane = new JPanel(new GridBagLayout());
|
||||
final int width = 200;
|
||||
final int height = 200;
|
||||
final GBC first = GBC.std().weight(0, 1).anchor(GridBagConstraints.WEST);
|
||||
final GBC second = GBC.eol().fill(GridBagConstraints.HORIZONTAL);
|
||||
final GBC buttonInsets = GBC.std().insets(5, 5, 0, 0);
|
||||
final JLabel mapWithAIApiUrl = new JLabel(tr("{0} API URLs", MapWithAIPlugin.NAME));
|
||||
mapWithAIApiUrl.setToolTipText(tr("The URL that will be called to get data from"));
|
||||
pane.add(mapWithAIApiUrl, first);
|
||||
|
||||
final JScrollPane scroll1 = new JScrollPane(mapwithaiUrlPreferenceTable);
|
||||
scroll1.setToolTipText(mapWithAIApiUrl.getToolTipText());
|
||||
pane.add(scroll1, GBC.eol().fill(GridBagConstraints.BOTH));
|
||||
scroll1.setPreferredSize(new Dimension(width, height));
|
||||
pane.add(new JLabel(), first);
|
||||
final JPanel replaceAddEditDeleteScroll1 = new JPanel(new GridBagLayout());
|
||||
pane.add(replaceAddEditDeleteScroll1, second);
|
||||
final JButton addScroll1 = new JButton(tr("Add"));
|
||||
replaceAddEditDeleteScroll1.add(addScroll1, buttonInsets);
|
||||
addScroll1.addActionListener(e -> {
|
||||
mapwithaiurlTableDisplayData.add(DataUrl.emptyData());
|
||||
mapwithaiUrlPreferenceTable.fireDataChanged();
|
||||
});
|
||||
final JButton editScroll1 = new JButton(tr("Edit Parameters"));
|
||||
replaceAddEditDeleteScroll1.add(editScroll1, buttonInsets);
|
||||
editScroll1.addActionListener(e -> {
|
||||
final List<DataUrl> toEdit = mapwithaiUrlPreferenceTable.getSelectedItems();
|
||||
if (toEdit.size() == MAX_SELECTED_TO_EDIT) {
|
||||
mapwithaiUrlPreferenceTable.editPreference(gui);
|
||||
}
|
||||
});
|
||||
final JButton deleteScroll1 = new JButton(tr("Delete"));
|
||||
replaceAddEditDeleteScroll1.add(deleteScroll1, buttonInsets);
|
||||
deleteScroll1.addActionListener(e -> {
|
||||
final List<DataUrl> toRemove = mapwithaiUrlPreferenceTable.getSelectedItems();
|
||||
if (!toRemove.isEmpty()) {
|
||||
mapwithaiurlTableDisplayData.removeAll(toRemove);
|
||||
}
|
||||
mapwithaiUrlPreferenceTable.fireDataChanged();
|
||||
});
|
||||
return pane;
|
||||
}
|
||||
MapWithAILayerInfo info = new MapWithAILayerInfo(MapWithAILayerInfo.instance);
|
||||
info = new MapWithAILayerInfo(MapWithAILayerInfo.instance);
|
||||
return new MapWithAIProvidersPanel(gui, info);
|
||||
}
|
||||
|
||||
|
@ -270,30 +209,18 @@ public class MapWithAIPreferences extends DefaultTabPreferenceSetting {
|
|||
|
||||
@Override
|
||||
public boolean ok() {
|
||||
final ArrayList<DataUrl> tData = new ArrayList<>(
|
||||
mapwithaiurlTableDisplayData.stream().distinct()
|
||||
.filter(data -> !data.getMap().getOrDefault("url", "http://example.com")
|
||||
.equalsIgnoreCase(DataUrl.emptyData().getMap().get("url")))
|
||||
.collect(Collectors.toList()));
|
||||
mapwithaiurlTableDisplayData.clear();
|
||||
mapwithaiurlTableDisplayData.addAll(tData);
|
||||
MapWithAIPreferenceHelper.setMapWithAIURLs(convertUrlPrefToMap(mapwithaiurlTableDisplayData));
|
||||
MapWithAIPreferenceHelper.setSwitchLayers(switchLayerCheckBox.isSelected(), true);
|
||||
final Object value = maximumAdditionSpinner.getValue();
|
||||
if (value instanceof Number) {
|
||||
MapWithAIPreferenceHelper.setMaximumAddition(((Number) value).intValue(), true);
|
||||
}
|
||||
info.save();
|
||||
MapWithAILayerInfo.instance.clear();
|
||||
MapWithAILayerInfo.instance.load(false);
|
||||
MapWithAIPreferenceHelper.setReplacementTags(convertReplacementPrefToMap(replacementTableDisplayData));
|
||||
return false;
|
||||
}
|
||||
|
||||
private static List<Map<String, String>> convertUrlPrefToMap(List<DataUrl> displayData) {
|
||||
return displayData.stream().map(DataUrl::getMap)
|
||||
.filter(map -> !map.getOrDefault("url", "").isEmpty()
|
||||
&& !DataUrl.emptyData().getMap().get("url").equals(map.get("url")))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static Map<String, String> convertReplacementPrefToMap(List<PrefEntry> displayData) {
|
||||
final Map<String, String> returnMap = displayData.isEmpty() ? Collections.emptyMap() : new TreeMap<>();
|
||||
displayData.forEach(entry -> returnMap.put(entry.getKey(), entry.getValue().getValue().toString()));
|
||||
|
|
|
@ -1,226 +0,0 @@
|
|||
// License: GPL. For details, see LICENSE file.
|
||||
package org.openstreetmap.josm.plugins.mapwithai.gui.preferences;
|
||||
|
||||
import static org.openstreetmap.josm.tools.I18n.tr;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonArray;
|
||||
import javax.json.JsonArrayBuilder;
|
||||
import javax.json.JsonObject;
|
||||
import javax.swing.DefaultCellEditor;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
|
||||
import org.openstreetmap.josm.gui.preferences.advanced.MapListEditor;
|
||||
import org.openstreetmap.josm.gui.preferences.advanced.PrefEntry;
|
||||
import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation.DataUrl;
|
||||
import org.openstreetmap.josm.spi.preferences.MapListSetting;
|
||||
import org.openstreetmap.josm.spi.preferences.Setting;
|
||||
import org.openstreetmap.josm.spi.preferences.StringSetting;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
|
||||
/**
|
||||
* Component for editing list of preferences as a table.
|
||||
*
|
||||
* @since 6021
|
||||
*/
|
||||
public class MapWithAIURLPreferenceTable extends JTable {
|
||||
private static final long serialVersionUID = -6221760175084963950L;
|
||||
private final URLTableModel model;
|
||||
private final transient List<DataUrl> displayData;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code PreferencesTable}.
|
||||
*
|
||||
* @param displayData The list of preferences entries to display
|
||||
*/
|
||||
public MapWithAIURLPreferenceTable(List<DataUrl> displayData) {
|
||||
this.displayData = displayData;
|
||||
model = new URLTableModel();
|
||||
setModel(model);
|
||||
putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
|
||||
getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new JosmTextField()));
|
||||
getColumnModel().getColumn(0)
|
||||
.setPreferredWidth(getFontMetrics(getFont()).stringWidth(MapWithAIPlugin.NAME) + 5);
|
||||
getColumnModel().getColumn(0).setMaxWidth(getColumnModel().getColumn(0).getPreferredWidth() * 2);
|
||||
getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(new JosmTextField()));
|
||||
getColumnModel().getColumn(2).setMaxWidth(getFontMetrics(getFont()).stringWidth("ENABLED"));
|
||||
getColumnModel().getColumn(3).setMaxWidth(getFontMetrics(getFont()).stringWidth("Parameters"));
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
if (e.getClickCount() == 2) {
|
||||
editPreference(MapWithAIURLPreferenceTable.this);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This method should be called when displayed data was changed form external
|
||||
* code
|
||||
*/
|
||||
public void fireDataChanged() {
|
||||
model.fireTableDataChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* The list of currently selected rows
|
||||
*
|
||||
* @return newly created list of DataUrl
|
||||
*/
|
||||
public List<DataUrl> getSelectedItems() {
|
||||
final List<DataUrl> entries = new ArrayList<>();
|
||||
for (final int row : getSelectedRows()) {
|
||||
final DataUrl p = displayData.get(row);
|
||||
entries.add(p);
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this to edit selected row in preferences table
|
||||
*
|
||||
* @param gui - parent component for messagebox
|
||||
* @return true if editing was actually performed during this call
|
||||
*/
|
||||
public boolean editPreference(final JComponent gui) {
|
||||
final int column = 3;
|
||||
if (getSelectedRowCount() != 1) {
|
||||
JOptionPane.showMessageDialog(gui, tr("Please select the row to edit."), tr("Warning"),
|
||||
JOptionPane.WARNING_MESSAGE);
|
||||
return false;
|
||||
}
|
||||
final Object e = model.getValueAt(getSelectedRow(), column);
|
||||
Setting<?> stg = e instanceof PrefEntry ? ((PrefEntry) e).getValue() : null;
|
||||
if (e instanceof JsonArray) {
|
||||
final JsonArray array = (JsonArray) e;
|
||||
final List<Map<String, String>> map = array.stream().filter(JsonObject.class::isInstance)
|
||||
.map(JsonObject.class::cast).map(obj -> {
|
||||
final Map<String, String> tMap = new TreeMap<>();
|
||||
obj.forEach((str, val) -> tMap.put(str, val.toString()));
|
||||
return tMap;
|
||||
}).collect(Collectors.toList());
|
||||
stg = new MapListSetting(map);
|
||||
}
|
||||
boolean ok = false;
|
||||
if ((stg instanceof StringSetting) || (e instanceof String) || (e instanceof Boolean)) {
|
||||
editCellAt(getSelectedRow(), getSelectedColumn());
|
||||
final Component editor = getEditorComponent();
|
||||
if (editor != null) {
|
||||
editor.requestFocus();
|
||||
}
|
||||
} else if (stg instanceof MapListSetting) {
|
||||
final PrefEntry pref = e instanceof PrefEntry ? (PrefEntry) e
|
||||
: new PrefEntry("Parameters", stg, stg, false);
|
||||
ok = doEditMapList(gui, pref, (MapListSetting) stg);
|
||||
if (!e.equals(pref) && (e instanceof JsonArray)) {
|
||||
final JsonArrayBuilder array = Json.createArrayBuilder();
|
||||
((MapListSetting) pref.getValue()).getValue()
|
||||
.forEach(entry -> array.add(Json.createObjectBuilder(objectify(entry)).build()));
|
||||
model.setValueAt(array.build(), getSelectedRow(), column);
|
||||
fireDataChanged();
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a map of Map<String, String> to a map of Map<String,
|
||||
* Object>
|
||||
*
|
||||
* @param map The map of strings to strings to convert (e.g., "1"->int 1,
|
||||
* "true" -> boolean true, etc)
|
||||
* @return A converted map
|
||||
*/
|
||||
public static Map<String, Object> objectify(Map<String, String> map) {
|
||||
final Map<String, Object> returnMap = new TreeMap<>();
|
||||
for (final Entry<String, String> entry : map.entrySet()) {
|
||||
Object obj = null;
|
||||
if (entry.getValue().equalsIgnoreCase("true") || entry.getValue().equalsIgnoreCase("false")
|
||||
|| "enabled".equals(entry.getKey())) {
|
||||
obj = Boolean.parseBoolean(entry.getValue());
|
||||
} else if (entry.getValue().matches("[0-9.-]+")) {
|
||||
try {
|
||||
obj = Double.parseDouble(entry.getValue());
|
||||
obj = Integer.parseInt(entry.getValue());
|
||||
} catch (final NumberFormatException e) {
|
||||
Logging.trace("{0}: {1}", MapWithAIPlugin.NAME, e);
|
||||
}
|
||||
} else {
|
||||
obj = entry.getValue().replaceAll("(^(\\\\*\\\"+)+|(\\\\*\\\"+)+$)", "");
|
||||
}
|
||||
returnMap.put(entry.getKey(), obj);
|
||||
}
|
||||
return returnMap;
|
||||
}
|
||||
|
||||
private static boolean doEditMapList(final JComponent gui, final PrefEntry e, MapListSetting mlSetting) {
|
||||
final MapListEditor mlEditor = new MapListEditor(gui, e, mlSetting);
|
||||
mlEditor.showDialog();
|
||||
if (mlEditor.getValue() == 1) {
|
||||
final List<Map<String, String>> data = mlEditor.getData();
|
||||
if (!mlSetting.equalVal(data)) {
|
||||
e.setValue(new MapListSetting(data));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
final class URLTableModel extends DefaultTableModel {
|
||||
private static final long serialVersionUID = 2435772137483913278L;
|
||||
|
||||
URLTableModel() {
|
||||
super();
|
||||
setColumnIdentifiers(new String[] { tr("Source"), tr("URL"), tr("Enabled"), tr("Parameters") });
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getColumnClass(int column) {
|
||||
return getRowCount() == 0 ? Object.class : getValueAt(0, column).getClass();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRowCount() {
|
||||
return displayData.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValueAt(int row, int column) {
|
||||
return displayData.get(row).getDataList().get(column);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValueAt(Object o, int row, int column) {
|
||||
final List<Object> setList = new ArrayList<>(displayData.get(row).getDataList());
|
||||
if (!o.equals(setList.get(column))) {
|
||||
setList.remove(column);
|
||||
setList.add(column, o);
|
||||
displayData.get(row).setDataList(setList);
|
||||
if ((o instanceof String) && ((String) o).isEmpty()) {
|
||||
displayData.get(row).reset();
|
||||
displayData.remove(row);
|
||||
fireTableRowsDeleted(row, row);
|
||||
} else {
|
||||
fireTableCellUpdated(row, column);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,19 +16,20 @@ import org.openstreetmap.josm.gui.util.WindowGeometry;
|
|||
*
|
||||
*/
|
||||
public class AddMapWithAIDialog extends ExtendedDialog implements ContentValidationListener {
|
||||
private static final long serialVersionUID = 7513676077181970148L;
|
||||
|
||||
/**
|
||||
* Constructs a new AddImageryDialog.
|
||||
* Constructs a new AddMapWithAIDialog.
|
||||
*
|
||||
* @param parent The parent element that will be used for position and maximum
|
||||
* size
|
||||
* @param panel The content that will be displayed in the message dialog
|
||||
*/
|
||||
public AddMapWithAIDialog(Component parent, AddMapWithAIPanel panel) {
|
||||
super(parent, tr("Add Imagery URL"), tr("OK"), tr("Cancel"));
|
||||
super(parent, tr("Add MapWithAI URL"), tr("OK"), tr("Cancel"));
|
||||
setButtonIcons("ok", "cancel");
|
||||
setCancelButton(2);
|
||||
configureContextsensitiveHelp("/Preferences/Imagery", true /* show help button */);
|
||||
configureContextsensitiveHelp("/Preferences/MapWithAI", true /* show help button */);
|
||||
setContent(panel, false);
|
||||
setMinimumSize(new Dimension(300, 400));
|
||||
panel.addContentValidationListener(this);
|
||||
|
|
|
@ -25,13 +25,13 @@ import javax.swing.event.DocumentListener;
|
|||
import javax.swing.text.JTextComponent;
|
||||
|
||||
import org.openstreetmap.josm.actions.ExpertToggleAction;
|
||||
import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
||||
import org.openstreetmap.josm.data.imagery.TMSCachedTileLoaderJob;
|
||||
import org.openstreetmap.josm.gui.preferences.imagery.AddImageryPanel.ContentValidationListener;
|
||||
import org.openstreetmap.josm.gui.preferences.imagery.HeadersTable;
|
||||
import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
||||
import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo.MapWithAIType;
|
||||
import org.openstreetmap.josm.tools.GBC;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
|
||||
|
@ -145,7 +145,7 @@ public class AddMapWithAIPanel extends JPanel {
|
|||
return s.replaceAll("[\r\n]+", "").trim();
|
||||
}
|
||||
|
||||
protected static String sanitize(String s, ImageryType type) {
|
||||
protected static String sanitize(String s, MapWithAIType type) {
|
||||
String ret = s;
|
||||
String imageryType = type.getTypeString() + ':';
|
||||
if (ret.startsWith(imageryType)) {
|
||||
|
@ -155,11 +155,11 @@ public class AddMapWithAIPanel extends JPanel {
|
|||
return sanitize(ret);
|
||||
}
|
||||
|
||||
protected final String getImageryName() {
|
||||
protected final String getInfoName() {
|
||||
return sanitize(name.getText());
|
||||
}
|
||||
|
||||
protected final String getImageryRawUrl() {
|
||||
protected final String getInfoRawUrl() {
|
||||
return sanitize(rawUrl.getText());
|
||||
}
|
||||
|
||||
|
@ -169,7 +169,7 @@ public class AddMapWithAIPanel extends JPanel {
|
|||
|
||||
/**
|
||||
* Registers a new ContentValidationListener
|
||||
*
|
||||
*
|
||||
* @param l The new ContentValidationListener that will be notified of
|
||||
* validation status changes
|
||||
*/
|
||||
|
|
|
@ -49,13 +49,13 @@ import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon;
|
|||
import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle;
|
||||
import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
||||
import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryBounds;
|
||||
import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryCategory;
|
||||
import org.openstreetmap.josm.data.imagery.Shape;
|
||||
import org.openstreetmap.josm.data.preferences.NamedColorProperty;
|
||||
import org.openstreetmap.josm.gui.MainApplication;
|
||||
import org.openstreetmap.josm.gui.bbox.JosmMapViewer;
|
||||
import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
|
||||
import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
||||
import org.openstreetmap.josm.gui.preferences.imagery.ImageryProvidersPanel;
|
||||
import org.openstreetmap.josm.gui.util.GuiHelper;
|
||||
import org.openstreetmap.josm.gui.widgets.FilterField;
|
||||
import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
||||
|
@ -71,11 +71,12 @@ import org.openstreetmap.josm.tools.Logging;
|
|||
|
||||
/**
|
||||
* A panel displaying imagery providers. Largely duplicates
|
||||
* {@link ImageryprovidersPanel}.
|
||||
* {@link ImageryProvidersPanel}.
|
||||
*
|
||||
* @since 15115 (extracted from ImageryPreferences)
|
||||
*/
|
||||
public class MapWithAIProvidersPanel extends JPanel {
|
||||
private static final long serialVersionUID = -5876039771496409422L;
|
||||
// Public JTables and JosmMapViewer
|
||||
/** The table of active providers **/
|
||||
public final JTable activeTable;
|
||||
|
@ -110,18 +111,19 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
private final transient MapWithAILayerInfo layerInfo;
|
||||
|
||||
/**
|
||||
* class to render the URL information of Imagery source
|
||||
* class to render the URL information of MapWithAI source
|
||||
*
|
||||
* @since 8065
|
||||
*/
|
||||
private static class ImageryURLTableCellRenderer extends DefaultTableCellRenderer {
|
||||
private static class MapWithAIURLTableCellRenderer extends DefaultTableCellRenderer {
|
||||
private static final long serialVersionUID = 184934756853356357L;
|
||||
|
||||
private static final NamedColorProperty IMAGERY_BACKGROUND_COLOR = new NamedColorProperty(
|
||||
marktr("Imagery Background: Default"), new Color(200, 255, 200));
|
||||
marktr("MapWithAI Background: Default"), new Color(200, 255, 200));
|
||||
|
||||
private final transient List<MapWithAIInfo> layers;
|
||||
|
||||
ImageryURLTableCellRenderer(List<MapWithAIInfo> layers) {
|
||||
MapWithAIURLTableCellRenderer(List<MapWithAIInfo> layers) {
|
||||
this.layers = layers;
|
||||
}
|
||||
|
||||
|
@ -146,11 +148,12 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
}
|
||||
|
||||
/**
|
||||
* class to render an information of Imagery source
|
||||
* class to render an information of MapWithAI source
|
||||
*
|
||||
* @param <T> type of information
|
||||
*/
|
||||
private static class MapWithAITableCellRenderer<T> extends DefaultTableCellRenderer {
|
||||
private static final long serialVersionUID = 1739280307217707613L;
|
||||
private final Function<T, Object> mapper;
|
||||
private final Function<T, String> tooltip;
|
||||
private final BiConsumer<T, JLabel> decorator;
|
||||
|
@ -182,7 +185,7 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
}
|
||||
|
||||
/**
|
||||
* class to render the category information of Imagery source
|
||||
* class to render the category information of MapWithAI source
|
||||
*/
|
||||
private static class MapWithAICategoryTableCellRenderer
|
||||
extends MapWithAIProvidersPanel.MapWithAITableCellRenderer<MapWithAICategory> {
|
||||
|
@ -195,10 +198,12 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
}
|
||||
|
||||
/**
|
||||
* class to render the country information of Imagery source
|
||||
* class to render the country information of MapWithAI source
|
||||
*/
|
||||
private static class MapWithAICountryTableCellRenderer
|
||||
extends MapWithAIProvidersPanel.MapWithAITableCellRenderer<String> {
|
||||
private static final long serialVersionUID = 5975643008500799758L;
|
||||
|
||||
MapWithAICountryTableCellRenderer() {
|
||||
super(code -> code, ImageryInfo::getLocalizedCountry, null);
|
||||
}
|
||||
|
@ -209,13 +214,15 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
*/
|
||||
private static class MapWithAINameTableCellRenderer
|
||||
extends MapWithAIProvidersPanel.MapWithAITableCellRenderer<MapWithAIInfo> {
|
||||
private static final long serialVersionUID = 6669934435517244629L;
|
||||
|
||||
MapWithAINameTableCellRenderer() {
|
||||
super(info -> info == null ? null : info.getName(), MapWithAIInfo::getToolTipText, null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new {@code ImageryProvidersPanel}.
|
||||
* Constructs a new {@code MapWithAIProvidersPanel}.
|
||||
*
|
||||
* @param gui The parent preference tab pane
|
||||
* @param layerInfoArg The list of imagery entries to display
|
||||
|
@ -252,7 +259,7 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
|
||||
TableColumnModel mod = defaultTable.getColumnModel();
|
||||
mod.getColumn(3).setPreferredWidth(775);
|
||||
mod.getColumn(3).setCellRenderer(new ImageryURLTableCellRenderer(layerInfo.getLayers()));
|
||||
mod.getColumn(3).setCellRenderer(new MapWithAIURLTableCellRenderer(layerInfo.getLayers()));
|
||||
mod.getColumn(2).setPreferredWidth(475);
|
||||
mod.getColumn(2).setCellRenderer(new MapWithAINameTableCellRenderer());
|
||||
mod.getColumn(1).setCellRenderer(new MapWithAICountryTableCellRenderer());
|
||||
|
@ -262,7 +269,7 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
|
||||
mod = activeTable.getColumnModel();
|
||||
mod.getColumn(1).setPreferredWidth(800);
|
||||
mod.getColumn(1).setCellRenderer(new ImageryURLTableCellRenderer(layerInfo.getAllDefaultLayers()));
|
||||
mod.getColumn(1).setCellRenderer(new MapWithAIURLTableCellRenderer(layerInfo.getAllDefaultLayers()));
|
||||
mod.getColumn(0).setPreferredWidth(200);
|
||||
|
||||
RemoveEntryAction remove = new RemoveEntryAction();
|
||||
|
@ -477,6 +484,7 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
}
|
||||
|
||||
private class RemoveEntryAction extends AbstractAction implements ListSelectionListener {
|
||||
private static final long serialVersionUID = 2666450386256004180L;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code RemoveEntryAction}.
|
||||
|
@ -507,6 +515,7 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
}
|
||||
|
||||
private class ActivateAction extends AbstractAction implements ListSelectionListener {
|
||||
private static final long serialVersionUID = -452335751201424801L;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code ActivateAction}.
|
||||
|
@ -560,6 +569,7 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
}
|
||||
|
||||
private class ReloadAction extends AbstractAction {
|
||||
private static final long serialVersionUID = 7801339998423585685L;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code ReloadAction}.
|
||||
|
@ -571,12 +581,13 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent evt) {
|
||||
layerInfo.loadDefaults(true, MainApplication.worker, false);
|
||||
defaultModel.fireTableDataChanged();
|
||||
defaultTable.getSelectionModel().clearSelection();
|
||||
defaultTableListener.clearMap();
|
||||
/* loading new file may change active layers */
|
||||
activeModel.fireTableDataChanged();
|
||||
layerInfo.loadDefaults(true, MainApplication.worker, false, () -> {
|
||||
defaultModel.fireTableDataChanged();
|
||||
defaultTable.getSelectionModel().clearSelection();
|
||||
defaultTableListener.clearMap();
|
||||
/* loading new file may change active layers */
|
||||
activeModel.fireTableDataChanged();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -587,10 +598,10 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
private static final long serialVersionUID = 60378230494588007L;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code ImageryLayerTableModel}.
|
||||
* Constructs a new {@code MapWithAILayerTableModel}.
|
||||
*/
|
||||
public MapWithAILayerTableModel() {
|
||||
setColumnIdentifiers(new String[] { tr("Menu Name"), tr("MapWithAI URL") });
|
||||
setColumnIdentifiers(new String[] { tr("Default Source Value"), tr("MapWithAI URL") });
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -663,8 +674,10 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
* The table model for the default imagery layer list
|
||||
*/
|
||||
public class MapWithAIDefaultLayerTableModel extends DefaultTableModel {
|
||||
private static final long serialVersionUID = -2966437364160797385L;
|
||||
|
||||
/**
|
||||
* Constructs a new {@code ImageryDefaultLayerTableModel}.
|
||||
* Constructs a new {@code MapWithAIDefaultLayerTableModel}.
|
||||
*/
|
||||
public MapWithAIDefaultLayerTableModel() {
|
||||
setColumnIdentifiers(new String[] { "", "", tr("Menu Name (Default)"), tr("MapWithAI URL (Default)") });
|
||||
|
@ -689,11 +702,11 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
public Class<?> getColumnClass(int columnIndex) {
|
||||
switch (columnIndex) {
|
||||
case 0:
|
||||
return ImageryCategory.class;
|
||||
return MapWithAICategory.class;
|
||||
case 1:
|
||||
return String.class;
|
||||
case 2:
|
||||
return ImageryInfo.class;
|
||||
return MapWithAIInfo.class;
|
||||
case 3:
|
||||
return String.class;
|
||||
default:
|
||||
|
|
|
@ -128,6 +128,8 @@ public class MapWithAISourceReader implements Closeable {
|
|||
}
|
||||
}
|
||||
MapWithAIInfo info = new MapWithAIInfo(name, url, type, eula, id);
|
||||
info.setDefaultEntry(values.getBoolean("default", false));
|
||||
info.setParameters(values.getJsonArray("parameters"));
|
||||
if (!bounds.isEmpty()) {
|
||||
ImageryBounds bound = bounds.get(0);
|
||||
bounds.remove(0);
|
||||
|
@ -155,7 +157,7 @@ public class MapWithAISourceReader implements Closeable {
|
|||
/**
|
||||
* Sets whether opening HTTP connections should fail fast, i.e., whether a
|
||||
* {@link HttpClient#setConnectTimeout(int) low connect timeout} should be used.
|
||||
*
|
||||
*
|
||||
* @param fastFail whether opening HTTP connections should fail fast
|
||||
* @see CachedFile#setFastFail(boolean)
|
||||
*/
|
||||
|
|
|
@ -11,7 +11,6 @@ import java.net.MalformedURLException;
|
|||
import java.net.URL;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
@ -19,28 +18,27 @@ import org.junit.Rule;
|
|||
import org.junit.Test;
|
||||
import org.openstreetmap.josm.actions.downloadtasks.DownloadParams;
|
||||
import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAILayerInfoTest;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
|
||||
public class DownloadMapWithAITaskTest {
|
||||
@Rule
|
||||
public JOSMTestRules rule = new JOSMTestRules().preferences().fakeAPI().projection();
|
||||
public JOSMTestRules rule = new JOSMTestRules().preferences().fakeAPI().projection().territories();
|
||||
WireMockServer wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock"));
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
wireMock.start();
|
||||
MapWithAIPreferenceHelper.setMapWithAIURLs(MapWithAIPreferenceHelper.getMapWithAIURLs().stream().map(map -> {
|
||||
map.put("url", GetDataRunnableTest.getDefaultMapWithAIAPIForTest(wireMock,
|
||||
map.getOrDefault("url", MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API)));
|
||||
return map;
|
||||
}).collect(Collectors.toList()));
|
||||
MapWithAILayerInfoTest.setupMapWithAILayerInfo(wireMock);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
wireMock.stop();
|
||||
MapWithAILayerInfoTest.resetMapWithAILayerInfo();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -49,7 +47,7 @@ public class DownloadMapWithAITaskTest {
|
|||
DownloadMapWithAITask task = new DownloadMapWithAITask();
|
||||
Future<?> future = task.download(
|
||||
new BoundingBoxMapWithAIDownloader(MapWithAIDataUtilsTest.getTestBounds(),
|
||||
MapWithAIPreferenceHelper.getMapWithAIUrl().get(0).get("url"), false),
|
||||
MapWithAILayerInfo.instance.getLayers().get(0), false),
|
||||
new DownloadParams(), MapWithAIDataUtilsTest.getTestBounds(), NullProgressMonitor.INSTANCE);
|
||||
future.get();
|
||||
assertNotNull(task.getDownloadedData(), "Data should be downloaded");
|
||||
|
|
|
@ -14,7 +14,6 @@ import java.util.Arrays;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
@ -30,6 +29,8 @@ import org.openstreetmap.josm.data.osm.WaySegment;
|
|||
import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
||||
import org.openstreetmap.josm.gui.MainApplication;
|
||||
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAILayerInfoTest;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
import org.openstreetmap.josm.tools.Geometry;
|
||||
|
||||
|
@ -37,23 +38,20 @@ import com.github.tomakehurst.wiremock.WireMockServer;
|
|||
|
||||
public class GetDataRunnableTest {
|
||||
@Rule
|
||||
public JOSMTestRules rule = new JOSMTestRules().projection().fakeAPI();
|
||||
public JOSMTestRules rule = new JOSMTestRules().projection().fakeAPI().territories();
|
||||
|
||||
WireMockServer wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock"));
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
wireMock.start();
|
||||
MapWithAIPreferenceHelper.setMapWithAIURLs(MapWithAIPreferenceHelper.getMapWithAIURLs().stream().map(map -> {
|
||||
map.put("url", getDefaultMapWithAIAPIForTest(wireMock,
|
||||
map.getOrDefault("url", MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API)));
|
||||
return map;
|
||||
}).collect(Collectors.toList()));
|
||||
MapWithAILayerInfoTest.setupMapWithAILayerInfo(wireMock);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
wireMock.stop();
|
||||
MapWithAILayerInfoTest.resetMapWithAILayerInfo();
|
||||
}
|
||||
|
||||
public static String getDefaultMapWithAIAPIForTest(WireMockServer wireMock, String url) {
|
||||
|
|
|
@ -8,7 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
|||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.awaitility.Durations;
|
||||
import org.junit.After;
|
||||
|
@ -17,6 +17,8 @@ import org.junit.Rule;
|
|||
import org.junit.Test;
|
||||
import org.openstreetmap.josm.data.coor.LatLon;
|
||||
import org.openstreetmap.josm.data.osm.BBox;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAILayerInfoTest;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
import org.openstreetmap.josm.tools.Territories;
|
||||
|
||||
|
@ -29,16 +31,14 @@ public class MapWithAIAvailabilityTest {
|
|||
|
||||
@Rule
|
||||
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
||||
public JOSMTestRules test = new JOSMTestRules().projection();
|
||||
public JOSMTestRules test = new JOSMTestRules().projection().territories();
|
||||
|
||||
WireMockServer wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock"));
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
wireMock.start();
|
||||
MapWithAIAvailability.setReleaseUrl(
|
||||
wireMock.baseUrl() + "/facebookmicrosites/Open-Mapping-At-Facebook/master/data/rapid_releases.geojson");
|
||||
Territories.initialize();
|
||||
MapWithAILayerInfoTest.setupMapWithAILayerInfo(wireMock);
|
||||
instance = DataAvailability.getInstance();
|
||||
LatLon temp = new LatLon(40, -100);
|
||||
await().atMost(Durations.TEN_SECONDS).until(() -> Territories.isIso3166Code("US", temp));
|
||||
|
@ -47,6 +47,7 @@ public class MapWithAIAvailabilityTest {
|
|||
@After
|
||||
public void tearDown() {
|
||||
wireMock.stop();
|
||||
MapWithAILayerInfoTest.resetMapWithAILayerInfo();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -82,13 +83,13 @@ public class MapWithAIAvailabilityTest {
|
|||
|
||||
@Test
|
||||
public void testNoURLs() {
|
||||
MapWithAIPreferenceHelper.setMapWithAIURLs(Collections.emptyList());
|
||||
new ArrayList<>(MapWithAILayerInfo.instance.getLayers()).forEach(i -> MapWithAILayerInfo.instance.remove(i));
|
||||
DataAvailability.getInstance();
|
||||
testgetDataLatLon();
|
||||
MapWithAIPreferenceHelper.setMapWithAIURLs(Collections.emptyList());
|
||||
MapWithAILayerInfo.instance.getLayers().forEach(i -> MapWithAILayerInfo.instance.remove(i));
|
||||
DataAvailability.getInstance();
|
||||
testHasDataLatLon();
|
||||
MapWithAIPreferenceHelper.setMapWithAIURLs(Collections.emptyList());
|
||||
MapWithAILayerInfo.instance.getLayers().forEach(i -> MapWithAILayerInfo.instance.remove(i));
|
||||
DataAvailability.getInstance();
|
||||
testHasDataBBox();
|
||||
}
|
||||
|
|
|
@ -12,7 +12,6 @@ import java.util.Arrays;
|
|||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -37,6 +36,8 @@ import org.openstreetmap.josm.gui.MainApplication;
|
|||
import org.openstreetmap.josm.gui.layer.GpxLayer;
|
||||
import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
||||
import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAILayerInfoTest;
|
||||
import org.openstreetmap.josm.spi.preferences.Config;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
|
@ -48,18 +49,15 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|||
public class MapWithAIDataUtilsTest {
|
||||
@Rule
|
||||
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
||||
public JOSMTestRules test = new JOSMTestRules().preferences().main().projection().fakeAPI();
|
||||
public JOSMTestRules test = new JOSMTestRules().preferences().main().projection().fakeAPI().territories();
|
||||
|
||||
WireMockServer wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock"));
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
wireMock.start();
|
||||
MapWithAIPreferenceHelper.setMapWithAIURLs(MapWithAIPreferenceHelper.getMapWithAIURLs().stream().map(map -> {
|
||||
map.put("url", getDefaultMapWithAIAPIForTest(
|
||||
map.getOrDefault("url", MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API)));
|
||||
return map;
|
||||
}).collect(Collectors.toList()));
|
||||
|
||||
MapWithAILayerInfoTest.resetMapWithAILayerInfo();
|
||||
MapWithAIDataUtils.setPaintStyleUrl(
|
||||
MapWithAIDataUtils.getPaintStyleUrl().replace(Config.getUrls().getJOSMWebsite(), wireMock.baseUrl()));
|
||||
}
|
||||
|
@ -67,14 +65,7 @@ public class MapWithAIDataUtilsTest {
|
|||
@After
|
||||
public void tearDown() {
|
||||
wireMock.stop();
|
||||
}
|
||||
|
||||
private String getDefaultMapWithAIAPIForTest(String url) {
|
||||
return getDefaultMapWithAIAPIForTest(url, "https://www.mapwith.ai");
|
||||
}
|
||||
|
||||
private String getDefaultMapWithAIAPIForTest(String url, String wireMockReplace) {
|
||||
return url.replace(wireMockReplace, wireMock.baseUrl());
|
||||
MapWithAILayerInfoTest.resetMapWithAILayerInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -184,22 +175,22 @@ public class MapWithAIDataUtilsTest {
|
|||
@Test
|
||||
public void testMapWithAIURLPreferences() {
|
||||
final String fakeUrl = "https://fake.url";
|
||||
assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream().noneMatch(
|
||||
map -> fakeUrl.equals(map.get("url"))), "fakeUrl shouldn't be in the current MapWithAI urls");
|
||||
MapWithAIPreferenceHelper.setMapWithAIUrl("Fake", fakeUrl, true, true);
|
||||
assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream()
|
||||
.anyMatch(map -> fakeUrl.equals(map.get("url"))), "fakeUrl should have been added");
|
||||
final List<Map<String, String>> urls = new ArrayList<>(MapWithAIPreferenceHelper.getMapWithAIURLs());
|
||||
.noneMatch(map -> fakeUrl.equals(map.getUrl())), "fakeUrl shouldn't be in the current MapWithAI urls");
|
||||
MapWithAIPreferenceHelper.setMapWithAIUrl(new MapWithAIInfo("Fake", fakeUrl), true, true);
|
||||
assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream()
|
||||
.anyMatch(map -> fakeUrl.equals(map.getUrl())), "fakeUrl should have been added");
|
||||
final List<MapWithAIInfo> urls = new ArrayList<>(MapWithAIPreferenceHelper.getMapWithAIUrl());
|
||||
assertEquals(2, urls.size(), "There should be two urls (fakeUrl and the default)");
|
||||
MapWithAIPreferenceHelper.setMapWithAIUrl("MapWithAI", MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API, true,
|
||||
true);
|
||||
MapWithAIPreferenceHelper.setMapWithAIUrl(
|
||||
new MapWithAIInfo("MapWithAI", MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API), true, true);
|
||||
assertTrue(
|
||||
MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream()
|
||||
.anyMatch(map -> MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API.equals(map.get("url"))),
|
||||
.anyMatch(map -> MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API.equals(map.getUrl())),
|
||||
"The default URL should exist");
|
||||
MapWithAIPreferenceHelper.setMapWithAIUrl("Fake2", fakeUrl, true, true);
|
||||
MapWithAIPreferenceHelper.setMapWithAIUrl(new MapWithAIInfo("Fake2", fakeUrl), true, false);
|
||||
assertEquals(1, MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream()
|
||||
.filter(map -> fakeUrl.equals(map.get("url"))).count(), "There should only be one fakeUrl");
|
||||
.filter(map -> fakeUrl.equals(map.getUrl())).count(), "There should only be one fakeUrl");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -15,7 +15,6 @@ import java.awt.Component;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.swing.Action;
|
||||
import javax.swing.JLabel;
|
||||
|
@ -40,6 +39,8 @@ import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer.ContinuousDownloadAction;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.commands.MapWithAIAddCommand;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAILayerInfoTest;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
|
@ -53,7 +54,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|||
public class MapWithAILayerTest {
|
||||
@Rule
|
||||
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
||||
public JOSMTestRules test = new JOSMTestRules().preferences().main().projection().fakeAPI();
|
||||
public JOSMTestRules test = new JOSMTestRules().preferences().main().projection().fakeAPI().territories();
|
||||
|
||||
WireMockServer wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock"));
|
||||
|
||||
|
@ -62,17 +63,14 @@ public class MapWithAILayerTest {
|
|||
@Before
|
||||
public void setUp() {
|
||||
wireMock.start();
|
||||
MapWithAIPreferenceHelper.setMapWithAIURLs(MapWithAIPreferenceHelper.getMapWithAIURLs().stream().map(map -> {
|
||||
map.put("url", map.getOrDefault("url", MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API)
|
||||
.replace("https://www.mapwith.ai", wireMock.baseUrl()));
|
||||
return map;
|
||||
}).collect(Collectors.toList()));
|
||||
MapWithAILayerInfoTest.setupMapWithAILayerInfo(wireMock);
|
||||
layer = new MapWithAILayer(new DataSet(), "test", null);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
wireMock.stop();
|
||||
MapWithAILayerInfoTest.resetMapWithAILayerInfo();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -107,7 +105,7 @@ public class MapWithAILayerTest {
|
|||
"The layer doesn't have its own switchlayer boolean");
|
||||
}
|
||||
|
||||
layer.setMapWithAIUrl("bad_url");
|
||||
layer.setMapWithAIUrl(new MapWithAIInfo("bad_url", "bad_url"));
|
||||
layer.setMaximumAddition(0);
|
||||
layer.setSwitchLayers(false);
|
||||
|
||||
|
|
|
@ -9,8 +9,6 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
|||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.openstreetmap.josm.tools.I18n.tr;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.awaitility.Durations;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
@ -21,6 +19,8 @@ import org.openstreetmap.josm.data.osm.BBox;
|
|||
import org.openstreetmap.josm.gui.MainApplication;
|
||||
import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerBadRequestException;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAILayerInfoTest;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
import org.openstreetmap.josm.tools.Utils;
|
||||
|
||||
|
@ -45,23 +45,20 @@ public class MapWithAIRemoteControlTest {
|
|||
*/
|
||||
@Rule
|
||||
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
||||
public JOSMTestRules test = new JOSMTestRules().main().projection();
|
||||
public JOSMTestRules test = new JOSMTestRules().main().projection().territories();
|
||||
|
||||
WireMockServer wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock"));
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
wireMock.start();
|
||||
MapWithAIPreferenceHelper.setMapWithAIURLs(MapWithAIPreferenceHelper.getMapWithAIURLs().stream().map(map -> {
|
||||
map.put("url", map.getOrDefault("url", MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API)
|
||||
.replace("https://www.mapwith.ai", wireMock.baseUrl()));
|
||||
return map;
|
||||
}).collect(Collectors.toList()));
|
||||
MapWithAILayerInfoTest.setupMapWithAILayerInfo(wireMock);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
wireMock.stop();
|
||||
MapWithAILayerInfoTest.resetMapWithAILayerInfo();
|
||||
}
|
||||
|
||||
private static MapWithAIRemoteControl newHandler(String url) throws RequestHandlerBadRequestException {
|
||||
|
@ -95,8 +92,8 @@ public class MapWithAIRemoteControlTest {
|
|||
*/
|
||||
@Test
|
||||
public void testNominalRequest() throws Exception {
|
||||
newHandler("https://localhost?url="
|
||||
+ Utils.encodeUrl(MapWithAIPreferenceHelper.getMapWithAIUrl().get(0).get("url"))).handle();
|
||||
newHandler("https://localhost?url=" + Utils.encodeUrl(MapWithAILayerInfo.instance.getLayers().get(0).getUrl()))
|
||||
.handle();
|
||||
assertFalse(MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty());
|
||||
|
||||
assertTrue(MapWithAIDataUtils.getLayer(false).getDataSet().getDataSourceBounds().isEmpty());
|
||||
|
@ -109,7 +106,7 @@ public class MapWithAIRemoteControlTest {
|
|||
assertFalse(MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty());
|
||||
|
||||
assertTrue(MapWithAIPreferenceHelper.getMapWithAIUrl().parallelStream()
|
||||
.anyMatch(map -> badUrl.equals(map.get("url"))));
|
||||
.anyMatch(map -> badUrl.equals(map.getUrl())));
|
||||
MainApplication.getLayerManager().removeLayer(MapWithAIDataUtils.getLayer(false));
|
||||
assertNotEquals(badUrl, MapWithAIPreferenceHelper.getMapWithAIUrl());
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ import org.openstreetmap.josm.gui.layer.GpxLayer;
|
|||
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
||||
import org.openstreetmap.josm.plugins.PluginException;
|
||||
import org.openstreetmap.josm.plugins.PluginInformation;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
|
||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
|
@ -38,7 +39,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|||
public class MapWithAIUploadHookTest {
|
||||
@Rule
|
||||
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
||||
public JOSMTestRules test = new JOSMTestRules().main().projection().preferences();
|
||||
public JOSMTestRules test = new JOSMTestRules().main().projection().preferences().territories();
|
||||
|
||||
/**
|
||||
* Test method for {@link MapWithAIUploadHook#modifyChangesetTags(Map)}.
|
||||
|
@ -81,7 +82,7 @@ public class MapWithAIUploadHookTest {
|
|||
Arrays.asList(tags.get("mapwithai:options").split(";")).contains("version=".concat(info.localversion)),
|
||||
"The version should be the localversion");
|
||||
|
||||
MapWithAIPreferenceHelper.setMapWithAIUrl("False URL", "false-url", true, true);
|
||||
MapWithAIPreferenceHelper.setMapWithAIUrl(new MapWithAIInfo("False URL", "false-url"), true, false);
|
||||
|
||||
tags.clear();
|
||||
|
||||
|
@ -94,7 +95,7 @@ public class MapWithAIUploadHookTest {
|
|||
List<String> split = Arrays.asList(tags.get("mapwithai:options").split(";"));
|
||||
assertEquals(2, split.size(), "There should be another option in mapwithai:options");
|
||||
assertTrue(split.contains("version=".concat(info.localversion)), "The version should match the local version");
|
||||
assertTrue(split.contains("url=false-url"), "The false-url should be shown in the changeset tag");
|
||||
assertTrue(split.contains("url_ids=false-url"), "The false-url should be shown in the changeset tag");
|
||||
|
||||
MapWithAIPreferenceHelper.setMaximumAddition(20, false);
|
||||
tags.clear();
|
||||
|
@ -102,7 +103,7 @@ public class MapWithAIUploadHookTest {
|
|||
split = Arrays.asList(tags.get("mapwithai:options").split(";"));
|
||||
assertEquals(3, split.size(), "There should be three ; in mapwithai:options");
|
||||
assertTrue(split.contains("version=".concat(info.localversion)), "The version should match the local version");
|
||||
assertTrue(split.contains("url=false-url"), "The false-url should be shown in the changeset tag");
|
||||
assertTrue(split.contains("url_ids=false-url"), "The false-url should be shown in the changeset tag");
|
||||
assertTrue(split.contains("maxadd=20"), "The maxadd should be 20");
|
||||
|
||||
BBox tBBox = new BBox(1, 0, 0, 1);
|
||||
|
@ -115,7 +116,7 @@ public class MapWithAIUploadHookTest {
|
|||
split = Arrays.asList(tags.get("mapwithai:options").split(";"));
|
||||
assertEquals(4, split.size(), "There should be four ; in mapwithai:options");
|
||||
assertTrue(split.contains("version=".concat(info.localversion)), "The version should match the local version");
|
||||
assertTrue(split.contains("url=false-url"), "The false-url should be shown in the changeset tag");
|
||||
assertTrue(split.contains("url_ids=false-url"), "The false-url should be shown in the changeset tag");
|
||||
assertTrue(split.contains("maxadd=20"), "The maxadd should be 20");
|
||||
assertTrue(split.contains("task=".concat(tBBox.toStringCSV(","))),
|
||||
"There should be a task in the mapwithai:options");
|
||||
|
|
|
@ -8,8 +8,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.openstreetmap.josm.tools.I18n.tr;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.Durations;
|
||||
import org.junit.After;
|
||||
|
@ -21,6 +19,7 @@ import org.openstreetmap.josm.gui.download.DownloadSettings;
|
|||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIPreferenceHelper;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
|
@ -34,11 +33,7 @@ public class MapWithAIDownloadReaderTest {
|
|||
@Before
|
||||
public void setUp() {
|
||||
wireMock.start();
|
||||
MapWithAIPreferenceHelper.setMapWithAIURLs(MapWithAIPreferenceHelper.getMapWithAIURLs().stream().map(map -> {
|
||||
map.put("url", getDefaultMapWithAIAPIForTest(
|
||||
map.getOrDefault("url", MapWithAIPreferenceHelper.DEFAULT_MAPWITHAI_API)));
|
||||
return map;
|
||||
}).collect(Collectors.toList()));
|
||||
MapWithAILayerInfo.instance.getLayers().forEach(i -> i.setUrl(getDefaultMapWithAIAPIForTest(i.getUrl())));
|
||||
}
|
||||
|
||||
@After
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
// License: GPL. For details, see LICENSE file.
|
||||
package org.openstreetmap.josm.plugins.mapwithai.gui.preferences;
|
||||
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.GetDataRunnableTest;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
|
||||
/**
|
||||
* This is currently (mostly) a utils class. It should be expanded to tests,
|
||||
* sometime.
|
||||
*
|
||||
* @author Taylor Smock
|
||||
*
|
||||
*/
|
||||
public class MapWithAILayerInfoTest {
|
||||
public static void setupMapWithAILayerInfo(WireMockServer wireMock) {
|
||||
resetMapWithAILayerInfo();
|
||||
MapWithAILayerInfo.instance.getLayers().stream()
|
||||
.forEach(i -> i.setUrl(GetDataRunnableTest.getDefaultMapWithAIAPIForTest(wireMock, i.getUrl())));
|
||||
MapWithAILayerInfo.instance.save();
|
||||
}
|
||||
|
||||
public static void resetMapWithAILayerInfo() {
|
||||
MapWithAILayerInfo.instance.clear();
|
||||
MapWithAILayerInfo.instance.getDefaultLayers().stream().filter(MapWithAIInfo::isDefaultEntry)
|
||||
.forEach(MapWithAILayerInfo.instance::add);
|
||||
MapWithAILayerInfo.instance.save();
|
||||
}
|
||||
}
|
|
@ -1,115 +0,0 @@
|
|||
// License: GPL. For details, see LICENSE file.
|
||||
package org.openstreetmap.josm.plugins.mapwithai.gui.preferences;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation.DataUrl;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAIURLPreferenceTable;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAIURLPreferenceTable.URLTableModel;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
|
||||
/**
|
||||
* @author Taylor Smock
|
||||
*
|
||||
*/
|
||||
public class MapWithAIURLPreferenceTableTest {
|
||||
@Rule
|
||||
public JOSMTestRules rule = new JOSMTestRules();
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link MapWithAIURLPreferenceTable#MapWithAIURLPreferenceTable}.
|
||||
*/
|
||||
@Test
|
||||
public void testMapWithAIURLPreferenceTable() {
|
||||
List<DataUrl> dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData()));
|
||||
MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls);
|
||||
assertEquals(4, table.getModel().getColumnCount(), "There should be four columns");
|
||||
assertEquals(1, table.getModel().getRowCount(), "There is only one entry");
|
||||
assertFalse(dataUrls.isEmpty(), "The backing list should not be empty");
|
||||
assertSame(dataUrls.get(0).getMap().getOrDefault("source", "no-source-here"), table.getModel().getValueAt(0, 0),
|
||||
"The backing map and the table should have the same entries");
|
||||
|
||||
dataUrls.add(0, new DataUrl("no-source", "no-url", true));
|
||||
|
||||
table.fireDataChanged();
|
||||
assertEquals(4, table.getModel().getColumnCount(), "The column count should not change");
|
||||
assertEquals(2, table.getModel().getRowCount(), "An additional DataUrl was added");
|
||||
assertFalse(dataUrls.isEmpty(), "The backing list should not be empty");
|
||||
assertSame(dataUrls.get(0).getMap().getOrDefault("source", "no-source-here"), table.getModel().getValueAt(0, 0),
|
||||
"The backing map and table should have the same entries");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link MapWithAIURLPreferenceTable#objectify}
|
||||
*/
|
||||
@Test
|
||||
public void testObjectify() {
|
||||
Map<String, String> map = new TreeMap<>();
|
||||
map.put("source", "this-is-a-string");
|
||||
map.put("enabled", Boolean.FALSE.toString());
|
||||
map.put("timeout", Integer.toString(50));
|
||||
map.put("maxnodedistance", Double.toString(1.2));
|
||||
Map<String, Object> objectifiedMap = MapWithAIURLPreferenceTable.objectify(map);
|
||||
assertTrue(objectifiedMap.get("source") instanceof String, "Source should be a string");
|
||||
assertTrue(objectifiedMap.get("enabled") instanceof Boolean, "Enabled should be a boolean");
|
||||
assertTrue(objectifiedMap.get("timeout") instanceof Integer, "Timeout should be an integer");
|
||||
assertTrue(objectifiedMap.get("maxnodedistance") instanceof Double, "Maxnodedistance should be a double");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link MapWithAIURLPreferenceTable#getSelectedItems}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetSelectedItems() {
|
||||
List<DataUrl> dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData()));
|
||||
MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls);
|
||||
table.addRowSelectionInterval(0, 0);
|
||||
assertTrue(table.getSelectedItems().parallelStream().allMatch(dataUrls::contains),
|
||||
"All selected objects should be in dataUrls");
|
||||
assertEquals(1, table.getSelectedItems().size(), "There should only be one selected item");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link MapWithAIURLPreferenceTable.URLTableModel#getColumnClass}
|
||||
*/
|
||||
@Test
|
||||
public void testGetColumnClasses() {
|
||||
List<DataUrl> dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData()));
|
||||
MapWithAIURLPreferenceTable table = new MapWithAIURLPreferenceTable(dataUrls);
|
||||
for (int i = 0; i < dataUrls.get(0).getDataList().size(); i++) {
|
||||
assertEquals(dataUrls.get(0).getDataList().get(i).getClass(), table.getModel().getColumnClass(i),
|
||||
"The classes should match");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link MapWithAIURLPreferenceTable.URLTableModel#setValueAt}
|
||||
*/
|
||||
@Test
|
||||
public void testSetValueAt() {
|
||||
List<DataUrl> dataUrls = new ArrayList<>(Arrays.asList(DataUrl.emptyData()));
|
||||
MapWithAIURLPreferenceTable.URLTableModel table = (URLTableModel) new MapWithAIURLPreferenceTable(dataUrls)
|
||||
.getModel();
|
||||
DataUrl initial = DataUrl.emptyData(); // Don't need to clone the "current" first entry
|
||||
dataUrls.add(initial);
|
||||
table.setValueAt("New Source", 0, 0);
|
||||
assertEquals("New Source", dataUrls.get(0).getDataList().get(0),
|
||||
"The source should be set and passed through to the dataUrls");
|
||||
assertEquals(2, dataUrls.size(), "There should be two entries still");
|
||||
table.setValueAt("", 0, 0);
|
||||
assertSame(initial, dataUrls.get(0), "The \"initial\" dataUrl should be sorted to be first");
|
||||
}
|
||||
}
|
Ładowanie…
Reference in New Issue