Added DeserializedSubmodelComponent Editor project
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / serializing / IndirectGUIComponentCreator.java
1 package net.mograsim.logic.ui.serializing;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import com.google.gson.JsonElement;
10
11 import net.mograsim.logic.ui.model.ViewModelModifiable;
12 import net.mograsim.logic.ui.model.components.GUIComponent;
13 import net.mograsim.logic.ui.util.JsonHandler;
14
15 public class IndirectGUIComponentCreator
16 {
17         private static final Map<String, String> standardComponentIDs = new HashMap<>();
18
19         private static final Map<String, ComponentProvider> componentProviders = new HashMap<>();
20
21         static
22         {
23                 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardComponentIDMapping.json"))
24                 {
25                         if (s == null)
26                                 throw new IOException("Resource not found");
27                         Map<String, String> tmp = JsonHandler.readJson(s, Map.class);
28                         // don't use putAll to apply sanity checks
29                         tmp.forEach((st, id) ->
30                         {
31                                 try
32                                 {
33                                         addStandardComponentID(st, id);
34                                 }
35                                 catch (IllegalArgumentException e)
36                                 {
37                                         System.err.println("Component ID mapping contained illegal entry: " + e.getMessage());
38                                 }
39                         });
40                 }
41                 catch (IOException e)
42                 {
43                         System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage());
44                 }
45         }
46
47         public static Collection<String> getStandardComponentIDs()
48         {
49                 return standardComponentIDs.keySet();
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 void setComponentProvider(String className, ComponentProvider componentProvider)
60         {
61                 componentProviders.put(className, componentProvider);
62         }
63
64         public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
65         {
66                 if (id != null)
67                 {
68                         String resolvedID;
69                         if (id.startsWith("class:") || id.startsWith("file:"))
70                                 resolvedID = id;
71                         else
72                                 resolvedID = standardComponentIDs.get(id);
73                         if (resolvedID.startsWith("class:"))
74                         {
75                                 String className = resolvedID.substring(6);
76                                 tryLoadComponentClass(className);
77                                 ComponentProvider componentProvider = componentProviders.get(className);
78                                 if (componentProvider != null)
79                                         return componentProvider.create(model, params);
80                         } else
81                                 // we know id has to start with "file:" here
82                                 // because standardComponentIDs only contains strings starting with "class:" or "file:"
83                                 return SubmodelComponentDeserializer.create(model, resolvedID.substring(5));
84                 }
85                 throw new RuntimeException("Could not get component provider for ID " + id);
86         }
87
88         private static void tryLoadComponentClass(String componentClassName)
89         {
90                 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
91         }
92
93         public static interface ComponentProvider
94         {
95                 public GUIComponent create(ViewModelModifiable model, JsonElement params);
96         }
97 }