1 package net.mograsim.plugin.editors;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.math.BigInteger;
6 import java.util.Collection;
7 import java.util.HashSet;
9 import org.eclipse.core.resources.IFile;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IProgressMonitor;
12 import org.eclipse.jface.util.IPropertyChangeListener;
13 import org.eclipse.jface.util.SafeRunnable;
14 import org.eclipse.jface.viewers.TableViewerColumn;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Font;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.ScrollBar;
24 import org.eclipse.swt.widgets.Table;
25 import org.eclipse.swt.widgets.TableColumn;
26 import org.eclipse.swt.widgets.Text;
27 import org.eclipse.ui.IEditorInput;
28 import org.eclipse.ui.IEditorSite;
29 import org.eclipse.ui.IFileEditorInput;
30 import org.eclipse.ui.PartInitException;
31 import org.eclipse.ui.part.EditorPart;
32 import org.eclipse.ui.themes.ITheme;
33 import org.eclipse.ui.themes.IThemeManager;
35 import net.mograsim.machine.BitVectorMemory;
36 import net.mograsim.machine.MachineDefinition;
37 import net.mograsim.machine.Memory.MemoryCellModifiedListener;
38 import net.mograsim.machine.mi.MicroInstructionMemoryParseException;
39 import net.mograsim.machine.standard.memory.BitVectorBasedMemoryParser;
40 import net.mograsim.plugin.asm.AsmNumberUtil;
41 import net.mograsim.plugin.nature.MachineContext;
42 import net.mograsim.plugin.nature.ProjectMachineContext;
43 import net.mograsim.plugin.tables.AddressLabelProvider;
44 import net.mograsim.plugin.tables.DisplaySettings;
45 import net.mograsim.plugin.tables.LazyTableViewer;
46 import net.mograsim.plugin.tables.NumberColumnLabelProvider;
47 import net.mograsim.plugin.tables.RadixSelector;
48 import net.mograsim.plugin.tables.memory.MemoryCellEditingSupport;
49 import net.mograsim.plugin.tables.memory.MemoryTableContentProvider;
50 import net.mograsim.plugin.tables.memory.MemoryTableRow;
51 import net.mograsim.plugin.tables.memory.NumberVerifyListener;
53 public abstract class AbstractMemoryEditor extends EditorPart
55 private MachineContext context;
57 private BitVectorMemory memory;
59 private LazyTableViewer viewer;
60 private MemoryTableContentProvider provider;
61 private DisplaySettings displaySettings;
63 private Collection<Control> fontDependent = new HashSet<>();
65 private boolean dirty;
67 private final MemoryCellModifiedListener memListener;
69 private final static String font = "net.mograsim.plugin.memory.table_font";
70 private IPropertyChangeListener fontChangeListener;
72 public AbstractMemoryEditor()
74 memListener = this::cellModified;
78 public void createPartControl(Composite parent)
80 provider = new MemoryTableContentProvider();
81 displaySettings = new DisplaySettings();
83 parent.setLayout(new GridLayout(8, false));
87 displaySettings.addObserver(() -> viewer.refresh());
90 @SuppressWarnings("unused") // RadixSelector and exceptions
91 private void createHeader(Composite parent)
93 Text gotoText = new Text(parent, SWT.BORDER);
94 gotoText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
95 NumberVerifyListener vl = new NumberVerifyListener();
96 gotoText.addVerifyListener(vl);
97 gotoText.setText("0");
99 Runnable gotoAction = () ->
103 loadAround((int) (AsmNumberUtil.valueOf(gotoText.getText()).longValue() - provider.getLowerBound()));
104 viewer.getTable().deselectAll();
106 catch (NumberFormatException x)
108 // Nothing to do here
112 gotoText.addTraverseListener((e) ->
114 if (e.detail == SWT.TRAVERSE_RETURN)
118 Button gotoButton = new Button(parent, SWT.PUSH);
119 gotoButton.setText("Go to");
120 gotoButton.addListener(SWT.Selection, e ->
125 new Label(parent, SWT.NONE).setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
127 new RadixSelector(parent, displaySettings);
130 private void createViewer(Composite parent)
132 viewer = new LazyTableViewer(parent, SWT.FULL_SELECTION | SWT.BORDER | SWT.VIRTUAL);
133 fontDependent.add(viewer.getTable());
135 Table table = viewer.getTable();
136 table.setHeaderVisible(true);
137 table.setLinesVisible(true);
138 viewer.setUseHashlookup(true);
139 viewer.setContentProvider(provider);
140 getSite().setSelectionProvider(viewer);
141 viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 8, 1));
143 // TODO: Also support keyboard inputs for flexible scrolling
144 ScrollBar vBar = table.getVerticalBar();
145 vBar.addListener(SWT.Selection, (e) ->
148 if ((sel = vBar.getSelection()) < vBar.getMinimum() + vBar.getThumb() * 2 || sel > vBar.getMaximum() - vBar.getThumb() * 2)
150 loadAround(table.getTopIndex());
155 IThemeManager themeManager = getSite().getWorkbenchWindow().getWorkbench().getThemeManager();
156 themeManager.addPropertyChangeListener(fontChangeListener = (e) ->
158 if (IThemeManager.CHANGE_CURRENT_THEME.equals(e.getProperty()) || font.equals(e.getProperty()))
160 updateFont(themeManager.getCurrentTheme());
164 updateFont(themeManager.getCurrentTheme());
167 viewer.setInput(memory);
170 private void updateFont(ITheme theme)
172 Font newFont = theme.getFontRegistry().get(font);
173 // TODO: This is a quick fix! Still have to figure out why the CellEditors do not get the appropriate Font on their own
174 fontDependent.forEach(c -> c.setFont(newFont));
177 private void loadAround(int row)
179 long prevLb = provider.getLowerBound();
180 long address = prevLb + row;
181 long actualLb = Long.max(address - MemoryTableContentProvider.MAX_VISIBLE_ROWS / 2, memory.getDefinition().getMinimalAddress());
183 long prevHb = provider.getUpperBound();
184 // +- 1 row is not really important
185 long actualHb = Long.min(address + MemoryTableContentProvider.MAX_VISIBLE_ROWS / 2, memory.getDefinition().getMaximalAddress());
187 if (actualLb != prevLb || actualHb != prevHb)
189 Table table = viewer.getTable();
190 provider.setBounds(actualLb, actualHb);
191 int rowIndex = (int) (address - actualLb);
192 if (rowIndex >= 0 && rowIndex < table.getItemCount())
193 table.showItem(table.getItem(rowIndex));
198 private void createColumns()
200 TableViewerColumn addrCol = createTableViewerColumn("Address", 100);
201 addrCol.setLabelProvider(new AddressLabelProvider());
203 TableViewerColumn dataCol = createTableViewerColumn("Data", 100);
204 dataCol.setLabelProvider(new NumberColumnLabelProvider(displaySettings)
207 public BigInteger getAsBigInteger(Object element)
209 MemoryTableRow row = (MemoryTableRow) element;
210 return row.getMemory().getCellAsBigInteger(row.address);
214 public int getBitLength(Object element)
216 return ((MemoryTableRow) element).getMemory().getDefinition().getCellWidth();
219 MemoryCellEditingSupport eSup;
220 dataCol.setEditingSupport(eSup = new MemoryCellEditingSupport(viewer, displaySettings));
221 fontDependent.add(eSup.getCellEditorControl());
224 private TableViewerColumn createTableViewerColumn(String title, int width)
226 TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
227 TableColumn column = viewerColumn.getColumn();
228 column.setText(title);
229 column.setWidth(width);
230 column.setResizable(true);
231 column.setMoveable(false);
236 public void init(IEditorSite site, IEditorInput input) throws PartInitException
238 if (input instanceof IFileEditorInput)
240 IFileEditorInput fileInput = (IFileEditorInput) input;
241 context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject());
243 setPartName(fileInput.getName());
246 open(fileInput.getFile());
250 throw new PartInitException("Failed to read input!", e);
253 throw new IllegalArgumentException("MemoryEditor can only be used with Files");
260 public void doSave(IProgressMonitor monitor)
262 IEditorInput input = getEditorInput();
263 if (input instanceof IFileEditorInput)
264 SafeRunnable.getRunner().run(() -> save(((IFileEditorInput) input).getFile(), monitor));
267 private void save(IFile file, IProgressMonitor monitor) throws CoreException, IOException
271 throw new MicroInstructionMemoryParseException("Failed to write the memory to File. No memory assigned.");
273 try (InputStream toWrite = BitVectorBasedMemoryParser.write(memory))
275 file.setContents(toWrite, 0, monitor);
280 private void open(IFile file) throws IOException, CoreException
282 MachineDefinition machDef = context.getMachineDefinition()
283 .orElseThrow(() -> new MicroInstructionMemoryParseException("No MachineDefinition assigned!"));
284 memory = createEmptyMemory(machDef);
285 BitVectorBasedMemoryParser.parseMemory(memory, file.getContents());
286 memory.registerCellModifiedListener(memListener);
288 viewer.setInput(memory);
291 protected abstract BitVectorMemory createEmptyMemory(MachineDefinition activeMachineDefinition);
293 private void cellModified(@SuppressWarnings("unused") long address)
298 private void setDirty(boolean newDirty)
301 firePropertyChange(PROP_DIRTY);
305 public void doSaveAs()
307 throw new UnsupportedOperationException();
311 public boolean isDirty()
317 public boolean isSaveAsAllowed()
323 public void setFocus()
325 viewer.getTable().setFocus();
329 public void dispose()
331 getSite().getWorkbenchWindow().getWorkbench().getThemeManager().removePropertyChangeListener(fontChangeListener);
333 memory.deregisterCellModifiedListener(memListener);