82b3bbba4dfeb1c7f9484960bddf2c93c06dafcd
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / GUIBitDisplay.java
1 package net.mograsim.logic.ui.model.components;
2
3 import net.haspamelodica.swt.helper.gcs.GeneralGC;
4 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
5 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
7 import net.mograsim.logic.core.LogicObservable;
8 import net.mograsim.logic.core.LogicObserver;
9 import net.mograsim.logic.core.components.BitDisplay;
10 import net.mograsim.logic.core.types.BitVectorFormatter;
11 import net.mograsim.logic.ui.model.ViewModelModifiable;
12 import net.mograsim.logic.ui.model.wires.Pin;
13 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
14 import net.mograsim.logic.ui.modeladapter.componentadapters.BitDisplayAdapter;
15
16 public class GUIBitDisplay extends GUIComponent
17 {
18         private static final double width = 20;
19         private static final double height = 15;
20         private static final double fontHeight = 5;
21
22         private final Pin inputPin;
23
24         private final LogicObserver logicObs;
25         private BitDisplay bitDisplay;
26
27         public GUIBitDisplay(ViewModelModifiable model)
28         {
29                 super(model);
30                 logicObs = (i) -> requestRedraw();
31
32                 setSize(width, height);
33                 addPin(this.inputPin = new Pin(this, "", 1, 0, height / 2));
34         }
35
36         @Override
37         public void render(GeneralGC gc, Rectangle visibleRegion)
38         {
39                 gc.drawRectangle(getBounds());
40                 String label = bitDisplay == null ? BitVectorFormatter.formatAsString(null)
41                                 : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
42                 Font oldFont = gc.getFont();
43                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
44                 gc.setFont(labelFont);
45                 Point textExtent = gc.textExtent(label);
46                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
47                 gc.setFont(oldFont);
48         }
49
50         public void setLogicModelBinding(BitDisplay bitDisplay)
51         {
52                 deregisterLogicObs(this.bitDisplay);
53                 this.bitDisplay = bitDisplay;
54                 registerLogicObs(bitDisplay);
55         }
56
57         private void registerLogicObs(LogicObservable observable)
58         {
59                 if (observable != null)
60                         observable.registerObserver(logicObs);
61         }
62
63         private void deregisterLogicObs(LogicObservable observable)
64         {
65                 if (observable != null)
66                         observable.deregisterObserver(logicObs);
67         }
68
69         public BitDisplay getBitDisplay()
70         {
71                 return bitDisplay;
72         }
73
74         public Pin getInputPin()
75         {
76                 return inputPin;
77         }
78
79         static
80         {
81                 ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
82         }
83 }