Eclipse RCP - ILazyTreeContentProvider 实现出人意料地渴望

2023-08-21Java开发问题
2

本文介绍了Eclipse RCP - ILazyTreeContentProvider 实现出人意料地渴望的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在开发一个 Eclipse RCP 应用程序,并尝试使用 ILazyTreeContentProvider 实现,以便在特定时间仅显示可见项目.

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.

代码:

在扩展 ViewPart 的类中:

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();
    }

在 ContentProvider 内部:

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);
            }

        }

问题是每次我展开 TreeItem 的子元素时,它都会加载所有子元素,而不仅仅是可见的子元素,而 System.out.println(updateElementCounter++); 命令会打印出来尽管只有 7 个树项可见,但全部 22 个.

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.

ILazyTreeContentProvider 不应该只加载可见的吗?(在本例中为 7 项)

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:

配置此结构化查看器是否使用内部哈希表加快元素和 SWT 项之间的映射.这必须是在为查看器提供输入之前调用(通过 setInput).

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.

这篇关于Eclipse RCP - ILazyTreeContentProvider 实现出人意料地渴望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13