Implemented GUIAm2901DestDecode
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / GUICustomComponent.java
1 package net.mograsim.logic.ui.model.components;
2
3 import java.io.IOException;
4 import java.lang.reflect.Constructor;
5 import java.lang.reflect.InvocationTargetException;
6
7 import net.mograsim.logic.ui.model.ViewModelModifiable;
8 import net.mograsim.logic.ui.model.components.ComponentParams.InnerComponentParams;
9 import net.mograsim.logic.ui.model.components.ComponentParams.InnerWireParams;
10 import net.mograsim.logic.ui.model.wires.GUIWire;
11
12 public class GUICustomComponent extends SimpleRectangularSubmodelComponent
13 {
14         private String path = "NONE";
15
16         public static GUICustomComponent create(ViewModelModifiable model, String path)
17         {
18                 try
19                 {
20                         ComponentParams params = ComponentParams.readJSON(path);
21                         return create(model, params);
22                 }
23                 catch (IOException e)
24                 {
25                         System.err.println(String.format("Failed to create custom component from invalid path: %s", path));
26                         e.printStackTrace();
27                 }
28                 return new GUICustomComponent(model, 0, "ERROR");
29         }
30
31         public static GUICustomComponent create(ViewModelModifiable model, ComponentParams params)
32         {
33                 GUICustomComponent comp = new GUICustomComponent(model, params.logicWidth, params.displayName);
34                 comp.setSubmodelScale(params.innerScale);
35                 comp.setInputCount(params.inputCount);
36                 comp.setOutputCount(params.outputCount);
37                 comp.initSubmodelComponents(params);
38                 return comp;
39         }
40
41         private GUICustomComponent(ViewModelModifiable model, int logicWidth, String displayName)
42         {
43                 super(model, logicWidth, displayName);
44         }
45
46         @SuppressWarnings("unused")
47         private void initSubmodelComponents(ComponentParams params)
48         {
49                 try
50                 {
51                         GUIComponent[] components = new GUIComponent[params.subComps.length];
52                         for (int i = 0; i < components.length; i++)
53                         {
54                                 InnerComponentParams cParams = params.subComps[i];
55                                 String path = cParams.type;
56                                 if (path.startsWith("class:"))
57                                 {
58                                         path = path.substring(6);
59                                         components[i] = createComponent(path, cParams.logicWidth);
60                                         components[i].moveTo(cParams.pos.x, cParams.pos.y);
61                                 } else if (path.startsWith("file:"))
62                                 {
63                                         path = path.substring(5);
64                                         components[i] = create(submodelModifiable, path);
65                                         components[i].moveTo(cParams.pos.x, cParams.pos.y);
66                                 } else
67                                         throw new IllegalArgumentException("Invalid submodel type! Type was neither prefixed by 'class:' nor by 'file:'");
68                         }
69
70                         for (int i = 0; i < params.innerWires.length; i++)
71                         {
72                                 InnerWireParams innerWire = params.innerWires[i];
73                                 new GUIWire(submodelModifiable,
74                                                 submodelModifiable.getComponents().get(innerWire.pin1.compId).getPins().get(innerWire.pin1.pinIndex),
75                                                 submodelModifiable.getComponents().get(innerWire.pin2.compId).getPins().get(innerWire.pin2.pinIndex),
76                                                 innerWire.path);
77                         }
78                 }
79                 catch (Exception e)
80                 {
81                         System.err.println("Failed to create custom component!");
82                         e.printStackTrace();
83                 }
84         }
85
86         private GUIComponent createComponent(String classname, int logicWidth) throws InstantiationException, IllegalAccessException,
87                         IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException
88         {
89                 Class<?> c = Class.forName(classname);
90                 Object comp;
91                 try
92                 {
93                         Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class);
94                         comp = constructor.newInstance(submodelModifiable);
95                 }
96                 catch (@SuppressWarnings("unused") NoSuchMethodException e)
97                 {
98                         Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class, int.class);
99                         comp = constructor.newInstance(submodelModifiable, logicWidth);
100                 }
101
102                 if (comp instanceof GUIComponent)
103                         return (GUIComponent) comp;
104                 throw new IllegalArgumentException("Class given as subcomponent was not a GUIComponent!");
105         }
106
107         public String getPath()
108         {
109                 return path;
110         }
111 }