IT Mill Toolkit Data Model is one of the core concepts of the library. Let us
revisit the model-view-controller design pattern. The model consists of application
data and business logic, which acts on the data. The user interface generates
events, which are processed by the controller, which controls the user interface and
the data. To allow the view (user interface components) to access the application
data directly, without need to annoy the controller with every mundane change, we
need a standard data interface. Application data needs a common interface so that
the data can be accessed by the view and the controller alike. In the Toolkit, we
have solved this with the Data Model.
At the heart of Data Model is the property that consists of a
value and its data type. A property is always typed and the type can be any object
type in Java. Properties are in themselves unnamed objects. Properties are
collected in an item, which associates the properties with
names, the Property Identifiers or PIDs.
Items can be contained in containers and are identified with Item
Identifiers or IIDs.
The Data Model is realized as a set of interface classes in the fittigly named
package com.itmill.toolkit.data. The package contains
interfaces Property, Item, and
Container, along with a number of more specialized interfaces
and classes.
Notice that the Data Model does not define data representation, but only
interfaces. This leaves the representation fully to the implementation of the
containers. The representation can be almost anything, such as a Java object
structure, a filesystem, or a database query.
The Data Model is used heavily in UI components of the Toolkit. A key feature of
all UI components is that they can either maintain their data by themselves or be
bound to an external data source. For example, many UI components, such as
Button, Label, or
TextField have a single property which they control. You can
access this property through the Property interface inherited
by the components. By default, the property is contained within the component, but
you can bind the the components to external data sources with the
setPropertyDataSource() method of the
com.itmill.toolkit.ui.AbstractField class inherited by such
components.
Many UI components are actually both containers and
properties. This is especially true for selectable
components (that implement
Select), because they are
containers that contain selectable items. Their
property is the currently selected item. For more
details on components, see Chapter 4, User Interface Components.
By implementing a container interface, you can bind UI
components directly to data. As containers can be
unordered, ordered, indexed, or hierarchical, they can
interface practically any kind of data
representation. The Toolkit includes data connectors
for some common data sources, such as the
filesystem.
The Data Model has several important features, such as
support for change notification, transactions,
validation, and lazy loading. These features are
discussed in detail below.