X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.plugin.core%2Fsrc%2Fnet%2Fmograsim%2Fplugin%2Ftables%2Fmi%2FInstructionView.java;h=235e3e533b27c6353a210de40b1298b7662c1098;hb=a6f5743665014d5590c1b2a314cb4a6888ec2245;hp=af7e211302afc500e6994c1ab831125840f42e6a;hpb=6afa196302d15c2a7d6492c577a57a91c3cb85a4;p=Mograsim.git diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/InstructionView.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/InstructionView.java index af7e2113..235e3e53 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/InstructionView.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/InstructionView.java @@ -19,6 +19,7 @@ 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; @@ -67,9 +68,13 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis 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)); + 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)); + } }); } @@ -77,12 +82,35 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis { Button activationButton = new Button(parent, SWT.PUSH); activationButton.setText("Set Active"); - activationButton.addListener(SWT.Selection, - e -> context.getActiveMachine().ifPresent(m -> m.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) { @@ -93,37 +121,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(context.getMachineDefinition() - .orElseThrow(() -> new MicroInstructionMemoryParseException("No MachineDefinition assigned!")) - .getMicroInstructionMemoryDefinition(), file.getContents())); - } - catch (IOException | MicroInstructionMemoryParseException | CoreException e) - { - - // TODO: Proper handling via IProgressMonitor - 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 @@ -139,9 +155,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 @@ -167,13 +192,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; - context = ProjectMachineContext.getMachineContextOf(fileInput.getFile()); - setPartName(fileInput.getName()); - open(fileInput.getFile()); + throw new PartInitException("Failed to read input!", e); } }