Made highlighted/modified cells bold; New default modified cell color
[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 import java.util.concurrent.atomic.AtomicBoolean;
5
6 import org.eclipse.swt.widgets.Display;
7 import org.eclipse.swt.widgets.Table;
8
9 import net.mograsim.plugin.tables.LazyTableViewer;
10
11 public class RowHighlighter
12 {
13         private int highlighted = -1, toHighlight;
14         private LazyTableViewer viewer;
15         private AtomicBoolean waiting = new AtomicBoolean();
16         private ColorProvider cProv;
17
18         public RowHighlighter(LazyTableViewer viewer, ColorProvider cProv)
19         {
20                 this.viewer = viewer;
21                 this.cProv = cProv;
22         }
23
24         public void highlight(int row)
25         {
26                 synchronized (waiting)
27                 {
28                         toHighlight = row;
29                         if (!waiting.get())
30                         {
31                                 waiting.set(true);
32                                 Display.getDefault().asyncExec(() ->
33                                 {
34                                         synchronized (waiting)
35                                         {
36                                                 waiting.set(false);
37                                                 if (!viewer.getTable().isDisposed())
38                                                         innerHighlight(toHighlight);
39                                         }
40                                 });
41                         }
42                 }
43         }
44
45         private void innerHighlight(int row)
46         {
47                 Table table = viewer.getTable();
48                 cProv.highlight(row);
49                 if (row != -1)
50                 {
51                         table.showItem(table.getItem(Math.min(table.getItemCount(), row + 2)));
52                         table.showItem(table.getItem(row));
53                         Optional.of(table.getItem(row).getData()).ifPresent(d -> viewer.update(d, null));
54                 }
55                 if (highlighted != -1)
56                         Optional.of(table.getItem(highlighted).getData()).ifPresent(d -> viewer.update(d, null));
57                 highlighted = row;
58         }
59 }