Example usage for com.vaadin.ui Grid getDataProvider

List of usage examples for com.vaadin.ui Grid getDataProvider

Introduction

In this page you can find the example usage for com.vaadin.ui Grid getDataProvider.

Prototype

@Override
    public DataProvider<T, ?> getDataProvider() 

Source Link

Usage

From source file:de.symeda.sormas.ui.utils.GridExportStreamResource.java

License:Open Source License

public GridExportStreamResource(Grid<?> grid, String tempFilePrefix, String filename,
        String... ignoredPropertyIds) {
    super(new StreamSource() {
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override//from www  .j  a  v  a 2 s . c om
        public InputStream getStream() {
            List<String> ignoredPropertyIdsList = Arrays.asList(ignoredPropertyIds);
            List<Column> columns = new ArrayList<>(grid.getColumns());
            columns.removeIf(c -> c.isHidden());
            columns.removeIf(c -> ignoredPropertyIdsList.contains(c.getId()));

            try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
                try (CSVWriter writer = CSVUtils.createCSVWriter(
                        new OutputStreamWriter(byteStream, StandardCharsets.UTF_8.name()),
                        FacadeProvider.getConfigFacade().getCsvSeparator())) {

                    List<String> headerRow = new ArrayList<>();
                    columns.forEach(c -> {
                        headerRow.add(c.getCaption());
                    });
                    writer.writeNext(headerRow.toArray(new String[headerRow.size()]));

                    String[] rowValues = new String[columns.size()];

                    int totalRowCount = grid.getDataProvider().size(new Query());
                    for (int i = 0; i < totalRowCount; i += 100) {
                        grid.getDataProvider().fetch(new Query(i, 100, grid.getSortOrder(), null, null))
                                .forEach(row -> {
                                    for (int c = 0; c < columns.size(); c++) {
                                        Column column = columns.get(c);
                                        Object value = column.getValueProvider().apply(row);
                                        String valueString;
                                        if (value != null) {
                                            if (value instanceof Date) {
                                                valueString = DateHelper.formatLocalDateTime((Date) value);
                                            } else if (value instanceof Boolean) {
                                                if ((Boolean) value == true) {
                                                    valueString = I18nProperties
                                                            .getEnumCaption(YesNoUnknown.YES);
                                                } else
                                                    valueString = I18nProperties
                                                            .getEnumCaption(YesNoUnknown.NO);
                                            } else {
                                                valueString = value.toString();
                                            }
                                        } else {
                                            valueString = "";
                                        }
                                        rowValues[c] = valueString;
                                    }
                                    writer.writeNext(rowValues);
                                });
                        writer.flush();
                    }
                }
                return new BufferedInputStream(new ByteArrayInputStream(byteStream.toByteArray()));
            } catch (IOException e) {
                // TODO This currently requires the user to click the "Export" button again or reload the page as the UI
                // is not automatically updated; this should be changed once Vaadin push is enabled (see #516)
                new Notification(I18nProperties.getString(Strings.headingExportFailed),
                        I18nProperties.getString(Strings.messageExportFailed), Type.ERROR_MESSAGE, false)
                                .show(Page.getCurrent());
                return null;
            }
        }
    }, filename);
    setMIMEType("text/csv");
    setCacheTime(0);
}

From source file:dhbw.clippinggorilla.userinterface.views.AboutUsView.java

public AboutUsView() {
    setMargin(true);/*from  w  ww .ja  v  a  2  s  .c  o m*/
    setWidth("55%");

    Label aboutUsHeader = new Label();
    Language.set(Word.ABOUT_US, aboutUsHeader);
    aboutUsHeader.setStyleName(ValoTheme.LABEL_H1);
    addComponent(aboutUsHeader);

    Label aboutUsGroupHeadline = new Label();
    Language.set(Word.GROUP_PROJECT_OF_GROUP_4, aboutUsGroupHeadline);
    aboutUsGroupHeadline.setStyleName(ValoTheme.LABEL_H2);
    addComponent(aboutUsGroupHeadline);

    Label aboutUsText = new Label();
    Language.set(Word.GROUP_PROJECT_BODY, aboutUsText);
    aboutUsText.setWidth("100%");
    aboutUsText.setContentMode(com.vaadin.shared.ui.ContentMode.HTML);
    addComponent(aboutUsText);

    Label theTeamHeader = new Label();
    Language.set(Word.OUR_TEAM, theTeamHeader);
    theTeamHeader.setStyleName(ValoTheme.LABEL_H2);
    addComponent(theTeamHeader);

    Grid<Person> theTeamGrid = new Grid<>();
    List<Person> persons = Arrays.asList(new Person("Dan-Pierre", "Drehlich", Person.Function.TEAMLEADER),
            new Person("Stefan", "Schmid", Person.Function.RESPONSIBLE_FOR_RESEARCH),
            new Person("Jan", "Striegel", Person.Function.TECHNICAL_ASSISTANT),
            new Person("Lisa", "Hartung",
                    Person.Function.RESPONSIBLE_FOR_MODELING_QUALITY_ASSURANCE_AND_DOCUMENTATION),
            new Person("Tim", "Heinzelmann", Person.Function.RESPONSIBLE_FOR_TESTS),
            new Person("Josua", "Frank", Person.Function.RESPONSIBLE_FOR_IMPLEMENTATION));
    Grid.Column c1 = theTeamGrid.addColumn(p -> p.getFirstName());
    Language.setCustom(Word.FIRST_NAME, s -> c1.setCaption(s));
    Grid.Column c2 = theTeamGrid.addColumn(p -> p.getLastName());
    Language.setCustom(Word.LAST_NAME, s -> c2.setCaption(s));
    Grid.Column c3 = theTeamGrid.addColumn(p -> p.getResposibility());
    Language.setCustom(Word.RESPONSIBILITY, s -> {
        c3.setCaption(s);
        theTeamGrid.getDataProvider().refreshAll();
    });
    theTeamGrid.setItems(persons);
    theTeamGrid.setWidth("100%");
    theTeamGrid.setHeightByRows(6);
    addComponent(theTeamGrid);
    SESSIONS.put(VaadinSession.getCurrent(), this);
}

