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