Added file version and closed resource leak
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / SubmodelComponentParams.java
1 package net.mograsim.logic.ui.model.components;
2
3 import java.io.IOException;
4 import java.util.Map;
5
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
7 import net.mograsim.logic.ui.util.JsonHandler;
8
9 /**
10  * This class contains all the information necessary to create a new {@link SubmodelComponent}
11  */
12 public class SubmodelComponentParams
13 {
14         String type;
15         double width, height;
16         InterfacePinParams[] interfacePins;
17         ComponentCompositionParams composition;
18         Map<String, Object> specialized;
19
20         public static class InterfacePinParams
21         {
22                 public Point location;
23                 public int logicWidth;
24         }
25
26         public static class InnerWireParams
27         {
28                 public InnerPinParams pin1, pin2;
29                 public Point[] path;
30         }
31
32         public static class InnerPinParams
33         {
34                 public int compId, pinIndex;
35         }
36
37         public static class ComponentCompositionParams
38         {
39                 double innerScale;
40                 InnerComponentParams[] subComps;
41                 InnerWireParams[] innerWires;
42
43                 public static class InnerComponentParams
44                 {
45                         public Point pos;
46                         public String type;
47                         public int logicWidth;
48                 }
49         }
50
51         public static SubmodelComponentParams readJson(String path) throws IOException
52         {
53                 return JsonHandler.readJson(path, SubmodelComponentParams.class);
54         }
55
56         /**
57          * Writes this {@link SubmodelComponentParams} object into a file in json format. The correct file extension is important! Check
58          * {@link SubmodelComponentParams}.fileExtension
59          */
60         public void writeJson(String path)
61         {
62                 try
63                 {
64                         JsonHandler.writeJson(this, path);
65                 }
66                 catch (IOException e)
67                 {
68                         System.err.println("Failed to write SubComponentParams to file");
69                         e.printStackTrace();
70                 }
71         }
72 }