Skip to content

Commit

Permalink
feat: add lazy support to available items grid
Browse files Browse the repository at this point in the history
Close #130
  • Loading branch information
flang committed Jul 24, 2023
1 parent 5caf060 commit 199e613
Show file tree
Hide file tree
Showing 24 changed files with 1,149 additions and 388 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.flowingcode.vaadin.addons.twincolgrid;

import com.vaadin.flow.data.provider.SortOrder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class BaseLazyFilter<T> implements LazyFilter<T> {

private Collection<T> selectedItems = new HashSet<>();

private List<SortOrder<String>> sorting = new ArrayList<>();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.flowingcode.vaadin.addons.twincolgrid;

import com.flowingcode.vaadin.addons.twincolgrid.TwinColModel.TwinColModelMode;

/**
* Eager filter configuration for {@link TwinColGrid}.
*
* @param <T>
*/
public class EagerFilterConfiguration<T> extends FilterConfiguration<T, EagerFilterableColumn<T>> {

@SafeVarargs
public EagerFilterConfiguration(EagerFilterableColumn<T>... columns) {
super(columns);
}

void apply(TwinColGrid<T> grid) {
filteredColumns.forEach(grid::addFilterableColumn);
}

@Override
boolean supports(TwinColModelMode mode) {
return TwinColModelMode.EAGER.equals(mode);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.flowingcode.vaadin.addons.twincolgrid;

import com.vaadin.flow.function.SerializableBiPredicate;
import lombok.Getter;

/**
* Enables in memory filtering support to column.
*
* @param <T>
*/
@Getter
public class EagerFilterableColumn<T> extends FilterableColumn<T> {

/**
* filter condition to apply to column values.
*/
private final SerializableBiPredicate<T, String> filterCondition;

public EagerFilterableColumn(TwinColumn<T> column, String filterPlaceholder,
boolean enableClearButton, SerializableBiPredicate<T, String> filterCondition) {
super(column, filterPlaceholder, enableClearButton);
this.filterCondition = filterCondition;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.flowingcode.vaadin.addons.twincolgrid;

import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.Grid.Column;
import com.vaadin.flow.component.grid.dnd.GridDropLocation;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.provider.InMemoryDataProvider;
import com.vaadin.flow.data.provider.ListDataProvider;
import com.vaadin.flow.data.value.ValueChangeMode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import lombok.NonNull;

/**
* Model that supports {@link InMemoryDataProvider} for {@link TwinColGrid} available and selection
* grids.
*
* @param <T>
*/
class EagerTwinColModel<T> extends TwinColModel<T, EagerFilterableColumn<T>> {

EagerTwinColModel(@NonNull Grid<T> grid, String className) {
super(grid, className);
this.grid.setDataProvider(DataProvider.ofCollection(new ArrayList<>()));
}

@Override
@SuppressWarnings("unchecked")
ListDataProvider<T> getDataProvider() {
return (ListDataProvider<T>) grid.getDataProvider();
}

@Override
void addAll(Collection<T> items) {
getDataProvider().getItems().addAll(items);
}

@Override
void removeAll(Collection<T> items) {
getDataProvider().getItems().removeAll(items);
}

@Override
void addFilterableColumn(Column<T> column, EagerFilterableColumn<T> filter) {
TextField filterField = new TextField();
filterField.setClearButtonVisible(filter.isEnableClearButton());
filterField.setValueChangeMode(ValueChangeMode.EAGER);
filterField.setSizeFull();
filterField.setPlaceholder(filter.getFilterPlaceholder());
filterField.addValueChangeListener(
event -> getDataProvider()
.addFilter(item -> filter.getFilterCondition().test(item, filterField.getValue())));

if (headerRow == null) {
setHeaderRow(grid.appendHeaderRow());
}

headerRow.getCell(column).setComponent(filterField);
}

void clear() {
getDataProvider().getItems().clear();
}

void addItems(Collection<T> draggedItems, T dropOverItem, GridDropLocation dropLocation) {
if (dropOverItem != null) {
Collection<T> collection = getDataProvider().getItems();
List<T> list = new ArrayList<>(collection);
int dropIndex = list.indexOf(dropOverItem) + (dropLocation == GridDropLocation.BELOW ? 1 : 0);
list.addAll(dropIndex, draggedItems);
getDataProvider().getItems().clear();
addAll(list);
} else {
addAll(draggedItems);
}
getDataProvider().refreshAll();
}

Collection<T> getItems() {
return getDataProvider().getItems();
}

@Override
TwinColModelMode getMode() {
return TwinColModelMode.EAGER;
}

@Override
boolean supportsAddAll() {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.flowingcode.vaadin.addons.twincolgrid;

import com.flowingcode.vaadin.addons.twincolgrid.TwinColModel.TwinColModelMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public abstract class FilterConfiguration<T, C extends FilterableColumn<T>> {

protected final Collection<C> filteredColumns = new ArrayList<>();

@SafeVarargs
public FilterConfiguration(C... columns) {
filteredColumns.addAll(Arrays.asList(columns));
}

public void addFilteredColumn(C column) {
filteredColumns.add(column);
}

/**
* Applies this {@link FilterConfiguration} to grid.
*
* @param grid
*/
abstract void apply(TwinColGrid<T> grid);

/**
* Checks if {@link FilterConfiguration} supports the given {@link TwinColModelMode}
*
* @param mode mode to check.
* @return true mode is supported.
*/
abstract boolean supports(TwinColModelMode mode);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.flowingcode.vaadin.addons.twincolgrid;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Getter
public abstract class FilterableColumn<T> {

private final TwinColumn<T> column;
private final String filterPlaceholder;
private final boolean enableClearButton;

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public FilterableTwinColumn(Column<T> availableColumn, Column<T> selectionColumn
* @see TextField#setPlaceholder(String)
*/
public FilterableTwinColumn<T> setFilterPlaceholder(String filterPlaceholder) {
TwinColGrid.getFilterTextField(getAvailableColumn()).setPlaceholder(filterPlaceholder);
TwinColGrid.getFilterTextField(getSelectionColumn()).setPlaceholder(filterPlaceholder);
LegacyTwinColGrid.getFilterTextField(getAvailableColumn()).setPlaceholder(filterPlaceholder);
LegacyTwinColGrid.getFilterTextField(getSelectionColumn()).setPlaceholder(filterPlaceholder);
return this;
}

Expand All @@ -33,8 +33,8 @@ public FilterableTwinColumn<T> setFilterPlaceholder(String filterPlaceholder) {
* @see TextField#setClearButtonVisible(boolean)
*/
public FilterableTwinColumn<T> setClearButtonVisible(boolean clearButtonVisible) {
TwinColGrid.getFilterTextField(getAvailableColumn()).setClearButtonVisible(clearButtonVisible);
TwinColGrid.getFilterTextField(getSelectionColumn()).setClearButtonVisible(clearButtonVisible);
LegacyTwinColGrid.getFilterTextField(getAvailableColumn()).setClearButtonVisible(clearButtonVisible);
LegacyTwinColGrid.getFilterTextField(getSelectionColumn()).setClearButtonVisible(clearButtonVisible);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.flowingcode.vaadin.addons.twincolgrid;

import com.vaadin.flow.data.provider.SortOrder;
import java.util.Collection;
import java.util.List;

public interface LazyFilter<T> {

/**
* Items already selected.
*
* @return
*/
Collection<T> getSelectedItems();

/**
* Sorting criterias.
*
* @return
*/
List<SortOrder<String>> getSorting();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.flowingcode.vaadin.addons.twincolgrid;

import com.flowingcode.vaadin.addons.twincolgrid.TwinColModel.TwinColModelMode;
import lombok.RequiredArgsConstructor;

/**
* Lazy filter configuration for {@link TwinColGrid}.
*
* @param <T>
*/
@RequiredArgsConstructor
public class LazyFilterConfiguration<T> extends FilterConfiguration<T, LazyFilterableColumn<T>> {

private final LazyFilter<T> lazyFilter;

@SafeVarargs
public LazyFilterConfiguration(LazyFilter<T> lazyFilter, LazyFilterableColumn<T>... columns) {
super(columns);
this.lazyFilter = lazyFilter;
}

@Override
void apply(TwinColGrid<T> grid) {
filteredColumns.forEach(grid::addFilterableColumn);
grid.setLazyFilter(lazyFilter);
}

@Override
boolean supports(TwinColModelMode mode) {
return TwinColModelMode.LAZY.equals(mode);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.flowingcode.vaadin.addons.twincolgrid;

import com.vaadin.flow.function.SerializableBiPredicate;
import com.vaadin.flow.function.SerializableConsumer;
import lombok.Getter;

/**
* Enables lazy filtering support to column.
*
* @param <T>
*/
@Getter
public class LazyFilterableColumn<T> extends FilterableColumn<T> {

/**
* filter bean field to store the query string.
*/
private final SerializableConsumer<String> lazyFilterField;

/**
* filter condition to apply to column values in selection grid.
*/
private final SerializableBiPredicate<T, String> eagerFilterCondition;

public LazyFilterableColumn(TwinColumn<T> column, String filterPlaceholder,
boolean enableClearButton, SerializableConsumer<String> lazyFilterField,
SerializableBiPredicate<T, String> eagerFilterCondition) {
super(column, filterPlaceholder, enableClearButton);
this.lazyFilterField = lazyFilterField;
this.eagerFilterCondition = eagerFilterCondition;
}

public EagerFilterableColumn<T> asEager() {
return new EagerFilterableColumn<>(super.getColumn(), super.getFilterPlaceholder(),
super.isEnableClearButton(), eagerFilterCondition);
}

}
Loading

0 comments on commit 199e613

Please sign in to comment.