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