Implemented things needed for setHighLevelState
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / GUICustomComponentCreator.java
index c27b015..1e2034e 100644 (file)
@@ -1,45 +1,39 @@
 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.params.GeneralComponentParams;
-import net.mograsim.logic.ui.model.components.params.GeneralComponentParams.InnerComponentParams;
-import net.mograsim.logic.ui.model.components.params.SubComponentParams;
-import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerWireParams;
-import net.mograsim.logic.ui.model.components.params.SubComponentParams.InterfacePinParams;
+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;
 
-public class GUICustomComponentCreator
+/**
+ * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams}
+ */
+public final class GUICustomComponentCreator
 {
        private static final String rectC = SimpleRectangularSubmodelComponent.class.getSimpleName();
 
-       private static class CustomRectComponent extends SimpleRectangularSubmodelComponent
-       {
-               private String path;
-
-               protected CustomRectComponent(ViewModelModifiable model, int logicWidth, String label, String path)
-               {
-                       super(model, logicWidth, label);
-                       this.path = path;
-               }
-
-               @Override
-               public String getIdentifier()
-               {
-                       return "file:".concat(path);
-               }
-       }
-
+       /**
+        * Creates a {@link SubmodelComponent} from the {@link SubmodelComponentParams}, specified at the given path. The returned
+        * SubmodelComponent can also be e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the
+        * {@link SubmodelComponentParams} describe.
+        * 
+        * @param path The path of the file describing the {@link SubmodelComponentParams}, which define the new {@link SubmodelComponent}
+        * @return A new SubmodelComponent, as described in the file located at the given path
+        */
        public static SubmodelComponent create(ViewModelModifiable model, String path)
        {
                try
                {
-                       SubComponentParams params = SubComponentParams.readJson(path);
-                       SubmodelComponent ret = create(model, params, path);
+                       SubmodelComponentParams params = SubmodelComponentParams.readJson(path);
+                       SubmodelComponent ret = create(model, params);
                        return ret;
                }
                catch (IOException e)
@@ -47,112 +41,94 @@ public class GUICustomComponentCreator
                        System.err.println("Failed to construct GUICustomComponent. Parameters were not found.");
                        e.printStackTrace();
                }
-               return new CustomRectComponent(model, 0, "ERROR", "NONE");
+               return new SimpleRectangularSubmodelComponent(model, 0, "ERROR");
        }
 
        /**
-        * @param path This value is used when the new SubmodelComponent is an inner component to a different SubmodelComponent, which is being
-        *             saved to a file; Then, the new SubmodelComponent is referenced by its given path within the file.
+        * Creates a {@link SubmodelComponent} from the specified {@link SubmodelComponentParams}. The returned SubmodelComponent can also be
+        * e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the {@link SubmodelComponentParams} describe.
+        * 
+        * @param params The parameters describing the {@link SubmodelComponent}
+        * 
+        * @return A new SubmodelComponent, as described by the {@link SubmodelComponentParams}
         */
-       public static SubmodelComponent create(ViewModelModifiable model, SubComponentParams params, String path)
+       public static SubmodelComponent create(ViewModelModifiable model, SubmodelComponentParams params)
        {
-               // TODO: Clean up this mess
                SubmodelComponent comp = null;
                if (rectC.equals(params.type))
                {
-                       try
-                       {
-                               Map<String, Object> m = params.specialized;
-                               SimpleRectangularSubmodelComponent rect = new CustomRectComponent(model,
-                                               ((Number) m.get(SimpleRectangularSubmodelComponent.kLogicWidth)).intValue(),
-                                               (String) m.get(SimpleRectangularSubmodelComponent.kLabel), path);
-                               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());
-                               comp = rect;
-                       }
-                       catch (ClassCastException | NullPointerException e)
-                       {
-                               System.err.println("Failed to specialize component!");
-                               e.printStackTrace();
-                       }
+                       comp = createRectComponent(model, params);
                }
