[英]Create model with containing CellList using GWT Editor Framework?
I have the following model: 我有以下模型:
class Book {
String title;
// getter and setter
}
class Author {
private name;
private List<Book> books;
// getter and setter
}
I want to create a GWT Widget where I can create an author and create as many books as possible while creating the author in the same view. 我想创建一个GWT窗口小部件,可以在同一视图中创建作者的同时创建作者并创建尽可能多的书籍。 Here is how it might look like:
这可能是这样的:
You can add as many books as you want: 您可以根据需要添加任意数量的图书:
I want to use the GWT Editor framework to populate the data between the view and the models. 我想使用GWT编辑器框架在视图和模型之间填充数据。 Here is how I do it only for the
Author
model without the list. 这是我仅对没有列表的
Author
模型执行的操作。
My UiBinder: 我的UiBinder:
<g:TextBox ui:field="nameInput"/>
My Widget.java: 我的Widget.java:
public class MyWidgetView {
@Path(value="name")
@UiField
TextBox nameInput;
public interface EditorDriver extends SimpleBeanEditorDriver<Author, MyWidgetView> { }
@Override
public SimpleBeanEditorDriver<Author, ?> createEditorDriver() {
EditorDriver driver = GWT.create(EditorDriver.class);
driver.initialize(this);
return driver;
}
}
In my presenter I created: 在我的演示者中,我创建了:
SimpleBeanEditorDriver<TriggerDto, ?> editorDriver = getView().createEditorDriver();
This works fine for a simple model but I want to use it for the above author model which contains a List<Book>
. 这对于简单的模型来说效果很好,但是我想在上面包含
List<Book>
作者模型中使用它。
How do I have to setup the UiBinder such that I can add Books to the author and what do I have to do with the editor to get the created list of Book
s flushed into the author model? 如何设置UiBinder,以便可以将
Book
添加到作者中,并且与编辑器有什么关系才能将创建的Book
列表刷新到作者模型中?
Here how you can achieve that. 在这里,您将如何实现这一目标。 You'll need to create an Editor, a ListEditor and an EditorSource
您需要创建一个编辑器,一个ListEditor和一个EditorSource
public class BooksEditor implements IsWidget, IsEditor<ListEditor<Book, BookEditor>> {
private final HTMLPanel main;
private final ListEditor<Book, BookEditor>> editor;
@Inject
BooksEditor(
Provider<BookEditor> bookEditorProvider) {
main = new HTMLPanel("");
editor = ListEditor.of(new BookEditorSource(bookEditorProvider, main));
}
@Override
public ListEditor<Book, BookEditor> asEditor() {
return editor;
}
@Override
public Widget asWidget() {
return main;
}`
}
public class BookEditorSource extends EditorSource<BookEditor> {
private final Provider<BookEditor> bookEditorProvider;
private final HTMLPanel books;
public BookEditorSource(
Provider<BookEditor> bookEditorProvider,
HTMLPanel books) {
this.bookEditorProvider = bookEditorProvider;
this.books = books;
}
@Override
public BookEditor create(int index) {
BookEditor bookEditor = bookEditorProvider.get();
books.add(bookEditor);
return bookEditor;
}
@Override
public void dispose(BookEditor subEditor) {
super.dispose(subEditor);
books.remove(subEditor);
}
}
To create the BookEditor class, simply create a new Widget that implements Editor<Book>
and create the according UiFields to be able to edit your Book object. 要创建BookEditor类,只需创建一个新的Widget,该Widget实现
Editor<Book>
并创建相应的UiFields即可编辑Book对象。 The values will be handled by the parent Author SimpleBeanDriver. 这些值将由父Author SimpleBeanDriver处理。 Then in your
MyWidgetView
, simply add a UiField : 然后在您的
MyWidgetView
,只需添加一个UiField:
@UiField(provided = true)
@Path("books")
BooksEditor booksInput;
If you want to add/remove widgets on the fly and synchronize it with the ListEditor, simply work with the BooksEditor class. 如果要动态添加/删除小部件并将其与ListEditor同步,只需使用BooksEditor类即可。
editor.getList().add(new Book())
will automatically add a new BookEditor in your panel. editor.getList().add(new Book())
将在您的面板中自动添加一个新的BookEditor。 Calling editor.getList().remove(bookToRemove)
will remove it (see the dispose() method in BookEditorSource, it's called when calling remove on the list). 调用
editor.getList().remove(bookToRemove)
将其删除(请参阅BookEditorSource中的dispose()方法,在列表上调用remove时会调用它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.