-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add lazy support to available items grid
Close #130
- Loading branch information
Showing
24 changed files
with
1,266 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/flowingcode/vaadin/addons/twincolgrid/BaseLazyFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/flowingcode/vaadin/addons/twincolgrid/EagerFilterConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.flowingcode.vaadin.addons.twincolgrid; | ||
|
||
import com.flowingcode.vaadin.addons.twincolgrid.TwinColModel.TwinColModelMode; | ||
|
||
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); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/flowingcode/vaadin/addons/twincolgrid/EagerFilterableColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/com/flowingcode/vaadin/addons/twincolgrid/EagerTwinColModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/flowingcode/vaadin/addons/twincolgrid/FilterConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/flowingcode/vaadin/addons/twincolgrid/FilterableColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/flowingcode/vaadin/addons/twincolgrid/LazyFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/flowingcode/vaadin/addons/twincolgrid/LazyFilterConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.flowingcode.vaadin.addons.twincolgrid; | ||
|
||
import com.flowingcode.vaadin.addons.twincolgrid.TwinColModel.TwinColModelMode; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@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); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/flowingcode/vaadin/addons/twincolgrid/LazyFilterableColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.