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

Vaadin JPAContainer

21.4.4. Hierarchical Container

JPAContainer implements the Container.Hierarchical interface and can be bound to hierarchical components such as a Tree or TreeTable. The feature requires that the hierarchy is represented with a parent property that refers to the parent item. At database level, this would be a column with IDs.

The representation would be as follows:

@Entity

public class CelestialBody implements Serializable { @Id

@GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;

private String name;

@ManyToOne

private CelestialBody parent;

...

} ...

// Create some entities

 

CelestialBody sun

=

new CelestialBody("The Sun", null);

CelestialBody mercury

=

new CelestialBody("Mercury", sun);

CelestialBody venus

=

new CelestialBody("Venus", sun);

CelestialBody earth

=

new CelestialBody("Earth", sun);

CelestialBody moon

=

new CelestialBody("The Moon", earth);

CelestialBody mars

=

new CelestialBody("Mars", sun);

...

 

 

You set up a JPAContainer to have hierarchy by calling setParentProperty() with the name of the property that refers to the parent. Coincidentally, it is named "parent" in the example:

//Create the container JPAContainer<CelestialBody> bodies =

JPAContainerFactory.make(CelestialBody.class, "my-unit");

//Set it up for hierarchical representation bodies.setParentProperty("parent");

//Bind it to a hierarhical component

Tree tree = new Tree("Celestial Bodies", bodies); tree.setItemCaptionMode(Tree.ITEM_CAPTION_MODE_PROPERTY); tree.setItemCaptionPropertyId("name");

You can use the rootItemIds() to acquire the item IDs of the root elements with no parent.

// Expand the tree

for (Object rootId: bodies.rootItemIds()) tree.expandItemsRecursively(rootId);

Unsupported Hierarchical Features

Using setParent() in the container to define parenthood is not supported.

Also, the current implementation does not support setChildrenAllowed(), which controls whether the user can expand a node by clicking a toggle. The toggle is by default visible for all nodes, even if they have no children. The method is not supported because it would require storing the information outside the entities. You can override areChildrenAllowed() to implement the functionality using a custom logic.

Hierarchical Container

465

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