I am developing an Eclipse RCP application, and am trying to use a ILazyTreeContentProvider
implementation in order to show only the visible items at a certain time.
The code:
Inside the class extending ViewPart:
public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.VIRTUAL);
drillDownAdapter = new DrillDownAdapter(viewer);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
//viewer.setSorter(new NameSorter());
viewer.setInput(getViewSite());
// Create the help context id for the viewer's control
PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "test.provider.lazy.viewer");
makeActions();
hookContextMenu();
hookDoubleClickAction();
contributeToActionBars();
}
Inside the ContentProvider:
@Override
public void updateElement(Object parent, int index) {
System.out.println(updateElementCounter++);
if (parent.equals(getViewSite())) {
if (index == 0) {
TreeParent child = new TreeParent("Parent 1");
viewer.replace(parent, index, child);
viewer.setHasChildren(child, true);
} else {
TreeParent child = new TreeParent("Parent 2");
viewer.replace(parent, index, child);
viewer.setHasChildren(child, true);
//viewer.setChildCount(child, 1);
}
} else {
if (parent instanceof TreeParent) {
TreeParent treeParent = (TreeParent) parent;
if (treeParent.getName().equals("Parent 1")) {
TreeObject child = new TreeObject("Leaf "+index);
viewer.replace(treeParent, index, child);
viewer.setHasChildren(child, false);
//viewer.setChildCount(child, 0);
} else {
TreeObject child = new TreeObject("Special One");
viewer.replace(treeParent, index, child);
viewer.setHasChildren(child, false);
//viewer.setChildCount(child,0);
}
}
}
}
@Override
public void updateChildCount(Object element, int currentChildCount) {
if (element.equals(getViewSite())) {
viewer.setChildCount(getViewSite(), 2);
} else
if (element instanceof TreeParent) {
TreeParent parent = (TreeParent) element;
if (parent.getName().equals("Root")) {
viewer.setChildCount(parent, 2);
} else if (parent.getName().equals("Parent 1")) {
viewer.setChildCount(parent, 20);
} else {
viewer.setChildCount(parent, 1);
}
} else {
viewer.setChildCount(element, 0);
}
}
The problem is that each time I expand the children of a TreeItem, it loads all the subelements, not just the visible ones, whereas the System.out.println(updateElementCounter++);
command prints out all 22 despite having only 7 Tree Items visible.
Shouldn't the ILazyTreeContentProvider
only load the ones that are visible? (In this case it's 7 items)
I must be doing something wrong...
Anyways, any opinions are very appreciated!
I have compared my example with this one And I've found only one difference that made all the difference:
v.setUseHashlookup(true); //v is the TreeViewer
What the method states in its doc:
Configures whether this structured viewer uses an internal hash table to speeds up the mapping between elements and SWT items. This must be called before the viewer is given an input (via setInput).
Now the content provider is actually lazy. Only paints the visible items, not ALL of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With