The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / ModelBitDisplay.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.LogicObserver;
10 import net.mograsim.logic.core.components.CoreBitDisplay;
11 import net.mograsim.logic.core.types.BitVectorFormatter;
12 import net.mograsim.logic.model.model.LogicModelModifiable;
13 import net.mograsim.logic.model.model.components.ModelComponent;
14 import net.mograsim.logic.model.model.wires.Pin;
15 import net.mograsim.logic.model.model.wires.PinUsage;
16 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
17 import net.mograsim.logic.model.modeladapter.componentadapters.BitDisplayAdapter;
18 import net.mograsim.logic.model.serializing.IdentifyParams;
19 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
20 import net.mograsim.preferences.Preferences;
21
22 public class ModelBitDisplay extends ModelComponent
23 {
24         private static final double width = 20;
25         private static final double height = 15;
26         private static final double fontHeight = 5;
27
28         public final int logicWidth;
29         private final Pin inputPin;
30
31         private final LogicObserver logicObs;
32         private CoreBitDisplay bitDisplay;
33
34         public ModelBitDisplay(LogicModelModifiable model, int logicWidth)
35         {
36                 this(model, logicWidth, null);
37         }
38
39         public ModelBitDisplay(LogicModelModifiable model, int logicWidth, String name)
40         {
41                 super(model, name, false);
42                 this.logicWidth = logicWidth;
43                 logicObs = (i) -> model.requestRedraw();
44
45                 setSize(width, height);
46                 addPin(this.inputPin = new Pin(model, this, "", logicWidth, PinUsage.INPUT, 0, height / 2));
47
48                 init();
49         }
50
51         @Override
52         public void render(GeneralGC gc, Rectangle visibleRegion)
53         {
54                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
55                 if (foreground != null)
56                         gc.setForeground(foreground);
57                 gc.drawRectangle(getBounds());
58                 String label = bitDisplay == null ? BitVectorFormatter.formatAsString(null)
59                                 : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
60                 Font oldFont = gc.getFont();
61                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
62                 gc.setFont(labelFont);
63                 Point textExtent = gc.textExtent(label);
64                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
65                 if (textColor != null)
66                         gc.setForeground(textColor);
67                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
68                 gc.setFont(oldFont);
69         }
70
71         public void setCoreModelBinding(CoreBitDisplay bitDisplay)
72         {
73                 if (this.bitDisplay != null)
74                         this.bitDisplay.deregisterObserver(logicObs);
75                 this.bitDisplay = bitDisplay;
76                 if (bitDisplay != null)
77                         bitDisplay.registerObserver(logicObs);
78         }
79
80         public boolean hasCoreModelBinding()
81         {
82                 return bitDisplay != null;
83         }
84
85         public CoreBitDisplay getBitDisplay()
86         {
87                 return bitDisplay;
88         }
89
90         public Pin getInputPin()
91         {
92                 return inputPin;
93         }
94
95         @Override
96         public String getIDForSerializing(IdentifyParams idParams)
97         {
98                 return "BitDisplay";
99         }
100
101         @Override
102         public Integer getParamsForSerializing(IdentifyParams idParams)
103         {
104                 return logicWidth;
105         }
106
107         static
108         {
109                 LogicCoreAdapter.addComponentAdapter(new BitDisplayAdapter());
110                 IndirectModelComponentCreator.setComponentSupplier(ModelBitDisplay.class.getCanonicalName(),
111                                 (m, p, n) -> new ModelBitDisplay(m, p.getAsInt(), n));
112         }
113 }