com.vaadin.data.provider.ListDataProvider Java Examples

The following examples show how to use com.vaadin.data.provider.ListDataProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 7 votes vote down vote up
@Override
public void selectAll(final String filter) {
    final ListDataProvider<T> listDataProvider = ((ListDataProvider) getDataProvider());
    final Set<String> addedItems = listDataProvider.getItems()
            .stream()
            .filter(t -> {
                final String caption = getItemCaptionGenerator().apply(t);
                if (t == null) {
                    return false;
                }
                return caption.toLowerCase()
                        .contains(filter.toLowerCase());
            })
            .map(t -> itemToKey(t))
            .collect(Collectors.toSet());
    updateSelection(addedItems, new HashSet<>(), true);
}
 
Example #2
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 7 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * Filtering will use a case insensitive match to show all items where the filter text is a substring of the caption displayed for that item.
 */
@Override
public void setItems(final Collection<T> items) {
    final ListDataProvider<T> listDataProvider = DataProvider.ofCollection(items);

    setDataProvider(listDataProvider);

    // sets the PageLength to 10.
    // if there are less then 10 items in the combobox, PageLength will get the amount of items.
    setPageLength(getDataProvider().size(new Query<>()) >= ComboBoxMultiselect.DEFAULT_PAGE_LENGTH ? ComboBoxMultiselect.DEFAULT_PAGE_LENGTH : getDataProvider().size(new Query<>()));
}
 
Example #3
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 6 votes vote down vote up
@Override
public void clear(final String filter) {
    final ListDataProvider<T> listDataProvider = ((ListDataProvider) getDataProvider());
    final Set<String> removedItems = listDataProvider.getItems()
            .stream()
            .filter(t -> {
                final String caption = getItemCaptionGenerator().apply(t);
                if (t == null) {
                    return false;
                }
                return caption.toLowerCase()
                        .contains(filter.toLowerCase());
            })
            .map(t -> itemToKey(t))
            .collect(Collectors.toSet());
    updateSelection(new HashSet<>(), removedItems, true);
}
 
Example #4
Source File: CustomizedTableWindow.java    From mycollab with GNU Affero General Public License v3.0 6 votes vote down vote up
private void setSelectedViewColumns() {
    final Collection<String> viewColumnIds = this.getViewColumns();

    final ListDataProvider<TableViewField> dataProvider = (ListDataProvider<TableViewField>) listBuilder.getDataProvider();
    final Collection<TableViewField> itemIds = dataProvider.getItems();
    final Set<TableViewField> selectedColumns = new HashSet<>();

    for (String viewColumnId : viewColumnIds) {
        for (TableViewField viewField : itemIds) {
            if (viewColumnId.equals(viewField.getField())) {
                selectedColumns.add(viewField);
            }
        }
    }

    listBuilder.setValue(selectedColumns);
}
 
Example #5
Source File: GridCellFilter.java    From vaadin-grid-util with MIT License 6 votes vote down vote up
/**
 * keeps link to Grid and added HeaderRow<br>
 * afterwards you need to set filter specification for each row<br>
 * please take care that your Container implements Filterable!
 *
 * @param grid that should get added a HeaderRow that this component will manage
 */
public GridCellFilter(Grid<T> grid, Class<T> beanType) {
    this.grid = grid;
    filterHeaderRow = grid.appendHeaderRow();
    cellFilters = new HashMap<>();
    assignedFilters = new HashMap<>();
    cellFilterChangedListeners = new ArrayList<>();


    if (!(grid.getDataProvider() instanceof ListDataProvider)) {
        throw new RuntimeException("works only with ListDataProvider");
    } else {
        dataProvider = (ListDataProvider<T>) grid.getDataProvider();
        propertySet = BeanPropertySet.get(beanType);
    }
}
 
Example #6
Source File: GridWithGridListener.java    From context-menu with Apache License 2.0 6 votes vote down vote up
public GridWithGridListener() {
    setCaption("Grid with Grid specific listener");
    setHeightByRows(3);
    final GridContextMenu<String[]> gridContextMenu = new GridContextMenu<>(this);

    addColumn(arr -> arr[0]).setCaption("Column 1(right-click here)");
    addColumn(arr -> arr[1]).setCaption("Column 2(right-click here)");

    final ListDataProvider<String[]> dataSource = new ListDataProvider<>(
            Collections.singletonList(new String[]{"foo", "bar"}));
    setDataProvider(dataSource);
    gridContextMenu.addGridHeaderContextMenuListener(e -> {
        gridContextMenu.removeItems();
        gridContextMenu.addItem(getText(e), null);
    });

    gridContextMenu.addGridBodyContextMenuListener(e -> {
        gridContextMenu.removeItems();
        gridContextMenu.addItem(getText(e), null);
    });

}
 
