More helpful error for standard ID not occurring in standard mapping
[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 import java.util.Set;
10
11 import com.google.gson.JsonElement;
12 import com.google.gson.JsonNull;
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 Set<String> standardComponentIDSetUnmodifiable = Collections.unmodifiableSet(standardComponentIDs.keySet());
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 Set<String> getStandardComponentIDs()
60         {
61                 return standardComponentIDSetUnmodifiable;
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;
89                         if (id.startsWith("class:") || id.startsWith("file:"))
90                                 resolvedID = id;
91                         else
92                                 resolvedID = standardComponentIDs.get(id);
93                         if (resolvedID != null)
94                         {
95                                 if (resolvedID.startsWith("class:"))
96                                 {
97                                         String className = resolvedID.substring(6);
98                                         tryLoadComponentClass(className);
99                                         ComponentSupplier componentSupplier = componentSuppliers.get(className);
100                                         if (componentSupplier != null)
101                                                 return componentSupplier.create(model, params, name);
102                                 } else
103                                 // we know id has to start with "file:" here
104                                 // because standardComponentIDs only contains strings starting with "class:" or "file:"
105                                 if (params != null && !JsonNull.INSTANCE.equals(params))
106                                         throw new IllegalArgumentException("Can't give params to a component deserialized from a JSON file");
107                                 try
108                                 {
109                                         return SubmodelComponentSerializer.deserialize(model, resolvedID.substring(5), name, id, null);
110                                 }
111                                 catch (IOException e)
112                                 {
113                                         throw new UncheckedIOException(e);
114                                 }
115                         }
116                 }
117                 throw new RuntimeException("Could not get component supplier for ID " + id);
118         }
119
120         private static void tryLoadComponentClass(String componentClassName)
121         {
122                 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
123         }
124
125         public static interface ComponentSupplier
126         {
127                 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);
128         }
129 }