import net.mograsim.logic.ui.model.components.SubmodelComponent;
import net.mograsim.logic.ui.model.components.mi.nandbased.GUIfulladder;
import net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder;
-import net.mograsim.logic.ui.model.components.params.SubComponentParams;
+import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
import net.mograsim.logic.ui.model.wires.GUIWire;
public class JsonExample
{
public static void main(String[] args)
{
- SimpleLogicUIStandalone.executeVisualisation(JsonExample::createHalfAdderExample);
+ SimpleLogicUIStandalone.executeVisualisation(JsonExample::refJsonFromJsonTest);
}
private static class TestComponent extends SimpleRectangularSubmodelComponent
super(model, 1, "Test");
setInputCount(1);
setSubmodelScale(.4);
- GUICustomComponentCreator.create(submodelModifiable, "HalfAdder.rc");
+ GUICustomComponentCreator.create(submodelModifiable, "HalfAdder.json");
}
}
- // Execute only after HalfAdder.rc has been created
+ // Execute only after HalfAdder.json has been created
public static void refJsonFromJsonTest(ViewModelModifiable model)
{
TestComponent t = new TestComponent(model);
- t.calculateParams().writeJson("Test.sc");
- SubmodelComponent c = GUICustomComponentCreator.create(model, "Test.sc");
+ t.calculateParams().writeJson("Test.json");
+ SubmodelComponent c = GUICustomComponentCreator.create(model, "Test.json");
c.moveTo(0, 50);
}
{
GUIhalfadder tmp = new GUIhalfadder(model);
tmp.moveTo(1000, 50);
- SubComponentParams p = tmp.calculateParams();
- SubComponentParams pC = tmp.calculateParams();
+ SubmodelComponentParams p = tmp.calculateParams();
try
{
- p.writeJson("HalfAdder.rc");
- pC.writeJson("HalfAdder.sc");
- p = SubComponentParams.readJson("HalfAdder.rc");
- pC = SubComponentParams.readJson("HalfAdder.sc");
+ p.writeJson("HalfAdder.json");
+ p = SubmodelComponentParams.readJson("HalfAdder.json");
}
catch (IOException e)
{
e.printStackTrace();
}
- SubmodelComponent adder = GUICustomComponentCreator.create(model, p, "");
- adder = GUICustomComponentCreator.create(model, pC, "");
- adder.moveTo(0, 200);
+ GUICustomComponentCreator.create(model, p, "");
}
@SuppressWarnings("unused") // for GUIWires being created
public static void createFromJsonExample(ViewModelModifiable model)
{
- SimpleRectangularSubmodelComponent tmp = new GUIhalfadder(model);
- tmp.moveTo(1000, 50);
- SubComponentParams p = tmp.calculateParams();
- try
- {
- p.writeJson("HalfAdder.rc");
- p = SubComponentParams.readJson("HalfAdder.rc");
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- tmp = new GUIfulladder(model);
- SubComponentParams pC = tmp.calculateParams();
+ SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model);
+ SubmodelComponentParams pC = tmp.calculateParams();
tmp.moveTo(1000, 100);
try
{
- pC.writeJson("FullAdder.sc");
- pC = SubComponentParams.readJson("FullAdder.sc");
+ pC.writeJson("FullAdder.json");
+ pC = SubmodelComponentParams.readJson("FullAdder.json");
}
catch (IOException e)
{
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
+import java.util.function.Supplier;
import net.haspamelodica.swt.helper.gcs.GeneralGC;
import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
import net.mograsim.logic.ui.model.ViewModelModifiable;
+import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
import net.mograsim.logic.ui.model.wires.Pin;
public abstract class GUIComponent
private final List<Runnable> redrawListeners;
private final Runnable redrawListenerForSubcomponents;
+ // Defines how the GUIComponent is referenced in SubmodelComponentParams
+ protected Supplier<String> identifierDelegate = () -> "class:".concat(getClass().getCanonicalName());
public GUIComponent(ViewModelModifiable model)
{
callRedrawListeners();
}
+ /**
+ * @return an identifier used to reference this GUIComponent inside of {@link SubmodelComponentParams}
+ */
public String getIdentifier()
{
- return "class:".concat(getClass().getCanonicalName());
+ return identifierDelegate.get();
}
}
\ No newline at end of file
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.params.ComponentCompositionParams;
+import net.mograsim.logic.ui.model.components.params.ComponentCompositionParams.InnerComponentParams;
+import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
+import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InnerWireParams;
+import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InterfacePinParams;
import net.mograsim.logic.ui.model.wires.GUIWire;
-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);
+ SubmodelComponentParams params = SubmodelComponentParams.readJson(path);
SubmodelComponent ret = create(model, params, path);
return ret;
}
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}
+ * @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, SubComponentParams params, String path)
+ public static SubmodelComponent create(ViewModelModifiable model, SubmodelComponentParams params, String path)
{
- // 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 = createSubmodelComponent(model, params);
+ }
+ comp.identifierDelegate = () -> "file:".concat(path);
+ initInnerComponents(comp, params.composition);
+ return comp;
+ }
- 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);
- }
+ // May return null
+ private static SimpleRectangularSubmodelComponent createRectComponent(ViewModelModifiable model, SubmodelComponentParams params)
+ {
+ try
+ {
+ 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);
+ rect.setSize(params.width, params.height);
+ rect.setInputCount(((Number) m.get(SimpleRectangularSubmodelComponent.kInCount)).intValue());
+ rect.setOutputCount(((Number) m.get(SimpleRectangularSubmodelComponent.kOutCount)).intValue());
+ return rect;
+ }
+ catch (ClassCastException | NullPointerException e)
+ {
+ System.err.println("Failed to specialize component!");
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ private static SubmodelComponent createSubmodelComponent(ViewModelModifiable model, SubmodelComponentParams params)
+ {
+ // 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)
+ {
+ comp.addSubmodelInterface(iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y);
}
- initSubmodelComponents(comp, params.composition);
return comp;
}
@SuppressWarnings("unused")
- private static void initSubmodelComponents(SubmodelComponent comp, GeneralComponentParams params)
+ private static void initInnerComponents(SubmodelComponent comp, ComponentCompositionParams params)
{
try
{
innerWire.path);
}
}
- catch (Exception e)
+ catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException
+ | ClassNotFoundException | IllegalArgumentException e)
{
- System.err.println("Failed to create custom component!");
+ System.err.println("Failed to initialize custom component!");
e.printStackTrace();
}
}
import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.params.SubComponentParams;
+import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
import net.mograsim.logic.ui.model.wires.Pin;
public class SimpleRectangularSubmodelComponent extends SubmodelComponent
}
@Override
- public SubComponentParams calculateParams()
+ public SubmodelComponentParams calculateParams()
{
- SubComponentParams ret = super.calculateParams();
+ SubmodelComponentParams ret = super.calculateParams();
ret.type = SimpleRectangularSubmodelComponent.class.getSimpleName();
Map<String, Object> m = new TreeMap<>();
m.put(kLabel, label);
import net.mograsim.logic.ui.LogicUIRenderer;
import net.mograsim.logic.ui.model.ViewModel;
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.InterfacePinParams;
+import net.mograsim.logic.ui.model.components.params.ComponentCompositionParams;
+import net.mograsim.logic.ui.model.components.params.ComponentCompositionParams.InnerComponentParams;
+import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
+import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InterfacePinParams;
import net.mograsim.logic.ui.model.wires.GUIWire;
import net.mograsim.logic.ui.model.wires.Pin;
-import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerPinParams;
-import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerWireParams;
+import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InnerPinParams;
+import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InnerWireParams;
public abstract class SubmodelComponent extends GUIComponent
{
}
}
- public SubComponentParams calculateParams()
+ /**
+ * @return {@link SubmodelComponentParams}, which describe this {@link SubmodelComponent}.
+ */
+ public SubmodelComponentParams calculateParams()
{
- SubComponentParams params = new SubComponentParams();
+ SubmodelComponentParams params = new SubmodelComponentParams();
params.type = SubmodelComponent.class.getSimpleName();
params.composition = calculateCompositionParams();
return params;
}
- protected GeneralComponentParams calculateCompositionParams()
+ protected ComponentCompositionParams calculateCompositionParams()
{
- GeneralComponentParams params = new GeneralComponentParams();
+ ComponentCompositionParams params = new ComponentCompositionParams();
params.innerScale = getSubmodelScale();
List<GUIComponent> compList = submodelModifiable.getComponents();
--- /dev/null
+package net.mograsim.logic.ui.model.components.params;
+
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InnerWireParams;
+
+public class ComponentCompositionParams
+{
+ public double innerScale;
+ public InnerComponentParams[] subComps;
+ public InnerWireParams[] innerWires;
+
+ public static class InnerComponentParams
+ {
+ public Point pos;
+ public String type;
+ public int logicWidth;
+ }
+}
+++ /dev/null
-package net.mograsim.logic.ui.model.components.params;
-
-import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
-import net.mograsim.logic.ui.model.components.params.SubComponentParams.InnerWireParams;
-
-public class GeneralComponentParams
-{
- public double innerScale;
- public InnerComponentParams[] subComps;
- public InnerWireParams[] innerWires;
-
- public static class InnerComponentParams
- {
- public Point pos;
- public String type;
- public int logicWidth;
- }
-}
+++ /dev/null
-package net.mograsim.logic.ui.model.components.params;
-
-import java.io.IOException;
-import java.util.Map;
-
-import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
-
-public class SubComponentParams
-{
- public String type;
- public final static String fileExtension = ".sc";
- public double width, height;
- public InterfacePinParams[] interfacePins;
- public GeneralComponentParams composition;
- public Map<String, Object> specialized;
-
- public static class InterfacePinParams
- {
- public Point location;
- public int logicWidth;
- }
-
- public static class InnerWireParams
- {
- public InnerPinParams pin1, pin2;
- public Point[] path;
- }
-
- public static class InnerPinParams
- {
- public int compId, pinIndex;
- }
-
- public static SubComponentParams readJson(String path) throws IOException
- {
- return JsonHandler.readJson(path, SubComponentParams.class);
- }
-
- /**
- * Writes this {@link SubComponentParams} object into a file in json format. The correct file extension is important! Check
- * {@link SubComponentParams}.fileExtension
- */
- public void writeJson(String path)
- {
- try
- {
- JsonHandler.writeJson(this, path);
- }
- catch (IOException e)
- {
- System.err.println("Failed to write SubComponentParams to file");
- e.printStackTrace();
- }
- }
-}
\ No newline at end of file
--- /dev/null
+package net.mograsim.logic.ui.model.components.params;
+
+import java.io.IOException;
+import java.util.Map;
+
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.mograsim.logic.ui.model.components.SubmodelComponent;
+
+/**
+ * This class contains all the information necessary to create a new {@link SubmodelComponent}
+ */
+public class SubmodelComponentParams
+{
+ public String type;
+ public double width, height;
+ public InterfacePinParams[] interfacePins;
+ public ComponentCompositionParams composition;
+ public Map<String, Object> specialized;
+
+ public static class InterfacePinParams
+ {
+ public Point location;
+ public int logicWidth;
+ }
+
+ public static class InnerWireParams
+ {
+ public InnerPinParams pin1, pin2;
+ public Point[] path;
+ }
+
+ public static class InnerPinParams
+ {
+ public int compId, pinIndex;
+ }
+
+ public static SubmodelComponentParams readJson(String path) throws IOException
+ {
+ return JsonHandler.readJson(path, SubmodelComponentParams.class);
+ }
+
+ /**
+ * Writes this {@link SubmodelComponentParams} object into a file in json format. The correct file extension is important! Check
+ * {@link SubmodelComponentParams}.fileExtension
+ */
+ public void writeJson(String path)
+ {
+ try
+ {
+ JsonHandler.writeJson(this, path);
+ }
+ catch (IOException e)
+ {
+ System.err.println("Failed to write SubComponentParams to file");
+ e.printStackTrace();
+ }
+ }
+}
\ No newline at end of file