Merge branch 'development' of
authorFabian Stemmler <stemmler@in.tum.de>
Wed, 29 May 2019 16:12:22 +0000 (18:12 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Wed, 29 May 2019 16:12:22 +0000 (18:12 +0200)
https://gitlab.lrz.de/lrr-tum/students/eragp-misim-2019 into development

# Conflicts:
# LogicUI/oldsrc/RSLatchGUIExample.java

LogicUI/src/era/mi/gui/LogicUICanvas.java
LogicUI/src/era/mi/gui/examples/Playground.java
LogicUI/src/era/mi/gui/model/components/GUIManualSwitch.java
LogicUI/src/era/mi/gui/model/wires/WireCrossPoint.java
LogicUI/src/era/mi/gui/modeladapter/ViewLogicModelAdapter.java
LogicUI/src/era/mi/gui/modeladapter/componentadapters/ComponentAdapter.java
LogicUI/src/era/mi/gui/modeladapter/componentadapters/ManualSwitchAdapter.java [new file with mode: 0644]
LogicUI/src/era/mi/gui/modeladapter/componentadapters/SimpleGateAdapter.java

index 7faefe3..6610e2e 100644 (file)
@@ -22,6 +22,8 @@ import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;
  */
 public class LogicUICanvas extends ZoomableCanvas
 {
+       private static final boolean DRAW_PINS = false;
+
        private final ViewModel model;
 
        public LogicUICanvas(Composite parent, int style, ViewModel model)
@@ -84,11 +86,14 @@ public class LogicUICanvas extends ZoomableCanvas
        private void drawComponent(GeneralGC gc, GUIComponent component, Rectangle visibleRegion)
        {
                component.render(gc, visibleRegion);
-               gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_CYAN));
-               for (Pin p : component.getPins())
+               if (DRAW_PINS)
                {
-                       Point pos = p.getPos();
-                       gc.fillOval(pos.x - 1, pos.y - 1, 2, 2);
+                       gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_DARK_CYAN));
+                       for (Pin p : component.getPins())
+                       {
+                               Point pos = p.getPos();
+                               gc.fillOval(pos.x - 1, pos.y - 1, 2, 2);
+                       }
                }
        }
 
index cf9f1e0..9309eef 100644 (file)
@@ -1,12 +1,13 @@
 package era.mi.gui.examples;
 
-import org.eclipse.swt.SWT;
-
 import era.mi.gui.LogicUIStandalone;
 import era.mi.gui.model.ViewModel;
 import era.mi.gui.model.components.GUIAndGate;
+import era.mi.gui.model.components.GUIManualSwitch;
 import era.mi.gui.model.components.GUINotGate;
+import era.mi.gui.model.components.GUIOrGate;
 import era.mi.gui.model.wires.GUIWire;
+import era.mi.gui.model.wires.WireCrossPoint;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 
 public class Playground
@@ -14,28 +15,73 @@ public class Playground
        public static void main(String[] args)
        {
                ViewModel model = new ViewModel();
-               GUIAndGate andGate = new GUIAndGate(model, 1);
-               andGate.moveTo(10, 10);
-               GUINotGate notGate = new GUINotGate(model, 1);
-               notGate.moveTo(10, 40);
+               createRSLatchExample(model);
+               LogicUIStandalone ui = new LogicUIStandalone(model);
+               ui.run();
+       }
 
