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

Binding Components to Data

You can use the same method in the Editor interface to bind a component that allows editing a particular property type to a property.

//Have a data model ObjectProperty property =

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

//Have a component that implements Viewer TextField editor = new TextField("Edit Greeting");

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

As all field components implement the Property interface, you can bind any component implementing the Viewer interface to any field, assuming that the viewer is able the view the object type of the field. Continuing from the above example, we can bind a Label to the TextField value:

Label viewer = new Label(); viewer.setPropertyDataSource(editor);

//The value shown in the viewer is updated immediately

//after editing the value in the editor (once it

//loses the focus)

editor.setImmediate(true);

9.2.2. ObjectProperty Implementation

The ObjectProperty class is a simple implementation of the Property interface that allows storing an arbitrary Java object.

// Have a component that implements Viewer interface final TextField tf = new TextField("Name");

//Have a data model with some data String myObject = "Hello";

//Wrap it in an ObjectProperty ObjectProperty property =

new ObjectProperty(myObject, String.class);

//Bind the property to the component tf.setPropertyDataSource(property);

9.2.3.Converting Between Property Type and Representation

Fields allow editing a certain type, such as a String or Date. The bound property, on the other hand, could have some entirely different type. Conversion between a representation edited by the field and the model defined in the property is handler with a converter that implements the Converter interface.

Most common type conversions, such as between string and integer, are handled by the default converters. They are created in a converter factory global in the application.

Basic Use of Converters

The setConverter(Converter) method sets the converter for a field. The method is defined in AbstractField.

// Have an integer property

final ObjectProperty<Integer> property = new ObjectProperty<Integer>(42);

ObjectProperty Implementation

243

Binding Components to Data

//Create a TextField, which edits Strings final TextField tf = new TextField("Name");

//Use a converter between String and Integer tf.setConverter(new StringToIntegerConverter());

//And bind the field tf.setPropertyDataSource(property);

The built-in converters are the following:

Table 9.1. Built-in Converters

StringToIntegerConverter

String

Integer

StringToDoubleConverter

String

Double

StringToNumberConverter

String

Number

StringToBooleanConverter

String

Boolean

StringToDateConverter

String

Date

DateToLongConverter

Date

Long

In addition, there is a ReverseConverter that takes a converter as a parameter and reverses the conversion direction.

If a converter already exists for a type, the setConverter(Class) retrieves the converter for the given type from the converter factory, and then sets it for the field.This method is used implicitly when binding field to a property data source.

Implementing a Converter

A conversion always occurs between a representation type, edited by the field component, and a model type, that is, the type of the property data source. Converters implement the Converter interface defined in the com.vaadin.data.util.converter package.

For example, let us assume that we have a simple Complex type for storing complex values.

public class ComplexConverter

implements Converter<String, Complex> { @Override

public Complex convertToModel(String value, Locale locale) throws ConversionException {

String parts[] =

value.replaceAll("[\\(\\)]", "").split(","); if (parts.length != 2)

throw new ConversionException(

"Unable to parse String to Complex"); return new Complex(Double.parseDouble(parts[0]),

Double.parseDouble(parts[1]));

}

@Override

public String convertToPresentation(Complex value, Locale locale)

throws ConversionException {

return "("+value.getReal()+","+value.getImag()+")";

}

@Override

public Class<Complex> getModelType() { return Complex.class;

244

Converting Between Property Type and Representation

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