From 9bde656ba15da281b347a8b87e9ed77bed9443e1 Mon Sep 17 00:00:00 2001 From: emmebi Date: Fri, 17 Mar 2023 12:13:47 +0000 Subject: [PATCH 1/3] Allows add-on properties of type `undefined` to change type Once an add-on property gets a type `undefined` it becomes unmutable. This change allows, only for the properties of type `undefined` to change type once. --- .../model/gamedata/MemoryDataStore.java | 19 +++------ .../model/gamedata/MemoryDataStoreTest.java | 40 ++++++++++++++++--- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/rptools/maptool/model/gamedata/MemoryDataStore.java b/src/main/java/net/rptools/maptool/model/gamedata/MemoryDataStore.java index 19b4168631..e68b9f8e59 100644 --- a/src/main/java/net/rptools/maptool/model/gamedata/MemoryDataStore.java +++ b/src/main/java/net/rptools/maptool/model/gamedata/MemoryDataStore.java @@ -17,17 +17,6 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; import net.rptools.lib.MD5Key; import net.rptools.maptool.model.Asset; import net.rptools.maptool.model.gamedata.data.DataType; @@ -38,6 +27,11 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + /** Class that implements the DataStore interface. */ public class MemoryDataStore implements DataStore { @@ -203,8 +197,7 @@ private DataValue setData(String type, String namespace, DataValue value) { var dataMap = namespaceDataMap.computeIfAbsent( new PropertyTypeNamespace(type, namespace), k -> new ConcurrentHashMap<>()); - if (existing == null) { - + if (existing == null || existing.getDataType() == DataType.UNDEFINED) { dataMap.put(value.getName(), value); } else { var newValue = DataType.convert(value, existing.getDataType()); diff --git a/src/test/java/net/rptools/maptool/model/gamedata/MemoryDataStoreTest.java b/src/test/java/net/rptools/maptool/model/gamedata/MemoryDataStoreTest.java index 6de93c2cdb..d221e74e83 100644 --- a/src/test/java/net/rptools/maptool/model/gamedata/MemoryDataStoreTest.java +++ b/src/test/java/net/rptools/maptool/model/gamedata/MemoryDataStoreTest.java @@ -14,19 +14,20 @@ */ package net.rptools.maptool.model.gamedata; -import static org.junit.jupiter.api.Assertions.*; - import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; import net.rptools.maptool.model.gamedata.data.DataType; import net.rptools.maptool.model.gamedata.data.DataValueFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import static org.junit.jupiter.api.Assertions.*; + class MemoryDataStoreTest { private MemoryDataStore mds1; @@ -1096,4 +1097,31 @@ void createNamespaceWithTypes() throws ExecutionException, InterruptedException DataType.UNDEFINED, mds.getPropertyDataType("testType2", "testNamespace3", "invalid").get())); } + + @Test + void emptyValueCanBeUpdated() throws ExecutionException, InterruptedException { + final String PROPERTY_TYPE = "pt"; + final String NAMESPACE = "ns"; + final String PROPERTY_NAME = "a"; + + var mdsWithUndefined = new MemoryDataStore(); + mdsWithUndefined + .createNamespaceWithTypes( + PROPERTY_TYPE, NAMESPACE, Map.of(PROPERTY_NAME, DataType.UNDEFINED)) + .get(); + assertEquals( + DataType.UNDEFINED, + mdsWithUndefined.getProperty(PROPERTY_TYPE, NAMESPACE, PROPERTY_NAME).get().getDataType()); + + mdsWithUndefined + .setProperty(PROPERTY_TYPE, NAMESPACE, DataValueFactory.fromString(PROPERTY_NAME, "1")) + .get(); + + assertEquals( + DataType.STRING, + mdsWithUndefined.getProperty(PROPERTY_TYPE, NAMESPACE, PROPERTY_NAME).get().getDataType()); + assertEquals( + "1", + mdsWithUndefined.getProperty(PROPERTY_TYPE, NAMESPACE, PROPERTY_NAME).get().asString()); + } } From 37c463be023579ca3fc4b1c2eae9999974f3bc31 Mon Sep 17 00:00:00 2001 From: emmebi Date: Fri, 17 Mar 2023 12:24:38 +0000 Subject: [PATCH 2/3] Spotless apply --- .../maptool/model/gamedata/MemoryDataStore.java | 9 ++++----- .../maptool/model/gamedata/MemoryDataStoreTest.java | 13 ++++++------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/rptools/maptool/model/gamedata/MemoryDataStore.java b/src/main/java/net/rptools/maptool/model/gamedata/MemoryDataStore.java index e68b9f8e59..250b50612e 100644 --- a/src/main/java/net/rptools/maptool/model/gamedata/MemoryDataStore.java +++ b/src/main/java/net/rptools/maptool/model/gamedata/MemoryDataStore.java @@ -17,6 +17,10 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; import net.rptools.lib.MD5Key; import net.rptools.maptool.model.Asset; import net.rptools.maptool.model.gamedata.data.DataType; @@ -27,11 +31,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.util.*; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; - /** Class that implements the DataStore interface. */ public class MemoryDataStore implements DataStore { diff --git a/src/test/java/net/rptools/maptool/model/gamedata/MemoryDataStoreTest.java b/src/test/java/net/rptools/maptool/model/gamedata/MemoryDataStoreTest.java index d221e74e83..4820b50bf5 100644 --- a/src/test/java/net/rptools/maptool/model/gamedata/MemoryDataStoreTest.java +++ b/src/test/java/net/rptools/maptool/model/gamedata/MemoryDataStoreTest.java @@ -14,19 +14,18 @@ */ package net.rptools.maptool.model.gamedata; +import static org.junit.jupiter.api.Assertions.*; + import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import net.rptools.maptool.model.gamedata.data.DataType; -import net.rptools.maptool.model.gamedata.data.DataValueFactory; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; - -import static org.junit.jupiter.api.Assertions.*; +import net.rptools.maptool.model.gamedata.data.DataType; +import net.rptools.maptool.model.gamedata.data.DataValueFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; class MemoryDataStoreTest { From a508c0bc79bc7a7aa1be226b1166e6e4c95963d9 Mon Sep 17 00:00:00 2001 From: emmebi Date: Fri, 17 Mar 2023 14:20:23 +0000 Subject: [PATCH 3/3] Enables add/update buttons on state type change Fixes #1711 --- .../TokenStatesController.java | 46 +++---------------- 1 file changed, 7 insertions(+), 39 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/ui/campaignproperties/TokenStatesController.java b/src/main/java/net/rptools/maptool/client/ui/campaignproperties/TokenStatesController.java index 8880fa6b17..aa2c7839f5 100644 --- a/src/main/java/net/rptools/maptool/client/ui/campaignproperties/TokenStatesController.java +++ b/src/main/java/net/rptools/maptool/client/ui/campaignproperties/TokenStatesController.java @@ -14,11 +14,7 @@ */ package net.rptools.maptool.client.ui.campaignproperties; -import java.awt.Color; -import java.awt.Component; -import java.awt.Graphics2D; -import java.awt.Rectangle; -import java.awt.Shape; +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; @@ -26,19 +22,10 @@ import java.io.File; import java.io.IOException; import java.text.ParseException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashMap; +import java.util.*; import java.util.List; -import java.util.Map; -import java.util.Set; import javax.swing.*; -import javax.swing.event.ChangeEvent; -import javax.swing.event.ChangeListener; -import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; +import javax.swing.event.*; import javax.swing.filechooser.FileFilter; import net.rptools.lib.MD5Key; import net.rptools.maptool.client.AppConstants; @@ -46,30 +33,9 @@ import net.rptools.maptool.client.swing.AbeillePanel; import net.rptools.maptool.client.swing.ColorWell; import net.rptools.maptool.client.ui.PreviewPanelFileChooser; -import net.rptools.maptool.client.ui.token.AbstractTokenOverlay; -import net.rptools.maptool.client.ui.token.BooleanTokenOverlay; -import net.rptools.maptool.client.ui.token.ColorDotTokenOverlay; -import net.rptools.maptool.client.ui.token.CornerImageTokenOverlay; -import net.rptools.maptool.client.ui.token.CrossTokenOverlay; -import net.rptools.maptool.client.ui.token.DiamondTokenOverlay; -import net.rptools.maptool.client.ui.token.FlowColorDotTokenOverlay; -import net.rptools.maptool.client.ui.token.FlowColorSquareTokenOverlay; -import net.rptools.maptool.client.ui.token.FlowDiamondTokenOverlay; -import net.rptools.maptool.client.ui.token.FlowImageTokenOverlay; -import net.rptools.maptool.client.ui.token.FlowTriangleTokenOverlay; -import net.rptools.maptool.client.ui.token.FlowYieldTokenOverlay; -import net.rptools.maptool.client.ui.token.ImageTokenOverlay; -import net.rptools.maptool.client.ui.token.OTokenOverlay; -import net.rptools.maptool.client.ui.token.ShadedTokenOverlay; -import net.rptools.maptool.client.ui.token.TriangleTokenOverlay; -import net.rptools.maptool.client.ui.token.XTokenOverlay; -import net.rptools.maptool.client.ui.token.YieldTokenOverlay; +import net.rptools.maptool.client.ui.token.*; import net.rptools.maptool.language.I18N; -import net.rptools.maptool.model.Asset; -import net.rptools.maptool.model.AssetManager; -import net.rptools.maptool.model.Campaign; -import net.rptools.maptool.model.CampaignProperties; -import net.rptools.maptool.model.Token; +import net.rptools.maptool.model.*; import net.rptools.maptool.model.drawing.AbstractTemplate.Quadrant; import net.rptools.maptool.util.StringUtil; @@ -382,6 +348,7 @@ public void actionPerformed(ActionEvent e) { // Change the enabled data components. } else if (TYPE.equals(name)) { enableDataComponents(); + changedUpdate(null); // Update the selected overlay } else if (UPDATE.equals(name)) { @@ -597,6 +564,7 @@ public void valueChanged(ListSelectionEvent e) { // Set the type and change components formPanel.getComboBox(TYPE).setSelectedIndex(type.ordinal()); enableDataComponents(); + changedUpdate(null); } }