1 package net.mograsim.logic.model.serializing;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Collection;
6 import java.util.HashMap;
9 import com.google.gson.JsonElement;
10 import com.google.gson.JsonNull;
12 import net.mograsim.logic.model.model.ViewModelModifiable;
13 import net.mograsim.logic.model.model.components.GUIComponent;
14 import net.mograsim.logic.model.util.JsonHandler;
16 public class IndirectGUIComponentCreator
18 private static final Map<String, String> standardComponentIDs = new HashMap<>();
20 private static final Map<String, ComponentSupplier> componentSuppliers = new HashMap<>();
24 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json"))
27 throw new IOException("Resource not found");
28 Map<String, String> tmp = JsonHandler.readJson(s, Map.class);
29 // don't use putAll to apply sanity checks
30 tmp.forEach((st, id) ->
34 addStandardComponentID(st, id);
36 catch (IllegalArgumentException e)
38 System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
44 System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
48 public static void addStandardComponentID(String standardComponentID, String associatedComponentID)
50 if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:"))
51 throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
52 standardComponentIDs.put(standardComponentID, associatedComponentID);
55 public static Collection<String> getStandardComponentIDs()
57 return standardComponentIDs.keySet();
60 public static void setComponentSupplier(String className, ComponentSupplier componentSupplier)
62 componentSuppliers.put(className, componentSupplier);
65 public static GUIComponent createComponent(ViewModelModifiable model, String id)
67 return createComponent(model, id, (String) null);
70 public static GUIComponent createComponent(ViewModelModifiable model, String id, String name)
72 return createComponent(model, id, JsonNull.INSTANCE, name);
75 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
77 return createComponent(model, id, params, null);
80 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name)
85 if (id.startsWith("class:") || id.startsWith("file:"))
88 resolvedID = standardComponentIDs.get(id);
89 if (resolvedID.startsWith("class:"))
91 String className = resolvedID.substring(6);
92 tryLoadComponentClass(className);
93 ComponentSupplier componentSupplier = componentSuppliers.get(className);
94 if (componentSupplier != null)
95 return componentSupplier.create(model, params, name);
97 // we know id has to start with "file:" here
98 // because standardComponentIDs only contains strings starting with "class:" or "file:"
99 return SubmodelComponentDeserializer.create(model, resolvedID.substring(5), name);
101 throw new RuntimeException("Could not get component supplier for ID " + id);
104 private static void tryLoadComponentClass(String componentClassName)
106 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
109 public static interface ComponentSupplier
111 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);