2009年5月8日

Use Eclipse JFace DeferredContentProvider


import java.text.Collator;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.deferred.DeferredContentProvider;
import org.eclipse.jface.viewers.deferred.SetModel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class DeferredContentProviderViewer extends Composite {

private TableViewer viewer;
private SetModel times;
private Runnable runnable;

public DeferredContentProviderViewer(Composite parent, int style) {
super(parent, style);
initialize();
this.setLayout(new FillLayout());
}

private void initialize() {
setSize(new Point(300, 200));
setLayout(new GridLayout());

viewer = new TableViewer(this, SWT.VIRTUAL | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setContentProvider(new DeferredContentProvider(Collator.getInstance()));

times = new SetModel();
viewer.setInput(times);

runnable = new Runnable() {

@Override
public void run() {
long initTimeMillis = System.currentTimeMillis();
try {
while (!isDisposed()) {
long currentTimeMillis = System.currentTimeMillis();
synchronized (times) {
times.addAll(new String[] { String.format("%TT",
initTimeMillis - currentTimeMillis) });
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};

Thread thread = new Thread(runnable);
thread.start();
}

public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setText("DeferredContentProvider");
shell.setLayout(new FillLayout());
shell.setSize(new Point(300, 200));
new DeferredContentProviderViewer(shell, SWT.NONE);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

}

沒有留言:

網誌存檔