-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from vaadin-component-factory/feature/customfil…
…tertype Custom Filter and multiSelect
- Loading branch information
Showing
19 changed files
with
1,504 additions
and
639 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
30 changes: 30 additions & 0 deletions
30
...field-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/CreateItemView.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,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); | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/CustomFilterTypeView.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,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); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...eld-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/CustomFilterView.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,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); | ||
} | ||
|
||
} |
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
28 changes: 28 additions & 0 deletions
28
...p-field-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/MultipleView.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.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); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...ld-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/bean/PersonFilter.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,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); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...demo/src/main/java/com/vaadin/componentfactory/lookupfield/filter/CustomFilterPerson.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,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; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...demo/src/main/java/com/vaadin/componentfactory/lookupfield/filter/CustomFilterString.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,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; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
.../src/main/java/com/vaadin/componentfactory/lookupfield/service/FilteredPersonService.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,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(); | ||
} | ||
|
||
} |
Oops, something went wrong.