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.ui.IEditorInput;
15 import org.eclipse.ui.IEditorSite;
16 import org.eclipse.ui.IFileEditorInput;
17 import org.eclipse.ui.PartInitException;
18 import org.eclipse.ui.part.EditorPart;
20 import net.mograsim.machine.Memory.MemoryCellModifiedListener;
21 import net.mograsim.machine.mi.AssignableMicroInstructionMemory.MIMemoryReassignedListener;
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.nature.MachineContext;
27 import net.mograsim.plugin.nature.ProjectMachineContext;
28 import net.mograsim.plugin.tables.DisplaySettings;
29 import net.mograsim.plugin.tables.RadixSelector;
31 public class InstructionView extends EditorPart implements MemoryCellModifiedListener, ActiveMicroInstructionChangedListener
33 private InstructionTableContentProvider provider;
34 private boolean dirty = false;
35 private MicroInstructionMemory memory;
36 private InstructionTable table;
37 private MachineContext context;
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);
51 table = new InstructionTable(parent, displaySettings, getSite().getWorkbenchWindow().getWorkbench().getThemeManager());
52 table.setContentProvider(provider);
53 table.bindMicroInstructionMemory(memory);
55 GridData viewerData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
56 viewerData.horizontalSpan = 3;
57 table.getTableViewer().getTable().setLayoutData(viewerData);
60 public void highlight(int row)
65 private void addActivationButton(Composite parent)
67 Button activationButton = new Button(parent, SWT.PUSH);
68 activationButton.setText("Set Active");
69 activationButton.addListener(SWT.Selection, e -> context.getActiveMachine().ifPresent(m ->
71 // clear highlighting if the memory is reassigned
72 MIMemoryReassignedListener memReassignedListener = n ->
77 m.getMicroInstructionMemory().registerMemoryReassignedListener(memReassignedListener);
78 // clear highlighting if the active machine changes
79 context.addActiveMachineListener(n ->
81 if (n.isEmpty() || n.get() != m)
84 m.getMicroInstructionMemory().deregisterMemoryReassignedListener(memReassignedListener);
87 m.getMicroInstructionMemory().bind(memory);
91 public void bindMicroInstructionMemory(MicroInstructionMemory memory)
93 if (this.memory != null)
95 this.memory.deregisterCellModifiedListener(this);
96 this.memory.deregisterActiveMicroInstructionChangedListener(this);
101 this.memory.registerCellModifiedListener(this);
102 this.memory.registerActiveMicroInstructionChangedListener(this);
105 table.bindMicroInstructionMemory(memory);
108 private void open(IFile file) throws IOException, MicroInstructionMemoryParseException, CoreException
110 bindMicroInstructionMemory(MicroInstructionMemoryParser.parseMemory(
111 context.getMachineDefinition().orElseThrow(() -> new MicroInstructionMemoryParseException("No MachineDefinition assigned!"))
112 .getMicroInstructionMemoryDefinition(),
113 file.getContents()));
116 private void save(IFile file, IProgressMonitor progressMonitor) throws IOException, CoreException, MicroInstructionMemoryParseException
120 throw new MicroInstructionMemoryParseException(
121 "Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned.");
123 try (InputStream toWrite = MicroInstructionMemoryParser.write(memory))
125 file.setContents(toWrite, 0, progressMonitor);
130 public void setFocus()
132 table.getTableViewer().getControl().setFocus();
136 public void doSave(IProgressMonitor progressMonitor)
138 IEditorInput input = getEditorInput();
139 if (input instanceof IFileEditorInput)
141 IFileEditorInput pathInput = (IFileEditorInput) input;
144 save(pathInput.getFile(), progressMonitor);
150 progressMonitor.setCanceled(true);
153 progressMonitor.setCanceled(true);
157 public void doSaveAs()
159 // openSaveAsDialog();
162 // private void openSaveAsDialog()
164 // FileDialog d = new FileDialog(table.getTableViewer().getTable().getShell(), SWT.SAVE);
166 // String filename = d.getFileName();
167 // if (!filename.equals(""))
169 // save(d.getFilterPath() + File.separator + filename);
175 public void init(IEditorSite site, IEditorInput input) throws PartInitException
181 if (input instanceof IFileEditorInput)
183 IFileEditorInput fileInput = (IFileEditorInput) input;
184 context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject());
185 context.activateMachine();
186 setPartName(fileInput.getName());
187 open(fileInput.getFile());
192 throw new PartInitException("Failed to read input!", e);
198 public boolean isDirty()
204 public boolean isSaveAsAllowed()
210 public void update(long address)
216 private void setDirty(boolean value)
219 firePropertyChange(PROP_DIRTY);
223 public void activeMicroInstructionChanged(long address)
225 highlight((int) (address - memory.getDefinition().getMinimalAddress()));
229 public void dispose()