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