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