Skip to content

Commit 84bda53

Browse files
committed
Make TreeItem.getExpanded consistent across platforms #2834
When the children of a previously expanded TreeItem are removed, the call to getExpanded() should continue to return "true". On both Linux and MacOS, this property is not persisted and therefore stored in a local variable. But on Linux, a call to getExpanded() still returns the result from a call to the GTK API. To harmonize the behavior between the different operating systems, following changes are done: 1) The call to getExpanded() now always returns the local variable, similar to how it's done for MacOS. 2) The call to setExpanded() doesn't modify the tree item if it is already expanded or a leaf node. Closes #2834
1 parent cd186a7 commit 84bda53

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TreeItem.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,7 @@ public boolean getChecked () {
535535
*/
536536
public boolean getExpanded () {
537537
checkWidget();
538-
long path = GTK.gtk_tree_model_get_path (parent.modelHandle, handle);
539-
boolean answer = GTK.gtk_tree_view_row_expanded (parent.handle, path);
540-
GTK.gtk_tree_path_free (path);
541-
return answer;
538+
return isExpanded;
542539
}
543540

544541
/**
@@ -1226,7 +1223,8 @@ public void setChecked (boolean checked) {
12261223
public void setExpanded (boolean expanded) {
12271224
checkWidget();
12281225
long path = GTK.gtk_tree_model_get_path (parent.modelHandle, handle);
1229-
if (expanded != GTK.gtk_tree_view_row_expanded (parent.handle, path)) {
1226+
// Do nothing when the item is a leaf or already expanded
1227+
if (expanded != GTK.gtk_tree_view_row_expanded (parent.handle, path) && GTK.gtk_tree_model_iter_n_children (parent.modelHandle, handle) != 0) {
12301228
if (expanded) {
12311229
OS.g_signal_handlers_block_matched (parent.handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, TEST_EXPAND_ROW);
12321230
GTK.gtk_tree_view_expand_row (parent.handle, path, false);
@@ -1237,9 +1235,9 @@ public void setExpanded (boolean expanded) {
12371235
GTK.gtk_tree_view_collapse_row (parent.handle, path);
12381236
OS.g_signal_handlers_unblock_matched (parent.handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, TEST_COLLAPSE_ROW);
12391237
}
1238+
isExpanded = expanded;
12401239
}
12411240
GTK.gtk_tree_path_free (path);
1242-
isExpanded = expanded;
12431241
}
12441242

12451243

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_TreeItem.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,22 @@ public void test_getBoundsI() {
535535

536536
@Test
537537
public void test_getExpanded() {
538+
assertFalse(treeItem.getExpanded());
539+
// do nothing when the item is a leaf
540+
treeItem.setExpanded(true);
538541
assertFalse(treeItem.getExpanded());
539542
// there must be at least one subitem before you can set the treeitem expanded
540543
new TreeItem(treeItem, 0);
541544
treeItem.setExpanded(true);
542545
assertTrue(treeItem.getExpanded());
543546
treeItem.setExpanded(false);
544547
assertFalse(treeItem.getExpanded());
548+
treeItem.setExpanded(true);
549+
treeItem.removeAll();
550+
assertTrue(treeItem.getExpanded());
551+
// do nothing when the item is a leaf
552+
treeItem.setExpanded(false);
553+
assertTrue(treeItem.getExpanded());
545554
}
546555

547556
@Test

0 commit comments

Comments
 (0)