加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – ILazyContentProvider更新每个viewer.setItemCount()的

发布时间:2020-12-15 04:17:36 所属栏目:Java 来源:网络整理
导读:问候亲爱的Stackoverflowians, 几个月前,我正在处理ILazyTreeContentProvider,最后按照Eclipse RCP – ILazyTreeContentProvider implementation is unexpectedly eager修复它 但是我遇到了与ILazyContentProvider完全相同的问题,尽管我采取了与树相似的步骤
问候亲爱的Stackoverflowians,

几个月前,我正在处理ILazyTreeContentProvider,最后按照Eclipse RCP – ILazyTreeContentProvider implementation is unexpectedly eager修复它

但是我遇到了与ILazyContentProvider完全相同的问题,尽管我采取了与树相似的步骤,但我感到很茫然.
在这个表中,我每秒在表中添加大约1000个元素,并且每隔100毫秒通过setItemCount()在查看器上触发刷新.

窗口大小小于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<MyEntity> 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<MyEntity>) 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<MyEntity> 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<MyEntity> createModel() {
        List<MyEntity> list = new Vector<MyEntity>(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();
    }
}

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

解决方法

javadoc for

TableViewer.setSelection(ISelection selection,boolean reveal)

声明如下:

Sets a new selection for this viewer and optionally makes it visible. The TableViewer implementation of this method is inefficient for the ILazyContentProvider as lookup is done by indices rather than elements and may require population of the entire table in worse case.

Use Table#setSelection(int[] indices) and Table#showSelection() if you wish to set selection more efficiently when using a ILazyContentProvider.

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读