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;
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<>();
21 private static final Map<String, String> standardComponentIDsUnmodifiable = Collections.unmodifiableMap(standardComponentIDs);
23 private static final Map<String, ComponentSupplier> componentSuppliers = new HashMap<>();
27 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("standardComponentIDMapping.json"))
30 throw new IOException("Resource not found");
31 Map<String, String> tmp = JsonHandler.readJson(s, Map.class);
32 // don't use putAll to apply sanity checks
33 tmp.forEach((st, id) ->
37 addStandardComponentID(st, id);
39 catch (IllegalArgumentException e)
41 System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
47 System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
51 public static void addStandardComponentID(String standardComponentID, String associatedComponentID)
53 if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:"))
54 throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
55 standardComponentIDs.put(standardComponentID, associatedComponentID);
58 public static Map<String, String> getStandardComponentIDs()
60 return standardComponentIDsUnmodifiable;
63 public static void setComponentSupplier(String className, ComponentSupplier componentSupplier)
65 componentSuppliers.put(className, componentSupplier);
68 public static GUIComponent createComponent(ViewModelModifiable model, String id)
70 return createComponent(model, id, (String) null);
73 public static GUIComponent createComponent(ViewModelModifiable model, String id, String name)
75 return createComponent(model, id, JsonNull.INSTANCE, name);
78 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
80 return createComponent(model, id, params, null);
83 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name)
87 String resolvedID = resolveID(id);
88 if (resolvedID != null)
90 if (resolvedID.startsWith("class:"))
92 String className = resolvedID.substring(6);
93 tryLoadComponentClass(className);
94 ComponentSupplier componentSupplier = componentSuppliers.get(className);
95 if (componentSupplier != null)
96 return componentSupplier.create(model, params, name);
97 throw new IllegalArgumentException("Component supplier not found for ID " + id + " (resolved: " + resolvedID + ")");
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);
113 throw new RuntimeException("Could not get component supplier for ID " + id);
116 public static String resolveID(String id)
118 if (id.startsWith("class:") || id.startsWith("file:"))
120 return standardComponentIDs.get(id);
123 private static void tryLoadComponentClass(String componentClassName)
125 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
128 public static interface ComponentSupplier
130 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);