Skip to content

Commit

Permalink
Added Transaction for executeWithRedrawDisabled
Browse files Browse the repository at this point in the history
After Review by @HeikoKlare in eclipse-platform#1072, this method
allows for other methods to perform costly operations on
an undrawn tree without manually dis- and re-enabling the tree.
  • Loading branch information
Maximilian Wittmer authored and Maximilian Wittmer committed Oct 2, 2023
1 parent 3f3e945 commit 87a47e2
Showing 1 changed file with 67 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}
});
});
}

Expand Down Expand Up @@ -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);
}
});
}

/**
Expand Down Expand Up @@ -3517,7 +3512,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)}
Expand All @@ -3530,4 +3525,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);
}
}
}

}

0 comments on commit 87a47e2

Please sign in to comment.