Component json files are now created with the version in the first line.
Removed a superfluous FileWriter in JsonHandler.
+mograsim version: 0.1.0
{
"type": "SimpleRectangularSubmodelComponent",
"width": 35.0,
+mograsim version: 0.1.0
{
"type": "SimpleRectangularSubmodelComponent",
"width": 35.0,
+mograsim version: 0.1.0
{
"type": "SimpleRectangularSubmodelComponent",
"width": 35.0,
import net.mograsim.logic.ui.model.components.GUIManualSwitch;
import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
import net.mograsim.logic.ui.model.components.SubmodelComponent;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams;
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.SubmodelComponentParams;
import net.mograsim.logic.ui.model.wires.GUIWire;
public class JsonExample
e.printStackTrace();
}
- SimpleRectangularSubmodelComponent adder = new GUIfulladder(model);
+ SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) GUICustomComponentCreator.create(model,
+ "FullAdder.json");
GUIManualSwitch swA = new GUIManualSwitch(model);
swA.moveTo(0, 0);
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
import java.util.Map;
import net.mograsim.logic.ui.model.ViewModelModifiable;
-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.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams;
+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;
/**
private static final String rectC = SimpleRectangularSubmodelComponent.class.getSimpleName();
/**
- * 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.
+ * 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
}
/**
- * 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.
+ * 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
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.SubmodelComponentParams;
import net.mograsim.logic.ui.model.wires.Pin;
public class SimpleRectangularSubmodelComponent extends SubmodelComponent
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.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.components.SubmodelComponentParams.ComponentCompositionParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerPinParams;
+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.Pin;
-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
{
--- /dev/null
+package net.mograsim.logic.ui.model.components;
+
+import java.io.IOException;
+import java.util.Map;
+
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.mograsim.logic.ui.util.JsonHandler;
+
+/**
+ * This class contains all the information necessary to create a new {@link SubmodelComponent}
+ */
+public class SubmodelComponentParams
+{
+ String type;
+ double width, height;
+ InterfacePinParams[] interfacePins;
+ ComponentCompositionParams composition;
+ 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 class ComponentCompositionParams
+ {
+ double innerScale;
+ InnerComponentParams[] subComps;
+ InnerWireParams[] innerWires;
+
+ public static class InnerComponentParams
+ {
+ public Point pos;
+ public String type;
+ public int logicWidth;
+ }
+ }
+
+ 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
+++ /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 java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-
-public class JsonHandler
-{
- private static Gson parser = new GsonBuilder().setPrettyPrinting().create();
-
- @SuppressWarnings("resource")
- public static <T> T readJson(String path, Class<T> type) throws IOException
- {
- FileReader reader = new FileReader(path);
- T params = parser.fromJson(new FileReader(path), type);
- reader.close();
- return params;
- }
-
- public static void writeJson(Object o, String path) throws IOException
- {
- @SuppressWarnings("resource")
- FileWriter writer = new FileWriter(path);
- writer.write(parser.toJson(o));
- writer.close(); // TODO: Insure that writer is closed
- }
-}
+++ /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
--- /dev/null
+package net.mograsim.logic.ui.util;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+public class JsonHandler
+{
+ private static Gson parser = new GsonBuilder().setPrettyPrinting().create();
+
+ @SuppressWarnings("resource")
+ public static <T> T readJson(String path, Class<T> type) throws IOException
+ {
+ FileReader reader = new FileReader(path);
+ BufferedReader bf = new BufferedReader(reader);
+ bf.readLine(); // Skip version
+ String json = bf.lines().dropWhile(s -> s.length() == 0 || s.charAt(0) != '{').reduce("", (x, y) -> x.concat(y));
+ T params = parser.fromJson(json, type);
+ reader.close();
+ return params;
+ }
+
+ public static void writeJson(Object o, String path) throws IOException
+ {
+ @SuppressWarnings("resource")
+ FileWriter writer = new FileWriter(path);
+ writer.write(String.format("mograsim version: %s\n", Version.current.toString()));
+ writer.write(parser.toJson(o));
+ writer.close(); // TODO: Insure that writer is closed
+ }
+}
--- /dev/null
+package net.mograsim.logic.ui.util;
+
+public final class Version
+{
+ public final static Version current = new Version(0, 1, 0);
+ public final int major, minor, patch;
+
+ public Version(int major, int minor, int patch)
+ {
+ super();
+ this.major = major;
+ this.minor = minor;
+ this.patch = patch;
+ }
+
+ public int[] getVersionNumbers()
+ {
+ return new int[] { major, minor, patch };
+ }
+
+ @Override
+ public String toString()
+ {
+ return major + "." + minor + "." + patch;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + major;
+ result = prime * result + minor;
+ result = prime * result + patch;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+ if (!(obj instanceof Version))
+ return false;
+ Version other = (Version) obj;
+ if (major != other.major)
+ return false;
+ if (minor != other.minor)
+ return false;
+ if (patch != other.patch)
+ return false;
+ return true;
+ }
+
+ public boolean is(int major)
+ {
+ return major != this.major;
+ }
+
+ public boolean is(int major, int minor)
+ {
+ return is(major) && this.minor == minor;
+ }
+
+ public boolean is(int major, int minor, int patch)
+ {
+ return is(major, minor) && this.patch == patch;
+ }
+}
\ No newline at end of file