Restructured serializing/deserializing
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / serializing / IndirectGUIComponentCreator.java
1 package net.mograsim.logic.model.serializing;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.UncheckedIOException;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import com.google.gson.JsonElement;
10 import com.google.gson.JsonNull;
11
12 import net.mograsim.logic.model.model.ViewModelModifiable;
13 import net.mograsim.logic.model.model.components.GUIComponent;
14 import net.mograsim.logic.model.snippets.CodeSnippetSupplier;
15 import net.mograsim.logic.model.util.JsonHandler;
16
17 public class IndirectGUIComponentCreator
18 {
19         private static final Map<String, String> standardComponentIDs = new HashMap<>();
20
21         private static final Map<String, ComponentSupplier> componentSuppliers = new HashMap<>();
22
23         static
24         {
25                 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json"))
26                 {
27                         if (s == null)
28                                 throw new IOException("Resource not found");
29                         Map<String, String> tmp = JsonHandler.readJson(s, Map.class);
30                         // don't use putAll to apply sanity checks
31                         tmp.forEach((st, id) ->
32                         {
33                                 try
34                                 {
35                                         addStandardComponentID(st, id);
36                                 }
37                                 catch (IllegalArgumentException e)
38                                 {
39                                         System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
40                                 }
41                         });
42                 }
43                 catch (IOException e)
44                 {
45                         System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
46                 }
47         }
48
49         public static void addStandardComponentID(String standardComponentID, String associatedComponentID)
50         {
51                 if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:"))
52                         throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
53                 standardComponentIDs.put(standardComponentID, associatedComponentID);
54         }
55
56         public static void setComponentSupplier(String className, ComponentSupplier componentSupplier)
57         {
58                 componentSuppliers.put(className, componentSupplier);
59         }
60
61         public static GUIComponent createComponent(ViewModelModifiable model, String id)
62         {
63                 return createComponent(model, id, (String) null);
64         }
65
66         public static GUIComponent createComponent(ViewModelModifiable model, String id, String name)
67         {
68                 return createComponent(model, id, JsonNull.INSTANCE, name);
69         }
70
71         public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
72         {
73                 return createComponent(model, id, params, null);
74         }
75
76         public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name)
77         {
78                 if (id != null)
79                 {
80                         String resolvedID;
81                         if (id.startsWith("class:") || id.startsWith("file:"))
82                                 resolvedID = id;
83                         else
84                                 resolvedID = standardComponentIDs.get(id);
85                         if (resolvedID.startsWith("class:"))
86                         {
87                                 String className = resolvedID.substring(6);
88                                 tryLoadComponentClass(className);
89                                 ComponentSupplier componentSupplier = componentSuppliers.get(className);
90                                 if (componentSupplier != null)
91                                         return componentSupplier.create(model, params, name);
92                         } else
93                         // we know id has to start with "file:" here
94                         // because standardComponentIDs only contains strings starting with "class:" or "file:"
95                         if (params != null && !JsonNull.INSTANCE.equals(params))
96                                 throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file");
97                         try
98                         {
99                                 return SubmodelComponentSerializer.deserialize(model, resolvedID.substring(5), name, id, null);
100                         }
101                         catch (IOException e)
102                         {
103                                 throw new UncheckedIOException(e);
104                         }
105                 }
106                 throw new RuntimeException("Could not get component supplier for ID " + id);
107         }
108
109         private static void tryLoadComponentClass(String componentClassName)
110         {
111                 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
112         }
113
114         public static interface ComponentSupplier
115         {
116                 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);
117         }
118 }