Fixed issue with row highlighting
authorFabian Stemmler <stemmler@in.tum.de>
Sat, 28 Sep 2019 12:41:27 +0000 (14:41 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Sat, 28 Sep 2019 12:41:27 +0000 (14:41 +0200)
Too many highlight requests were being sent to the SWT Eventqueue when
running the simulation at a faster speed. Now there can be at most one
such request at the same time.

plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/InstructionView.java
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/RowHighlighter.java [new file with mode: 0644]

index 0158c7b..7a692c9 100644 (file)
@@ -11,7 +11,6 @@ import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.IFileEditorInput;
@@ -27,17 +26,16 @@ import net.mograsim.machine.mi.MicroInstructionMemoryParser;
 import net.mograsim.plugin.nature.MachineContext;
 import net.mograsim.plugin.nature.ProjectMachineContext;
 import net.mograsim.plugin.tables.DisplaySettings;
-import net.mograsim.plugin.tables.LazyTableViewer;
 import net.mograsim.plugin.tables.RadixSelector;
 
 public class InstructionView extends EditorPart implements MemoryCellModifiedListener, ActiveMicroInstructionChangedListener
 {
        private InstructionTableContentProvider provider;
-       private int highlighted = 0;
        private boolean dirty = false;
        private MicroInstructionMemory memory;
        private InstructionTable table;
        private MachineContext context;
+       private RowHighlighter highlighter;
 
        @SuppressWarnings("unused")
        @Override
@@ -58,23 +56,12 @@ public class InstructionView extends EditorPart implements MemoryCellModifiedLis
                GridData viewerData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
                viewerData.horizontalSpan = 3;
                table.getTableViewer().getTable().setLayoutData(viewerData);
+               highlighter = new RowHighlighter(table.getTableViewer());
        }
 
        public void highlight(int index)
        {
-               Display.getDefault().asyncExec(() ->
-               {
-                       LazyTableViewer viewer = table.getTableViewer();
-                       viewer.highlightRow(highlighted, false);
-                       highlighted = index;
-                       if (index != -1)
-                       {
-                               viewer.highlightRow(index, true);
-                               viewer.getTable()
-                                               .showItem(viewer.getTable().getItem(Math.min((int) memory.getDefinition().getMaximalAddress(), index + 2)));
-                               viewer.getTable().showItem(viewer.getTable().getItem(index));
-                       }
-               });
+               highlighter.highlight(index);
        }
 
        private void addActivationButton(Composite parent)
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/RowHighlighter.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/RowHighlighter.java
new file mode 100644 (file)
index 0000000..18e0108
--- /dev/null
@@ -0,0 +1,54 @@
+package net.mograsim.plugin.tables.mi;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Table;
+
+import net.mograsim.plugin.tables.LazyTableViewer;
+
+public class RowHighlighter
+{
+       private int highlighted, toHighlight;
+       private LazyTableViewer viewer;
+       private AtomicBoolean waiting = new AtomicBoolean();
+
+       public RowHighlighter(LazyTableViewer viewer)
+       {
+               this.viewer = viewer;
+       }
+
+       public void highlight(int row)
+       {
+               synchronized (waiting)
+               {
+                       toHighlight = row;
+                       if (!waiting.get())
+                       {
+                               waiting.set(true);
+                               Display.getDefault().asyncExec(() ->
+                               {
+                                       synchronized (waiting)
+                                       {
+                                               waiting.set(false);
+                                               if (!viewer.getTable().isDisposed())
+                                                       innerHighlight(toHighlight);
+                                       }
+                               });
+                       }
+               }
+       }
+
+       private void innerHighlight(int row)
+       {
+               viewer.highlightRow(highlighted, false);
+               highlighted = row;
+               if (row != -1)
+               {
+                       viewer.highlightRow(row, true);
+                       Table table = viewer.getTable();
+                       table.showItem(table.getItem(Math.min(table.getItemCount(), row + 2)));
+                       table.showItem(table.getItem(row));
+               }
+       }
+}