Merge remote-tracking branch 'origin/development' into development
[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.Collection;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import com.google.gson.JsonElement;
11 import com.google.gson.JsonNull;
12
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;
17
18 public class IndirectGUIComponentCreator
19 {
20         private static final Map<String, String> standardComponentIDs = new HashMap<>();
21
22         private static final Map<String, ComponentSupplier> componentSuppliers = new HashMap<>();
23
24         static
25         {
26                 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json"))
27                 {
28                         if (s == null)
29                                 throw new IOException("Resource not found");
30                         Map<String, String> tmp = JsonHandler.readJson(s, Map.class);
31                         // don't use putAll to apply sanity checks
32                         tmp.forEach((st, id) ->
33                         {
34                                 try
35                                 {
36                                         addStandardComponentID(st, id);
37                                 }
38                                 catch (IllegalArgumentException e)
39                                 {
40                                         System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
41                                 }
42                         });
43                 }
44                 catch (IOException e)
45                 {
46                         System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
47                 }
48         }
49
50         public static void addStandardComponentID(String standardComponentID, String associatedComponentID)
51         {
52                 if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:"))
53                         throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
54                 standardComponentIDs.put(standardComponentID, associatedComponentID);
55         }
56
57         public static Collection<String> getStandardComponentIDs()
58         {
59                 return standardComponentIDs.keySet();
60         }
61
62         public static void setComponentSupplier(String className, ComponentSupplier componentSupplier)
63         {
64                 componentSuppliers.put(className, componentSupplier);
65         }
66
67         public static GUIComponent createComponent(ViewModelModifiable model, String id)
68         {
69                 return createComponent(model, id, (String) null);
70         }
71
72         public static GUIComponent createComponent(ViewModelModifiable model, String id, String name)
73         {
74                 return createComponent(model, id, JsonNull.INSTANCE, name);
75         }
76
77         public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
78         {
79                 return createComponent(model, id, params, null);
80         }
81
82         public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name)
83         {
84                 if (id != null)
85                 {
86                         String resolvedID;
87                         if (id.startsWith("class:") || id.startsWith("file:"))
88                                 resolvedID = id;
89                         else
90                                 resolvedID = standardComponentIDs.get(id);
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                         } else
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");
103                         try
104                         {
105                                 return SubmodelComponentSerializer.deserialize(model, resolvedID.substring(5), name, id, null);
106                         }
107                         catch (IOException e)
108                         {
109                                 throw new UncheckedIOException(e);
110                         }
111                 }
112                 throw new RuntimeException("Could not get component supplier for ID " + id);
113         }
114
115         private static void tryLoadComponentClass(String componentClassName)
116         {
117                 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
118         }
119
120         public static interface ComponentSupplier
121         {
122                 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);
123         }
124 }