diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIInfo.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIInfo.java index 0d45974..2d7933f 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIInfo.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIInfo.java @@ -6,6 +6,7 @@ import static org.openstreetmap.josm.tools.I18n.tr; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonObject; +import javax.json.JsonValue; import javax.json.stream.JsonParser; import java.io.Serializable; @@ -30,10 +31,12 @@ import org.openstreetmap.josm.data.sources.SourceInfo; import org.openstreetmap.josm.data.sources.SourcePreferenceEntry; import org.openstreetmap.josm.tools.CheckParameterUtil; import org.openstreetmap.josm.tools.Logging; +import org.openstreetmap.josm.tools.Utils; public class MapWithAIInfo extends SourceInfo { + private static final String PARAMETER_STRING = "parameter"; private List categories; private JsonArray parameters; private Supplier> replacementTagsSupplier; @@ -300,11 +303,31 @@ public class MapWithAIInfo extends && Objects.equals(this.categories, other.categories) && Objects.equals(this.alreadyConflatedKey, other.alreadyConflatedKey) && (this.source == null || other.source == null || Objects.equals(this.source, other.source)) - && ((this.parameters == null && other.parameters == null) - || Objects.equals(this.parameters, other.parameters)); + && compareParameters(this.parameters, other.parameters); // CHECKSTYLE.ON: BooleanExpressionComplexity } + private static boolean compareParameters(JsonArray first, JsonArray second) { + if (Objects.equals(first, second)) { + return true; + } + if (first != null && second != null && first.size() == second.size()) { + for (JsonObject value : Utils.filteredCollection(first, JsonObject.class)) { + if (value.containsKey(PARAMETER_STRING) + && value.get(PARAMETER_STRING).getValueType() == JsonValue.ValueType.STRING + && Utils.filteredCollection(second, JsonObject.class).stream() + .filter(obj -> obj.containsKey(PARAMETER_STRING) + && obj.get(PARAMETER_STRING).getValueType() == JsonValue.ValueType.STRING) + .map(obj -> obj.getString(PARAMETER_STRING)) + .noneMatch(obj -> value.getString(PARAMETER_STRING).equals(obj))) { + return false; + } + } + return true; + } + return false; + } + private static final Map localizedCountriesCache = new HashMap<>(); static { localizedCountriesCache.put("", tr("Worldwide")); @@ -378,8 +401,9 @@ public class MapWithAIInfo extends private static List getParametersString(JsonArray parameters) { 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()); + .filter(map -> map.getBoolean("enabled", false)) + .filter(map -> map.containsKey(PARAMETER_STRING)).map(map -> map.getString(PARAMETER_STRING)) + .collect(Collectors.toList()); } diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/gui/preferences/mapwithai/AddMapWithAIPanel.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/gui/preferences/mapwithai/AddMapWithAIPanel.java index 1b19b50..8bb4e8f 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/gui/preferences/mapwithai/AddMapWithAIPanel.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/gui/preferences/mapwithai/AddMapWithAIPanel.java @@ -3,17 +3,6 @@ package org.openstreetmap.josm.plugins.mapwithai.gui.preferences.mapwithai; import static org.openstreetmap.josm.tools.I18n.tr; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.LayoutManager; -import java.awt.event.ItemEvent; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.concurrent.TimeUnit; - import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonArrayBuilder; @@ -29,6 +18,17 @@ import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.JTextComponent; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.LayoutManager; +import java.awt.event.ItemEvent; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + import org.openstreetmap.josm.actions.ExpertToggleAction; import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType; import org.openstreetmap.josm.data.imagery.TMSCachedTileLoaderJob; @@ -196,8 +196,8 @@ class AddMapWithAIPanel extends JPanel { JsonArrayBuilder builder = Json.createArrayBuilder(); for (Map.Entry> entry : parameters.entrySet()) { JsonObjectBuilder entryBuilder = Json.createObjectBuilder(); - entryBuilder.add("parameter", entry.getKey()); - entryBuilder.add("description", entry.getValue().a); + entryBuilder.add("description", entry.getKey()); + entryBuilder.add("parameter", entry.getValue().a); entryBuilder.add("enabled", entry.getValue().b); builder.add(entryBuilder.build()); }