2009年5月25日

Eclipse JFace Data Binding (TableViewer)

Eclipse JFace Data Binding
Eclipse Databinding with Eclipse RCP applications - Tutorial

// -- Main.java Eclipse 3.5 --
import java.beans.beancontext.BeanContextChildSupport;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
import org.eclipse.jface.databinding.viewers.ViewerSupport;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;


public class Main {

private Shell shell;
private TableViewerColumn column;

public Main(Shell s) {
this.shell = s;
Realm.runWithDefault(SWTObservables.getRealm(s.getDisplay()), new Runnable() {

public void run() {
Display display = shell.getDisplay();

TableViewer viewer = createUI();
List model = createModel();

ViewerSupport.bind(viewer,
new WritableList(model, Person.class),
BeanProperties.value(Person.class, "name"));

DataBindingContext bindingContext = new DataBindingContext();
column.setEditingSupport(new InlineEditingSupport(viewer, bindingContext));

shell.setSize(200, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
});
}

private List createModel() {
List persons = new ArrayList();
persons.add(new Person("A"));
persons.add(new Person("B"));
persons.add(new Person("C"));
return persons;
}


private TableViewer createUI() {
Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);

TableViewer viewer = new TableViewer(table);
column = new TableViewerColumn(viewer, SWT.NONE);

TableColumn col = column.getColumn();
col.setText("Name");
col.setWidth(188);
return viewer;
}

private static class InlineEditingSupport extends
ObservableValueEditingSupport {
private CellEditor cellEditor;

public InlineEditingSupport(ColumnViewer viewer, DataBindingContext dbc) {
super(viewer, dbc);
cellEditor = new TextCellEditor((Composite) viewer.getControl());
}

protected CellEditor getCellEditor(Object element) {
return cellEditor;
}

protected IObservableValue doCreateCellEditorObservable(
CellEditor cellEditor) {
return SWTObservables.observeText(cellEditor.getControl(),
SWT.Modify);
}

protected IObservableValue doCreateElementObservable(Object element,
ViewerCell cell) {
return BeansObservables.observeValue(element, "name");
}
}

public static void main(String[] args) {
Display display = new Display();

Shell shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.VERTICAL));
new Main(shell);

display.dispose();
}

class Person extends BeanContextChildSupport {

private String name;

public Person() {

}

public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setName(String name) {
firePropertyChange("name", this.name, this.name = name);
}
}

}

沒有留言:

網誌存檔