Slight improvements in documentation.
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / GUICustomComponentCreator.java
index ea45b61..1e2034e 100644 (file)
@@ -1,16 +1,17 @@
 package net.mograsim.logic.ui.model.components;
 
 import java.io.IOException;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Map;
 
 import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerWireParams;
 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InterfacePinParams;
 import net.mograsim.logic.ui.model.wires.GUIWire;
+import net.mograsim.logic.ui.model.wires.MovablePin;
 
 /**
  * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams}
@@ -32,7 +33,7 @@ public final class GUICustomComponentCreator
                try
                {
                        SubmodelComponentParams params = SubmodelComponentParams.readJson(path);
-                       SubmodelComponent ret = create(model, params, path);
+                       SubmodelComponent ret = create(model, params);
                        return ret;
                }
                catch (IOException e)
@@ -48,11 +49,10 @@ public final class GUICustomComponentCreator
         * e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the {@link SubmodelComponentParams} describe.
         * 
         * @param params The parameters describing the {@link SubmodelComponent}
-        * @param path   This value is used when the new {@link SubmodelComponent} is an inner component to a different SubmodelComponent, which
-        *               is being saved to a file; Then, the new {@link SubmodelComponent} is referenced by its given path within the file.
+        * 
         * @return A new SubmodelComponent, as described by the {@link SubmodelComponentParams}
         */
-       public static SubmodelComponent create(ViewModelModifiable model, SubmodelComponentParams params, String path)
+       public static SubmodelComponent create(ViewModelModifiable model, SubmodelComponentParams params)
        {
                SubmodelComponent comp = null;
                if (rectC.equals(params.type))
@@ -64,12 +64,13 @@ public final class GUICustomComponentCreator
                {
                        comp = createSubmodelComponent(model, params);
                }
-               comp.identifierDelegate = () -> "file:".concat(path);
+               comp.identifierDelegate = () -> params.name;
                initInnerComponents(comp, params.composition);
                return comp;
        }
 
        // May return null
+       @SuppressWarnings("unchecked")
        private static SimpleRectangularSubmodelComponent createRectComponent(ViewModelModifiable model, SubmodelComponentParams params)
        {
                try
@@ -79,9 +80,13 @@ public final class GUICustomComponentCreator
                                        ((Number) m.get(SimpleRectangularSubmodelComponent.kLogicWidth)).intValue(),
                                        (String) m.get(SimpleRectangularSubmodelComponent.kLabel));
                        rect.setSubmodelScale(params.composition.innerScale);
-                       rect.setSize(params.width, params.height);
-                       rect.setInputCount(((Number) m.get(SimpleRectangularSubmodelComponent.kInCount)).intValue());
-                       rect.setOutputCount(((Number) m.get(SimpleRectangularSubmodelComponent.kOutCount)).intValue());
+
+                       Object[] names = ((ArrayList<Object>) m.get(SimpleRectangularSubmodelComponent.kInCount)).toArray();
+                       rect.setInputPins(Arrays.copyOf(names, names.length, String[].class));
+
+                       names = ((ArrayList<Object>) m.get(SimpleRectangularSubmodelComponent.kOutCount)).toArray();
+                       rect.setOutputPins(Arrays.copyOf(names, names.length, String[].class));
+
                        return rect;
                }
                catch (ClassCastException | NullPointerException e)
@@ -100,7 +105,8 @@ public final class GUICustomComponentCreator
                comp.setSize(params.width, params.height);
                for (InterfacePinParams iPinParams : params.interfacePins)
                {
-                       comp.addSubmodelInterface(iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y);
+                       comp.addSubmodelInterface(
+                                       new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y));
                }
                return comp;
        }
@@ -108,63 +114,21 @@ public final class GUICustomComponentCreator
        @SuppressWarnings("unused")
        private static void initInnerComponents(SubmodelComponent comp, ComponentCompositionParams params)
        {
-               try
+               GUIComponent[] components = new GUIComponent[params.subComps.length];
+               for (int i = 0; i < components.length; i++)
                {
-                       GUIComponent[] components = new GUIComponent[params.subComps.length];
-                       for (int i = 0; i < components.length; i++)
-                       {
-                               InnerComponentParams cParams = params.subComps[i];
-                               String path = cParams.type;
-                               if (path.startsWith("class:"))
-                               {
-                                       path = path.substring(6);
-                                       components[i] = createInnerComponentFromClass(comp, path, cParams.logicWidth);
-                                       components[i].moveTo(cParams.pos.x, cParams.pos.y);
-                               } else if (path.startsWith("file:"))
-                               {
-                                       path = path.substring(5);
-                                       components[i] = create(comp.submodelModifiable, path);
-                                       components[i].moveTo(cParams.pos.x, cParams.pos.y);
-                               } else
-                                       throw new IllegalArgumentException("Invalid submodel type! Type was neither prefixed by 'class:' nor by 'file:'");
-                       }
-
-                       for (int i = 0; i < params.innerWires.length; i++)
-                       {
-                               InnerWireParams innerWire = params.innerWires[i];
-                               new GUIWire(comp.submodelModifiable,
-                                               comp.submodelModifiable.getComponents().get(innerWire.pin1.compId).getPins().get(innerWire.pin1.pinIndex),
-                                               comp.submodelModifiable.getComponents().get(innerWire.pin2.compId).getPins().get(innerWire.pin2.pinIndex),
-                                               innerWire.path);
-                       }
+                       InnerComponentParams cParams = params.subComps[i];
+                       String path = cParams.name;
+                       components[i] = GUIComponentCreator.create(comp.submodelModifiable, cParams.name, cParams.params);
+                       components[i].moveTo(cParams.pos.x, cParams.pos.y);
                }
-               catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException
-                               | ClassNotFoundException | IllegalArgumentException e)
-               {
-                       System.err.println("Failed to initialize custom component!");
-                       e.printStackTrace();
-               }
-       }
 
-       private static GUIComponent createInnerComponentFromClass(SubmodelComponent parent, String classname, int logicWidth)
-                       throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
-                       NoSuchMethodException, SecurityException, ClassNotFoundException
-       {
-               Class<?> c = Class.forName(classname);
-               Object comp;
-               try
-               {
-                       Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class);
-                       comp = constructor.newInstance(parent.submodelModifiable);
-               }
-               catch (@SuppressWarnings("unused") NoSuchMethodException e)
+               for (int i = 0; i < params.innerWires.length; i++)
                {
-                       Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class, int.class);
-                       comp = constructor.newInstance(parent.submodelModifiable, logicWidth);
+                       InnerWireParams innerWire = params.innerWires[i];
+                       new GUIWire(comp.submodelModifiable,
+                                       comp.submodelModifiable.getComponents().get(innerWire.pin1.compId).getPin(innerWire.pin1.pinName),
+                                       comp.submodelModifiable.getComponents().get(innerWire.pin2.compId).getPin(innerWire.pin2.pinName), innerWire.path);
                }
-
-               if (comp instanceof GUIComponent)
-                       return (GUIComponent) comp;
-               throw new IllegalArgumentException("Class given as subcomponent was not a GUIComponent!");
        }
 }