1 package net.mograsim.logic.model.serializing;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.UncheckedIOException;
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.snippets.CodeSnippetSupplier;
15 import net.mograsim.logic.model.util.JsonHandler;
17 public class IndirectGUIComponentCreator
19 private static final Map<String, String> standardComponentIDs = new HashMap<>();
21 private static final Map<String, ComponentSupplier> componentSuppliers = new HashMap<>();
25 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json"))
28 throw new IOException("Resource not found");
29 Map<String, String> tmp = JsonHandler.readJson(s, Map.class);
30 // don't use putAll to apply sanity checks
31 tmp.forEach((st, id) ->
35 addStandardComponentID(st, id);
37 catch (IllegalArgumentException e)
39 System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
45 System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
49 public static void addStandardComponentID(String standardComponentID, String associatedComponentID)
51 if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:"))
52 throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
53 standardComponentIDs.put(standardComponentID, associatedComponentID);
56 public static void setComponentSupplier(String className, ComponentSupplier componentSupplier)
58 componentSuppliers.put(className, componentSupplier);
61 public static GUIComponent createComponent(ViewModelModifiable model, String id)
63 return createComponent(model, id, (String) null);
66 public static GUIComponent createComponent(ViewModelModifiable model, String id, String name)
68 return createComponent(model, id, JsonNull.INSTANCE, name);
71 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
73 return createComponent(model, id, params, null);
76 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name)
81 if (id.startsWith("class:") || id.startsWith("file:"))
84 resolvedID = standardComponentIDs.get(id);
85 if (resolvedID.startsWith("class:"))
87 String className = resolvedID.substring(6);
88 tryLoadComponentClass(className);
89 ComponentSupplier componentSupplier = componentSuppliers.get(className);
90 if (componentSupplier != null)
91 return componentSupplier.create(model, params, name);
93 // we know id has to start with "file:" here
94 // because standardComponentIDs only contains strings starting with "class:" or "file:"
95 if (params != null && !JsonNull.INSTANCE.equals(params))
96 throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file");
99 return SubmodelComponentSerializer.deserialize(model, resolvedID.substring(5), name, id, null);
101 catch (IOException e)
103 throw new UncheckedIOException(e);
106 throw new RuntimeException("Could not get component supplier for ID " + id);
109 private static void tryLoadComponentClass(String componentClassName)
111 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
114 public static interface ComponentSupplier
116 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);