Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

Binding Components to Data

//using the default field factory FieldGroup binder = new FieldGroup(item); binder.bindMemberFields(form);

//And the form can be used in an higher-level layout layout.addComponent(form);

Encapsulating in CustomComponent

Using a CustomComponent can be better for hiding the implementation details than extending a layout. Also, the use of the FieldGroup can be encapsulated in the form class.

Consider the following as an alternative for the form implementation presented earlier:

// A form component that allows editing an item class MyForm extends CustomComponent {

//Member that will bind to the "name" property TextField name = new TextField("Name");

//Member that will bind to the "age" property @PropertyId("age")

TextField ageField = new TextField("Age");

public MyForm(Item item) {

FormLayout layout = new FormLayout(); layout.addComponent(name); layout.addComponent(ageField);

// Now use a binder to bind the members FieldGroup binder = new FieldGroup(item); binder.bindMemberFields(this);

setCompositionRoot(layout);

}

}

// And the form can be used as a component layout.addComponent(new MyForm(item));

9.4.4. Buffering Forms

A FieldGroup handles buffering the form content so that it is written to the data model only when the commit() is called for the FieldGroup. Edits can be discarded, so that the underlying property value is reloaded, by calling discard(). Buffering is enabled by default, but can be set with the setBuffered() method in FieldGroup.

// Have an item of some sort

 

final PropertysetItem item =

new PropertysetItem();

item.addItemProperty("name",

new ObjectProperty<String>("Q"));

item.addItemProperty("age",

new ObjectProperty<Integer>(42));

//Have some layout and create the fields Panel form = new Panel("Buffered Form"); form.setContent(new FormLayout());

//Build and bind the fields using the default field factory final FieldGroup binder = new FieldGroup(item); form.addComponent(binder.buildAndBind("Name", "name")); form.addComponent(binder.buildAndBind("Age", "age"));

//Enable buffering (actually enabled by default) binder.setBuffered(true);

//A button to commit the buffer

Buffering Forms

251

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]