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.Collections;
8 import java.util.HashMap;
12 import com.google.gson.JsonElement;
13 import com.google.gson.JsonNull;
15 import net.mograsim.logic.model.model.ViewModelModifiable;
16 import net.mograsim.logic.model.model.components.GUIComponent;
17 import net.mograsim.logic.model.snippets.CodeSnippetSupplier;
18 import net.mograsim.logic.model.util.JsonHandler;
20 public class IndirectGUIComponentCreator
22 private static final Map<String, String> standardComponentIDs = new HashMap<>();
23 private static final Set<String> standardComponentIDSetUnmodifiable = Collections.unmodifiableSet(standardComponentIDs.keySet());
25 private static final Map<String, ComponentSupplier> componentSuppliers = new HashMap<>();
29 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json"))
32 throw new IOException("Resource not found");
33 Map<String, String> tmp = JsonHandler.readJson(s, Map.class);
34 // don't use putAll to apply sanity checks
35 tmp.forEach((st, id) ->
39 addStandardComponentID(st, id);
41 catch (IllegalArgumentException e)
43 System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
49 System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
53 public static void addStandardComponentID(String standardComponentID, String associatedComponentID)
55 if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:"))
56 throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
57 standardComponentIDs.put(standardComponentID, associatedComponentID);
60 public static Collection<String> getStandardComponentIDs()
62 return standardComponentIDSetUnmodifiable;
65 public static void setComponentSupplier(String className, ComponentSupplier componentSupplier)
67 componentSuppliers.put(className, componentSupplier);
70 public static GUIComponent createComponent(ViewModelModifiable model, String id)
72 return createComponent(model, id, (String) null);
75 public static GUIComponent createComponent(ViewModelModifiable model, String id, String name)
77 return createComponent(model, id, JsonNull.INSTANCE, name);
80 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
82 return createComponent(model, id, params, null);
85 public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name)
90 if (id.startsWith("class:") || id.startsWith("file:"))
93 resolvedID = standardComponentIDs.get(id);
94 if (resolvedID.startsWith("class:"))
96 String className = resolvedID.substring(6);
97 tryLoadComponentClass(className);
98 ComponentSupplier componentSupplier = componentSuppliers.get(className);
99 if (componentSupplier != null)
100 return componentSupplier.create(model, params, name);
102 // we know id has to start with "file:" here
103 // because standardComponentIDs only contains strings starting with "class:" or "file:"
104 if (params != null && !JsonNull.INSTANCE.equals(params))
105 throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file");
108 return SubmodelComponentSerializer.deserialize(model, resolvedID.substring(5), name, id, null);
110 catch (IOException e)
112 throw new UncheckedIOException(e);
115 throw new RuntimeException("Could not get component supplier for ID " + id);
118 private static void tryLoadComponentClass(String componentClassName)
120 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
123 public static interface ComponentSupplier
125 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);