-               new GUIWire(model, andGate.getPins().get(0), notGate.getPins().get(1), new Point(20, 50));
+       private static void createRSLatchExample(ViewModel model)
+       {
+               GUIManualSwitch rIn = new GUIManualSwitch(model);
+               rIn.moveTo(100, 100);
+               GUIManualSwitch sIn = new GUIManualSwitch(model);
+               sIn.moveTo(100, 200);
 
-               LogicUIStandalone ui = new LogicUIStandalone(model);
+               GUIOrGate or1 = new GUIOrGate(model, 1);
+               or1.moveTo(160, 102.5);
+               new GUIWire(model, rIn.getOutputPin(), or1.getInputPins().get(0));
 
-               ui.getLogicUICanvas().addListener(SWT.KeyDown, e -> notGate.moveTo(150, 10));
-               ui.run();
+               GUIOrGate or2 = new GUIOrGate(model, 1);
+               or2.moveTo(160, 192.5);
+               new GUIWire(model, sIn.getOutputPin(), or2.getInputPins().get(1));
+
+               GUINotGate not1 = new GUINotGate(model, 1);
+               not1.moveTo(200, 107.5);
+               new GUIWire(model, or1.getOutputPin(), not1.getInputPins().get(0));
+
+               GUINotGate not2 = new GUINotGate(model, 1);
+               not2.moveTo(200, 197.5);
+               new GUIWire(model, or2.getOutputPin(), not2.getInputPins().get(0));
+
+               WireCrossPoint p1 = new WireCrossPoint(model, 1);
+               p1.moveTo(250, 112.5);
+               new GUIWire(model, not1.getOutputPin(), p1.getPin());
+               new GUIWire(model, p1.getPin(), or2.getInputPins().get(0), new Point(250, 130), new Point(140, 185), new Point(140, 197.5));
+
+               WireCrossPoint p2 = new WireCrossPoint(model, 1);
+               p2.moveTo(250, 202.5);
+               new GUIWire(model, not2.getOutputPin(), p2.getPin());
+               new GUIWire(model, p2.getPin(), or1.getInputPins().get(1), new Point(250, 185), new Point(140, 130), new Point(140, 117.5));
+
+               WireCrossPoint o1 = new WireCrossPoint(model, 1);
+               o1.moveTo(270, 112.5);
+               new GUIWire(model, p1.getPin(), o1.getPin());
+
+               WireCrossPoint o2 = new WireCrossPoint(model, 1);
+               o2.moveTo(270, 202.5);
+               new GUIWire(model, p2.getPin(), o2.getPin());
        }
 
-       public static void addComponentsAndWires(LogicUIStandalone ui, ViewModel model)
+       @SuppressWarnings("unused")
+       private static void createBasicExample(ViewModel model)
        {
                GUIAndGate andGate = new GUIAndGate(model, 1);
                andGate.moveTo(10, 10);
                GUINotGate notGate = new GUINotGate(model, 1);
                notGate.moveTo(10, 40);
 
-               new GUIWire(model, andGate.getPins().get(0), notGate.getPins().get(1), new Point(20, 50));
+               WireCrossPoint wcp1 = new WireCrossPoint(model, 1);
+               wcp1.moveTo(150, 10);
+
+               new GUIWire(model, andGate.getOutputPin(), notGate.getInputPins().get(0), new Point(60, 50));
+               new GUIWire(model, notGate.getOutputPin(), wcp1.getPin());
+
+               GUIManualSwitch sw1 = new GUIManualSwitch(model);
+               sw1.moveTo(-20, 0);
+               GUIManualSwitch sw2 = new GUIManualSwitch(model);
+               sw2.moveTo(-20, 50);
 
-               ui.getLogicUICanvas().addListener(SWT.KeyDown, e -> notGate.moveTo(150, 10));
+               new GUIWire(model, sw1.getOutputPin(), andGate.getInputPins().get(0));
+               new GUIWire(model, sw2.getOutputPin(), andGate.getInputPins().get(1));
        }
 }
\ No newline at end of file
index 6008329..c45224a 100644 (file)
@@ -16,6 +16,8 @@ public class GUIManualSwitch extends GUIComponent
        private static final double height = 15;
        private static final double fontHeight = 5;
 
+       private final Pin outputPin;
+
        private ManualSwitch logicSwitch;
        private ReadEnd end;
 
@@ -23,19 +25,22 @@ public class GUIManualSwitch extends GUIComponent
        {
                super(model);
                setSize(width, height);
-               addPin(new Pin(this, 1, width, height / 2));
+               addPin(this.outputPin = new Pin(this, 1, width, height / 2));
        }
 
        @Override
        public void render(GeneralGC gc, Rectangle visibleRegion)
        {
-               gc.drawRectangle(0, 0, width, height);
+               double posX = getBounds().x;
+               double posY = getBounds().y;
+
+               gc.drawRectangle(posX, posY, width, height);
                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, (width - textExtent.x) / 2, (height - textExtent.y) / 2, true);
+               gc.drawText(label, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
                gc.setFont(oldFont);
        }
 
