Removed ...core.model package from manifest
[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.HashMap;
6 import java.util.Map;
7
8 import com.google.gson.JsonElement;
9 import com.google.gson.JsonNull;
10
11 import net.mograsim.logic.model.model.ViewModelModifiable;
12 import net.mograsim.logic.model.model.components.GUIComponent;
13 import net.mograsim.logic.model.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, ComponentSupplier> componentSuppliers = 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 void addStandardComponentID(String standardComponentID, String associatedComponentID)
48         {
49                 if (!associatedComponentID.startsWith("file:") && !associatedComponentID.startsWith("class:"))
50                         throw new IllegalArgumentException("Unrecognized component ID format: " + associatedComponentID);
51                 standardComponentIDs.put(standardComponentID, associatedComponentID);
52         }
53
54         public static void setComponentSupplier(String className, ComponentSupplier componentSupplier)
55         {
56                 componentSuppliers.put(className, componentSupplier);
57         }
58
59         public static GUIComponent createComponent(ViewModelModifiable model, String id)
60         {
61                 return createComponent(model, id, (String) null);
62         }
63
64         public static GUIComponent createComponent(ViewModelModifiable model, String id, String name)
65         {
66                 return createComponent(model, id, JsonNull.INSTANCE, name);
67         }
68
69         public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params)
70         {
71                 return createComponent(model, id, params, null);
72         }
73
74         public static GUIComponent createComponent(ViewModelModifiable model, String id, JsonElement params, String name)
75         {
76                 if (id != null)
77                 {
78                         String resolvedID;
79                         if (id.startsWith("class:") || id.startsWith("file:"))
80                                 resolvedID = id;
81                         else
82                                 resolvedID = standardComponentIDs.get(id);
83                         if (resolvedID.startsWith("class:"))
84                         {
85                                 String className = resolvedID.substring(6);
86                                 tryLoadComponentClass(className);
87                                 ComponentSupplier componentSupplier = componentSuppliers.get(className);
88                                 if (componentSupplier != null)
89                                         return componentSupplier.create(model, params, name);
90                         } else
91                                 // we know id has to start with "file:" here
92                                 // because standardComponentIDs only contains strings starting with "class:" or "file:"
93                                 return SubmodelComponentDeserializer.create(model, resolvedID.substring(5), name);
94                 }
95                 throw new RuntimeException("Could not get component supplier for ID " + id);
96         }
97
98         private static void tryLoadComponentClass(String componentClassName)
99         {
100                 CodeSnippetSupplier.tryInvokeStaticInitializer(componentClassName, "Error loading component class %s: %s\n");
101         }
102
103         public static interface ComponentSupplier
104         {
105                 public GUIComponent create(ViewModelModifiable model, JsonElement params, String name);
106         }
107 }