X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fserializing%2FIndirectGUIComponentCreator.java;fp=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fserializing%2FIndirectGUIComponentCreator.java;h=874a8a8b377912a1e6f4561a3d1de6fc0cd66ecf;hb=b5d8c2d71e27350ea7c9314e40df5bb0584271cd;hp=0000000000000000000000000000000000000000;hpb=69cb6725ef670328959d55649257ded6ac924b33;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 new file mode 100644 index 00000000..874a8a8b --- /dev/null +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java @@ -0,0 +1,107 @@ +package net.mograsim.logic.model.serializing; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; + +import net.mograsim.logic.model.model.ViewModelModifiable; +import net.mograsim.logic.model.model.components.GUIComponent; +import net.mograsim.logic.model.util.JsonHandler; + +public class IndirectGUIComponentCreator +{ + private static final Map standardComponentIDs = new HashMap<>(); + + private static final Map componentSuppliers = new HashMap<>(); + + static + { + try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json")) + { + if (s == null) + throw new IOException("Resource not found"); + Map tmp = JsonHandler.readJson(s, Map.class); + // don't use putAll to apply sanity checks + tmp.forEach((st, id) -> + { + try + { + addStandardComponentID(st, id); + } + catch (IllegalArgumentException e) + { + System.err.println("Component ID mapping contained illegal entry: " + e.getMessage()); + } + }); + } + catch (IOException e) + { + System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage()); + } + } + + public static void addStandardComponentID(String standardComponentID, String associatedComponentID) + { + if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:")) + throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID); + standardComponentIDs.put(standardComponentID, associatedComponentID); + } + + public static void setComponentSupplier(String className, ComponentSupplier componentSupplier) + { + componentSuppliers.put(className, componentSupplier); + } + + public static GUIComponent createComponent(ViewModelModifiable model, String id) + { + return createComponent(model, id, (String) null); + } + + public static GUIComponent createComponent(ViewModelModifiable model, String id, String name) + { + return createComponent(model, id, JsonNull.INSTANCE, name); + } + + public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params) + { + return createComponent(model, id, params, null); + } + + public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name) + { + if (id != null) + { + String resolvedID; + if (id.startsWith("class:") || id.startsWith("file:")) + resolvedID = id; + else + resolvedID = standardComponentIDs.get(id); + 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); + } 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); + } + throw new RuntimeException("Could not get component supplier for ID " + id); + } + + private static void tryLoadComponentClass(String componentClassName) + { + CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n"); + } + + public static interface ComponentSupplier + { + public GUIComponent create(ViewModelModifiable model, JsonElement params, String name); + } +} \ No newline at end of file