From source file:dhbw.clippinggorilla.userinterface.views.ArchiveView.java

public ArchiveView() {
    HorizontalLayout optionsLayout = new HorizontalLayout();
    optionsLayout.setWidth("100%");

    Grid<Clipping> gridClippings = new Grid<>();
    Set<Clipping> clippings = ClippingUtils.getUserClippings(UserUtils.getCurrent(),
            LocalDate.now(ZoneId.of("Europe/Berlin")));
    gridClippings.setItems(clippings);// w  w w  .j av a2 s.  c  om

    InlineDateTimeField datePicker = new InlineDateTimeField();
    datePicker.setValue(LocalDateTime.now(ZoneId.of("Europe/Berlin")));
    datePicker.setLocale(Locale.GERMANY);
    datePicker.setResolution(DateTimeResolution.DAY);
    datePicker.addValueChangeListener(date -> {
        Set<Clipping> clippingsOfDate = ClippingUtils.getUserClippings(UserUtils.getCurrent(),
                date.getValue().toLocalDate());
        gridClippings.setItems(clippingsOfDate);
        gridClippings.getDataProvider().refreshAll();
    });

    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
    formatter.withZone(ZoneId.of("Europe/Berlin"));
    Column columnTime = gridClippings.addColumn(c -> {
        return c.getDate().format(formatter);
    });
    Language.setCustom(Word.TIME, s -> columnTime.setCaption(s));
    Column columnAmountArticles = gridClippings.addColumn(c -> {
        long amountArticles = c.getArticles().values().stream().flatMap(l -> l.stream()).count();
        amountArticles += c.getArticlesFromGroup().values().stream().flatMap(l -> l.stream()).count();
        if (amountArticles != 1) {
            return amountArticles + " " + Language.get(Word.ARTICLES);
        } else {
            return amountArticles + " " + Language.get(Word.ARTICLE);
        }
    });
    Language.setCustom(Word.ARTICLES, s -> {
        columnAmountArticles.setCaption(s);
        gridClippings.getDataProvider().refreshAll();
    });

    gridClippings.setHeight("100%");
    gridClippings.setSelectionMode(Grid.SelectionMode.SINGLE);
    gridClippings.addSelectionListener(c -> {
        if (c.getFirstSelectedItem().isPresent()) {
            currentClipping = c.getFirstSelectedItem().get();
            showClippingBy(currentClipping, currentSort);
        }
    });

    optionsLayout.addComponents(datePicker, gridClippings);
    optionsLayout.setComponentAlignment(datePicker, Alignment.BOTTOM_CENTER);
    optionsLayout.setComponentAlignment(gridClippings, Alignment.BOTTOM_RIGHT);
    optionsLayout.setExpandRatio(gridClippings, 5);

    VerticalLayout sortLayout = new VerticalLayout();

    comboBoxSortOptions = new ComboBox<>(Language.get(Word.SORT_BY));
    Language.setCustom(Word.SORT_BY, s -> {
        comboBoxSortOptions.setCaption(s);
        comboBoxSortOptions.getDataProvider().refreshAll();
    });
    comboBoxSortOptions.setItems(EnumSet.allOf(ClippingView.SortOptions.class));
    comboBoxSortOptions.setItemCaptionGenerator(s -> s.getName());
    comboBoxSortOptions.setItemIconGenerator(s -> s.getIcon());
    comboBoxSortOptions.setValue(currentSort);
    comboBoxSortOptions.setTextInputAllowed(false);
    comboBoxSortOptions.setEmptySelectionAllowed(false);
    comboBoxSortOptions.addStyleName("comboboxsort");
    comboBoxSortOptions.addValueChangeListener(e -> {
        currentSort = e.getValue();
        showClippingBy(currentClipping, currentSort);
    });
    comboBoxSortOptions.setVisible(false);

    sortLayout.setMargin(false);
    sortLayout.setSpacing(false);
    sortLayout.addComponent(comboBoxSortOptions);

    clippingArticlesLayout = new VerticalLayout();
    clippingArticlesLayout.setSpacing(true);
    clippingArticlesLayout.setMargin(false);
    clippingArticlesLayout.setSizeFull();

    addComponents(optionsLayout, sortLayout, clippingArticlesLayout);
}

