aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/grid/GridDetailsAndUndefinedHeight.java
blob: b30211412edf7ceeae02326d366160a07fe4f68b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.vaadin.tests.components.grid;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.stream.IntStream;

import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.ui.grid.HeightMode;
import com.vaadin.tests.components.AbstractReindeerTestUI;
import com.vaadin.tests.data.bean.Person;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

public class GridDetailsAndUndefinedHeight extends AbstractReindeerTestUI {

    @Override
    protected void setup(VaadinRequest request) {
        ArrayList<Person> itemsCollection = new ArrayList<>();

        final Grid<Person> grid = new Grid<Person>() {
            @Override
            public void setItems(Collection<Person> items) {
                itemsCollection.clear();
                itemsCollection.addAll(items);
                super.setItems(items);
            }
        };
        addComponent(grid);

        grid.addColumn(Person::getFirstName);
        grid.addColumn(Person::getLastName);
        grid.addColumn(Person::getEmail);

        grid.setItems(IntStream.range(0, 5).mapToObj(this::createPerson));

        grid.setWidthFull();
        grid.setHeightMode(HeightMode.UNDEFINED);

        grid.setSelectionMode(SelectionMode.SINGLE);

        grid.addSelectionListener(event -> {
            itemsCollection
                    .forEach(item -> grid.setDetailsVisible(item, false));
            final Set<Person> selections = event.getAllSelectedItems();
            if (!selections.isEmpty()) {
                Person selection = selections.iterator().next();
                grid.setDetailsVisible(selection, true);
                grid.scrollTo(itemsCollection.indexOf(selection));
            }
        });
        grid.setDetailsGenerator(person -> new VerticalLayout(
                new Label("Details " + itemsCollection.indexOf(person))));
    }

    private Person createPerson(int index) {
        Person person = new Person();
        person.setFirstName("cell " + index + " 0");
        person.setLastName("cell " + index + " 1");
        person.setEmail("cell " + index + " 2");
        return person;
    }

    @Override
    protected String getTestDescription() {
        return "Second selection should successfully close "
                + "the details row from the first selection.";
    }

    @Override
    protected Integer getTicketNumber() {
        return 11856;
    }
}