Moved ResourceChangeListener to DebugTarget; Listener now deregisters
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / tables / mi / InstructionView.java
index 0158c7b..1da1cbf 100644 (file)
@@ -2,43 +2,79 @@ package net.mograsim.plugin.tables.mi;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Optional;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.swt.SWT;
+import org.eclipse.debug.ui.DebugUITools;
+import org.eclipse.debug.ui.contexts.IDebugContextManager;
+import org.eclipse.debug.ui.contexts.IDebugContextService;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.part.EditorPart;
 
+import net.mograsim.machine.Machine;
+import net.mograsim.machine.Machine.ActiveMicroInstructionChangedListener;
 import net.mograsim.machine.Memory.MemoryCellModifiedListener;
-import net.mograsim.machine.mi.AssignableMicroInstructionMemory.MIMemoryReassignedListener;
 import net.mograsim.machine.mi.MicroInstructionMemory;
-import net.mograsim.machine.mi.MicroInstructionMemory.ActiveMicroInstructionChangedListener;
 import net.mograsim.machine.mi.MicroInstructionMemoryParseException;
 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
+import net.mograsim.plugin.launch.MachineDebugContextListener;
+import net.mograsim.plugin.launch.MachineDebugTarget;
 import net.mograsim.plugin.nature.MachineContext;
 import net.mograsim.plugin.nature.ProjectMachineContext;
 import net.mograsim.plugin.tables.DisplaySettings;
-import net.mograsim.plugin.tables.LazyTableViewer;
 import net.mograsim.plugin.tables.RadixSelector;
 
-public class InstructionView extends EditorPart implements MemoryCellModifiedListener, ActiveMicroInstructionChangedListener
+public class InstructionView extends EditorPart
 {
        private InstructionTableContentProvider provider;
-       private int highlighted = 0;
        private boolean dirty = false;
        private MicroInstructionMemory memory;
        private InstructionTable table;
        private MachineContext context;
 
+       private IFile file;
+
+       // Listeners
+       private MemoryCellModifiedListener cellModifiedListener = address ->
+       {
+               setDirty(true);
+               table.refresh();
+       };
+
+       private ActiveMicroInstructionChangedListener instChangeListener = (oldAddress, newAddress) ->
+       {
+               highlight((int) (newAddress - memory.getDefinition().getMinimalAddress()));
+       };
+
+       private MachineDebugContextListener debugContextListener = new MachineDebugContextListener()
+       {
+               @Override
+               public void machineDebugContextChanged(Optional<MachineDebugTarget> oldTarget, Optional<MachineDebugTarget> newTarget)
+               {
+                       instChangeListener.instructionChanged(-1, -1);
+                       oldTarget.ifPresent(target -> target.getMachine().removeActiveMicroInstructionChangedListener(instChangeListener));
+
+                       newTarget.ifPresent(target ->
+                       {
+                               if (file.equals(target.getMPMFile()))
+                               {
+                                       Machine m = target.getMachine();
+                                       target.getMachine().addActiveMicroInstructionChangedListener(instChangeListener);
+                                       instChangeListener.instructionChanged(-1, m.getActiveMicroInstructionAddress());
+                               }
+                       });
+               }
+       };
+
        @SuppressWarnings("unused")
        @Override
        public void createPartControl(Composite parent)
@@ -50,7 +86,6 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
                DisplaySettings displaySettings = new DisplaySettings();
                new RadixSelector(parent, displaySettings);
 
-               addActivationButton(parent);
                table = new InstructionTable(parent, displaySettings, getSite().getWorkbenchWindow().getWorkbench().getThemeManager());
                table.setContentProvider(provider);
                table.bindMicroInstructionMemory(memory);
@@ -58,63 +93,29 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
                GridData viewerData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
                viewerData.horizontalSpan = 3;
                table.getTableViewer().getTable().setLayoutData(viewerData);
-       }
 
