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.Collections;
7 import java.util.HashMap;
11 import com.google.gson.JsonElement;
12 import com.google.gson.JsonNull;
14 import net.mograsim.logic.model.model.ViewModelModifiable;
15 import net.mograsim.logic.model.model.components.GUIComponent;
16 import net.mograsim.logic.model.snippets.CodeSnippetSupplier;
17 import net.mograsim.logic.model.util.JsonHandler;
19 public class IndirectGUIComponentCreator
21 private static final Map<String, String> standardComponentIDs = new HashMap<>();
22 private static final Set<String> standardComponentIDSetUnmodifiable = Collections.unmodifiableSet(standardComponentIDs.keySet());
24 private static final Map<String, ComponentSupplier> componentSuppliers = new HashMap<>();
28 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json"))
31 throw new IOException("Resource not found");
32 Map<String, String> tmp = JsonHandler.readJson(s, Map.class);
33 // don't use putAll to apply sanity checks
34 tmp.forEach((st, id) ->
38 addStandardComponentID(st, id);
40 catch (IllegalArgumentException e)
42 System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
48 System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
52 public static void addStandardComponentID(String standardComponentID, String associatedComponentID)
54 if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:"))
55 throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
56 standardComponentIDs.put(standardComponentID, associatedComponentID);
59 public static Set<String> getStandardComponentIDs()
61 return standardComponentIDSetUnmodifiable;
64 public static void setComponentSupplier(String className, ComponentSupplier componentSupplier)
66 componentSuppliers.put(className, componentSupplier);
69 public static GUIComponent createComponent(ViewModelModifiable model, String id)
71 return createComponent(model, id, (String) null);
74 public static GUIComponent createComponent(ViewModelModifiable model, String id, String name)
76 return createComponent(model, id, JsonNull.INSTANCE, name);
79 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
81 return createComponent(model, id, params, null);
84 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name)
89 if (id.startsWith("class:") || id.startsWith("file:"))
92 resolvedID = standardComponentIDs.get(id);
93 if (resolvedID.startsWith("class:"))
95 String className = resolvedID.substring(6);
96 tryLoadComponentClass(className);
97 ComponentSupplier componentSupplier = componentSuppliers.get(className);
98 if (componentSupplier != null)
99 return componentSupplier.create(model, params, name);
101 // we know id has to start with "file:" here
102 // because standardComponentIDs only contains strings starting with "class:" or "file:"
103 if (params != null && !JsonNull.INSTANCE.equals(params))
104 throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file");
107 return SubmodelComponentSerializer.deserialize(model, resolvedID.substring(5), name, id, null);
109 catch (IOException e)
111 throw new UncheckedIOException(e);
114 throw new RuntimeException("Could not get component supplier for ID " + id);
117 private static void tryLoadComponentClass(String componentClassName)
119 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
122 public static interface ComponentSupplier
124 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);