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.Collection;
7 import java.util.HashMap;
10 import com.google.gson.JsonElement;
11 import com.google.gson.JsonNull;
13 import net.mograsim.logic.model.model.ViewModelModifiable;
14 import net.mograsim.logic.model.model.components.GUIComponent;
15 import net.mograsim.logic.model.snippets.CodeSnippetSupplier;
16 import net.mograsim.logic.model.util.JsonHandler;
18 public class IndirectGUIComponentCreator
20 private static final Map<String, String> standardComponentIDs = new HashMap<>();
22 private static final Map<String, ComponentSupplier> componentSuppliers = new HashMap<>();
26 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json"))
29 throw new IOException("Resource not found");
30 Map<String, String> tmp = JsonHandler.readJson(s, Map.class);
31 // don't use putAll to apply sanity checks
32 tmp.forEach((st, id) ->
36 addStandardComponentID(st, id);
38 catch (IllegalArgumentException e)
40 System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
46 System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
50 public static void addStandardComponentID(String standardComponentID, String associatedComponentID)
52 if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:"))
53 throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
54 standardComponentIDs.put(standardComponentID, associatedComponentID);
57 public static Collection<String> getStandardComponentIDs()
59 return standardComponentIDs.keySet();
62 public static void setComponentSupplier(String className, ComponentSupplier componentSupplier)
64 componentSuppliers.put(className, componentSupplier);
67 public static GUIComponent createComponent(ViewModelModifiable model, String id)
69 return createComponent(model, id, (String) null);
72 public static GUIComponent createComponent(ViewModelModifiable model, String id, String name)
74 return createComponent(model, id, JsonNull.INSTANCE, name);
77 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
79 return createComponent(model, id, params, null);
82 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name)
87 if (id.startsWith("class:") || id.startsWith("file:"))
90 resolvedID = standardComponentIDs.get(id);
91 if (resolvedID.startsWith("class:"))
93 String className = resolvedID.substring(6);
94 tryLoadComponentClass(className);
95 ComponentSupplier componentSupplier = componentSuppliers.get(className);
96 if (componentSupplier != null)
97 return componentSupplier.create(model, params, name);
99 // we know id has to start with "file:" here
100 // because standardComponentIDs only contains strings starting with "class:" or "file:"
101 if (params != null && !JsonNull.INSTANCE.equals(params))
102 throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file");
105 return SubmodelComponentSerializer.deserialize(model, resolvedID.substring(5), name, id, null);
107 catch (IOException e)
109 throw new UncheckedIOException(e);
112 throw new RuntimeException("Could not get component supplier for ID " + id);
115 private static void tryLoadComponentClass(String componentClassName)
117 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
120 public static interface ComponentSupplier
122 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);