ActiveInstructionChangedListener moved to Machine and updated
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / tables / mi / InstructionView.java
index 37aab63..93a7632 100644 (file)
@@ -11,30 +11,60 @@ 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.part.EditorPart;
 
+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.MachineContext;
+import net.mograsim.plugin.nature.MachineContext;
+import net.mograsim.plugin.nature.MachineContext.ActiveMachineListener;
+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;
+
+       // Listeners
+       private MemoryCellModifiedListener cellModifiedListener = address ->
+       {
+               setDirty(true);
+               table.refresh();
+       };
+
+       private ActiveMicroInstructionChangedListener instChangeListener = (oldAddress, newAddress) ->
+       {
+               highlight((int) (newAddress - memory.getDefinition().getMinimalAddress()));
+       };
+
+       private MIMemoryReassignedListener reassignedListener = newAssignee ->
+       {
+               // clear highlighting if the memory is reassigned
+               if (newAssignee != memory)
+                       highlight(-1);
+       };
+
+       private ActiveMachineListener activeMachineListener = (oldMachine, newMachine) ->
+       {
+               // clear highlighting if the active machine changes
+               if (newMachine.isEmpty() || !newMachine.equals(oldMachine))
+               {
+                       highlight(-1);
+                       oldMachine.ifPresent(m -> m.getMicroInstructionMemory().deregisterMemoryReassignedListener(reassignedListener));
+               }
+       };
 
        @SuppressWarnings("unused")
        @Override
@@ -48,8 +78,7 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
                new RadixSelector(parent, displaySettings);
 
                addActivationButton(parent);
-
-               table = new InstructionTable(parent, displaySettings);
+               table = new InstructionTable(parent, displaySettings, getSite().getWorkbenchWindow().getWorkbench().getThemeManager());
                table.setContentProvider(provider);
                table.bindMicroInstructionMemory(memory);
 
@@ -58,67 +87,58 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
                table.getTableViewer().getTable().setLayoutData(viewerData);
        }
 
-       public void highlight(int index)
+       public void highlight(int row)
        {
-               Display.getDefault().asyncExec(() ->
-               {
-                       LazyTableViewer viewer = table.getTableViewer();
-                       viewer.highlightRow(highlighted, false);
-                       highlighted = index;
-                       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));
-               });
+               table.highlight(row);
        }
 
        private void addActivationButton(Composite parent)
        {
                Button activationButton = new Button(parent, SWT.PUSH);
                activationButton.setText("Set Active");
-               activationButton.addListener(SWT.Selection,
-                               e -> MachineContext.getInstance().getMachine().getMicroInstructionMemory().bind(memory));
+               activationButton.addListener(SWT.Selection, e -> context.getActiveMachine().ifPresent(m ->
+               {
+                       m.getMicroInstructionMemory().registerMemoryReassignedListener(reassignedListener);
+                       context.addActiveMachineListener(activeMachineListener);
+                       m.getMicroInstructionMemory().bind(memory);
+                       m.addActiveMicroInstructionChangedListener(instChangeListener);
+               }));
        }
 
        public void bindMicroInstructionMemory(MicroInstructionMemory memory)
        {
+               if (this.memory != null)
+               {
+                       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);
        }
 
-       private void open(IFile file)
+       private void open(IFile file) throws IOException, MicroInstructionMemoryParseException, CoreException
        {
-               try
-               {
-                       bindMicroInstructionMemory(MicroInstructionMemoryParser.parseMemory(
-                                       MachineContext.getInstance().getMachine().getDefinition().getMicroInstructionMemoryDefinition(), file.getContents()));
-               }
-               catch (IOException | MicroInstructionMemoryParseException | CoreException e)
-               {
-                       e.printStackTrace();
-               }
+               bindMicroInstructionMemory(MicroInstructionMemoryParser.parseMemory(
+                               context.getMachineDefinition().orElseThrow(() -> new MicroInstructionMemoryParseException("No MachineDefinition assigned!"))
+                                               .getMicroInstructionMemoryDefinition(),
+                               file.getContents()));
        }
 
-       private void save(IFile file, IProgressMonitor progressMonitor)
+       private void save(IFile file, IProgressMonitor progressMonitor) throws IOException, CoreException, MicroInstructionMemoryParseException
        {
                if (memory == null)
                {
-                       System.err.println("Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned.");
-                       return;
+                       throw new MicroInstructionMemoryParseException(
+                                       "Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned.");
                }
                try (InputStream toWrite = MicroInstructionMemoryParser.write(memory))
                {
                        file.setContents(toWrite, 0, progressMonitor);
                }
-               catch (IOException | CoreException e)
-               {
-                       e.printStackTrace();
-               }
        }
 
        @Override
@@ -134,9 +154,18 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
                if (input instanceof IFileEditorInput)
                {
                        IFileEditorInput pathInput = (IFileEditorInput) input;
-                       save(pathInput.getFile(), progressMonitor);
-                       setDirty(false);
-               }
+                       try
+                       {
+                               save(pathInput.getFile(), progressMonitor);
+                               setDirty(false);
+                       }
+                       catch (Exception e)
+                       {
+                               e.printStackTrace();
+                               progressMonitor.setCanceled(true);
+                       }
+               } else
+                       progressMonitor.setCanceled(true);
        }
 
        @Override
@@ -162,11 +191,20 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
        {
                setSite(site);
                setInput(input);
-               if (input instanceof IFileEditorInput)
+               try
                {
-                       IFileEditorInput fileInput = (IFileEditorInput) input;
-                       setPartName(fileInput.getName());
-                       open(fileInput.getFile());
+                       if (input instanceof IFileEditorInput)
+                       {
+                               IFileEditorInput fileInput = (IFileEditorInput) input;
+                               context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject());
+                               context.activateMachine();
+                               setPartName(fileInput.getName());
+                               open(fileInput.getFile());
+                       }
+               }
+               catch (Exception e)
+               {
+                       throw new PartInitException("Failed to read input!", e);
                }
 
        }
@@ -183,13 +221,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;
@@ -197,8 +228,15 @@ 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);
+               context.getActiveMachine().ifPresent(m ->
+               {
+                       m.removeActiveMicroInstructionChangedListener(instChangeListener);
+                       m.getMicroInstructionMemory().deregisterMemoryReassignedListener(reassignedListener);
+               });
+               context.removeActiveMachineListener(activeMachineListener);
+               super.dispose();
        }
 }