ActiveInstructionChangedListener moved to Machine and updated
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / tables / mi / RowHighlighter.java
1 package net.mograsim.plugin.tables.mi;
2
3 import java.util.Optional;
4
5 import org.eclipse.swt.widgets.Table;
6
7 import net.mograsim.plugin.tables.LazyTableViewer;
8 import net.mograsim.plugin.util.SingleSWTRequest;
9
10 public class RowHighlighter
11 {
12         private int highlighted = -1;
13         private LazyTableViewer viewer;
14         private ColorProvider cProv;
15         private SingleSWTRequest requester = new SingleSWTRequest();
16
17         public RowHighlighter(LazyTableViewer viewer, ColorProvider cProv)
18         {
19                 this.viewer = viewer;
20                 this.cProv = cProv;
21         }
22
23         public void highlight(int row)
24         {
25                 requester.request(() ->
26                 {
27                         if (!viewer.getTable().isDisposed())
28                                 innerHighlight(row);
29                 });
30         }
31
32         private void innerHighlight(int row)
33         {
34                 Table table = viewer.getTable();
35                 cProv.highlight(row);
36                 if (row != -1)
37                 {
38                         table.showItem(table.getItem(Math.min(table.getItemCount(), row + 2)));
39                         table.showItem(table.getItem(row));
40                         Optional.ofNullable(table.getItem(row).getData()).ifPresent(d -> viewer.update(d, null));
41                 }
42                 if (highlighted != -1)
43                         Optional.ofNullable(table.getItem(highlighted).getData()).ifPresent(d -> viewer.update(d, null));
44                 highlighted = row;
45         }
46 }