Reworked some parts of the MachineContext to make its status clear.
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / tables / mi / InstructionView.java
index b50dc84..10d2f4d 100644 (file)
@@ -23,7 +23,8 @@ 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;
@@ -35,6 +36,7 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
        private boolean dirty = false;
        private MicroInstructionMemory memory;
        private InstructionTable table;
+       private MachineContext context;
 
        @SuppressWarnings("unused")
        @Override
@@ -66,7 +68,8 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
                        viewer.highlightRow(highlighted, false);
                        highlighted = index;
                        viewer.highlightRow(index, true);
-                       viewer.getTable().setTopIndex(index);
+                       viewer.getTable().showItem(viewer.getTable().getItem(Math.min((int) memory.getDefinition().getMaximalAddress(), index + 2)));
+                       viewer.getTable().showItem(viewer.getTable().getItem(index));
                });
        }
 
@@ -75,11 +78,16 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
                Button activationButton = new Button(parent, SWT.PUSH);
                activationButton.setText("Set Active");
                activationButton.addListener(SWT.Selection,
-                               e -> MachineContext.getInstance().getMachine().getMicroInstructionMemory().bind(memory));
+                               e -> context.getActiveMachine().ifPresent(m -> 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 +98,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 +132,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 +169,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);
                }
 
        }