1 package net.mograsim.plugin.tables.mi;
3 import java.io.IOException;
4 import java.io.InputStream;
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Button;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.ui.IEditorInput;
16 import org.eclipse.ui.IEditorSite;
17 import org.eclipse.ui.IFileEditorInput;
18 import org.eclipse.ui.PartInitException;
19 import org.eclipse.ui.part.EditorPart;
21 import net.mograsim.machine.Memory.MemoryCellModifiedListener;
22 import net.mograsim.machine.mi.MicroInstructionMemory;
23 import net.mograsim.machine.mi.MicroInstructionMemory.ActiveMicroInstructionChangedListener;
24 import net.mograsim.machine.mi.MicroInstructionMemoryParseException;
25 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
26 import net.mograsim.plugin.MachineContext;
27 import net.mograsim.plugin.tables.DisplaySettings;
28 import net.mograsim.plugin.tables.LazyTableViewer;
29 import net.mograsim.plugin.tables.RadixSelector;
31 public class InstructionView extends EditorPart implements MemoryCellModifiedListener, ActiveMicroInstructionChangedListener
33 private InstructionTableContentProvider provider;
34 private int highlighted = 0;
35 private boolean dirty = false;
36 private MicroInstructionMemory memory;
37 private InstructionTable table;
39 @SuppressWarnings("unused")
41 public void createPartControl(Composite parent)
43 provider = new InstructionTableLazyContentProvider();
44 GridLayout layout = new GridLayout(3, false);
45 parent.setLayout(layout);
47 DisplaySettings displaySettings = new DisplaySettings();
48 new RadixSelector(parent, displaySettings);
50 addActivationButton(parent);
52 table = new InstructionTable(parent, displaySettings);
53 table.setContentProvider(provider);
54 table.bindMicroInstructionMemory(memory);
56 GridData viewerData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
57 viewerData.horizontalSpan = 3;
58 table.getTableViewer().getTable().setLayoutData(viewerData);
61 public void highlight(int index)
63 Display.getDefault().asyncExec(() ->
65 LazyTableViewer viewer = table.getTableViewer();
66 viewer.highlightRow(highlighted, false);
68 viewer.highlightRow(index, true);
69 viewer.getTable().setTopIndex(index);
73 private void addActivationButton(Composite parent)
75 Button activationButton = new Button(parent, SWT.PUSH);
76 activationButton.setText("Set Active");
77 activationButton.addListener(SWT.Selection,
78 e -> MachineContext.getInstance().getMachine().getMicroInstructionMemory().bind(memory));
81 public void bindMicroInstructionMemory(MicroInstructionMemory memory)
86 this.memory.registerCellModifiedListener(this);
87 this.memory.registerActiveMicroInstructionChangedListener(this);
90 table.bindMicroInstructionMemory(memory);
93 private void open(IFile file)
97 bindMicroInstructionMemory(MicroInstructionMemoryParser.parseMemory(
98 MachineContext.getInstance().getMachine().getDefinition().getMicroInstructionMemoryDefinition(), file.getContents()));
100 catch (IOException | MicroInstructionMemoryParseException | CoreException e)
106 private void save(IFile file, IProgressMonitor progressMonitor)
110 System.err.println("Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned.");
113 try (InputStream toWrite = MicroInstructionMemoryParser.write(memory))
115 file.setContents(toWrite, 0, progressMonitor);
117 catch (IOException | CoreException e)
124 public void setFocus()
126 table.getTableViewer().getControl().setFocus();
130 public void doSave(IProgressMonitor progressMonitor)
132 IEditorInput input = getEditorInput();
133 if (input instanceof IFileEditorInput)
135 IFileEditorInput pathInput = (IFileEditorInput) input;
136 save(pathInput.getFile(), progressMonitor);
142 public void doSaveAs()
144 // openSaveAsDialog();
147 // private void openSaveAsDialog()
149 // FileDialog d = new FileDialog(table.getTableViewer().getTable().getShell(), SWT.SAVE);
151 // String filename = d.getFileName();
152 // if (!filename.equals(""))
154 // save(d.getFilterPath() + File.separator + filename);
160 public void init(IEditorSite site, IEditorInput input) throws PartInitException
164 if (input instanceof IFileEditorInput)
166 IFileEditorInput fileInput = (IFileEditorInput) input;
167 setPartName(fileInput.getName());
168 open(fileInput.getFile());
174 public boolean isDirty()
180 public boolean isSaveAsAllowed()
186 public void update(long address)
192 private void setDirty(boolean value)
195 firePropertyChange(PROP_DIRTY);
199 public void activeMicroInstructionChanged(long address)
201 highlight((int) (address - memory.getDefinition().getMinimalAddress()));