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

Binding Components to Data

}

@Override

public Class<String> getPresentationType() { return String.class;

}

}

The conversion methods get the locale for the conversion as a parameter.

Converter Factory

If a field does not directly allow editing a property type, a default converter is attempted to create using an application-global converter factory. If you define your own converters that you wish to include in the converter factory, you need to implement one yourself. While you could implement the ConverterFactory interface, it is usually easier to just extend DefaultConverterFactory.

class MyConverterFactory extends DefaultConverterFactory { @Override

public <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> createConverter(Class<PRESENTATION> presentationType,

Class<MODEL> modelType) { // Handle one particular type conversion

if (String.class == presentationType && Complex.class == modelType)

return (Converter<PRESENTATION, MODEL>) new ComplexConverter();

// Default to the supertype

return super.createConverter(presentationType, modelType);

}

}

// Use the factory globally in the application Application.getCurrentApplication().setConverterFactory(

new MyConverterFactory());

9.2.4. Implementing the Property Interface

Implementation of the Property interface requires defining setters and getters for the value and the read-only mode. Only a getter is needed for the property type, as the type is often fixed in property implementations.

The following example shows a simple implementation of the Property interface:

class MyProperty implements Property { Integer data = 0;

boolean readOnly = false;

// Return the data type of the model public Class<?> getType() {

return Integer.class;

}

public Object getValue() { return data;

}

// Override the default implementation in Object @Override

public String toString() {

return Integer.toHexString(data);

}

Implementing the Property Interface

245

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