Reordered the disposal of the old resources, because of errors
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / editors / SimulationViewEditor.java
index f83dfa7..0420334 100644 (file)
@@ -10,6 +10,7 @@ import org.eclipse.jface.util.SafeRunnable;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.MouseEvent;
 import org.eclipse.swt.events.MouseTrackListener;
+import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
@@ -29,8 +30,13 @@ import net.mograsim.logic.core.components.CoreClock;
 import net.mograsim.logic.model.LogicExecuter;
 import net.mograsim.logic.model.LogicUICanvas;
 import net.mograsim.machine.Machine;
+import net.mograsim.machine.Memory.MemoryCellModifiedListener;
+import net.mograsim.machine.mi.AssignableMicroInstructionMemory;
 import net.mograsim.plugin.nature.MachineContext;
 import net.mograsim.plugin.nature.ProjectMachineContext;
+import net.mograsim.plugin.tables.DisplaySettings;
+import net.mograsim.plugin.tables.mi.ActiveInstructionPreviewContentProvider;
+import net.mograsim.plugin.tables.mi.InstructionTable;
 
 //TODO what if we open multiple editors?
 //TODO actually save / load register and latch states
@@ -45,9 +51,14 @@ public class SimulationViewEditor extends EditorPart
        private Button sbseButton;
        private Button pauseButton;
        private Slider simSpeedSlider;
+       private Composite canvasParent;
        private LogicUICanvas canvas;
+       private InstructionTable instPreview;
        private Label noMachineLabel;
 
+       private MemoryCellModifiedListener currentRegisteredCellListener;
+       private LogicObserver currentClockObserver;
+
        @Override
        public void createPartControl(Composite parent)
        {
@@ -58,6 +69,10 @@ public class SimulationViewEditor extends EditorPart
                noMachineLabel = new Label(parent, SWT.NONE);
                noMachineLabel.setText("No machine present...");// TODO internationalize?
                addSimulationControlWidgets(parent);
+               canvasParent = new Composite(parent, SWT.NONE);
+               canvasParent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+               canvasParent.setLayout(new FillLayout());
+               addInstructionPreviewControlWidgets(parent);
                recreateContextDependentControls();
        }
 
@@ -67,10 +82,28 @@ public class SimulationViewEditor extends EditorPart
                        // createPartControls has not been called yet
                        return;
 
-               if (canvas != null)
-                       canvas.dispose();
+               double offX;
+               double offY;
+               double zoom;
                if (exec != null)
                        exec.stopLiveExecution();
+               if (machine != null)
+               {
+                       machine.getMicroInstructionMemory().deregisterCellModifiedListener(currentRegisteredCellListener);
+                       machine.getClock().deregisterObserver(currentClockObserver);
+               }
+               if (canvas != null)
+               {
+                       offX = canvas.getOffX();
+                       offY = canvas.getOffY();
+                       zoom = canvas.getZoom();
+                       canvas.dispose();
+               } else
+               {
+                       offX = 0;
+                       offY = 0;
+                       zoom = -1;
+               }
 
                Optional<Machine> machineOptional;
                if (context != null && (machineOptional = context.getActiveMachine()).isPresent())
@@ -79,15 +112,25 @@ public class SimulationViewEditor extends EditorPart
                        sbseButton.setEnabled(true);
                        pauseButton.setEnabled(true);
                        simSpeedSlider.setEnabled(true);
+
                        machine = machineOptional.get();
-                       canvas = new LogicUICanvas(parent, SWT.NONE, machine.getModel());
+                       canvas = new LogicUICanvas(canvasParent, SWT.NONE, machine.getModel());
                        ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
                        userInput.buttonDrag = 3;
                        userInput.buttonZoom = 2;
                        userInput.enableUserInput();
+                       if (zoom > 0)
+                       {
+                               canvas.moveTo(offX, offY, zoom);
+                               canvas.commitTransform();
+                       }
 
-                       GridData uiData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
-                       canvas.setLayoutData(uiData);
+                       AssignableMicroInstructionMemory mIMemory = machine.getMicroInstructionMemory();
+                       instPreview.bindMicroInstructionMemory(mIMemory);
+                       currentRegisteredCellListener = a -> instPreview.refresh();
+                       mIMemory.registerCellModifiedListener(currentRegisteredCellListener);
+
+                       canvasParent.layout();
 
                        // initialize executer
                        exec = new LogicExecuter(machine.getTimeline());
@@ -108,16 +151,18 @@ public class SimulationViewEditor extends EditorPart
 
                sbseButton = new Button(c, SWT.CHECK);
                pauseButton = new Button(c, SWT.TOGGLE);
-               LogicObserver clockObserver = o ->
+               currentClockObserver = o ->
                {
                        if (((CoreClock) o).isOn())
                        {
                                exec.pauseLiveExecution();
-                               Display.getDefault().asyncExec(() ->
-                               {
-                                       pauseButton.setSelection(false);
-                                       setPauseText(pauseButton, false);
-                               });
+                               if (!pauseButton.isDisposed())
+                                       Display.getDefault().asyncExec(() ->
+                                       {
+                                               if (!pauseButton.isDisposed())
+                                                       pauseButton.setSelection(false);
+                                               setPauseText(pauseButton, false);
+                                       });
                        }
                };
 
@@ -126,9 +171,9 @@ public class SimulationViewEditor extends EditorPart
                {
                        CoreClock cl = machine.getClock();
                        if (sbseButton.getSelection())
-                               cl.registerObserver(clockObserver);
+                               cl.registerObserver(currentClockObserver);
                        else
-                               cl.deregisterObserver(clockObserver);
+                               cl.deregisterObserver(currentClockObserver);
                });
                sbseButton.setSelection(false);
 
@@ -191,6 +236,13 @@ public class SimulationViewEditor extends EditorPart
                c.pack();
        }
 
+       private void addInstructionPreviewControlWidgets(Composite parent)
+       {
+               instPreview = new InstructionTable(parent, new DisplaySettings());
+               instPreview.getTableViewer().getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
+               instPreview.setContentProvider(new ActiveInstructionPreviewContentProvider(instPreview.getTableViewer()));
+       }
+
        private static void setPauseText(Button pauseButton, boolean hovered)
        {
                if (hovered)
@@ -211,6 +263,7 @@ public class SimulationViewEditor extends EditorPart
                {
                        IFileEditorInput fileInput = (IFileEditorInput) input;
                        context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject());
+                       context.registerObserver(m -> recreateContextDependentControls());
                        recreateContextDependentControls();
 
                        setPartName(fileInput.getName());