From 8af421cb4aa60fd68dcecac9a430ea91f4ddfb13 Mon Sep 17 00:00:00 2001 From: Maximilian Wittmer Date: Mon, 2 Oct 2023 13:34:06 +0200 Subject: [PATCH] Added Transaction for executeWithRedrawDisabled After Review by @HeikoKlare in #1072, this method allows for other methods to perform costly operations on an undrawn tree without manually dis- and re-enabling the tree. --- .../jface/viewers/AbstractTreeViewer.java | 105 +++++++++++------- 1 file changed, 67 insertions(+), 38 deletions(-) diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTreeViewer.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTreeViewer.java index bc29402b192..26c370edf2b 100644 --- a/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTreeViewer.java +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTreeViewer.java @@ -800,16 +800,15 @@ public void collapseAll() { */ public void collapseToLevel(Object elementOrTreePath, int level) { Assert.isNotNull(elementOrTreePath); - Control control = getControl(); - try { - control.setRedraw(false); - Widget w = internalGetWidgetToSelect(elementOrTreePath); - if (w != null) { - internalCollapseToLevel(w, level); + executeWithRedrawDisabled(new Runnable() { + @Override + public void run() { + Widget w = internalGetWidgetToSelect(elementOrTreePath); + if (w != null) { + internalCollapseToLevel(w, level); + } } - } finally { - control.setRedraw(true); - } + }); } /** @@ -1169,20 +1168,15 @@ public void expandToLevel(Object elementOrTreePath, int level) { public void expandToLevel(Object elementOrTreePath, int level, boolean disableRedraw) { if (checkBusy()) return; - Control control = getControl(); - try { - if (disableRedraw) { - control.setRedraw(false); - } - Widget w = internalExpand(elementOrTreePath, true); - if (w != null) { - internalExpandToLevel(w, level); - } - } finally { - if (disableRedraw) { - control.setRedraw(true); + executeWithRedrawDisabled(new Runnable() { + @Override + public void run() { + Widget w = internalExpand(elementOrTreePath, true); + if (w != null) { + internalExpandToLevel(w, level); + } } - } + }, disableRedraw); } /** @@ -1643,15 +1637,15 @@ public void treeCollapsed(TreeEvent event) { @Override protected void inputChanged(Object input, Object oldInput) { preservingSelection(() -> { - Control tree = getControl(); - tree.setRedraw(false); - try { - removeAll(tree); - tree.setData(getRoot()); - internalInitializeTree(tree); - } finally { - tree.setRedraw(true); - } + executeWithRedrawDisabled(new Runnable() { + @Override + public void run() { + Control tree = getControl(); + removeAll(tree); + tree.setData(getRoot()); + internalInitializeTree(tree); + } + }); }); } @@ -2336,12 +2330,13 @@ private boolean isExpandable(Item item, TreePath parentPath, Object element) { @Override protected void labelProviderChanged() { - // we have to walk the (visible) tree and update every item - Control tree = getControl(); - tree.setRedraw(false); - // don't pick up structure changes, but do force label updates - internalRefresh(tree, getRoot(), false, true); - tree.setRedraw(true); + executeWithRedrawDisabled(new Runnable() { + @Override + public void run() { + Control tree = getControl(); + internalRefresh(tree, getRoot(), false, true); + } + }); } /** @@ -3520,7 +3515,7 @@ public boolean contains(Object parent, Object element) { } /** - * Returns wether the given Widget is a single child. Used as functional + * Returns whether the given Widget is a single child. Used as functional * Parameter to {@link #internalConditionalExpandToLevel(Widget, int, Function)} * while expanding paths of single children * {@link #setAutoExpandOnSingleChildLevel(int)} @@ -3533,4 +3528,38 @@ private boolean doesWidgetHaveExactlyOneChild(Widget w) { return getChildren(w).length == 1; } + /** + * Transactionally perform an action on the TreeViewer without redrawing + * regularly. Disables redraw, executes toExecute and enables redraw again. + * + * @param toExecute the transaction to execute on the TreeViewer while it is not + * reDrawing + * + */ + private void executeWithRedrawDisabled(Runnable toExecute) { + executeWithRedrawDisabled(toExecute, true); + } + + /** + * Transactionally perform an action on the TreeViewer without redrawing + * regularly. Disables redraw, executes toExecute and enables redraw again. + * + * @param toExecute the transaction to execute on the TreeViewer while it is not + * reDrawing + * + */ + private void executeWithRedrawDisabled(Runnable toExecute, boolean shouldDisableRedraw) { + Control control = getControl(); + try { + if (shouldDisableRedraw) { + control.setRedraw(false); + } + toExecute.run(); + } finally { + if (shouldDisableRedraw) { + control.setRedraw(true); + } + } + } + }