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

Binding Components to Data

public boolean isReadOnly() { return readOnly;

}

public void setReadOnly(boolean newStatus) { readOnly = newStatus;

}

public void setValue(Object newValue)

throws ReadOnlyException, ConversionException { if (readOnly)

throw new ReadOnlyException();

//Already the same type as the internal representation if (newValue instanceof Integer)

data = (Integer) newValue;

//Conversion from a string is required

else if (newValue instanceof String) try {

data = Integer.parseInt((String) newValue, 16); } catch (NumberFormatException e) {

throw new ConversionException();

}

else

// Don't know how to convert any other types throw new ConversionException();

// Reverse decode the hexadecimal value

}

}

//Instantiate the property and set its data MyProperty property = new MyProperty(); property.setValue(42);

//Bind it to a component

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

The components get the displayed value by the toString() method, so it is necessary to override it. To allow editing the value, value returned in the toString() must be in a format that is accepted by the setValue() method, unless the property is read-only.The toString() can perform any type conversion necessary to make the internal type a string, and the setValue() must be able to make a reverse conversion.

The implementation example does not notify about changes in the property value or in the readonly mode. You should normally also implement at least the Property.ValueChangeNotifier and Property.ReadOnlyStatusChangeNotifier. See the ObjectProperty class for an example of the implementation.

9.3. Holding properties in Items

The Item interface provides access to a set of named properties. Each property is identified by a property identifier (PID) and a reference to such a property can be queried from an Item with getItemProperty() using the identifier.

Examples on the use of items include rows in a Table, with the properties corresponding to table columns, nodes in a Tree, and the the data bound to a Form, with item's properties bound to individual form fields.

246

Holding properties in Items

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