Vla*_*lie 4 java swt eclipse-plugin jface
问候亲爱的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<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();
}
}
Run Code Online (Sandbox Code Playgroud)
任何建议,意见和选择都非常感谢.你们好棒!
小智 5
javadoc for
TableViewer.setSelection(ISelection selection, boolean reveal)
Run Code Online (Sandbox Code Playgroud)
声明如下:
为此查看器设置新选择,并可选择使其可见.对于ILazyContentProvider,此方法的TableViewer实现效率低,因为查找是通过索引而不是元素完成的,并且可能需要整个表的填充情况更糟.
如果您希望在使用ILazyContentProvider时更有效地设置选择,请使用Table#setSelection(int [] indices)和Table#showSelection().
因此,你可以写这样的东西:
v.getTable().setSelection(counter - 1);
v.getTable().showSelection();
Run Code Online (Sandbox Code Playgroud)
使用这种方法,绘画操作平均需要10毫秒.