I spent some time looking for a good example on how to wrap a CellTable so that it can be used in the Editor Framework. Since there was none, I wrote one by myself:
package com.mictale.example.client;
import java.util.ArrayList;
import java.util.List;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.TextInputCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.editor.client.adapters.HasDataEditor;
import com.google.gwt.editor.client.adapters.ListEditor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.MultiSelectionModel;
/**
* Shows how to use {@link HasDataEditor}.
*
* {@link HasDataEditor} wraps an implementation of {@link HasData}
* so that you can use it with the editor framework. This example wraps
* a {@link CellTable} but you can also use it with {@link CellList} and others.
*
In this example, the {@link Driver} only implements the workflow
* for the {@link List} of persons. Usually, you will have a hierarchy
* of {@link Editor}s and one of your model classes would return the list.
*
To improve reusability, consider wrapping {@link #persons}
* and {@link #editor} into a {@link Widget} that creates
* the {@link CellTable} and implements
* {@code IsEditor>>}.
*
* @author michael@mictale.com
*/
public class HasDataEditorExample implements EntryPoint {
public interface Driver extends SimpleBeanEditorDriver, ListEditor>> {
}
private final MultiSelectionModel selectionModel = new MultiSelectionModel();
private CellTable persons;
private ListEditor> editor;
private Driver driver;
public void onModuleLoad() {
final FlowPanel panel = new FlowPanel();
panel.add(persons = createList());
panel.add(new Button("Delete Selected", new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
editor.getList().removeAll(selectionModel.getSelectedSet());
}
}));
panel.add(new Button("Add New Entry", new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
editor.getList().add(new Person());
}
}));
panel.add(new Button("Save", new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
doSave();
}
}));
editor = HasDataEditor.of(persons);
RootPanel.get().add(panel);
driver = GWT.create(Driver.class);
driver.initialize(editor);
final List persons = loadPersons();
driver.edit(persons);
}
private void doSave() {
List persons = driver.flush();
StringBuilder sb = new StringBuilder();
for (Person p : persons) {
sb.append(p.toString());
sb.append("\n");
}
Window.alert(sb.toString());
}
/**
* Creates and initializes a fake model.
* @return the fake model.
*/
private List loadPersons() {
List persons = new ArrayList();
persons.add(new Person("Michael", "Schollmeyer", "michael@mictale.com"));
persons.add(new Person("Liv", "Schollmeyer", "liv@mictale.com"));
return persons;
}
/**
* Create and initialize a {@link CellList}
* @return the new list.
*/
private CellTable createList() {
CellTable list = new CellTable();
final TextInputCell cell = new TextInputCell();
list.setSelectionModel(selectionModel, DefaultSelectionEventManager. createCheckboxManager());
Column checkColumn = new Column(new CheckboxCell(true, false)) {
@Override
public Boolean getValue(Person object) {
return selectionModel.isSelected(object);
}
};
list.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("
"));
list.setColumnWidth(checkColumn, 40, Unit.PX);
Column firstNameColumn = new Column(cell) {
@Override
public String getValue(Person item) {
return item.getFirstName();
}
};
firstNameColumn.setFieldUpdater(new FieldUpdater() {
@Override
public void update(int index, Person object, String value) {
object.setFirstName(value);
}
});
list.addColumn(firstNameColumn, "First Name");
Column lastNameColumn = new Column(cell) {
@Override
public String getValue(Person item) {
return item.getLastName();
}
};
lastNameColumn.setFieldUpdater(new FieldUpdater() {
@Override
public void update(int index, Person object, String value) {
object.setLastName(value);
}
});
list.addColumn(lastNameColumn, "Last Name");
Column emailColumn = new Column(cell) {
@Override
public String getValue(Person item) {
return item.getEmail();
}
};
emailColumn.setFieldUpdater(new FieldUpdater() {
@Override
public void update(int index, Person object, String value) {
object.setEmail(value);
}
});
list.addColumn(emailColumn, "Email");
return list;
}
/**
* The elements in the list that we use here.
*/
public static class Person {
private String firstName;
private String lastName;
private String email;
public Person() {
}
public Person(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return firstName + ", " + lastName + ", " + email;
}
}
}