From source file:dhbw.clippinggorilla.userinterface.views.ImpressumView.java

public ImpressumView() {
    setMargin(true);/* ww w. j  av a 2  s.  c  o m*/
    setWidth("55%");

    Label impressumHeader = new Label();
    Language.set(Word.IMPRESSUM, impressumHeader);
    impressumHeader.setStyleName(ValoTheme.LABEL_H1);
    addComponent(impressumHeader);

    Label impressumText = new Label();
    Language.set(Word.IMPRESSUM_BODY, impressumText);
    impressumText.setWidth("100%");
    impressumText.setContentMode(com.vaadin.shared.ui.ContentMode.HTML);
    addComponent(impressumText);

    Grid<Person> studentsGrid = new Grid<>();
    List<Person> persons = Arrays.asList(new Person("Dan-Pierre", "Drehlich", Person.Function.TEAMLEADER),
            new Person("Stefan", "Schmid", Person.Function.RESPONSIBLE_FOR_RESEARCH),
            new Person("Jan", "Striegel", Person.Function.TECHNICAL_ASSISTANT),
            new Person("Lisa", "Hartung",
                    Person.Function.RESPONSIBLE_FOR_MODELING_QUALITY_ASSURANCE_AND_DOCUMENTATION),
            new Person("Tim", "Heinzelmann", Person.Function.RESPONSIBLE_FOR_TESTS),
            new Person("Josua", "Frank", Person.Function.RESPONSIBLE_FOR_IMPLEMENTATION));
    Column c1 = studentsGrid.addColumn(p -> p.getFirstName());
    Language.setCustom(Word.FIRST_NAME, s -> c1.setCaption(s));
    Column c2 = studentsGrid.addColumn(p -> p.getLastName());
    Language.setCustom(Word.LAST_NAME, s -> c2.setCaption(s));
    Column c3 = studentsGrid.addColumn(p -> p.getResposibility());
    Language.setCustom(Word.RESPONSIBILITY, s -> {
        c3.setCaption(s);
        studentsGrid.getDataProvider().refreshAll();
    });

    studentsGrid.setItems(persons);
    studentsGrid.setWidth("100%");
    studentsGrid.setHeightByRows(6);
    addComponent(studentsGrid);

    Label liabilityHeadline = new Label();
    Language.set(Word.LIABILITY, liabilityHeadline);
    liabilityHeadline.setStyleName(ValoTheme.LABEL_H1);
    addComponent(liabilityHeadline);

    Label liabilityContentHeadline = new Label();
    Language.set(Word.LIABILITY_CONTENT, liabilityContentHeadline);
    liabilityContentHeadline.setStyleName(ValoTheme.LABEL_H2);
    addComponent(liabilityContentHeadline);

    Label liabilityContentText = new Label();
    Language.set(Word.LIABILITY_CONTENT_BODY, liabilityContentText);
    liabilityContentText.setWidth("100%");
    addComponent(liabilityContentText);

    Label liabilityLinksHeadline = new Label();
    Language.set(Word.LIABILITY_LINKS, liabilityLinksHeadline);
    liabilityLinksHeadline.setStyleName(ValoTheme.LABEL_H2);
    addComponent(liabilityLinksHeadline);

    Label liabilityLinksText = new Label();
    Language.set(Word.LIABILITY_LINKS_BODY, liabilityLinksText);
    liabilityLinksText.setWidth("100%");
    addComponent(liabilityLinksText);

    Label copyrightHeadline = new Label();
    Language.set(Word.COPYRIGHT, copyrightHeadline);
    copyrightHeadline.setStyleName(ValoTheme.LABEL_H2);
    addComponent(copyrightHeadline);

    Label copyrightText = new Label();
    Language.set(Word.COPYRIGHT_BODY, copyrightText);
    copyrightText.setWidth("100%");
    addComponent(copyrightText);

    Label dataProtectionHeadline = new Label();
    Language.set(Word.DATAPROTECTION, dataProtectionHeadline);
    dataProtectionHeadline.setStyleName(ValoTheme.LABEL_H2);
    addComponent(dataProtectionHeadline);

    Label dataProtectionText = new Label();
    Language.set(Word.DATAPROTECTION_BODY, dataProtectionText);
    dataProtectionText.setWidth("100%");
    addComponent(dataProtectionText);
    SESSIONS.put(VaadinSession.getCurrent(), this);
}