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.lang.reflect.InvocationTargetException;
7 import java.util.Collections;
8 import java.util.HashMap;
10 import java.util.Objects;
11 import java.util.function.Supplier;
13 import com.google.gson.JsonElement;
14 import com.google.gson.JsonNull;
15 import com.google.gson.JsonObject;
17 import net.mograsim.logic.model.model.ViewModelModifiable;
18 import net.mograsim.logic.model.model.components.GUIComponent;
19 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
20 import net.mograsim.logic.model.snippets.CodeSnippetSupplier;
21 import net.mograsim.logic.model.util.JsonHandler;
23 public class IndirectGUIComponentCreator
25 private static final Map<String, String> standardComponentIDs = new HashMap<>();
26 private static final Map<String, String> standardComponentIDsUnmodifiable = Collections.unmodifiableMap(standardComponentIDs);
28 private static final Map<String, ComponentSupplier> componentSuppliers = new HashMap<>();
29 private static final Map<String, ResourceLoader> resourceLoaders = new HashMap<>();
33 loadStandardComponentIDs(IndirectGUIComponentCreator.class.getResourceAsStream("standardComponentIDMapping.json"));
36 public static void loadStandardComponentIDs(InputStream standardComponentIdMappingStream)
38 try (InputStream s = standardComponentIdMappingStream)
41 throw new IOException("Resource not found");
42 Map<String, String> tmp = JsonHandler.readJson(s, Map.class);
43 // don't use putAll to apply sanity checks
44 tmp.forEach((st, id) ->
48 addStandardComponentID(st, id);
50 catch (IllegalArgumentException e)
52 System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
58 System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
62 public static void addStandardComponentID(String standardComponentID, String associatedComponentID)
64 if (!associatedComponentID.matches("(file|class|resource):.+"))
65 throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
66 standardComponentIDs.put(standardComponentID, associatedComponentID);
69 public static Map<String, String> getStandardComponentIDs()
71 return standardComponentIDsUnmodifiable;
74 public static void setComponentSupplier(String className, ComponentSupplier componentSupplier)
76 componentSuppliers.put(className, componentSupplier);
79 public static GUIComponent createComponent(ViewModelModifiable model, String id)
81 return createComponent(model, id, (String) null);
84 public static GUIComponent createComponent(ViewModelModifiable model, String id, String name)
86 return createComponent(model, id, JsonNull.INSTANCE, name);
89 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
91 return createComponent(model, id, params, null);
94 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name)
98 String resolvedID = resolveID(id);
99 if (resolvedID != null)
101 if (resolvedID.startsWith("class:"))
103 String className = resolvedID.substring(6);
104 tryLoadComponentClass(className);
105 ComponentSupplier componentSupplier = componentSuppliers.get(className);
106 if (componentSupplier != null)
107 return componentSupplier.create(model, params, name);
108 throw new IllegalArgumentException("Component supplier not found for ID " + id + " (resolved: " + resolvedID + ")");
109 } else if (params != null && !JsonNull.INSTANCE.equals(params))
110 throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file");
111 if (resolvedID.startsWith("resource:"))
113 String[] parts = resolvedID.split(":");
114 if (parts.length != 3)
115 throw new IllegalArgumentException("invaild resource id: " + resolvedID);
116 String rLoadID = parts[1];
117 String resID = parts[2];
120 ResourceLoader loader;
121 if (!resourceLoaders.containsKey(rLoadID))
123 Class<?> c = Class.forName(rLoadID);
124 if (ResourceLoader.class.isAssignableFrom(c))
125 loader = (ResourceLoader) c.getConstructor().newInstance();
127 loader = (ResourceLoader) Objects.requireNonNull(c.getMethod("resourceLoader").invoke(null));
128 resourceLoaders.put(rLoadID, loader);
131 loader = Objects.requireNonNull(resourceLoaders.get(parts[1]));
133 JsonObject jsonContents = JsonHandler.readJson(loader.loadResource(resID), JsonObject.class);
134 return loadComponentFromJsonObject(model, id, name, jsonContents);
136 catch (IOException e)
138 throw new UncheckedIOException(e);
140 catch (ClassCastException | ReflectiveOperationException e)
142 throw new IllegalArgumentException("invaild resource loader specified:" + parts[1], e);
144 } else if (resolvedID.startsWith("file:"))
148 String filename = resolvedID.substring(5);
149 JsonObject jsonContents = JsonHandler.readJson(filename, JsonObject.class);
150 return loadComponentFromJsonObject(model, id, name, jsonContents);
152 catch (IOException e)
154 throw new UncheckedIOException(e);
158 throw new IllegalArgumentException("unable to resolve/interpret id" + resolvedID);
162 throw new RuntimeException("Could not get component supplier for ID " + id);
165 public static String resolveID(String id)
167 if (id.matches("(file|class|resource):.+"))
169 return standardComponentIDs.get(id);
172 private static SubmodelComponent loadComponentFromJsonObject(ViewModelModifiable model, String id, String name, JsonObject jsonContents)
174 SerializablePojo jsonContentsAsSerializablePojo = JsonHandler.parser.fromJson(jsonContents, SerializablePojo.class);
175 if (jsonContentsAsSerializablePojo.version == null)
176 return LegacySubmodelComponentSerializer.deserialize(model,
177 JsonHandler.parser.fromJson(jsonContents, LegacySubmodelComponentParams.class), name, id, null);
178 return SubmodelComponentSerializer.deserialize(model, JsonHandler.parser.fromJson(jsonContents, SubmodelComponentParams.class),
182 public static void registerResourceLoader(ResourceLoader resourceLoader)
184 registerResourceLoader(resourceLoader, resourceLoader.getClass());
187 public static void registerResourceLoader(ResourceLoader resourceLoader, Class<?> reference)
189 resourceLoaders.put(reference.getName(), Objects.requireNonNull(resourceLoader));
192 public static void registerResourceLoader(ResourceLoader resourceLoader, String reference)
194 resourceLoaders.put(reference, Objects.requireNonNull(resourceLoader));
197 private static void tryLoadComponentClass(String componentClassName)
199 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
202 public static interface ComponentSupplier
204 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);