Completely changed the structure and switched to Eclipse Plugin.
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / GUIManualSwitch.java
index 486639e..563f928 100644 (file)
-package net.mograsim.logic.ui.model.components;\r
-\r
-import net.mograsim.logic.ui.model.ViewModel;\r
-import net.mograsim.logic.ui.model.wires.Pin;\r
-import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
-import net.haspamelodica.swt.helper.swtobjectwrappers.Font;\r
-import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
-import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;\r
-import net.mograsim.logic.core.components.ManualSwitch;\r
-import net.mograsim.logic.core.types.BitVectorFormatter;\r
-import net.mograsim.logic.core.wires.Wire.ReadEnd;\r
-\r
-public class GUIManualSwitch extends GUIComponent\r
-{\r
-       private static final double width = 20;\r
-       private static final double height = 15;\r
-       private static final double fontHeight = 5;\r
-\r
-       private final Pin outputPin;\r
-\r
-       private ManualSwitch logicSwitch;\r
-       private ReadEnd end;\r
-\r
-       public GUIManualSwitch(ViewModel model)\r
-       {\r
-               super(model);\r
-               setSize(width, height);\r
-               addPin(this.outputPin = new Pin(this, 1, width, height / 2));\r
-       }\r
-\r
-       @Override\r
-       public void render(GeneralGC gc, Rectangle visibleRegion)\r
-       {\r
-               double posX = getBounds().x;\r
-               double posY = getBounds().y;\r
-\r
-               gc.drawRectangle(posX, posY, width, height);\r
-               String label = BitVectorFormatter.formatValueAsString(end);\r
-               Font oldFont = gc.getFont();\r
-               Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());\r
-               gc.setFont(labelFont);\r
-               Point textExtent = gc.textExtent(label);\r
-               gc.drawText(label, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);\r
-               gc.setFont(oldFont);\r
-       }\r
-\r
-       public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)\r
-       {\r
-               this.logicSwitch = logicSwitch;\r
-               this.end = end;\r
-               // TODO when ManualSwitch supports it, add listeners\r
-               end.registerObserver((i) -> callComponentLookChangedListeners());\r
-       }\r
-\r
-       @Override\r
-       public boolean clicked(double x, double y)\r
-       {\r
-               if (logicSwitch != null)\r
-                       logicSwitch.toggle();\r
-               return true;\r
-       }\r
-\r
-       public Pin getOutputPin()\r
-       {\r
-               return outputPin;\r
-       }\r
+package net.mograsim.logic.ui.model.components;
+
+import net.haspamelodica.swt.helper.gcs.GeneralGC;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
+import net.mograsim.logic.core.LogicObservable;
+import net.mograsim.logic.core.LogicObserver;
+import net.mograsim.logic.core.components.ManualSwitch;
+import net.mograsim.logic.core.types.BitVectorFormatter;
+import net.mograsim.logic.core.wires.Wire.ReadEnd;
+import net.mograsim.logic.ui.model.ModelVisitor;
+import net.mograsim.logic.ui.model.ViewModelModifiable;
+import net.mograsim.logic.ui.model.wires.Pin;
+import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
+import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
+
+public class GUIManualSwitch extends GUIComponent
+{
+       private static final double width = 20;
+       private static final double height = 15;
+       private static final double fontHeight = 5;
+
+       private final Pin outputPin;
+
+       private final LogicObserver logicObs;
+       private ManualSwitch logicSwitch;
+       private ReadEnd end;
+
+       public GUIManualSwitch(ViewModelModifiable model)
+       {
+               super(model);
+               logicObs = (i) -> requestRedraw();
+
+               setSize(width, height);
+               addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
+       }
+
+       @Override
+       public void render(GeneralGC gc, Rectangle visibleRegion)
+       {
+               // TODO maybe draw switch state too?
+               gc.drawRectangle(getBounds());
+               String label = BitVectorFormatter.formatValueAsString(end);
+               Font oldFont = gc.getFont();
+               Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
+               gc.setFont(labelFont);
+               Point textExtent = gc.textExtent(label);
+               gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
+               gc.setFont(oldFont);
+       }
+
+       public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
+       {
+               deregisterLogicObs(this.end);
+               deregisterLogicObs(this.logicSwitch);
+               this.logicSwitch = logicSwitch;
+               this.end = end;
+               registerLogicObs(end);
+               registerLogicObs(logicSwitch);
+       }
+
+       private void registerLogicObs(LogicObservable observable)
+       {
+               if (observable != null)
+                       observable.registerObserver(logicObs);
+       }
+
+       private void deregisterLogicObs(LogicObservable observable)
+       {
+               if (observable != null)
+                       observable.deregisterObserver(logicObs);
+       }
+
+       @Override
+       public boolean clicked(double x, double y)
+       {
+               if (logicSwitch != null)
+                       logicSwitch.toggle();
+               return true;
+       }
+
+       public ManualSwitch getManualSwitch()
+       {
+               return logicSwitch;
+       }
+
+       public Pin getOutputPin()
+       {
+               return outputPin;
+       }
+
+       @Override
+       public void accept(ModelVisitor mv)
+       {
+               mv.visit(this);
+       }
+
+       static
+       {
+               ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
+       }
 }
\ No newline at end of file