Restructured serializing classes
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / serializing / IndirectGUIComponentCreator.java
diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/IndirectGUIComponentCreator.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/IndirectGUIComponentCreator.java
new file mode 100644 (file)
index 0000000..f60b403
--- /dev/null
@@ -0,0 +1,79 @@
+package net.mograsim.logic.ui.serializing;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+
+import net.mograsim.logic.ui.model.ViewModelModifiable;
+import net.mograsim.logic.ui.model.components.GUIComponent;
+import net.mograsim.logic.ui.model.components.atomic.SimpleRectangularGUIGate;
+import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
+import net.mograsim.logic.ui.model.wires.WireCrossPoint;
+import net.mograsim.logic.ui.util.JsonHandler;
+
+public class IndirectGUIComponentCreator
+{
+       private final static Map<String, String> componentMapping;
+
+       static
+       {
+               Map<String, String> tmp;
+               try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./mapping.json"))
+               {
+                       tmp = JsonHandler.readJson(s, Map.class);
+               }
+               catch (IOException e)
+               {
+                       System.err.println("Failed to initialize component mapping; Components cannot be created from file.");
+                       e.printStackTrace();
+                       tmp = new HashMap<>();
+               }
+               componentMapping = tmp;
+       }
+
+       public static GUIComponent create(ViewModelModifiable model, String name, Map<String, Object> params)
+       {
+               try
+               {
+                       String path = componentMapping.get(name);
+                       if (path.startsWith("class:"))
+                       {
+                               path = path.substring(6);
+                               return createComponentFromClass(model, path, params);
+                       } else if (path.startsWith("file:"))
+                       {
+                               path = path.substring(5);
+                               return SubmodelComponentDeserializer.create(model, path);
+                       } else
+                               throw new IllegalArgumentException("Invalid submodel type! Type was neither prefixed by 'class:' nor by 'file:'");
+               }
+               catch (NullPointerException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException
+                               | SecurityException | ClassNotFoundException | IllegalArgumentException e)
+               {
+                       System.err.println("Failed to create requested component!");
+                       e.printStackTrace();
+                       return new SimpleRectangularSubmodelComponent(model, 1, "ERROR");
+               }
+       }
+
+       private static GUIComponent createComponentFromClass(ViewModelModifiable model, String classname, Map<String, Object> params)
+                       throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException,
+                       ClassNotFoundException
+       {
+               Class<?> c = Class.forName(classname);
+               Object comp;
+               if (SimpleRectangularGUIGate.class.isAssignableFrom(c) || WireCrossPoint.class.equals(c))
+               {
+                       Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class, int.class);
+                       comp = constructor.newInstance(model, ((Number) params.get(SimpleRectangularGUIGate.kLogicWidth)).intValue());
+               } else
+               {
+                       Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class);
+                       comp = constructor.newInstance(model);
+               }
+               return (GUIComponent) comp;
+       }
+}