ActiveInstructionChangedListener moved to Machine and updated
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / tables / mi / ActiveInstructionPreviewContentProvider.java
index 604c285..d3a14ec 100644 (file)
@@ -3,14 +3,24 @@ package net.mograsim.plugin.tables.mi;
 import org.eclipse.jface.viewers.TableViewer;
 import org.eclipse.jface.viewers.Viewer;
 
+import net.mograsim.machine.Machine;
+import net.mograsim.machine.Machine.ActiveMicroInstructionChangedListener;
 import net.mograsim.machine.mi.MicroInstructionMemory;
-import net.mograsim.machine.mi.MicroInstructionMemory.ActiveMicroInstructionChangedListener;
+import net.mograsim.plugin.util.SingleSWTRequest;
 
-public class ActiveInstructionPreviewContentProvider implements InstructionTableContentProvider, ActiveMicroInstructionChangedListener
+public class ActiveInstructionPreviewContentProvider implements InstructionTableContentProvider
 {
        private TableViewer viewer;
        private MicroInstructionMemory memory;
        private InstructionTableRow activeRow;
+       private Machine machine;
+       private SingleSWTRequest requester = new SingleSWTRequest();
+
+       private ActiveMicroInstructionChangedListener instChanged = (oldAddress, newAddress) ->
+       {
+               activeRow = new InstructionTableRow(Long.max(0, newAddress), memory);
+               requester.request(() -> updateElement(0));
+       };
 
        public ActiveInstructionPreviewContentProvider(TableViewer viewer)
        {
@@ -18,13 +28,6 @@ public class ActiveInstructionPreviewContentProvider implements InstructionTable
                viewer.setItemCount(1);
        }
 
-       @Override
-       public void activeMicroInstructionChanged(long address)
-       {
-               this.activeRow = new InstructionTableRow(address, memory);
-               viewer.refresh();
-       }
-
        @Override
        public void update(long address)
        {
@@ -34,14 +37,19 @@ public class ActiveInstructionPreviewContentProvider implements InstructionTable
        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
        {
-               if (oldInput != null)
-                       ((MicroInstructionMemory) oldInput).deregisterActiveMicroInstructionChangedListener(this);
-
                memory = (MicroInstructionMemory) newInput;
-               if (memory != null)
+       }
+
+       public void setMachine(Machine newMachine)
+       {
+               if (machine != null)
+                       machine.removeActiveMicroInstructionChangedListener(instChanged);
+
+               machine = newMachine;
+               if (machine != null)
                {
-                       activeMicroInstructionChanged(0);
-                       memory.registerActiveMicroInstructionChangedListener(this);
+                       instChanged.instructionChanged(-1, 0);
+                       machine.addActiveMicroInstructionChangedListener(instChanged);
                }
        }