tmf: Fix IllegalArgumentException in TimeGraphCombo
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / widgets / timegraph / TimeGraphCombo.java
index 71d42fa368020492b61f1e0f8906c5e49d05a615..42d45a01a110fe36828a6ee73dfc784431725ec7 100644 (file)
@@ -169,27 +169,13 @@ public class TimeGraphCombo extends Composite {
         protected TimeGraphControl createTimeGraphControl(Composite composite, TimeGraphColorScheme colors) {
             return new TimeGraphControl(composite, colors) {
                 @Override
-                public void verticalZoom(boolean zoomIn, boolean adjustItems) {
-                    boolean changed = TimeGraphCombo.this.verticalZoom(zoomIn);
-                    if (changed) {
-                        /*
-                         * The time graph combo takes care of adjusting item
-                         * heights, only adjust the font in the time graph
-                         * control. Only do it if the time graph combo's tree
-                         * font has actually changed.
-                         */
-                        super.verticalZoom(zoomIn, false);
-                    }
+                public void verticalZoom(boolean zoomIn) {
+                    TimeGraphCombo.this.verticalZoom(zoomIn);
                 }
 
                 @Override
-                public void resetVerticalZoom(boolean adjustItems) {
+                public void resetVerticalZoom() {
                     TimeGraphCombo.this.resetVerticalZoom();
-                    /*
-                     * The time graph combo takes care of resetting item
-                     * heights, only reset the font in the time graph control.
-                     */
-                    super.resetVerticalZoom(false);
                 }
             };
         }
@@ -714,12 +700,12 @@ public class TimeGraphCombo extends Composite {
         });
     }
 
-    private boolean verticalZoom(boolean zoomIn) {
+    private void verticalZoom(boolean zoomIn) {
         Tree tree = fTreeViewer.getTree();
         FontData fontData = tree.getFont().getFontData()[0];
         int height = fontData.getHeight() + (zoomIn ? 1 : -1);
         if (height <= 0) {
-            return false;
+            return;
         }
         fontData.setHeight(height);
         if (fTreeFont != null) {
@@ -732,8 +718,6 @@ public class TimeGraphCombo extends Composite {
         fTimeGraphViewer.setHeaderHeight(tree.getHeaderHeight());
         fTimeGraphViewer.setItemHeight(getItemHeight(tree, true));
         alignTreeItems(false);
-        redraw();
-        return true;
     }
 
     private void resetVerticalZoom() {
@@ -748,7 +732,6 @@ public class TimeGraphCombo extends Composite {
         fTimeGraphViewer.setHeaderHeight(tree.getHeaderHeight());
         fTimeGraphViewer.setItemHeight(getItemHeight(tree, true));
         alignTreeItems(false);
-        redraw();
     }
 
     private void sendTimeViewAlignmentChanged() {
@@ -1240,6 +1223,43 @@ public class TimeGraphCombo extends Composite {
         TreeItem item = treeItems.get(topIndex);
         tree.setTopItem(item);
 
+        /*
+         * In GTK3, the bounds of the tree items are only sure to be correct
+         * after the tree has been painted.
+         */
+        tree.addPaintListener(new PaintListener() {
+            @Override
+            public void paintControl(PaintEvent e) {
+                tree.removePaintListener(this);
+                doAlignTreeItems();
+                redraw();
+                /*
+                 * Bug in GTK. Calling setTopItem() can scroll to the wrong item
+                 * when the 'tree view' is dirty. Set it again once it is clean.
+                 */
+                if (SWT.getPlatform().equals("gtk")) { //$NON-NLS-1$
+                    TreeItem topItem = tree.getTopItem();
+                    tree.getDisplay().asyncExec(() -> {
+                        if (!tree.isDisposed() && !topItem.isDisposed()) {
+                            tree.setTopItem(topItem);
+                        }
+                    });
+                }
+            }
+        });
+        /* Make sure the paint event is triggered. */
+        tree.redraw();
+    }
+
+    private void doAlignTreeItems() {
+        Tree tree = fTreeViewer.getTree();
+        List<TreeItem> treeItems = getVisibleExpandedItems(tree, false);
+        int topIndex = fTimeGraphViewer.getTopIndex();
+        if (topIndex >= treeItems.size()) {
+            return;
+        }
+        TreeItem item = treeItems.get(topIndex);
+
         // get the first filler item so we can calculate the last item's height
         TreeItem fillerItem = null;
         for (TreeItem treeItem : fTreeViewer.getTree().getItems()) {
@@ -1272,16 +1292,8 @@ public class TimeGraphCombo extends Composite {
          * newly visible items at the top of the viewer are also aligned.
          */
         fTimeGraphViewer.setTopIndex(topIndex);
-        item = treeItems.get(topIndex);
-        tree.setTopItem(item);
-        while (fTimeGraphViewer.getTopIndex() < topIndex) {
-            TreeItem nextItem = item;
-            topIndex--;
-            item = treeItems.get(topIndex);
-            tree.setTopItem(item);
-            bounds = item.getBounds();
-            alignTreeItem(item, bounds, nextItem);
-            fTimeGraphViewer.setTopIndex(topIndex);
+        if (fTimeGraphViewer.getTopIndex() != topIndex) {
+            alignTreeItems(false);
         }
     }
 
This page took 0.026871 seconds and 5 git commands to generate.