Container is the most advanced of the data model supported by IT Mill Toolkit. It provides a very flexible way of managing a set of items that share common properties. Each item is identified by an item id. Properties can be requested from container with item and property ids. Another way of accessing properties is to first request an item from container and then request its properties from it.
Container interface was designed with flexibility and efficiency in mind. It
contains inner interfaces for ordering the items sequentially, indexing the items
and accessing them hierarchically. Those ordering models provide the basis for the Table
,
Tree
, and Select
UI components. As with other data models, the containers support
events for notifying about the changes.
A set of utilities for converting between container models by adding external indexing or hierarchy into existing containers. In memory containers implementing indexed and hierarchical models provide easy to use tools for setting up in memory data storages. There is even a hierarchical container for direct file system access.
As the items in a Container
are not indexed,
iterating over the items has to be done using an
Iterator
. The
getItemIds()
method of
Container
returns a
Collection
of item identifiers over which you
can iterate. The following example demonstrates a typical case where
you iterate over the values of check boxes in a column of a
Table
component. The context of the example is
the example used in Section 4.10, “Table
”.
/* Collect the results of the iteration into this string. */ String items = ""; /* Iterate over the item identifiers of the table. */ for (Iterator i = table.getItemIds().iterator(); i.hasNext();) { /* Get the current item identifier, which is an integer. */ int iid = (Integer) i.next(); /* Now get the actual item from the table. */ Item item = table.getItem(iid); /* And now we can get to the actual checkbox object. */ Button button = (Button) (item.getItemProperty("ismember").getValue()); /* If the checkbox is selected. */ if ((Boolean)button.getValue() == true) { /* Do something with the selected item; collect the first names in a string. */ items += item.getItemProperty("First Name").getValue() + " "; } } /* Do something with the results; display the selected items. */ layout.addComponent (new Label("Selected items: " + items));
Notice that the getItemIds()
returns an
unmodifiable collection, so the
Container
may not be modified during
iteration. You can not, for example, remove items from the
Container
during iteration. The modification
includes modification in another thread. If the
Container
is modified during iteration, a
ConcurrentModificationException
is thrown and
the iterator may be left in an undefined state.