Wednesday, February 29, 2012

Eclipse JFace 'VIRTUAL' TreeViewer basically requires TreeColumnLayout?

When switching a TreeViewer from an ITreeContentProvider to a 'VIRTUAL' TreeViewer with ILazyTreeContentProvider, the tree display initially appeared blank. The items in the tree would only become visible after waiting a little, or by doing some window operations that result in a refresh of the tree. This happened especially on Mac OS X.

After looking closer, it became obvious that the one and only column of the tree had a nearly zero width. Note the arrow in the image that shows the column separator:




The issue seems to be that a non-VIRTUAL TreeViewer knows all its items right away and can size the tree columns appropriately. When swtiching to a VIRTUAL TreeViewer, the items are updated as needed, and the column refresh can lag.

Early "fixes" involved forcing a refresh after setting the TreeViewer input:

tree.setRedraw(false);
tree_viewer.setInput(config);
tree_viewer.refresh();
tree.setRedraw(true);

or

tree_viewer.expandAll();

But the best workaround seems to be to use a TreeColumnLayout that always auto-sizes the one and only column:


// Note that the TreeViewer needs to be the only widget
// under the parent widget. If necessary, add Composite
// to wrap the TreeViewer
final TreeColumnLayout layout = new TreeColumnLayout();
parent.setLayout(layout);
final TreeViewer v = new TreeViewer(shell, SWT.VIRTUAL | SWT.BORDER);
v.setLabelProvider(new LabelProvider());

No comments:

Post a Comment