1 package net.mograsim.plugin.tables.memory;
3 import org.eclipse.jface.viewers.ILazyContentProvider;
4 import org.eclipse.jface.viewers.TableViewer;
5 import org.eclipse.jface.viewers.Viewer;
6 import org.eclipse.swt.widgets.Display;
8 import net.mograsim.machine.MainMemory;
9 import net.mograsim.machine.MainMemoryDefinition;
10 import net.mograsim.machine.Memory.MemoryCellModifiedListener;
12 public class MemoryTableContentProvider implements ILazyContentProvider, MemoryCellModifiedListener
14 private long lower, upper;
15 private TableViewer viewer;
16 public final static int MAX_VISIBLE_ROWS = 1_000;
17 private MainMemory memory;
19 public void setBounds(long lower, long upper)
23 MainMemoryDefinition definition = memory.getDefinition();
24 this.lower = Long.min(definition.getMaximalAddress(), Long.max(definition.getMinimalAddress(), lower));
25 this.upper = Long.max(this.lower, Long.min(definition.getMaximalAddress(), upper));
29 this.upper = Long.max(this.lower, upper);
34 public void updateItemCount()
37 viewer.setItemCount(getAmount());
40 public long getLowerBound()
45 public long getUpperBound()
50 public int getAmount()
52 return (int) (upper - lower);
56 public void updateElement(int index)
58 if (index < getAmount())
60 long address = lower + index;
61 if (address <= memory.getDefinition().getMaximalAddress())
62 viewer.replace(new MemoryTableRow(address, memory), index);
67 public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
69 this.viewer = (TableViewer) viewer;
70 this.memory = (MainMemory) newInput;
72 ((MainMemory) oldInput).deregisterCellModifiedListener(this);
74 memory.registerCellModifiedListener(this);
75 setBounds(0, MAX_VISIBLE_ROWS);
79 public void update(long address)
81 // TODO check if viewer.refresh() does what we expect
82 Display.getDefault().asyncExec(address == -1 ? viewer::refresh : () -> updateElement((int) (address - lower)));