+
                if (comp == null)
                {
-                       // As SubmodelComponent is abstract, for now SubmodelComponents are instantiated as SimpleRectangularSubmodelComponents
-                       comp = new CustomRectComponent(model, 0, "", path);
-
-                       comp.setSubmodelScale(params.composition.innerScale);
-                       comp.setSize(params.width, params.height);
-                       for (InterfacePinParams iPinParams : params.interfacePins)
-                       {
-                               comp.addSubmodelInterface(iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y);
-                       }
+                       comp = createSubmodelComponent(model, params);
                }
-               initSubmodelComponents(comp, params.composition);
+               comp.identifierDelegate = () -> params.name;
+               initInnerComponents(comp, params.composition);
                return comp;
        }
 
-       @SuppressWarnings("unused")
-       private static void initSubmodelComponents(SubmodelComponent comp, GeneralComponentParams params)
+       // May return null
+       @SuppressWarnings("unchecked")
+       private static SimpleRectangularSubmodelComponent createRectComponent(ViewModelModifiable model, SubmodelComponentParams params)
        {
                try
                {
-                       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:'");
-                       }
+                       Map<String, Object> m = params.specialized;
+                       SimpleRectangularSubmodelComponent rect = new SimpleRectangularSubmodelComponent(model,
+                                       ((Number) m.get(SimpleRectangularSubmodelComponent.kLogicWidth)).intValue(),
+                                       (String) m.get(SimpleRectangularSubmodelComponent.kLabel));
+                       rect.setSubmodelScale(params.composition.innerScale);
+
+                       Object[] names = ((ArrayList<Object>) m.get(SimpleRectangularSubmodelComponent.kInCount)).toArray();
+                       rect.setInputPins(Arrays.copyOf(names, names.length, String[].class));
 
-                       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);
-                       }
+                       names = ((ArrayList<Object>) m.get(SimpleRectangularSubmodelComponent.kOutCount)).toArray();
+                       rect.setOutputPins(Arrays.copyOf(names, names.length, String[].class));
+
+                       return rect;
                }
-               catch (Exception e)
+               catch (ClassCastException | NullPointerException e)
                {
-                       System.err.println("Failed to create custom component!");
+                       System.err.println("Failed to specialize component!");
                        e.printStackTrace();
+                       return null;
                }
        }
 
-       private static GUIComponent createInnerComponentFromClass(SubmodelComponent parent, String classname, int logicWidth)
-                       throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
-                       NoSuchMethodException, SecurityException, ClassNotFoundException
+       private static SubmodelComponent createSubmodelComponent(ViewModelModifiable model, SubmodelComponentParams params)
        {
-               Class<?> c = Class.forName(classname);
-               Object comp;
-               try
+               // As SubmodelComponent is abstract, for now SubmodelComponents are instantiated as SimpleRectangularSubmodelComponents
+               SubmodelComponent comp = new SimpleRectangularSubmodelComponent(model, 0, "");
+               comp.setSubmodelScale(params.composition.innerScale);
+               comp.setSize(params.width, params.height);
+               for (InterfacePinParams iPinParams : params.interfacePins)
                {
-                       Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class);
-                       comp = constructor.newInstance(parent.submodelModifiable);
+                       comp.addSubmodelInterface(
+                                       new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y));
                }
-               catch (@SuppressWarnings("unused") NoSuchMethodException e)
+               return comp;
+       }
+
+       @SuppressWarnings("unused")
+       private static void initInnerComponents(SubmodelComponent comp, ComponentCompositionParams params)
+       {
+               GUIComponent[] components = new GUIComponent[params.subComps.length];
+               for (int i = 0; i < components.length; i++)
                {
-                       Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class, int.class);
-                       comp = constructor.newInstance(parent.submodelModifiable, logicWidth);
+                       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);
                }
 
-               if (comp instanceof GUIComponent)
-                       return (GUIComponent) comp;
-               throw new IllegalArgumentException("Class given as subcomponent was not a GUIComponent!");
+               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).getPin(innerWire.pin1.pinName),
+                                       comp.submodelModifiable.getComponents().get(innerWire.pin2.compId).getPin(innerWire.pin2.pinName), innerWire.path);
+               }
        }
 }