Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 356414 - TreeItem.setExpanded(false) can cause blank lines in a virtual tree.
Summary: TreeItem.setExpanded(false) can cause blank lines in a virtual tree.
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.7   Edit
Hardware: PC Linux-GTK
: P3 major (vote)
Target Milestone: 3.8 M5   Edit
Assignee: Silenio Quarti CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 356415 (view as bug list)
Depends on:
Blocks: 356415
  Show dependency tree
 
Reported: 2011-08-31 17:15 EDT by Pawel Piech CLA
Modified: 2012-01-26 10:01 EST (History)
7 users (show)

See Also:


Attachments
Screenshot of blank data in virtual tree produced by snippet. (7.62 KB, image/png)
2011-08-31 17:15 EDT, Pawel Piech CLA
no flags Details
Updated example with limited depth. (3.29 KB, text/x-java)
2012-01-25 00:00 EST, Pawel Piech CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pawel Piech CLA 2011-08-31 17:15:24 EDT
Created attachment 202568 [details]
Screenshot of blank data in virtual tree produced by snippet.

Build id: I20110613-1736 (Linux 32 bit, libgtk2.0-0: 2.20.1-0ubuntu2)

It seems that when tree elements are collapsed in a virtual tree and the scroll position is reset in the viewport, the items that became visible do not trigger an update, leaving them blank.  

To reproduce the problem:

1) Run the snippet below.
2) Scroll to bottom of tree.
3) Press refresh button 
4) Press the collapse button.

This snippet is a modified version of the virtual tree swt snippet.  The refresh and collapse buttons perform the equivalent operations as the jface TreeViewer ones.

public static void main(String[] args) {
	Display display = new Display();
	final Shell shell = new Shell(display);
	shell.setLayout (new GridLayout());
	
	final Tree tree = new Tree(shell, SWT.VIRTUAL | SWT.BORDER);
	tree.addListener(SWT.SetData, new Listener() {
		public void handleEvent(Event event) {
			TreeItem item = (TreeItem)event.item;
			TreeItem parentItem = item.getParentItem();
			String text = null;
			if (parentItem == null) {
				text = "node "+tree.indexOf(item);
			} else {
				text = parentItem.getText()+" - "+parentItem.indexOf(item);
			}
			item.setText(text);
			item.setItemCount(10);
		}
	});
	tree.setItemCount(20);
	tree.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
	
	Button refresh = new Button(shell, SWT.PUSH);
	refresh.setText("refresh");
	refresh.addSelectionListener(new SelectionListener() {
		public void widgetDefaultSelected(SelectionEvent e) {
			widgetSelected(e);
		}
		
		public void widgetSelected(SelectionEvent e) {
			tree.clearAll(true);
		}		
	});
	refresh.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	
	Button collapse = new Button(shell, SWT.PUSH);
	collapse.setText("collapse");
	collapse.addSelectionListener(new SelectionListener() {
		public void widgetDefaultSelected(SelectionEvent e) {
			widgetSelected(e);
		}
		
		public void widgetSelected(SelectionEvent e) {
			collapse(tree, tree);
		}		
	});
	collapse.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

	shell.setSize(400, 300);
	shell.open();
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}

private static void collapse(Tree tree, Widget widget) {
	if (widget instanceof Item) {
		Item item = (Item) widget;
		setExpanded(tree, item, false);
	}

	Item[] children = getChildren(widget);
	if (children != null) {
		for (int i = 0; i < children.length; i++) {
			collapse(tree, children[i]);
		}
	}
}

private static void setExpanded(Tree tree, Item node, boolean expand) {
	((TreeItem) node).setExpanded(expand);
	// force repaints to happen
	tree.update();
}