Example #7
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 6 votes vote down vote up
@Override
protected <F> SerializableConsumer<F> internalSetDataProvider(final DataProvider<T, F> dataProvider, final F initialFilter) {
    final SerializableConsumer<F> consumer = super.internalSetDataProvider(dataProvider, initialFilter);

    if (getDataProvider() instanceof ListDataProvider) {
        final ListDataProvider<T> listDataProvider = ((ListDataProvider<T>) getDataProvider());
        listDataProvider.setSortComparator((o1, o2) -> {
            final boolean selected1 = this.sortingSelection.contains(o1);
            final boolean selected2 = this.sortingSelection.contains(o2);

            if (selected1 && !selected2) {
                return -1;
            }
            if (!selected1 && selected2) {
                return 1;
            }

            return getItemCaptionGenerator().apply(o1)
                    .compareToIgnoreCase(getItemCaptionGenerator().apply(o2));
        });
    }

    return consumer;
}
 
Example #8
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 6 votes vote down vote up
@Override
public void clear(final String filter) {
    final ListDataProvider<T> listDataProvider = ((ListDataProvider) getDataProvider());
    final Set<String> removedItems = listDataProvider.getItems()
            .stream()
            .filter(t -> {
                final String caption = getItemCaptionGenerator().apply(t);
                if (t == null) {
                    return false;
                }
                return caption.toLowerCase()
                        .contains(filter.toLowerCase());
            })
            .map(t -> itemToKey(t))
            .collect(Collectors.toSet());
    updateSelection(new HashSet<>(), removedItems, true);
}
 
Example #9
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 6 votes vote down vote up
@Override
public void selectAll(final String filter) {
    final ListDataProvider<T> listDataProvider = ((ListDataProvider) getDataProvider());
    final Set<String> addedItems = listDataProvider.getItems()
            .stream()
            .filter(t -> {
                final String caption = getItemCaptionGenerator().apply(t);
                if (t == null) {
                    return false;
                }
                return caption.toLowerCase()
                        .contains(filter.toLowerCase());
            })
            .map(t -> itemToKey(t))
            .collect(Collectors.toSet());
    updateSelection(addedItems, new HashSet<>(), true);
}
 
Example #10
Source File: AttributesLocationCompanion.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected void removeFromSourceGrid(Grid currentSourceGrid, boolean isAttributesSourceGrid, AbstractComponent dropComponent, int dropIndex) {
    if (!droppedSuccessful || draggedItem == null) {
        return;
    }

    //noinspection unchecked
    List<CategoryAttribute> items = (List<CategoryAttribute>) ((ListDataProvider) currentSourceGrid.getDataProvider()).getItems();
    if (currentSourceGrid.equals(dropComponent) && dropIndex >= 0) {
        int removeIndex = items.indexOf(draggedItem) == dropIndex
                ? items.lastIndexOf(draggedItem)
                : items.indexOf(draggedItem);
        if (removeIndex >= 0 && removeIndex != dropIndex) {
            items.remove(removeIndex);
        }
    } else {
        items.remove(draggedItem);
    }

    if (isAttributesSourceGrid && AttributesLocationFrame.EMPTY_ATTRIBUTE_NAME.equals(draggedItem.getName())) {
        attributesSourceDataContainer.add(createEmptyAttribute());
    }

    currentSourceGrid.getDataProvider().refreshAll();
}
 
Example #11
Source File: AttributesLocationCompanion.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void initGridDragAndDrop(DataGrid<CategoryAttribute> dataGrid,
                                List<CategoryAttribute> dataContainer,
                                boolean isAttributesSourceDataGrid) {

    DataProvider<CategoryAttribute, SerializablePredicate<CategoryAttribute>> dataProvider =  new ListDataProvider<>(dataContainer);

    if (isAttributesSourceDataGrid) {
        attributesSourceDataContainer = dataContainer;
        attributesSourceDataProvider = dataProvider;
        attributesSourceGrid = dataGrid.unwrap(CubaGrid.class);
    }

    dataGrid.withUnwrapped(CubaGrid.class, grid -> {
        grid.setDataProvider(dataProvider);

        GridDragSource<CategoryAttribute> gridDragSource = new GridDragSource<>(grid);
        gridDragSource.addGridDragStartListener(this::onGridDragStart);

        GridDropTarget<CategoryAttribute> gridDropTarget = new GridDropTarget<>(grid, DropMode.BETWEEN);
        gridDropTarget.addGridDropListener(e -> onGridDrop(e, isAttributesSourceDataGrid));
    });
}
 
