Skip to content

Commit

Permalink
Merge pull request #4 from vaadin-component-factory/feature/customfil…
Browse files Browse the repository at this point in the history
…tertype

Custom Filter and multiSelect
  • Loading branch information
jcgueriaud1 authored Oct 7, 2021
2 parents d149345 + 02dc1cf commit 6b3e624
Show file tree
Hide file tree
Showing 19 changed files with 1,504 additions and 639 deletions.
6 changes: 3 additions & 3 deletions lookup-field-flow-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.vaadin.componentfactory</groupId>
<artifactId>lookup-field-flow-demo</artifactId>
<version>1.1.1</version>
<version>1.2.0</version>

<name>Lookup Field Demo</name>
<packaging>war</packaging>
Expand All @@ -18,7 +18,7 @@
</organization>

<properties>
<vaadin.version>14.5.3</vaadin.version>
<vaadin.version>14.7.1</vaadin.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -96,7 +96,7 @@
<dependency>
<groupId>com.vaadin.componentfactory</groupId>
<artifactId>lookup-field-flow</artifactId>
<version>1.1.1</version>
<version>1.2.0</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.vaadin.componentfactory.lookupfield;

import com.vaadin.componentfactory.theme.EnhancedDialogVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.router.Route;

import java.util.Arrays;
import java.util.List;

