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

Binding Components to Data

Figure 9.2. Interface Relationships in Vaadin Data Model

The Data Model has many important and useful features, such as support for change notification. Especially containers have many helper interfaces, including ones that allow indexing, ordering, sorting, and filtering the data. Also Field components provide a number of features involving the data model, such as buffering, validation, and lazy loading.

Vaadin provides a number of built-in implementations of the data model interfaces. The built-in implementations are used as the default data models in many field components.

In addition to the built-in implementations, many data model implementations, such as containers, are available as add-ons, either from the Vaadin Directory or from independent sources. Both commercial and free implementations exist. The JPAContainer, described in Chapter 21, Vaadin JPAContainer, is the most often used conmmercial container add-on. The installation of add-ons is described in Chapter 17, Using Vaadin Add-ons. Notice that unlike with most regular add-on components, you do not need to compile a widget set for add-ons that include just data model implementations.

9.2. Properties

The Property interface is the base of the Vaadin Data Model. It provides a standardized API for a single data object that can be read (get) and written (set). A property is always typed, but can optionally support data type conversions.The type of a property can be any Java class. Optionally, properties can provide value change events for following their changes.

The value of a property is written with setValue() and read with getValue(). The return value is a generic Object reference, so you need to cast it to the proper type. The type can be acquired with getType().

final TextField tf = new TextField("Name");

// Set the value

Properties

241

Binding Components to Data

tf.setValue("The text field value");

// When the field value is edited by the user tf.addListener(new Property.ValueChangeListener() {

public void valueChange(ValueChangeEvent event) {

//Get the value and cast it to proper type String value = (String) tf.getValue();

//Do something with it layout.addComponent(new Label(value));

}

});

Changes in the property value usually emit a ValueChangeEvent, which can be handled with a ValueChangeListener.The event object provides reference to the property with getProperty().

Properties are in themselves unnamed.They are collected in items, which associate the properties with names: the Property Identifiers or PIDs. Items can be further contained in containers and are identified with Item Identifiers or IIDs. In the spreadsheet analogy, Property Identifiers would correspond to column names and Item Identifiers to row names. The identifiers can be arbitrary objects, but must implement the equals(Object) and hashCode() methods so that they can be used in any standard Java Collection.

The Property interface can be utilized either by implementing the interface or by using some of the built-in property implementations. Vaadin includes a Property interface implementation for arbitrary function pairs and bean properties, with the MethodProperty class, and for simple object properties, with the ObjectProperty class, as described later.

In addition to the simple components, many selection components such as Select, Table, and Tree provide their current selection through the Property property. In single selection mode, the property is a single item identifier, while in multiple selection mode it is a set of item identifiers. Please see the documentation of the selection components for further details.

Components that can be bound to a property have an internal default data source object, typically a ObjectProperty, which is described later. As all such components are viewers or editors, also described later, so you can rebind a component to any data source with setPropertyDataSource().

9.2.1. Property Viewers and Editors

The most important function of the Property as well as of the other data model interfaces is to connect classes implementing the interface directly to editor and viewer classes. This means connecting a data source (model) to a user interface component (views) to allow editing or viewing the data model.

A property can be bound to a component implementing the Viewer interface with setPropertyDataSource().

//Have a data model ObjectProperty property =

new ObjectProperty("Hello", String.class);

//Have a component that implements Viewer Label viewer = new Label();

//Bind it to the data viewer.setPropertyDataSource(property);

242

Property Viewers and Editors

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