@@ -53,4 +58,9 @@ public class GUIManualSwitch extends GUIComponent
                logicSwitch.toggle();
                return true;
        }
+
+       public Pin getOutputPin()
+       {
+               return outputPin;
+       }
 }
\ No newline at end of file
index 73d98f5..04c5f1e 100644 (file)
@@ -10,6 +10,8 @@ import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 
 public class WireCrossPoint extends GUIComponent
 {
+       private final Pin pin;
+
        private ReadEnd end;
        private final int logicWidth;
 
@@ -18,13 +20,15 @@ public class WireCrossPoint extends GUIComponent
                super(model);
                this.logicWidth = logicWidth;
                setSize(0, 0);
-               addPin(new Pin(this, logicWidth, 0, 0));
+               addPin(this.pin = new Pin(this, logicWidth, 0, 0));
        }
 
        @Override
        public void render(GeneralGC gc, Rectangle visibleRegion)
        {
-               ColorHelper.executeWithDifferentBackground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.fillOval(-1, -1, 2, 2));
+               Rectangle bounds = getBounds();
+               ColorHelper.executeWithDifferentBackground(gc, BitVectorFormatter.formatAsColor(end),
+                               () -> gc.fillOval(bounds.x - 1, bounds.y - 1, 2, 2));
        }
 
        public void setLogicModelBinding(ReadEnd end)
@@ -37,4 +41,9 @@ public class WireCrossPoint extends GUIComponent
        {
                return logicWidth;
        }
+
+       public Pin getPin()
+       {
+               return pin;
+       }
 }
\ No newline at end of file
index c64ae97..1e7d341 100644 (file)
@@ -20,6 +20,7 @@ import era.mi.gui.model.wires.GUIWire;
 import era.mi.gui.model.wires.Pin;
 import era.mi.gui.model.wires.WireCrossPoint;
 import era.mi.gui.modeladapter.componentadapters.ComponentAdapter;
+import era.mi.gui.modeladapter.componentadapters.ManualSwitchAdapter;
 import era.mi.gui.modeladapter.componentadapters.SimpleGateAdapter;
 import era.mi.logic.components.Component;
 import era.mi.logic.components.gates.AndGate;
@@ -34,12 +35,14 @@ public class ViewLogicModelAdapter
        private final static Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdapters;
        static
        {
-               Map<Class<? extends GUIComponent>, ComponentAdapter<? extends GUIComponent>> componentAdaptersModifiable = new HashMap<>();
-               componentAdaptersModifiable.put(GUIOrGate.class, new SimpleGateAdapter(OrGate::new));
-               componentAdaptersModifiable.put(GUIAndGate.class, new SimpleGateAdapter(AndGate::new));
-               componentAdaptersModifiable.put(GUINotGate.class, new SimpleGateAdapter((t, p, o, i) -> new NotGate(t, p, i[0], o)));
+               Set<ComponentAdapter<? extends GUIComponent>> componentAdaptersModifiable = new HashSet<>();
+               componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIOrGate.class, OrGate::new));
+               componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUIAndGate.class, AndGate::new));
+               componentAdaptersModifiable.add(new SimpleGateAdapter<>(GUINotGate.class, (t, p, o, i) -> new NotGate(t, p, i[0], o)));
+               componentAdaptersModifiable.add(new ManualSwitchAdapter());
                // TODO list all "primitive" adapters here
-               componentAdapters = Collections.unmodifiableMap(componentAdaptersModifiable);
+               componentAdapters = Collections.unmodifiableMap(
+                               componentAdaptersModifiable.stream().collect(Collectors.toMap(ComponentAdapter::getSupportedClass, Function.identity())));
        }
 
        public static Timeline convert(ViewModel viewModel, LogicModelParameters params)
@@ -55,10 +58,14 @@ public class ViewLogicModelAdapter
                Map<GUIComponent, Component> oneToOneComponents = new HashMap<>();
                for (GUIComponent guiComp : viewModel.getComponents())
                {
-                       // WireCrossPoints just vanish
                        if (!(guiComp instanceof WireCrossPoint))
                                oneToOneComponents.put(guiComp, createAndLinkComponent(timeline, params, guiComp, logicWiresPerPinUnmodifiable,
                                                componentAdapters.get(guiComp.getClass())));
+                       else
+                       {
+                               WireCrossPoint guiCompCasted = (WireCrossPoint) guiComp;
+                               guiCompCasted.setLogicModelBinding(logicWiresPerPin.get(guiCompCasted.getPin()).createReadOnlyEnd());
+                       }
                }
 
                // TODO handle complex components
