Fixed issue with row highlighting
[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.concurrent.atomic.AtomicBoolean;
4
5 import org.eclipse.swt.widgets.Display;
6 import org.eclipse.swt.widgets.Table;
7
8 import net.mograsim.plugin.tables.LazyTableViewer;
9
10 public class RowHighlighter
11 {
12         private int highlighted, toHighlight;
13         private LazyTableViewer viewer;
14         private AtomicBoolean waiting = new AtomicBoolean();
15
16         public RowHighlighter(LazyTableViewer viewer)
17         {
18                 this.viewer = viewer;
19         }
20
21         public void highlight(int row)
22         {
23                 synchronized (waiting)
24                 {
25                         toHighlight = row;
26                         if (!waiting.get())
27                         {
28                                 waiting.set(true);
29                                 Display.getDefault().asyncExec(() ->
30                                 {
31                                         synchronized (waiting)
32                                         {
33                                                 waiting.set(false);
34                                                 if (!viewer.getTable().isDisposed())
35                                                         innerHighlight(toHighlight);
36                                         }
37                                 });
38                         }
39                 }
40         }
41
42         private void innerHighlight(int row)
43         {
44                 viewer.highlightRow(highlighted, false);
45                 highlighted = row;
46                 if (row != -1)
47                 {
48                         viewer.highlightRow(row, true);
49                         Table table = viewer.getTable();
50                         table.showItem(table.getItem(Math.min(table.getItemCount(), row + 2)));
51                         table.showItem(table.getItem(row));
52                 }
53         }
54 }