Removed visitor pattern used for one test only
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / SimpleRectangularSubmodelComponent.java
1 package net.mograsim.logic.ui.model.components;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.TreeMap;
10
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
13 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
15 import net.mograsim.logic.ui.model.ViewModelModifiable;
16 import net.mograsim.logic.ui.model.wires.MovablePin;
17 import net.mograsim.logic.ui.model.wires.Pin;
18
19 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
20 {
21         public static String kLabel = "label", kInCount = "input_count", kOutCount = "output_count", kLogicWidth = "logic_width";
22
23         private static final double width = 35;
24         private static final double pinDistance = 10;
25         private static final double pinNameMargin = .5;
26         private static final double labelFontHeight = 5;
27         private static final double pinNameFontHeight = 3.5;
28
29         private final String label;
30         protected final int logicWidth;
31
32         private final List<String> inputPinNames;
33         private final List<String> inputPinNamesUnmodifiable;
34         private final List<String> outputPinNames;
35         private final List<String> outputPinNamesUnmodifiable;
36
37         protected SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label)
38         {
39                 super(model);
40                 this.label = label;
41                 this.logicWidth = logicWidth;
42                 this.inputPinNames = new ArrayList<>();
43                 this.inputPinNamesUnmodifiable = Collections.unmodifiableList(inputPinNames);
44                 this.outputPinNames = new ArrayList<>();
45                 this.outputPinNamesUnmodifiable = Collections.unmodifiableList(outputPinNames);
46         }
47
48         protected void setInputPins(String... pinNames)
49         {
50                 setIOPins(0, inputPinNames, outputPinNames, pinNames);
51         }
52
53         protected void setOutputPins(String... pinNames)
54         {
55                 setIOPins(width, outputPinNames, inputPinNames, pinNames);
56         }
57
58         private void setIOPins(double relX, List<String> pinNamesListThisSide, List<String> pinNamesListOtherSide, String... newPinNames)
59         {
60                 int inputCount = newPinNames.length;
61                 List<String> newPinNamesList = Arrays.asList(newPinNames);
62                 if (new HashSet<>(newPinNamesList).size() != inputCount)
63                         throw new IllegalArgumentException("Pin names contain duplicates");
64                 for (String pinName : newPinNamesList)
65                         if (pinNamesListOtherSide.contains(pinName))
66                                 throw new IllegalArgumentException("Can't add pin. There is a pin on the other side with the same name: " + pinName);
67                 super.setSize(width, Math.max(inputCount, pinNamesListOtherSide.size()) * pinDistance);
68                 for (int i = 0; i < inputCount; i++)
69                 {
70                         String pinName = newPinNames[i];
71                         int oldPinIndex = pinNamesListThisSide.indexOf(pinName);
72                         if (oldPinIndex == -1)
73                                 super.addSubmodelInterface(new MovablePin(this, pinName, logicWidth, relX, pinDistance / 2 + i * pinDistance));
74                         else
75                                 getSupermodelMovablePin(pinName).setRelPos(relX, pinDistance / 2 + i * pinDistance);
76                 }
77                 for (String pinName : pinNamesListThisSide)
78                         if (!newPinNamesList.contains(pinName))
79                                 super.removeSubmodelInterface(pinName);
80                 pinNamesListThisSide.clear();
81                 pinNamesListThisSide.addAll(newPinNamesList);
82         }
83
84         public List<String> getInputPinNames()
85         {
86                 return inputPinNamesUnmodifiable;
87         }
88
89         public List<String> getOutputPinNames()
90         {
91                 return outputPinNamesUnmodifiable;
92         }
93
94         @Override
95         protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
96         {
97                 Font oldFont = gc.getFont();
98                 gc.setFont(new Font(oldFont.getName(), labelFontHeight, oldFont.getStyle()));
99                 Point textExtent = gc.textExtent(label);
100                 gc.drawText(label, getPosX() + (getWidth() - textExtent.x) / 2, getPosY() + (getHeight() - textExtent.y) / 2, true);
101                 gc.setFont(new Font(oldFont.getName(), pinNameFontHeight, oldFont.getStyle()));
102                 for (int i = 0; i < inputPinNames.size(); i++)
103                 {
104                         String pinName = inputPinNames.get(i);
105                         textExtent = gc.textExtent(pinName);
106                         gc.drawText(pinName, getPosX() + pinNameMargin, getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
107                 }
108                 for (int i = 0; i < outputPinNames.size(); i++)
109                 {
110                         String pinName = outputPinNames.get(i);
111                         textExtent = gc.textExtent(pinName);
112                         gc.drawText(pinName, getPosX() + width - textExtent.x - pinNameMargin,
113                                         getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
114                 }
115                 gc.setFont(oldFont);
116         }
117
118         @Override
119         protected void renderOutline(GeneralGC gc, Rectangle visibleRegion)
120         {
121                 gc.drawRectangle(getBounds());
122         }
123
124         @Override
125         public SubmodelComponentParams calculateParams()
126         {
127                 SubmodelComponentParams ret = super.calculateParams();
128                 ret.type = SimpleRectangularSubmodelComponent.class.getSimpleName();
129                 Map<String, Object> m = new TreeMap<>();
130                 m.put(kLabel, label);
131                 m.put(kInCount, inputPinNames.toArray());
132                 m.put(kOutCount, outputPinNames.toArray());
133                 m.put(kLogicWidth, logicWidth);
134                 ret.specialized = m;
135                 return ret;
136         }
137
138         @Override
139         protected Pin addSubmodelInterface(MovablePin supermodelPin)
140         {
141                 throw new UnsupportedOperationException(
142                                 "Can't add submodel interfaces to a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
143         }
144
145         @Override
146         protected void removeSubmodelInterface(String name)
147         {
148                 throw new UnsupportedOperationException(
149                                 "Can't remove submodel interfaces of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
150         }
151
152         @Override
153         protected void setSize(double width, double height)
154         {
155                 throw new UnsupportedOperationException(
156                                 "Can't set the size of a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
157         }
158 }