index 8598fa8..c109d73 100644 (file)
@@ -11,6 +11,8 @@ import era.mi.logic.wires.Wire;
 
 public interface ComponentAdapter<G extends GUIComponent>
 {
+       public Class<G> getSupportedClass();
+
        public Component createAndLinkComponent(Timeline timeline, LogicModelParameters params, G guiComponent,
                        Map<Pin, Wire> logicWiresPerPin);
 }
\ No newline at end of file
diff --git a/LogicUI/src/era/mi/gui/modeladapter/componentadapters/ManualSwitchAdapter.java b/LogicUI/src/era/mi/gui/modeladapter/componentadapters/ManualSwitchAdapter.java
new file mode 100644 (file)
index 0000000..3a6ea05
--- /dev/null
@@ -0,0 +1,31 @@
+package era.mi.gui.modeladapter.componentadapters;
+
+import java.util.Map;
+
+import era.mi.gui.model.components.GUIManualSwitch;
+import era.mi.gui.model.wires.Pin;
+import era.mi.gui.modeladapter.LogicModelParameters;
+import era.mi.logic.components.Component;
+import era.mi.logic.components.ManualSwitch;
+import era.mi.logic.timeline.Timeline;
+import era.mi.logic.wires.Wire;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
+
+public class ManualSwitchAdapter implements ComponentAdapter<GUIManualSwitch>
+{
+       @Override
+       public Class<GUIManualSwitch> getSupportedClass()
+       {
+               return GUIManualSwitch.class;
+       }
+
+       @Override
+       public Component createAndLinkComponent(Timeline timeline, LogicModelParameters params, GUIManualSwitch guiComponent,
+                       Map<Pin, Wire> logicWiresPerPin)
+       {
+               ReadWriteEnd end = logicWiresPerPin.get(guiComponent.getOutputPin()).createReadWriteEnd();
+               ManualSwitch manualSwitch = new ManualSwitch(timeline, end);
+               guiComponent.setLogicModelBinding(manualSwitch, end);
+               return manualSwitch;
+       }
+}
\ No newline at end of file
index e256ff0..b244973 100644 (file)
@@ -12,18 +12,25 @@ import era.mi.logic.wires.Wire;
 import era.mi.logic.wires.Wire.ReadEnd;
 import era.mi.logic.wires.Wire.ReadWriteEnd;
 
-public class SimpleGateAdapter implements ComponentAdapter<SimpleRectangularGUIGate>
+public class SimpleGateAdapter<G extends SimpleRectangularGUIGate> implements ComponentAdapter<G>
 {
+       private final Class<G> supportedClass;
        private final ComponentConstructor constructor;
 
-       public SimpleGateAdapter(ComponentConstructor constructor)
+       public SimpleGateAdapter(Class<G> supportedClass, ComponentConstructor constructor)
        {
+               this.supportedClass = supportedClass;
                this.constructor = constructor;
        }
 
        @Override
-       public Component createAndLinkComponent(Timeline timeline, LogicModelParameters params, SimpleRectangularGUIGate guiComponent,
-                       Map<Pin, Wire> logicWiresPerPin)
+       public Class<G> getSupportedClass()
+       {
+               return supportedClass;
+       }
+
+       @Override
+       public Component createAndLinkComponent(Timeline timeline, LogicModelParameters params, G guiComponent, Map<Pin, Wire> logicWiresPerPin)
        {
                ReadWriteEnd out = logicWiresPerPin.get(guiComponent.getOutputPin()).createReadWriteEnd();
                List<Pin> inputPins = guiComponent.getInputPins();
@@ -37,4 +44,5 @@ public class SimpleGateAdapter implements ComponentAdapter<SimpleRectangularGUIG
        {
                public Component newComponent(Timeline timeline, int processTime, ReadWriteEnd out, ReadEnd[] ins);
        }
+
 }
\ No newline at end of file