a1f2c8237f1f4d744370c99c92f5b34581fd1c83
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / serializing / SubmodelComponentParams.java
1 package net.mograsim.logic.ui.serializing;
2
3 import java.io.IOException;
4
5 import com.google.gson.JsonElement;
6
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
8 import net.mograsim.logic.ui.model.components.submodels.SubmodelComponent;
9 import net.mograsim.logic.ui.util.JsonHandler;
10
11 /**
12  * This class contains all the information necessary to create a new {@link SubmodelComponent}
13  */
14 public class SubmodelComponentParams
15 {
16         // basic stuff
17         public double width, height;
18         public InterfacePinParams[] interfacePins;
19         public SubmodelParameters submodel;
20
21         // functionality that needs to be expressed in Java code
22         public String outlineRendererSnippetClass;
23         public JsonElement outlineRendererParams;
24
25         public String symbolRendererSnippetClass;
26         public JsonElement symbolRendererParams;
27
28         public static class InterfacePinParams
29         {
30                 public Point location;
31                 public String name;
32                 public int logicWidth;
33         }
34
35         public static class SubmodelParameters
36         {
37                 public double innerScale;
38                 public InnerComponentParams[] subComps;
39                 public InnerWireParams[] innerWires;
40
41                 public static class InnerComponentParams
42                 {
43                         public Point pos;
44                         public String id;
45                         public JsonElement params;
46                 }
47
48                 public static class InnerWireParams
49                 {
50                         public InnerPinParams pin1, pin2;
51                         public Point[] path;
52
53                         public static class InnerPinParams
54                         {
55                                 public int compId;
56                                 public String pinName;
57                         }
58                 }
59         }
60
61         public static SubmodelComponentParams readJson(String path) throws IOException
62         {
63                 return JsonHandler.readJson(path, SubmodelComponentParams.class);
64         }
65
66         /**
67          * Writes this {@link SubmodelComponentParams} object into a file in json format. The correct file extension is important! Check
68          * {@link SubmodelComponentParams}.fileExtension
69          */
70         public void writeJson(String path)
71         {
72                 try
73                 {
74                         JsonHandler.writeJson(this, path);
75                 }
76                 catch (IOException e)
77                 {
78                         System.err.println("Failed to write SubComponentParams to file");
79                         e.printStackTrace();
80                 }
81         }
82 }