-       public void highlight(int index)
-       {
-               Display.getDefault().asyncExec(() ->
-               {
-                       LazyTableViewer viewer = table.getTableViewer();
-                       viewer.highlightRow(highlighted, false);
-                       highlighted = index;
-                       if (index != -1)
-                       {
-                               viewer.highlightRow(index, true);
-                               viewer.getTable()
-                                               .showItem(viewer.getTable().getItem(Math.min((int) memory.getDefinition().getMaximalAddress(), index + 2)));
-                               viewer.getTable().showItem(viewer.getTable().getItem(index));
-                       }
-               });
+               IDebugContextManager debugCManager = DebugUITools.getDebugContextManager();
+               IDebugContextService contextService = debugCManager.getContextService(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
+               contextService.addDebugContextListener(debugContextListener);
+               debugContextListener.debugContextChanged(contextService.getActiveContext());
+               parent.addDisposeListener(e -> contextService.removeDebugContextListener(debugContextListener));
        }
 
-       private void addActivationButton(Composite parent)
+       public void highlight(int row)
        {
-               Button activationButton = new Button(parent, SWT.PUSH);
-               activationButton.setText("Set Active");
-               activationButton.addListener(SWT.Selection, e -> context.getActiveMachine().ifPresent(m ->
-               {
-                       // clear highlighting if the memory is reassigned
-                       MIMemoryReassignedListener memReassignedListener = n ->
-                       {
-                               if (n != memory)
-                                       highlight(-1);
-                       };
-                       m.getMicroInstructionMemory().registerMemoryReassignedListener(memReassignedListener);
-                       // clear highlighting if the active machine changes
-                       context.addActiveMachineListener(n ->
-                       {
-                               if (n.isEmpty() || n.get() != m)
-                               {
-                                       highlight(-1);
-                                       m.getMicroInstructionMemory().deregisterMemoryReassignedListener(memReassignedListener);
-                               }
-                       });
-                       m.getMicroInstructionMemory().bind(memory);
-               }));
+               table.highlight(row);
        }
 
        public void bindMicroInstructionMemory(MicroInstructionMemory memory)
        {
                if (this.memory != null)
                {
-                       this.memory.deregisterCellModifiedListener(this);
-                       this.memory.deregisterActiveMicroInstructionChangedListener(this);
+                       this.memory.deregisterCellModifiedListener(cellModifiedListener);
                }
                this.memory = memory;
                if (memory != null)
                {
-                       this.memory.registerCellModifiedListener(this);
-                       this.memory.registerActiveMicroInstructionChangedListener(this);
+                       this.memory.registerCellModifiedListener(cellModifiedListener);
                }
                if (table != null)
                        table.bindMicroInstructionMemory(memory);
@@ -196,17 +197,18 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
                        if (input instanceof IFileEditorInput)
                        {
                                IFileEditorInput fileInput = (IFileEditorInput) input;
-                               context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject());
-                               context.activateMachine();
+                               file = fileInput.getFile();
+                               context = ProjectMachineContext.getMachineContextOf(file.getProject());
+
                                setPartName(fileInput.getName());
-                               open(fileInput.getFile());
-                       }
+                               open(file);
+                       } else
+                               throw new IllegalArgumentException("Expected IFileEditorInput!");
                }
                catch (Exception e)
                {
                        throw new PartInitException("Failed to read input!", e);
                }
-
        }
 
        @Override
@@ -221,13 +223,6 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
                return false;
        }
 
-       @Override
-       public void update(long address)
-       {
-               setDirty(true);
-               table.refresh();
-       }
-
        private void setDirty(boolean value)
        {
                dirty = value;
@@ -235,15 +230,14 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
        }
 
        @Override
-       public void activeMicroInstructionChanged(long address)
+       public void dispose()
        {
-               highlight((int) (address - memory.getDefinition().getMinimalAddress()));
+               memory.deregisterCellModifiedListener(cellModifiedListener);
+               super.dispose();
        }
 
-       @Override
-       public void dispose()
+       public IFile getFile()
        {
-               table.dispose();
-               super.dispose();
+               return file;
        }
 }