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=214bd8ce1bae257b89db149b047619db96c365a2;hb=648fc6e69e09fe4467cb6bac47934be1a7dcf0d6;hp=58676562f185f9bcffd327c45c6569b1cc4e0f56;hpb=7e700a185d137a754e47609e69cc044d0f65e9b3;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 58676562..214bd8ce 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,282 +1,244 @@ package net.mograsim.plugin.tables.mi; -import java.io.File; import java.io.IOException; -import java.util.Arrays; +import java.io.InputStream; import java.util.Optional; -import org.eclipse.jface.viewers.ColumnLabelProvider; -import org.eclipse.jface.viewers.EditingSupport; -import org.eclipse.jface.viewers.TableViewerColumn; -import org.eclipse.swt.SWT; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.debug.ui.DebugUITools; +import org.eclipse.debug.ui.contexts.IDebugContextManager; +import org.eclipse.debug.ui.contexts.IDebugContextService; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.FileDialog; -import org.eclipse.swt.widgets.Table; -import org.eclipse.swt.widgets.TableColumn; -import org.eclipse.ui.part.ViewPart; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorSite; +import org.eclipse.ui.IFileEditorInput; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.part.EditorPart; import net.mograsim.machine.Machine; -import net.mograsim.machine.mi.MicroInstructionDefinition; +import net.mograsim.machine.Machine.ActiveMicroInstructionChangedListener; +import net.mograsim.machine.Memory.MemoryCellModifiedListener; import net.mograsim.machine.mi.MicroInstructionMemory; import net.mograsim.machine.mi.MicroInstructionMemoryParseException; import net.mograsim.machine.mi.MicroInstructionMemoryParser; -import net.mograsim.machine.mi.parameters.MnemonicFamily; -import net.mograsim.machine.mi.parameters.ParameterClassification; -import net.mograsim.plugin.MachineContext; -import net.mograsim.plugin.MachineContext.ContextObserver; -import net.mograsim.plugin.tables.AddressLabelProvider; +import net.mograsim.plugin.launch.MachineDebugContextListener; +import net.mograsim.plugin.launch.MachineDebugTarget; +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; -import net.mograsim.plugin.util.DropDownMenu; -import net.mograsim.plugin.util.DropDownMenu.DropDownEntry; -public class InstructionView extends ViewPart implements ContextObserver +public class InstructionView extends EditorPart { - private String saveLoc = null; - private LazyTableViewer viewer; - private TableViewerColumn[] columns = new TableViewerColumn[0]; - private MicroInstructionDefinition miDef; - private MicroInstructionMemory memory; - private DisplaySettings displaySettings; private InstructionTableContentProvider provider; - private int highlighted = 0; + private boolean dirty = false; + private MicroInstructionMemory memory; + private InstructionTable table; + private MachineContext context; + + private IFile file; + + // Listeners + private MemoryCellModifiedListener cellModifiedListener = address -> + { + setDirty(true); + table.refresh(); + }; + + private ActiveMicroInstructionChangedListener instChangeListener = (oldAddress, newAddress) -> + { + highlight((int) (newAddress - memory.getDefinition().getMinimalAddress())); + }; + + private MachineDebugContextListener debugContextListener = new MachineDebugContextListener() + { + @Override + public void machineDebugContextChanged(Optional oldTarget, Optional newTarget) + { + instChangeListener.instructionChanged(-1, -1); + oldTarget.ifPresent(target -> target.getMachine().removeActiveMicroInstructionChangedListener(instChangeListener)); + + newTarget.ifPresent(target -> + { + if (file.equals(target.getMPMFile())) + { + Machine m = target.getMachine(); + target.getMachine().addActiveMicroInstructionChangedListener(instChangeListener); + instChangeListener.instructionChanged(-1, m.getActiveMicroInstructionAddress()); + } + }); + } + }; @SuppressWarnings("unused") @Override public void createPartControl(Composite parent) { - provider = new InstructionTableContentProvider(); + provider = new InstructionTableLazyContentProvider(); GridLayout layout = new GridLayout(3, false); - setupMenuButtons(parent); + parent.setLayout(layout); - displaySettings = new DisplaySettings(); + DisplaySettings displaySettings = new DisplaySettings(); new RadixSelector(parent, displaySettings); - parent.setLayout(layout); - viewer = new LazyTableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER | SWT.VIRTUAL); - - Table table = viewer.getTable(); - table.setHeaderVisible(true); - table.setLinesVisible(true); - viewer.setUseHashlookup(true); - viewer.setContentProvider(provider); - getSite().setSelectionProvider(viewer); + 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; - viewer.getTable().setLayoutData(viewerData); + table.getTableViewer().getTable().setLayoutData(viewerData); - displaySettings.addObserver(() -> viewer.refresh()); - MachineContext.getInstance().registerObserver(this); - setMachine(Optional.ofNullable(MachineContext.getInstance().getMachine())); + IDebugContextManager debugCManager = DebugUITools.getDebugContextManager(); + IDebugContextService contextService = debugCManager.getContextService(PlatformUI.getWorkbench().getActiveWorkbenchWindow()); + contextService.addDebugContextListener(debugContextListener); + debugContextListener.debugContextChanged(contextService.getActiveContext()); + parent.addDisposeListener(e -> contextService.removeDebugContextListener(debugContextListener)); } - public void highlight(int index) + public void highlight(int row) { - viewer.highlightRow(highlighted, false); - viewer.highlightRow(index, true); - viewer.getTable().setTopIndex(index); + table.highlight(row); } - @SuppressWarnings("unused") - private void setupMenuButtons(Composite parent) + public void bindMicroInstructionMemory(MicroInstructionMemory memory) { - DropDownEntry open = new DropDownEntry("Open", (e) -> - { - FileDialog d = new FileDialog(parent.getShell(), SWT.NONE); - d.open(); - String filename = d.getFileName(); - if (!filename.equals("")) - open(d.getFilterPath() + File.separator + filename); - }); - - DropDownEntry save = new DropDownEntry("Save", (e) -> + if (this.memory != null) { - if (saveLoc == null) - openSaveAsDialog(parent); - save(saveLoc); - }); - DropDownEntry saveAs = new DropDownEntry("SaveAs", (e) -> + this.memory.deregisterCellModifiedListener(cellModifiedListener); + } + this.memory = memory; + if (memory != null) { - openSaveAsDialog(parent); - save(saveLoc); - }); - new DropDownMenu(parent, "File", open, save, saveAs); + this.memory.registerCellModifiedListener(cellModifiedListener); + } + if (table != null) + table.bindMicroInstructionMemory(memory); } - private void openSaveAsDialog(Composite parent) + private void open(IFile file) throws IOException, MicroInstructionMemoryParseException, CoreException { - FileDialog d = new FileDialog(parent.getShell(), SWT.SAVE); - d.open(); - String filename = d.getFileName(); - if (!filename.equals("")) - saveLoc = d.getFilterPath() + File.separator + filename; + bindMicroInstructionMemory(MicroInstructionMemoryParser.parseMemory( + context.getMachineDefinition().orElseThrow(() -> new MicroInstructionMemoryParseException("No MachineDefinition assigned!")) + .getMicroInstructionMemoryDefinition(), + file.getContents())); } - public void bindMicroInstructionMemory(MicroInstructionMemory memory) + private void save(IFile file, IProgressMonitor progressMonitor) throws IOException, CoreException, MicroInstructionMemoryParseException { - deleteColumns(); - this.memory = memory; - viewer.setInput(memory); - this.miDef = memory.getDefinition().getMicroInstructionDefinition(); - createColumns(); + if (memory == null) + { + throw new MicroInstructionMemoryParseException( + "Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned."); + } + try (InputStream toWrite = MicroInstructionMemoryParser.write(memory)) + { + file.setContents(toWrite, 0, progressMonitor); + } } - private void deleteColumns() + @Override + public void setFocus() { - for (TableViewerColumn col : columns) - col.getColumn().dispose(); + table.getTableViewer().getControl().setFocus(); } - private void createColumns() + @Override + public void doSave(IProgressMonitor progressMonitor) { - int size = miDef.size(); - columns = new TableViewerColumn[size + 1]; - - TableViewerColumn col = createTableViewerColumn("Address", generateLongestHexStrings(12)); - columns[0] = col; - col.setLabelProvider(new AddressLabelProvider()); - - int bit = miDef.sizeInBits(); - ParameterClassification[] classes = miDef.getParameterClassifications(); - - for (int i = 0; i < size; i++) + IEditorInput input = getEditorInput(); + if (input instanceof IFileEditorInput) { - int startBit = bit - 1; - int endBit = bit = bit - classes[i].getExpectedBits(); - String name = startBit == endBit ? Integer.toString(startBit) : startBit + "..." + endBit; - - String[] longestPossibleContents; - switch (classes[i].getExpectedType()) + IFileEditorInput pathInput = (IFileEditorInput) input; + try { - case INTEGER_IMMEDIATE: - longestPossibleContents = generateLongestHexStrings(classes[i].getExpectedBits()); - break; - case BOOLEAN_IMMEDIATE: - case MNEMONIC: - longestPossibleContents = ((MnemonicFamily) classes[i]).getStringValues(); - break; - default: - longestPossibleContents = new String[0]; - break; + save(pathInput.getFile(), progressMonitor); + setDirty(false); } - - col = createTableViewerColumn(name, longestPossibleContents); - columns[i + 1] = col; - createEditingAndLabel(col, miDef, i); - } + catch (Exception e) + { + e.printStackTrace(); + progressMonitor.setCanceled(true); + } + } else + progressMonitor.setCanceled(true); } - private static final String[] HEX_DIGITS = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; - - private static String[] generateLongestHexStrings(int bitWidth) + @Override + public void doSaveAs() { - return Arrays.stream(HEX_DIGITS).map(s -> "0x" + s.repeat((bitWidth + 3) / 4)).toArray(String[]::new); +// openSaveAsDialog(); } - private void createEditingAndLabel(TableViewerColumn col, MicroInstructionDefinition miDef, int index) +// 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 { - ParameterClassification parameterClassification = miDef.getParameterClassifications()[index]; - EditingSupport support; - ColumnLabelProvider provider; - switch (parameterClassification.getExpectedType()) + setSite(site); + setInput(input); + try { - case BOOLEAN_IMMEDIATE: - support = new BooleanEditingSupport(viewer, miDef, index); - provider = new ParameterLabelProvider(index); - break; - case INTEGER_IMMEDIATE: - support = new IntegerEditingSupport(viewer, miDef, index, displaySettings, this.provider); - provider = new IntegerColumnLabelProvider(displaySettings, index); - break; - case MNEMONIC: - support = new MnemonicEditingSupport(viewer, miDef, index, this.provider); - provider = new ParameterLabelProvider(index); - break; - default: - throw new IllegalStateException( - "Unable to create EditingSupport for unknown ParameterType " + parameterClassification.getExpectedType()); + if (input instanceof IFileEditorInput) + { + IFileEditorInput fileInput = (IFileEditorInput) input; + file = fileInput.getFile(); + context = ProjectMachineContext.getMachineContextOf(file.getProject()); + + setPartName(fileInput.getName()); + open(file); + } else + throw new IllegalArgumentException("Expected IFileEditorInput!"); + } + catch (Exception e) + { + throw new PartInitException("Failed to read input!", e); } - col.setEditingSupport(support); - col.setLabelProvider(provider); - col.getColumn().setToolTipText(miDef.getParameterDescription(index).orElse("")); } - private TableViewerColumn createTableViewerColumn(String title, String... longestPossibleContents) + @Override + public boolean isDirty() { - TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE); - TableColumn column = viewerColumn.getColumn(); - int maxWidth = 0; - for (String s : longestPossibleContents) - { - column.setText(s); - column.pack(); - if (column.getWidth() > maxWidth) - maxWidth = column.getWidth(); - } - column.setText(title); - column.pack(); - if (column.getWidth() < maxWidth) - column.setWidth(maxWidth); - column.setResizable(true); - column.setMoveable(false); - return viewerColumn; + return dirty; } - private void open(String file) + @Override + public boolean isSaveAsAllowed() { - if (miDef == null) - { - System.err.println("Failed to parse MicroprogrammingMemory from File. No MicroInstructionDefinition assigned."); - return; - } - try - { - MicroInstructionMemoryParser.parseMemory(memory, file); - viewer.refresh(); - saveLoc = file; - } - catch (IOException | MicroInstructionMemoryParseException e) - { - e.printStackTrace(); - } + return false; } - private void save(String file) + private void setDirty(boolean value) { - if (memory == null) - { - System.err.println("Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned."); - } - if (saveLoc != null) - { - try - { - MicroInstructionMemoryParser.write(memory, file); - } - catch (IOException e) - { - e.printStackTrace(); - } - } + dirty = value; + firePropertyChange(PROP_DIRTY); } @Override - public void setFocus() + public void dispose() { - viewer.getControl().setFocus(); + if (memory != null) + memory.deregisterCellModifiedListener(cellModifiedListener); + super.dispose(); } - @Override - public void setMachine(Optional machine) + public IFile getFile() { - if (machine.isPresent()) - { - Machine actualMachine = machine.get(); - bindMicroInstructionMemory(actualMachine.getMicroInstructionMemory()); - } + return file; } }