Made get/setHighLevelState ordering uniform: get... comes before set...
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUIManualSwitch.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import org.eclipse.swt.graphics.Color;
4
5 import net.haspamelodica.swt.helper.gcs.GeneralGC;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
9 import net.mograsim.logic.core.LogicObservable;
10 import net.mograsim.logic.core.LogicObserver;
11 import net.mograsim.logic.core.components.ManualSwitch;
12 import net.mograsim.logic.core.types.Bit;
13 import net.mograsim.logic.core.types.BitVectorFormatter;
14 import net.mograsim.logic.core.wires.Wire.ReadEnd;
15 import net.mograsim.logic.model.model.ViewModelModifiable;
16 import net.mograsim.logic.model.model.components.GUIComponent;
17 import net.mograsim.logic.model.model.wires.Pin;
18 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
19 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
20 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
21 import net.mograsim.preferences.Preferences;
22
23 public class GUIManualSwitch extends GUIComponent
24 {
25         private static final double width = 20;
26         private static final double height = 15;
27         private static final double fontHeight = 5;
28
29         private final Pin outputPin;
30
31         private final LogicObserver logicObs;
32         private ManualSwitch logicSwitch;
33         private ReadEnd end;
34
35         public GUIManualSwitch(ViewModelModifiable model)
36         {
37                 this(model, null);
38         }
39
40         public GUIManualSwitch(ViewModelModifiable model, String name)
41         {
42                 super(model, name);
43                 logicObs = (i) -> requestRedraw();
44
45                 setSize(width, height);
46                 addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
47         }
48
49         @Override
50         public void render(GeneralGC gc, Rectangle visibleRegion)
51         {
52                 // TODO maybe draw switch state too?
53                 Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
54                 if (foreground != null)
55                         gc.setForeground(foreground);
56                 gc.drawRectangle(getBounds());
57                 String label = BitVectorFormatter.formatValueAsString(end);
58                 Font oldFont = gc.getFont();
59                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
60                 gc.setFont(labelFont);
61                 Point textExtent = gc.textExtent(label);
62                 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
63                 if (textColor != null)
64                         gc.setForeground(textColor);
65                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
66                 gc.setFont(oldFont);
67         }
68
69         public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
70         {
71                 deregisterLogicObs(this.end);
72                 deregisterLogicObs(this.logicSwitch);
73                 this.logicSwitch = logicSwitch;
74                 this.end = end;
75                 registerLogicObs(end);
76                 registerLogicObs(logicSwitch);
77         }
78
79         public boolean hasLogicModelBinding()
80         {
81                 return logicSwitch != null;
82         }
83
84         @Override
85         public Object getHighLevelState(String stateID)
86         {
87                 switch (stateID)
88                 {
89                 case "out":
90                         if (logicSwitch != null)
91                                 return logicSwitch.getValue();
92                         return null;
93                 default:
94                         return super.getHighLevelState(stateID);
95                 }
96         }
97
98         @Override
99         public void setHighLevelState(String stateID, Object newState)
100         {
101                 switch (stateID)
102                 {
103                 case "out":
104                         if (logicSwitch != null)
105                                 logicSwitch.setToValueOf((Bit) newState);
106                         break;
107                 default:
108                         super.setHighLevelState(stateID, newState);
109                         break;
110                 }
111         }
112
113         private void registerLogicObs(LogicObservable observable)
114         {
115                 if (observable != null)
116                         observable.registerObserver(logicObs);
117         }
118
119         private void deregisterLogicObs(LogicObservable observable)
120         {
121                 if (observable != null)
122                         observable.deregisterObserver(logicObs);
123         }
124
125         @Override
126         public boolean clicked(double x, double y)
127         {
128                 if (logicSwitch != null)
129                         logicSwitch.toggle();
130                 return true;
131         }
132
133         public ManualSwitch getManualSwitch()
134         {
135                 return logicSwitch;
136         }
137
138         public Pin getOutputPin()
139         {
140                 return outputPin;
141         }
142
143         static
144         {
145                 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
146                 IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(), (m, p, n) -> new GUIManualSwitch(m, n));
147         }
148 }