ILazyContentProvider更新每个viewer.setItemCount()的所有内容

 我要知道521无敌 发布于 2023-01-29 17:51

问候亲爱的Stackoverflowians,

几个月前我正在处理一个ILazyTreeContentProvider,最后根据Eclipse RCP修复它- ILazyTreeContentProvider实现意外地渴望

但我面临与ILazyContentProvider完全相同的问题,尽管我已经按照与树一样的步骤,但我感到很茫然.在这个表中,我每秒在表中添加大约1000个元素,并且setItemCount()每100毫秒在查看器上触发刷新.

窗口大小小于100行,因此每次调用setItemCount()查看器时,updateElement()方法都不应从第一个索引开始.

不幸的是,确实如此.它每次从0更新到最后一个索引.

这是代码:

package manyelementscontentprovider;

import java.util.List;
import java.util.Vector;
import org.eclipse.jface.viewers.ILazyContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class LargeDataSetTable {
    private class MyContentProvider implements ILazyContentProvider {
        private TableViewer viewer;
        public List elements;
        private int lastIndex=0;
        public MyContentProvider(TableViewer viewer) {
            this.viewer = viewer;
        }

        public void dispose() {

        }

        @SuppressWarnings("unchecked")
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            this.elements = (List) newInput;
        }
        @Override
        public void updateElement(int index) {
            System.out.println(index);
            if (!viewer.isBusy())
                viewer.replace(elements.get(index), index);
        }

    }

    public static class MyEntity {
        public int counter;
        public MyEntity(int counter) {
            this.counter = counter;
        }

        public String toString() {
            return "Item " + this.counter;
        }
    }

    List model;

    private int counter;
    private Display display;
    private TableViewer v;
    public LargeDataSetTable(Shell shell, Display display) {
        model = createModel();
        this.display=display;
        v= new TableViewer(shell, SWT.VIRTUAL);
        v.setLabelProvider(new LabelProvider());
        v.setContentProvider(new MyContentProvider(v));
        v.setInput(null);
        v.setUseHashlookup(true);
        counter = 0;

        v.setInput(model);
        v.setItemCount(model.size());

        v.getTable().setLinesVisible(true);
    }
    private void startSomeShit() {
        final Runnable gooeiUpdate = new Runnable() {

            @Override
            public void run() {
                long timeA = System.currentTimeMillis();
                v.setItemCount(counter);                            
                v.setSelection( new StructuredSelection( model.get(counter-1) ), true );
                v.setSelection(null);
                long timeB = System.currentTimeMillis();
                System.out.println("Paint lasted:"+(timeB-timeA));
            }

        };

        Runnable addThingsToModel = new Runnable() {

            public void run() {
                long currentTime=System.currentTimeMillis();
                long howManyGotIn =0;
                while (counter<4000000) {
                    for (int i = 0; i< 10; i++){
                        final MyEntity m = new MyEntity(counter);
                        model.add(m);
                        counter++;
                    }

                    if (System.currentTimeMillis()-currentTime>100) {
                        howManyGotIn=counter - howManyGotIn;
                        display.syncExec(gooeiUpdate);
                        currentTime=System.currentTimeMillis();
                        System.out.println("How many got in = "+howManyGotIn);
                        howManyGotIn=counter;
                    }

                    try {
                        Thread.sleep(0,25);
                    } catch(InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread th = new Thread(addThingsToModel);
        th.start();
    }
    private List createModel() {
        List list = new Vector(4000000);
        return list;
    }
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        LargeDataSetTable viewerCica = new LargeDataSetTable(shell,display);
        shell.open();
        viewerCica.startSomeShit();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

任何建议,意见和选择都非常感谢.你们好棒!

1 个回答
  • javadoc for

    TableViewer.setSelection(ISelection selection, boolean reveal)
    

    声明如下:

    为此查看器设置新选择,并可选择使其可见.对于ILazyContentProvider,此方法的TableViewer实现效率低,因为查找是通过索引而不是元素完成的,并且可能需要整个表的填充情况更糟.

    如果您希望在使用ILazyContentProvider时更有效地设置选择,请使用Table#setSelection(int [] indices)和Table#showSelection().

    因此,你可以写这样的东西:

    v.getTable().setSelection(counter - 1);
    v.getTable().showSelection();
    

    使用这种方法,绘画操作平均需要10毫秒.

    2023-01-29 17:54 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有