aaecc754fcdbd6f08ee510be6b5f568f3ba66f88
[Mograsim.git] /
1 package net.mograsim.plugin.tables.memory;
2
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;
7
8 import net.mograsim.machine.MainMemory;
9 import net.mograsim.machine.MemoryObserver;
10
11 public class MemoryTableContentProvider implements ILazyContentProvider, MemoryObserver
12 {
13         private long lower;
14         private TableViewer viewer;
15         private final static int limit = 10_000;
16         private int amount = 0;
17         private MainMemory memory;
18
19         public void setLowerBound(long lower)
20         {
21                 if (memory != null)
22                         this.lower = Long.min(memory.getDefinition().getMaximalAddress(), Long.max(memory.getDefinition().getMinimalAddress(), lower));
23                 else
24                         this.lower = lower;
25                 updateItemCount();
26         }
27
28         public void updateItemCount()
29         {
30                 if (memory != null)
31                 {
32                         long size = memory.getDefinition().getMaximalAddress() - lower;
33                         viewer.setItemCount(size > amount ? amount : (int) size);
34                 } else
35                         viewer.setItemCount(0);
36         }
37
38         public long getLowerBound()
39         {
40                 return lower;
41         }
42
43         public void setAmount(int amount)
44         {
45                 this.amount = Integer.min(amount, limit);
46                 updateItemCount();
47         }
48
49         public int getAmount()
50         {
51                 return amount;
52         }
53
54         @Override
55         public void updateElement(int index)
56         {
57                 long address = lower + index;
58                 if (address <= memory.getDefinition().getMaximalAddress())
59                         viewer.replace(new MemoryTableRow(address, memory), index);
60         }
61
62         @Override
63         public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
64         {
65                 this.viewer = (TableViewer) viewer;
66                 this.memory = (MainMemory) newInput;
67                 if (oldInput != null)
68                         ((MainMemory) oldInput).deregisterObserver(this);
69                 if (memory != null)
70                         memory.registerObserver(this);
71                 setLowerBound(0L);
72         }
73
74         @Override
75         public void update(long address)
76         {
77                 Display.getDefault().asyncExec(() -> updateElement((int) (address - lower)));
78         }
79 }