Example #12
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 6 votes vote down vote up
@Override
protected <F> SerializableConsumer<F> internalSetDataProvider(final DataProvider<T, F> dataProvider, final F initialFilter) {
    final SerializableConsumer<F> consumer = super.internalSetDataProvider(dataProvider, initialFilter);

    if (getDataProvider() instanceof ListDataProvider) {
        final ListDataProvider<T> listDataProvider = ((ListDataProvider<T>) getDataProvider());
        listDataProvider.setSortComparator((o1, o2) -> {
            final boolean selected1 = this.sortingSelection.contains(o1);
            final boolean selected2 = this.sortingSelection.contains(o2);

            if (selected1 && !selected2) {
                return -1;
            }
            if (!selected1 && selected2) {
                return 1;
            }

            return getItemCaptionGenerator().apply(o1)
                    .compareToIgnoreCase(getItemCaptionGenerator().apply(o2));
        });
    }

    return consumer;
}
 
Example #13
Source File: AttributesLocationCompanion.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected int addToDestinationGrid(GridDropEvent<CategoryAttribute> event, boolean isSourceGrid, DragSourceExtension source) {
    if (isSourceGrid && AttributesLocationFrame.EMPTY_ATTRIBUTE_NAME.equals(draggedItem.getName())) {
        droppedSuccessful = true;
        return -1;
    }

    if (source instanceof GridDragSource) {
        //noinspection unchecked
        ListDataProvider<CategoryAttribute> dataProvider = (ListDataProvider<CategoryAttribute>)
                event.getComponent().getDataProvider();
        List<CategoryAttribute> items = (List<CategoryAttribute>) dataProvider.getItems();

        int i = items.size();
        if (event.getDropTargetRow().isPresent()) {
            i = items.indexOf(event.getDropTargetRow().get())
                    + (event.getDropLocation() == DropLocation.BELOW ? 1 : 0);
        }

        items.add(i, draggedItem);
        dataProvider.refreshAll();

        droppedSuccessful = true;

        return i;
    }

    return -1;
}
 
Example #14
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * Filtering will use a case insensitive match to show all items where the filter text is a substring of the caption displayed for that item.
 */
@Override
public void setItems(final Collection<T> items) {
    final ListDataProvider<T> listDataProvider = DataProvider.ofCollection(items);

    setDataProvider(listDataProvider);

    // sets the PageLength to 10.
    // if there are less then 10 items in the combobox, PageLength will get the amount of items.
    setPageLength(getDataProvider().size(new Query<>()) >= ComboBoxMultiselect.DEFAULT_PAGE_LENGTH ? ComboBoxMultiselect.DEFAULT_PAGE_LENGTH : getDataProvider().size(new Query<>()));
}
 
Example #15
Source File: GridWithGenericListener.java    From context-menu with Apache License 2.0 5 votes vote down vote up
public GridWithGenericListener() {
    setCaption("Grid with generic listener");

    ContextMenu contextMenu2 = new ContextMenu(this, true);
    setHeightByRows(3);
    addColumn(arr -> arr[0]).setCaption(FIRST_NAME);
    addColumn(arr -> arr[1]).setCaption(LAST_NAME);
    addColumn(arr -> arr[2]).setCaption(SHOE_SIZE);

    ListDataProvider<String[]> ds = new ListDataProvider<>(Collections
            .singletonList(new String[] { "John", "Doe", "57" }));
    setDataProvider(ds);
    contextMenu2.addContextMenuOpenListener(this::onGenericContextMenu);
}
 
Example #16
Source File: GridGanttLayout.java    From gantt with Apache License 2.0 5 votes vote down vote up
private Grid<Step> createGridForGantt() {

        dataProvider = new ListDataProvider<>(new ArrayList<>(gantt.getSteps()));
        Grid<Step> grid = new Grid<>(dataProvider);
        grid.setWidth(400, Unit.PIXELS);
        grid.setHeight(100, Unit.PERCENTAGE);

        Column<Step, ?> c = grid.addColumn(Step::getCaption);
        c.setSortable(false);
        c.setResizable(false);

        gantt.setVerticalScrollDelegateTarget(grid);

        return grid;
    }
 
Example #17
Source File: WebAbstractDataGrid.java    From cuba with Apache License 2.0 4 votes vote down vote up
protected DataProvider<E, ?> createEmptyDataProvider() {
    return new ListDataProvider<>(Collections.emptyList());
}
 
