X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fserializing%2FIndirectGUIComponentCreator.java;h=6ff8adb1468cd2eb3d8c063ad75dc490f6359bc2;hb=976f7f2be00457b5cda2489545635ccd076c9afd;hp=874a8a8b377912a1e6f4561a3d1de6fc0cd66ecf;hpb=b5d8c2d71e27350ea7c9314e40df5bb0584271cd;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java index 874a8a8b..6ff8adb1 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java @@ -2,25 +2,30 @@ package net.mograsim.logic.model.serializing; import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import com.google.gson.JsonElement; import com.google.gson.JsonNull; +import com.google.gson.JsonObject; import net.mograsim.logic.model.model.ViewModelModifiable; import net.mograsim.logic.model.model.components.GUIComponent; +import net.mograsim.logic.model.snippets.CodeSnippetSupplier; import net.mograsim.logic.model.util.JsonHandler; public class IndirectGUIComponentCreator { private static final Map standardComponentIDs = new HashMap<>(); + private static final Map standardComponentIDsUnmodifiable = Collections.unmodifiableMap(standardComponentIDs); private static final Map componentSuppliers = new HashMap<>(); static { - try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json")) + try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("standardComponentIDMapping.json")) { if (s == null) throw new IOException("Resource not found"); @@ -51,6 +56,11 @@ public class IndirectGUIComponentCreator standardComponentIDs.put(standardComponentID, associatedComponentID); } + public static Map getStandardComponentIDs() + { + return standardComponentIDsUnmodifiable; + } + public static void setComponentSupplier(String className, ComponentSupplier componentSupplier) { componentSuppliers.put(className, componentSupplier); @@ -75,26 +85,49 @@ public class IndirectGUIComponentCreator { if (id != null) { - String resolvedID; - if (id.startsWith("class:") || id.startsWith("file:")) - resolvedID = id; - else - resolvedID = standardComponentIDs.get(id); - if (resolvedID.startsWith("class:")) + String resolvedID = resolveID(id); + if (resolvedID != null) { - String className = resolvedID.substring(6); - tryLoadComponentClass(className); - ComponentSupplier componentSupplier = componentSuppliers.get(className); - if (componentSupplier != null) - return componentSupplier.create(model, params, name); - } else + if (resolvedID.startsWith("class:")) + { + String className = resolvedID.substring(6); + tryLoadComponentClass(className); + ComponentSupplier componentSupplier = componentSuppliers.get(className); + if (componentSupplier != null) + return componentSupplier.create(model, params, name); + throw new IllegalArgumentException("Component supplier not found for ID " + id + " (resolved: " + resolvedID + ")"); + } else // we know id has to start with "file:" here // because standardComponentIDs only contains strings starting with "class:" or "file:" - return SubmodelComponentDeserializer.create(model, resolvedID.substring(5), name); + if (params != null && !JsonNull.INSTANCE.equals(params)) + throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file"); + try + { + String filename = resolvedID.substring(5); + JsonObject jsonContents = JsonHandler.readJson(filename, JsonObject.class); + SerializablePojo jsonContentsAsSerializablePojo = JsonHandler.parser.fromJson(jsonContents, SerializablePojo.class); + if (jsonContentsAsSerializablePojo.version == null) + return LegacySubmodelComponentSerializer.deserialize(model, + JsonHandler.parser.fromJson(jsonContents, LegacySubmodelComponentParams.class), name, id, null); + return SubmodelComponentSerializer.deserialize(model, + JsonHandler.parser.fromJson(jsonContents, SubmodelComponentParams.class), name, id, null); + } + catch (IOException e) + { + throw new UncheckedIOException(e); + } + } } throw new RuntimeException("Could not get component supplier for ID " + id); } + public static String resolveID(String id) + { + if (id.startsWith("class:") || id.startsWith("file:")) + return id; + return standardComponentIDs.get(id); + } + private static void tryLoadComponentClass(String componentClassName) { CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");