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