2013-09-30 88 views
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個樹項目可見。

enter image description here

不應該只ILazyTreeContentProvider加載可見的人? (在這種情況下,它的7個項目)

我必須做一些錯誤的...

不管怎麼說,任何意見都非常感謝!

回答

4

我比較我的例子與this one 而且我發現只有一個區別在於使所有的區別:

v.setUseHashlookup(true); //v is the TreeViewer 

什麼在其文檔的方法規定:

配置,這是否結構化查看器使用內部哈希表 來加速元素和SWT項目之間的映射。在給予查看者一個輸入(通過setInput)之前,必須調用 。

現在內容提供者實際上是懶惰的。只繪製可見項目,而不繪製所有項目。