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