Fixed issue with row highlighting
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / tables / mi / InstructionView.java
index b50dc84..7a692c9 100644 (file)
@@ -11,7 +11,6 @@ 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;
@@ -19,22 +18,24 @@ import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.part.EditorPart;
 
 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.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
 {
        private InstructionTableContentProvider provider;
-       private int highlighted = 0;
        private boolean dirty = false;
        private MicroInstructionMemory memory;
        private InstructionTable table;
+       private MachineContext context;
+       private RowHighlighter highlighter;
 
        @SuppressWarnings("unused")
        @Override
@@ -48,38 +49,54 @@ 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);
 
                GridData viewerData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
                viewerData.horizontalSpan = 3;
                table.getTableViewer().getTable().setLayoutData(viewerData);
+               highlighter = new RowHighlighter(table.getTableViewer());
        }
 
        public void highlight(int index)
        {
-               Display.getDefault().asyncExec(() ->
-               {
-                       LazyTableViewer viewer = table.getTableViewer();
-                       viewer.highlightRow(highlighted, false);
-                       highlighted = index;
-                       viewer.highlightRow(index, true);
-                       viewer.getTable().setTopIndex(index);
-               });
+               highlighter.highlight(index);
        }
 
        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 ->
+               {
+                       // 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);
+               }));
        }
 
        public void bindMicroInstructionMemory(MicroInstructionMemory memory)
        {
+               if (this.memory != null)
+               {
+                       this.memory.deregisterCellModifiedListener(this);
+                       this.memory.deregisterActiveMicroInstructionChangedListener(this);
+               }
                this.memory = memory;
                if (memory != null)
                {
@@ -90,34 +107,25 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
                        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
@@ -133,9 +141,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
@@ -161,11 +178,20 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
        {
                setSite(site);
                setInput(input);
-               if (input instanceof IFileEditorInput)
+               try
+               {
+                       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)
                {
-                       IFileEditorInput fileInput = (IFileEditorInput) input;
-                       setPartName(fileInput.getName());
-                       open(fileInput.getFile());
+                       throw new PartInitException("Failed to read input!", e);
                }
 
        }
@@ -200,4 +226,11 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
        {
                highlight((int) (address - memory.getDefinition().getMinimalAddress()));
        }
+
+       @Override
+       public void dispose()
+       {
+               table.dispose();
+               super.dispose();
+       }
 }