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=b352e0939312c781998078c3217f85cd6b007572;hb=ffae9b42c7c9d7c22e8f3a976aea5055d1cb9526;hp=11e8f3ae56725e7e8c9c641d1a5f107799c253c1;hpb=b90a853ca1f178048ae7ddeb7876d7eb0dff1699;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 11e8f3ae..b352e093 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 @@ -1,10 +1,10 @@ package net.mograsim.plugin.tables.mi; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; import java.io.IOException; +import java.io.InputStream; +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.swt.layout.GridData; @@ -12,10 +12,9 @@ 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.swt.widgets.FileDialog; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorSite; -import org.eclipse.ui.IPathEditorInput; +import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.EditorPart; @@ -24,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; @@ -34,9 +34,9 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis private InstructionTableContentProvider provider; private int highlighted = 0; private boolean dirty = false; - private String machineName; private MicroInstructionMemory memory; private InstructionTable table; + private MachineContext context; @SuppressWarnings("unused") @Override @@ -68,7 +68,7 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis viewer.highlightRow(highlighted, false); highlighted = index; viewer.highlightRow(index, true); - viewer.getTable().showItem(viewer.getTable().getItem(Math.min((int) memory.getDefinition().getMaximalAddress(), index - 4))); + viewer.getTable().showItem(viewer.getTable().getItem(Math.min((int) memory.getDefinition().getMaximalAddress(), index + 2))); viewer.getTable().showItem(viewer.getTable().getItem(index)); }); } @@ -78,45 +78,44 @@ 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; - this.memory.registerCellModifiedListener(this); - this.memory.registerActiveMicroInstructionChangedListener(this); + if (memory != null) + { + this.memory.registerCellModifiedListener(this); + this.memory.registerActiveMicroInstructionChangedListener(this); + } if (table != null) table.bindMicroInstructionMemory(memory); } - private void open(String file) + private void open(IFile file) throws IOException, MicroInstructionMemoryParseException, CoreException { - try (BufferedReader bf = new BufferedReader(new FileReader(file))) - { - machineName = bf.readLine(); - bindMicroInstructionMemory(MicroInstructionMemoryParser.parseMemory(machineName, bf)); - } - catch (IOException | MicroInstructionMemoryParseException e) - { - e.printStackTrace(); - } + bindMicroInstructionMemory(MicroInstructionMemoryParser.parseMemory( + context.getMachineDefinition().orElseThrow(() -> new MicroInstructionMemoryParseException("No MachineDefinition assigned!")) + .getMicroInstructionMemoryDefinition(), + file.getContents())); } - private void save(String file) + 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; - } - try - { - MicroInstructionMemoryParser.write(memory, machineName, file); + throw new MicroInstructionMemoryParseException( + "Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned."); } - catch (IOException e) + try (InputStream toWrite = MicroInstructionMemoryParser.write(memory)) { - e.printStackTrace(); + file.setContents(toWrite, 0, progressMonitor); } } @@ -130,43 +129,61 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis public void doSave(IProgressMonitor progressMonitor) { IEditorInput input = getEditorInput(); - if (input instanceof IPathEditorInput) + if (input instanceof IFileEditorInput) { - IPathEditorInput pathInput = (IPathEditorInput) input; - save(pathInput.getPath().toOSString()); - setDirty(false); - } + IFileEditorInput pathInput = (IFileEditorInput) input; + try + { + save(pathInput.getFile(), progressMonitor); + setDirty(false); + } + catch (Exception e) + { + e.printStackTrace(); + progressMonitor.setCanceled(true); + } + } else + progressMonitor.setCanceled(true); } @Override public void doSaveAs() { - openSaveAsDialog(); +// openSaveAsDialog(); } - private void openSaveAsDialog() - { - FileDialog d = new FileDialog(table.getTableViewer().getTable().getShell(), SWT.SAVE); - d.open(); - String filename = d.getFileName(); - if (!filename.equals("")) - { - save(d.getFilterPath() + File.separator + filename); - setDirty(false); - } - } +// private void openSaveAsDialog() +// { +// FileDialog d = new FileDialog(table.getTableViewer().getTable().getShell(), SWT.SAVE); +// d.open(); +// String filename = d.getFileName(); +// if (!filename.equals("")) +// { +// save(d.getFilterPath() + File.separator + filename); +// setDirty(false); +// } +// } @Override public void init(IEditorSite site, IEditorInput input) throws PartInitException { setSite(site); setInput(input); - if (input instanceof IPathEditorInput) + try { - IPathEditorInput pathInput = (IPathEditorInput) input; - setPartName(pathInput.getName()); - open(pathInput.getPath().toOSString()); + if (input instanceof IFileEditorInput) + { + IFileEditorInput fileInput = (IFileEditorInput) input; + context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject()); + setPartName(fileInput.getName()); + open(fileInput.getFile()); + } } + catch (Exception e) + { + throw new PartInitException("Failed to read input!", e); + } + } @Override @@ -178,7 +195,7 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis @Override public boolean isSaveAsAllowed() { - return true; + return false; } @Override