6ff8adb1468cd2eb3d8c063ad75dc490f6359bc2
[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.Collections;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import com.google.gson.JsonElement;
11 import com.google.gson.JsonNull;
12 import com.google.gson.JsonObject;
13
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;
18
19 public class IndirectGUIComponentCreator
20 {
21         private static final Map<String, String> standardComponentIDs = new HashMap<>();
22         private static final Map<String, String> standardComponentIDsUnmodifiable = Collections.unmodifiableMap(standardComponentIDs);
23
24         private static final Map<String, ComponentSupplier> componentSuppliers = new HashMap<>();
25
26         static
27         {
28                 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("standardComponentIDMapping.json"))
29                 {
30                         if (s == null)
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) ->
35                         {
36                                 try
37                                 {
38                                         addStandardComponentID(st, id);
39                                 }
40                                 catch (IllegalArgumentException e)
41                                 {
42                                         System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
43                                 }
44                         });
45                 }
46                 catch (IOException e)
47                 {
48                         System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
49                 }
50         }
51
52         public static void addStandardComponentID(String standardComponentID, String associatedComponentID)
53         {
54                 if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:"))
55                         throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
56                 standardComponentIDs.put(standardComponentID, associatedComponentID);
57         }
58
59         public static Map<String, String> getStandardComponentIDs()
60         {
61                 return standardComponentIDsUnmodifiable;
62         }
63
64         public static void setComponentSupplier(String className, ComponentSupplier componentSupplier)
65         {
66                 componentSuppliers.put(className, componentSupplier);
67         }
68
69         public static GUIComponent createComponent(ViewModelModifiable model, String id)
70         {
71                 return createComponent(model, id, (String) null);
72         }
73
74         public static GUIComponent createComponent(ViewModelModifiable model, String id, String name)
75         {
76                 return createComponent(model, id, JsonNull.INSTANCE, name);
77         }
78
79         public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
80         {
81                 return createComponent(model, id, params, null);
82         }
83
84         public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name)
85         {
86                 if (id != null)
87                 {
88                         String resolvedID = resolveID(id);
89                         if (resolvedID != null)
90                         {
91                                 if (resolvedID.startsWith("class:"))
92                                 {
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 + ")");
99                                 } else
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");
104                                 try
105                                 {
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);
114                                 }
115                                 catch (IOException e)
116                                 {
117                                         throw new UncheckedIOException(e);
118                                 }
119                         }
120                 }
121                 throw new RuntimeException("Could not get component supplier for ID " + id);
122         }
123
124         public static String resolveID(String id)
125         {
126                 if (id.startsWith("class:") || id.startsWith("file:"))
127                         return id;
128                 return standardComponentIDs.get(id);
129         }
130
131         private static void tryLoadComponentClass(String componentClassName)
132         {
133                 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
134         }
135
136         public static interface ComponentSupplier
137         {
138                 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);
139         }
140 }