Example #18
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 3 votes vote down vote up
/**
 * Sets a list data provider as the data provider of this combo box. Filtering will use a case insensitive match to show all items where the filter text is
 * a substring of the caption displayed for that item.
 * <p>
 * Note that this is a shorthand that calls {@link #setDataProvider(DataProvider)} with a wrapper of the provided list data provider. This means that
 * {@link #getDataProvider()} will return the wrapper instead of the original list data provider.
 *
 * @param listDataProvider the list data provider to use, not <code>null</code>
 * @since 8.0
 */
public void setDataProvider(final ListDataProvider<T> listDataProvider) {
    // Cannot use the case insensitive contains shorthand from
    // ListDataProvider since it wouldn't react to locale changes
    final CaptionFilter defaultCaptionFilter = (itemText, filterText) -> itemText.toLowerCase(getLocale())
            .contains(filterText.toLowerCase(getLocale()));

    setDataProvider(defaultCaptionFilter, listDataProvider);
}
 
Example #19
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 3 votes vote down vote up
/**
 * Sets a list data provider with an item caption filter as the data provider of this combo box. The caption filter is used to compare the displayed caption
 * of each item to the filter text entered by the user.
 *
 * @param captionFilter filter to check if an item is shown when user typed some text into the ComboBoxMultiselect
 * @param listDataProvider the list data provider to use, not <code>null</code>
 * @since 8.0
 */
public void setDataProvider(final CaptionFilter captionFilter, final ListDataProvider<T> listDataProvider) {
    Objects.requireNonNull(listDataProvider, "List data provider cannot be null");

    // Must do getItemCaptionGenerator() for each operation since it might
    // not be the same as when this method was invoked
    setDataProvider(listDataProvider, filterText -> item -> captionFilter.test(getItemCaptionGenerator().apply(item), filterText));
}
 
Example #20
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 3 votes vote down vote up
/**
 * Sets a list data provider with an item caption filter as the data provider of this combo box. The caption filter is used to compare the displayed caption
 * of each item to the filter text entered by the user.
 *
 * @param captionFilter filter to check if an item is shown when user typed some text into the ComboBoxMultiselect
 * @param listDataProvider the list data provider to use, not <code>null</code>
 * @since 8.0
 */
public void setDataProvider(final CaptionFilter captionFilter, final ListDataProvider<T> listDataProvider) {
    Objects.requireNonNull(listDataProvider, "List data provider cannot be null");

    // Must do getItemCaptionGenerator() for each operation since it might
    // not be the same as when this method was invoked
    setDataProvider(listDataProvider, filterText -> item -> captionFilter.test(getItemCaptionGenerator().apply(item), filterText));
}
 
Example #21
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 3 votes vote down vote up
/**
 * Sets a list data provider as the data provider of this combo box. Filtering will use a case insensitive match to show all items where the filter text is
 * a substring of the caption displayed for that item.
 * <p>
 * Note that this is a shorthand that calls {@link #setDataProvider(DataProvider)} with a wrapper of the provided list data provider. This means that
 * {@link #getDataProvider()} will return the wrapper instead of the original list data provider.
 *
 * @param listDataProvider the list data provider to use, not <code>null</code>
 * @since 8.0
 */
public void setDataProvider(final ListDataProvider<T> listDataProvider) {
    // Cannot use the case insensitive contains shorthand from
    // ListDataProvider since it wouldn't react to locale changes
    final CaptionFilter defaultCaptionFilter = (itemText, filterText) -> itemText.toLowerCase(getLocale())
            .contains(filterText.toLowerCase(getLocale()));

    setDataProvider(defaultCaptionFilter, listDataProvider);
}
 
Example #22
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the data items of this listing and a simple string filter with which the item string and the text the user has input are compared.
 * <p>
 * Note that unlike {@link #setItems(Collection)}, no automatic case conversion is performed before the comparison.
 *
 * @param captionFilter filter to check if an item is shown when user typed some text into the ComboBoxMultiselect
 * @param items the data items to display
 * @since 8.0
 */
public void setItems(final CaptionFilter captionFilter, final Collection<T> items) {
    final ListDataProvider<T> listDataProvider = DataProvider.ofCollection(items);

    setDataProvider(captionFilter, listDataProvider);
}
 
Example #23
Source File: ComboBoxMultiselect.java    From vaadin-combobox-multiselect with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the data items of this listing and a simple string filter with which the item string and the text the user has input are compared.
 * <p>
 * Note that unlike {@link #setItems(Collection)}, no automatic case conversion is performed before the comparison.
 *
 * @param captionFilter filter to check if an item is shown when user typed some text into the ComboBoxMultiselect
 * @param items the data items to display
 * @since 8.0
 */
public void setItems(final CaptionFilter captionFilter, final Collection<T> items) {
    final ListDataProvider<T> listDataProvider = DataProvider.ofCollection(items);

    setDataProvider(captionFilter, listDataProvider);
}