6
我正在開發一個Eclipse RCP應用程序,並試圖使用ILazyTreeContentProvider
實現,以便在特定時間僅顯示可見項目。Eclipse RCP - ILazyTreeContentProvider實現意外地渴望
代碼:
類內部延伸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();
}
的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);
}
}
的問題是,每次我展開TreeItem的孩子,這加載所有子元素,而不僅僅是可見的子元素,而System.out.println(updateElementCounter++);
命令打印出全部22個,儘管只有7個樹項目可見。
不應該只ILazyTreeContentProvider
加載可見的人? (在這種情況下,它的7個項目)
我必須做一些錯誤的...
不管怎麼說,任何意見都非常感謝!