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;
10 import com.google.gson.JsonElement;
11 import com.google.gson.JsonNull;
12 import com.google.gson.JsonObject;
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 Map<String, String> standardComponentIDsUnmodifiable = Collections.unmodifiableMap(standardComponentIDs);
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 Map<String, String> getStandardComponentIDs()
61 return standardComponentIDsUnmodifiable;
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)
88 String resolvedID = resolveID(id);
89 if (resolvedID != null)
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);
98 throw new IllegalArgumentException("Component supplier not found for ID " + id + " (resolved: " + resolvedID + ")");
100 // we know id has to start with "file:" here
101 // because standardComponentIDs only contains strings starting with "class:" or "file:"
102 if (params != null && !JsonNull.INSTANCE.equals(params))
103 throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file");
106 String filename = resolvedID.substring(5);
107 JsonObject jsonContents = JsonHandler.readJson(filename, JsonObject.class);
108 SerializablePojo jsonContentsAsSerializablePojo = JsonHandler.parser.fromJson(jsonContents, SerializablePojo.class);
109 if (jsonContentsAsSerializablePojo.version == null)
110 return LegacySubmodelComponentSerializer.deserialize(model,
111 JsonHandler.parser.fromJson(jsonContents, LegacySubmodelComponentParams.class), name, id, null);
112 return SubmodelComponentSerializer.deserialize(model,
113 JsonHandler.parser.fromJson(jsonContents, SubmodelComponentParams.class), name, id, null);
115 catch (IOException e)
117 throw new UncheckedIOException(e);
121 throw new RuntimeException("Could not get component supplier for ID " + id);
124 public static String resolveID(String id)
126 if (id.startsWith("class:") || id.startsWith("file:"))
128 return standardComponentIDs.get(id);
131 private static void tryLoadComponentClass(String componentClassName)
133 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
136 public static interface ComponentSupplier
138 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);