以下是ListEditor
纠正其他示例中发现的问题的最小示例。
public abstract class FooEditor extends Composite implements Editor<Foo> {
Widget root; // Instantiated explicitly or through uibinder
// Implemented as one of uibinder+fields, fields, methods, or LeafValueEditor.set/getValue()
public FooEditor() {
initWidget(root);
}
// Used for brevity, could be any triggering mechanism, click handler, event handler, etc.
abstract void onDeleteClicked();
}
public class FooListEditor extends Composite implements IsEditor<ListEditor<Foo, FooEditor>> {
private class FooEditorSource extends EditorSource<FooEditor> {
@Override
public FooEditor create(int index) {
FooEditor subEditor = new FooEditor()
{
@Override
public void onDeleteClicked()
{
// =======================================================
//
// This fixes the problem present in other examples
// by determining the current index at the time of removal
//
// =======================================================
int currentIndex = listEditor.getEditors().indexOf(this);
listEditor.getList().remove(currentIndex);
}
};
setIndex(subEditor, index);
return subEditor;
}
@Override
public void dispose(FooEditor subEditor) {
subEditor.removeFromParent();
}
@Override
public void setIndex(FooEditor subEditor, int index) {
listPanel.insert(subEditor, index);
}
}
FlowPanel listPanel; // Instantiated explicitly or through uibinder
ListEditor<Foo, FooEditor> listEditor = ListEditor.of(new FooEditorSource());
public FooListEditor() {
initWidget(listPanel);
}
@Override
public ListEditor<Foo, FooEditor> asEditor() {
return listEditor;
}
}