f60b4034206953d7c3b79a2c2aff097287828ebe
[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.lang.reflect.Constructor;
6 import java.lang.reflect.InvocationTargetException;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import net.mograsim.logic.ui.model.ViewModelModifiable;
11 import net.mograsim.logic.ui.model.components.GUIComponent;
12 import net.mograsim.logic.ui.model.components.atomic.SimpleRectangularGUIGate;
13 import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
14 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
15 import net.mograsim.logic.ui.util.JsonHandler;
16
17 public class IndirectGUIComponentCreator
18 {
19         private final static Map<String, String> componentMapping;
20
21         static
22         {
23                 Map<String, String> tmp;
24                 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./mapping.json"))
25                 {
26                         tmp = JsonHandler.readJson(s, Map.class);
27                 }
28                 catch (IOException e)
29                 {
30                         System.err.println("Failed to initialize component mapping; Components cannot be created from file.");
31                         e.printStackTrace();
32                         tmp = new HashMap<>();
33                 }
34                 componentMapping = tmp;
35         }
36
37         public static GUIComponent create(ViewModelModifiable model, String name, Map<String, Object> params)
38         {
39                 try
40                 {
41                         String path = componentMapping.get(name);
42                         if (path.startsWith("class:"))
43                         {
44                                 path = path.substring(6);
45                                 return createComponentFromClass(model, path, params);
46                         } else if (path.startsWith("file:"))
47                         {
48                                 path = path.substring(5);
49                                 return SubmodelComponentDeserializer.create(model, path);
50                         } else
51                                 throw new IllegalArgumentException("Invalid submodel type! Type was neither prefixed by 'class:' nor by 'file:'");
52                 }
53                 catch (NullPointerException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException
54                                 | SecurityException | ClassNotFoundException | IllegalArgumentException e)
55                 {
56                         System.err.println("Failed to create requested component!");
57                         e.printStackTrace();
58                         return new SimpleRectangularSubmodelComponent(model, 1, "ERROR");
59                 }
60         }
61
62         private static GUIComponent createComponentFromClass(ViewModelModifiable model, String classname, Map<String, Object> params)
63                         throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException,
64                         ClassNotFoundException
65         {
66                 Class<?> c = Class.forName(classname);
67                 Object comp;
68                 if (SimpleRectangularGUIGate.class.isAssignableFrom(c) || WireCrossPoint.class.equals(c))
69                 {
70                         Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class, int.class);
71                         comp = constructor.newInstance(model, ((Number) params.get(SimpleRectangularGUIGate.kLogicWidth)).intValue());
72                 } else
73                 {
74                         Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class);
75                         comp = constructor.newInstance(model);
76                 }
77                 return (GUIComponent) comp;
78         }
79 }