private static Item[] getChildren(Widget o) {
	if (o instanceof TreeItem) {
		return ((TreeItem) o).getItems();
	}
	if (o instanceof Tree) {
		return ((Tree) o).getItems();
	}
	return null;
}

}
Comment 1 Felipe Heidrich CLA 2011-09-02 10:32:28 EDT
Arun, can you please try that.
Comment 2 Pawel Piech CLA 2011-09-02 11:18:30 EDT
(In reply to comment #0)
> To reproduce the problem:
> 
> 1) Run the snippet below.
> 2) Scroll to bottom of tree.
> 3) Press refresh button 
> 4) Press the collapse button.
> 
Oops, I forgot a step: after scrolling to bottom of the tree (step 2) expand some elements 2 levels down and scroll down again.
Comment 3 Arun Thondapu CLA 2011-09-06 04:07:22 EDT
I have been able to reproduce the bug, will look into it.
Comment 4 Martin Oberhuber CLA 2012-01-24 04:35:18 EST
Arun, Felipe - any update on this ? - It looks like this problem creates a major issue with Platform/Debug in the Variables view.
Comment 5 Bogdan Gheorghe CLA 2012-01-24 15:21:59 EST
I think something is missing from the attached snippet as you were able to run it but when we tried it here on our GTK we got the trace below after following your instructions. (It also fails on Windows). The trace is reasonable as the tree has unlimited depth...


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOfRange(Arrays.java:3209)
	at java.lang.String.<init>(String.java:215)
	at java.lang.StringBuilder.toString(StringBuilder.java:430)
	at org.eclipse.swt.snippets.Snippet$1.handleEvent(Snippet.java:34)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4135)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1457)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1480)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1465)
	at org.eclipse.swt.widgets.Tree.checkData(Tree.java:319)
	at org.eclipse.swt.widgets.TreeItem.getItems(TreeItem.java:782)
	at org.eclipse.swt.snippets.Snippet.getChildren(Snippet.java:100)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:84)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
	at org.eclipse.swt.snippets.Snippet.collapse(Snippet.java:87)
Comment 6 Pawel Piech CLA 2012-01-25 00:00:09 EST
Created attachment 210026 [details]
Updated example with limited depth.
Comment 7 Pawel Piech CLA 2012-01-25 00:05:04 EST
(In reply to comment #5)
> I think something is missing from the attached snippet as you were able to run
> it but when we tried it here on our GTK we got the trace below after following
> your instructions. (It also fails on Windows). The trace is reasonable as the
> tree has unlimited depth...
In some ways the OutOfMemory error is even more disturbing because it would mean that when you run the example there are many levels of items being materialized instead of just the visible ones.  I was still able to run the example unmodified (with  I20120123-2200), but I did attach a modified version which limits the tree depth to 2.

I also get a bunch of the following exceptions in my console when I run the example: 
(SWT:4349): GLib-GObject-CRITICAL **: g_object_unref: assertion `G_IS_OBJECT (object)' failed
Comment 8 Pawel Piech CLA 2012-01-25 00:11:29 EST
(In reply to comment #7)
> (SWT:4349): GLib-GObject-CRITICAL **: g_object_unref: assertion `G_IS_OBJECT
> (object)' failed
I actually seem to get these even with the plain snippet: http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet202.java
Comment 9 Silenio Quarti CLA 2012-01-25 10:12:41 EST
Fixed both the redraw problems and the warnings.

http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=5dc68b1cb799fd3d7f3b8218db3d623442a9221c

The OutOfMemory is a problem in the example, it is creating all the items by calling getItems(). This is expected behaviour.
Comment 10 Martin Oberhuber CLA 2012-01-25 10:52:15 EST
Whow ! Thanks for the quick turnaround.

Could this be backported to 3.7.2 ? As it affects our product.

Many Thanks!
Martin
Comment 11 Silenio Quarti CLA 2012-01-25 12:07:57 EST
I am sorry. The fix needs more testing and 3.7.2 RC3 is next week.
Comment 12 Pawel Piech CLA 2012-01-25 12:17:54 EST
(In reply to comment #9)
> Fixed both the redraw problems and the warnings.
Thank You!  (Fixed in M5 I assume)

> The OutOfMemory is a problem in the example, it is creating all the items by
> calling getItems(). This is expected behaviour.
Any idea why I don't observe this error on my system then?
Comment 13 Pawel Piech CLA 2012-01-25 12:18:29 EST
*** Bug 356415 has been marked as a duplicate of this bug. ***
Comment 14 Silenio Quarti CLA 2012-01-25 13:02:57 EST
(In reply to comment #12)
> (In reply to comment #9)
> > Fixed both the redraw problems and the warnings.
> Thank You!  (Fixed in M5 I assume)
> 

Yes

> > The OutOfMemory is a problem in the example, it is creating all the items by
> > calling getItems(). This is expected behaviour.
> Any idea why I don't observe this error on my system then?

I am not sure, but it is probably because SWT.SetData is not sent synchronous in your system when the example calls getItems() or setExpanded().
Comment 15 Martin Oberhuber CLA 2012-01-26 10:01:53 EST
CQ:WIND00295043