1 package net.mograsim.logic.ui.model.components;
3 import java.io.IOException;
4 import java.lang.reflect.Constructor;
5 import java.lang.reflect.InvocationTargetException;
6 import java.util.HashMap;
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;
13 public class GUIComponentCreator
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
21 Map<String, String> tmp;
24 tmp = JsonHandler.readJson(componentMappingPath, Map.class);
28 System.err.println("Failed to initialize component mapping; Components cannot be created from file.");
30 tmp = new HashMap<>();
32 componentMapping = tmp;
35 public static GUIComponent create(ViewModelModifiable model, String name, Map<String, Object> params)
39 String path = componentMapping.get(name);
40 if (path.startsWith("class:"))
42 path = path.substring(6);
43 return createComponentFromClass(model, path, params);
44 } else if (path.startsWith("file:"))
46 path = path.substring(5);
47 return GUICustomComponentCreator.create(model, path);
49 throw new IllegalArgumentException("Invalid submodel type! Type was neither prefixed by 'class:' nor by 'file:'");
51 catch (NullPointerException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException
52 | SecurityException | ClassNotFoundException | IllegalArgumentException e)
54 System.err.println("Failed to create requested component!");
56 return new SimpleRectangularSubmodelComponent(model, 1, "ERROR");
60 private static GUIComponent createComponentFromClass(ViewModelModifiable model, String classname, Map<String, Object> params)
61 throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException,
62 ClassNotFoundException
64 Class<?> c = Class.forName(classname);
66 if (SimpleRectangularGUIGate.class.isAssignableFrom(c) || WireCrossPoint.class.equals(c))
68 Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class, int.class);
69 comp = constructor.newInstance(model, ((Number) params.get(SimpleRectangularGUIGate.kLogicWidth)).intValue());
72 Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class);
73 comp = constructor.newInstance(model);
75 return (GUIComponent) comp;