/**
* Basic example with setItems
*/
@Route(value = "create", layout = MainLayout.class)
public class CreateItemView extends Div {


public CreateItemView() {
LookupField<String> lookupField = new LookupField<>();
List<String> items = Arrays.asList("item1","item2", "item3");
lookupField.setDataProvider(DataProvider.ofCollection(items));
lookupField.getGrid().addColumn(s -> s).setHeader("item");
lookupField.setLabel("Item selector");
lookupField.addThemeVariants(EnhancedDialogVariant.SIZE_MEDIUM);
lookupField.addCreateItemListener(e -> Notification.show("Create item clicked"));
add(lookupField);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.vaadin.componentfactory.lookupfield;

import com.vaadin.componentfactory.lookupfield.bean.Address;
import com.vaadin.componentfactory.lookupfield.bean.Person;
import com.vaadin.componentfactory.lookupfield.bean.PersonFilter;
import com.vaadin.componentfactory.lookupfield.filter.CustomFilterPerson;
import com.vaadin.componentfactory.lookupfield.service.FilteredPersonService;
import com.vaadin.componentfactory.lookupfield.service.PersonService;
import com.vaadin.componentfactory.theme.EnhancedDialogVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.router.Route;

import java.util.Arrays;
import java.util.List;

/**
* Basic example with a binder and field validation
*/
@Route(value = "custom-filter-type", layout = MainLayout.class)
public class CustomFilterTypeView extends VerticalLayout {

private FilteredPersonService personService = new FilteredPersonService();

public CustomFilterTypeView() {
CustomFilterLookupField<Person, PersonFilter> lookupField = new CustomFilterLookupField<>( fullName -> new PersonFilter(fullName), p -> p.getLastName());
DataProvider<Person, PersonFilter> dataProvider =
DataProvider.fromFilteringCallbacks(
// First callback fetches items based on a query
query -> {
// The index of the first item to load
int offset = query.getOffset();

// The number of items to load
int limit = query.getLimit();

List<Person> persons = personService
.fetch(offset, limit, query.getFilter().orElse(null));

return persons.stream();
},
// Second callback fetches the total number of items currently in the Grid.
// The grid can then use it to properly adjust the scrollbars.
query -> personService.count(query.getFilter().orElse(null)));

lookupField.setDataProvider(dataProvider);
lookupField.setFilter(new CustomFilterPerson());
lookupField.getGrid().addColumn(s -> s).setHeader("item");
lookupField.setLabel("Select one item");
lookupField.addThemeVariants(EnhancedDialogVariant.SIZE_MEDIUM);
add(lookupField);

CustomFilterMultiSelectLookupField<Person, PersonFilter> multipleLookupField =
new CustomFilterMultiSelectLookupField<>( fullName -> new PersonFilter(fullName), p -> p.getLastName());


multipleLookupField.setDataProvider(dataProvider);
multipleLookupField.setFilter(new CustomFilterPerson());
multipleLookupField.getGrid().addColumn(s -> s).setHeader("item");
multipleLookupField.setLabel("Select Multiple items");
multipleLookupField.addThemeVariants(EnhancedDialogVariant.SIZE_MEDIUM);
add(multipleLookupField);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.vaadin.componentfactory.lookupfield;

import com.vaadin.componentfactory.lookupfield.filter.CustomFilterString;
import com.vaadin.componentfactory.theme.EnhancedDialogVariant;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.router.Route;

import java.util.Arrays;
import java.util.List;

/**
* Basic example with setItems
*/
@Route(value = "custom-filter", layout = MainLayout.class)
public class CustomFilterView extends Div {


public CustomFilterView() {
LookupField<String> lookupField = new LookupField<>();
List<String> items = Arrays.asList("item1","item2", "item3");
lookupField.setDataProvider(DataProvider.ofCollection(items));
lookupField.getGrid().addColumn(s -> s).setHeader("item");
lookupField.setLabel("Item selector");
lookupField.addThemeVariants(EnhancedDialogVariant.SIZE_MEDIUM);
lookupField.setFilter(new CustomFilterString());
add(lookupField);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ public MainLayout() {
final RouterLink binderView = new RouterLink("Binder example", BinderView.class);
final RouterLink customHeader = new RouterLink("Custom Header", CustomHeaderView.class);
final RouterLink enableSelectButtonView = new RouterLink("Selection Button enabled", EnableSelectButtonView.class);
final RouterLink enableMultipleButtonView = new RouterLink("Multiselect", MultipleView.class);
final RouterLink createView = new RouterLink("Create", CreateItemView.class);
final RouterLink customFilterView = new RouterLink("CustomFilter", CustomFilterView.class);
final RouterLink customFilterTypeView = new RouterLink("Filter Person", CustomFilterTypeView.class);
final VerticalLayout menuLayout = new VerticalLayout(personLookupField, simple, personLabelLookupField, i18nView,
binderView, customHeader, enableSelectButtonView);
binderView, customHeader, enableSelectButtonView, enableMultipleButtonView, createView, customFilterView,
customFilterTypeView);
addToDrawer(menuLayout);
addToNavbar(drawerToggle);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.vaadin.componentfactory.lookupfield;

import com.vaadin.componentfactory.theme.EnhancedDialogVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.router.Route;

import java.util.Arrays;
import java.util.List;

/**
* Basic example with setItems
*/
@Route(value = "multiple", layout = MainLayout.class)
public class MultipleView extends Div {


public MultipleView() {
MultiSelectLookupField<String> lookupField = new MultiSelectLookupField<>();
List<String> items = Arrays.asList("item1","item2", "item3");
lookupField.setDataProvider(DataProvider.ofCollection(items));
lookupField.getGrid().addColumn(s -> s).setHeader("item");
lookupField.setLabel("Item selector");
lookupField.addThemeVariants(EnhancedDialogVariant.SIZE_MEDIUM);
add(lookupField);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.vaadin.componentfactory.lookupfield.bean;

import org.apache.commons.lang3.StringUtils;

/**
* @author jcgueriaud
*/
public class PersonFilter {

private String firstName = "";
private String fullName = "";
private String lastName = "";

public PersonFilter(String fullName) {
this.fullName = fullName;
}

public PersonFilter(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public PersonFilter setFirstName(String firstName) {
this.firstName = firstName;
return this;
}

public String getLastName() {
return lastName;
}

public PersonFilter setLastName(String lastName) {
this.lastName = lastName;
return this;
}

public String getFullName() {
return fullName;
}

@Override
public String toString() {
return StringUtils.trim(firstName + " " + lastName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.vaadin.componentfactory.lookupfield.filter;

import com.vaadin.componentfactory.lookupfield.LookupFieldFilter;
import com.vaadin.componentfactory.lookupfield.LookupFieldFilterAction;
import com.vaadin.componentfactory.lookupfield.bean.PersonFilter;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.TextField;

/**
* @author jcgueriaud
*/
public class CustomFilterPerson implements LookupFieldFilter<PersonFilter> {

private final HorizontalLayout layout = new HorizontalLayout();
private final TextField firstNameField;
private final TextField lastNameField;

private LookupFieldFilterAction<PersonFilter> fieldFilterAction;

public CustomFilterPerson() {
layout.setDefaultVerticalComponentAlignment(FlexComponent.Alignment.BASELINE);
firstNameField = new TextField("First name");
lastNameField = new TextField("Last name");
layout.addAndExpand(firstNameField);
layout.addAndExpand(lastNameField);
Button filter = new Button("Filter");
filter.addClickListener(e -> {
if (fieldFilterAction != null) {
fieldFilterAction.filter(new PersonFilter(firstNameField.getValue(), lastNameField.getValue()));
}
});
layout.add(filter);
}

@Override
public Component getComponent() {
return layout;
}

@Override
public void setFilterAction(LookupFieldFilterAction<PersonFilter> fieldFilterAction) {
this.fieldFilterAction = fieldFilterAction;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.vaadin.componentfactory.lookupfield.filter;

import com.vaadin.componentfactory.lookupfield.LookupFieldFilter;
import com.vaadin.componentfactory.lookupfield.LookupFieldFilterAction;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.TextField;

/**
* @author jcgueriaud
*/
public class CustomFilterString implements LookupFieldFilter<String> {

private final HorizontalLayout layout = new HorizontalLayout();
private final TextField filterField;

private LookupFieldFilterAction<String> fieldFilterAction;

public CustomFilterString() {
layout.setDefaultVerticalComponentAlignment(FlexComponent.Alignment.BASELINE);
filterField = new TextField("filter");
layout.addAndExpand(filterField);
Button filter = new Button("Filter");
filter.addClickListener(e -> {
if (fieldFilterAction != null) {
fieldFilterAction.filter(filterField.getValue());
}
});
layout.add(filter);
}

@Override
public Component getComponent() {
return layout;
}

@Override
public void setFilterAction(LookupFieldFilterAction<String> fieldFilterAction) {
this.fieldFilterAction = fieldFilterAction;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.vaadin.componentfactory.lookupfield.service;

import com.vaadin.componentfactory.lookupfield.bean.Person;
import com.vaadin.componentfactory.lookupfield.bean.PersonFilter;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

public class FilteredPersonService {
private PersonData personData = new PersonData();

public List<Person> fetch(int offset, int limit, PersonFilter filter) {
int end = offset + limit;
List<Person> collect = personData.getPersons().stream().filter(person -> filter(person, filter)).collect(Collectors.toList());
int size = collect.size();
if (size <= end) {
end = size;
}
return collect.subList(offset, end);
}

private boolean filter(Person person, PersonFilter filter) {
boolean result = true;
if (filter == null || (StringUtils.isEmpty(filter.getLastName()) && StringUtils.isEmpty(filter.getFirstName()))) {
return true;
}
if ((StringUtils.isEmpty(filter.getLastName()) && StringUtils.isEmpty(filter.getFirstName()))) {
return person.toString().contains(filter.getFullName());
} else {
return person.getLastName().contains(filter.getLastName()) && person.getFirstName().contains(filter.getFirstName());
}
}

public int count(PersonFilter filter) {
return (int) personData.getPersons().stream().filter(person -> filter(person, filter)).count();
}

}
Loading

0 comments on commit 6b3e624

Please sign in to comment.