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.AssignableMicroInstructionMemory.MIMemoryReassignedListener;
23 import net.mograsim.machine.mi.MicroInstructionMemory;
24 import net.mograsim.machine.mi.MicroInstructionMemory.ActiveMicroInstructionChangedListener;
25 import net.mograsim.machine.mi.MicroInstructionMemoryParseException;
26 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
27 import net.mograsim.plugin.nature.MachineContext;
28 import net.mograsim.plugin.nature.ProjectMachineContext;
29 import net.mograsim.plugin.tables.DisplaySettings;
30 import net.mograsim.plugin.tables.LazyTableViewer;
31 import net.mograsim.plugin.tables.RadixSelector;
33 public class InstructionView extends EditorPart implements MemoryCellModifiedListener, ActiveMicroInstructionChangedListener
35 private InstructionTableContentProvider provider;
36 private int highlighted = 0;
37 private boolean dirty = false;
38 private MicroInstructionMemory memory;
39 private InstructionTable table;
40 private MachineContext context;
42 @SuppressWarnings("unused")
44 public void createPartControl(Composite parent)
46 provider = new InstructionTableLazyContentProvider();
47 GridLayout layout = new GridLayout(3, false);
48 parent.setLayout(layout);
50 DisplaySettings displaySettings = new DisplaySettings();
51 new RadixSelector(parent, displaySettings);
53 addActivationButton(parent);
55 table = new InstructionTable(parent, displaySettings);
56 table.setContentProvider(provider);
57 table.bindMicroInstructionMemory(memory);
59 GridData viewerData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
60 viewerData.horizontalSpan = 3;
61 table.getTableViewer().getTable().setLayoutData(viewerData);
64 public void highlight(int index)
66 Display.getDefault().asyncExec(() ->
68 LazyTableViewer viewer = table.getTableViewer();
69 viewer.highlightRow(highlighted, false);
73 viewer.highlightRow(index, true);
75 .showItem(viewer.getTable().getItem(Math.min((int) memory.getDefinition().getMaximalAddress(), index + 2)));
76 viewer.getTable().showItem(viewer.getTable().getItem(index));
81 private void addActivationButton(Composite parent)
83 Button activationButton = new Button(parent, SWT.PUSH);
84 activationButton.setText("Set Active");
85 activationButton.addListener(SWT.Selection, e -> context.getActiveMachine().ifPresent(m ->
87 // clear highlighting if the memory is reassigned
88 MIMemoryReassignedListener memReassignedListener = n ->
93 m.getMicroInstructionMemory().registerMemoryReassignedListener(memReassignedListener);
94 // clear highlighting if the active machine changes
95 context.addActiveMachineListener(n ->
97 if (n.isEmpty() || n.get() != m)
100 m.getMicroInstructionMemory().deregisterMemoryReassignedListener(memReassignedListener);
103 m.getMicroInstructionMemory().bind(memory);
107 public void bindMicroInstructionMemory(MicroInstructionMemory memory)
109 if (this.memory != null)
111 this.memory.deregisterCellModifiedListener(this);
112 this.memory.deregisterActiveMicroInstructionChangedListener(this);
114 this.memory = memory;
117 this.memory.registerCellModifiedListener(this);
118 this.memory.registerActiveMicroInstructionChangedListener(this);
121 table.bindMicroInstructionMemory(memory);
124 private void open(IFile file) throws IOException, MicroInstructionMemoryParseException, CoreException
126 bindMicroInstructionMemory(MicroInstructionMemoryParser.parseMemory(
127 context.getMachineDefinition().orElseThrow(() -> new MicroInstructionMemoryParseException("No MachineDefinition assigned!"))
128 .getMicroInstructionMemoryDefinition(),
129 file.getContents()));
132 private void save(IFile file, IProgressMonitor progressMonitor) throws IOException, CoreException, MicroInstructionMemoryParseException
136 throw new MicroInstructionMemoryParseException(
137 "Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned.");
139 try (InputStream toWrite = MicroInstructionMemoryParser.write(memory))
141 file.setContents(toWrite, 0, progressMonitor);
146 public void setFocus()
148 table.getTableViewer().getControl().setFocus();
152 public void doSave(IProgressMonitor progressMonitor)
154 IEditorInput input = getEditorInput();
155 if (input instanceof IFileEditorInput)
157 IFileEditorInput pathInput = (IFileEditorInput) input;
160 save(pathInput.getFile(), progressMonitor);
166 progressMonitor.setCanceled(true);
169 progressMonitor.setCanceled(true);
173 public void doSaveAs()
175 // openSaveAsDialog();
178 // private void openSaveAsDialog()
180 // FileDialog d = new FileDialog(table.getTableViewer().getTable().getShell(), SWT.SAVE);
182 // String filename = d.getFileName();
183 // if (!filename.equals(""))
185 // save(d.getFilterPath() + File.separator + filename);
191 public void init(IEditorSite site, IEditorInput input) throws PartInitException
197 if (input instanceof IFileEditorInput)
199 IFileEditorInput fileInput = (IFileEditorInput) input;
200 context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject());
201 context.activateMachine();
202 setPartName(fileInput.getName());
203 open(fileInput.getFile());
208 throw new PartInitException("Failed to read input!", e);
214 public boolean isDirty()
220 public boolean isSaveAsAllowed()
226 public void update(long address)
232 private void setDirty(boolean value)
235 firePropertyChange(PROP_DIRTY);
239 public void activeMicroInstructionChanged(long address)
241 highlight((int) (address - memory.getDefinition().getMinimalAddress()));