Skip to content
This repository was archived by the owner on Jul 29, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>org.vaadin.addons</groupId>
<artifactId>vaadin-combobox-multiselect-root</artifactId>
<packaging>pom</packaging>
<version>2.9-SNAPSHOT</version>
<version>2.9</version>
<name>ComboBoxMultiselect Add-on Project</name>

<prerequisites>
Expand Down
4 changes: 2 additions & 2 deletions vaadin-combobox-multiselect-addon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>org.vaadin.addons</groupId>
<artifactId>vaadin-combobox-multiselect</artifactId>
<packaging>jar</packaging>
<version>2.9-SNAPSHOT</version>
<version>2.9</version>
<name>ComboBoxMultiselect Add-on</name>

<prerequisites>
Expand Down Expand Up @@ -113,7 +113,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadoc</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
*/
@SuppressWarnings("serial")
public class ComboBoxMultiselect<T> extends AbstractMultiSelect<T>
implements FieldEvents.BlurNotifier, FieldEvents.FocusNotifier, HasFilterableDataProvider<T, String> {
implements FieldEvents.BlurNotifier, FieldEvents.FocusNotifier, HasFilterableDataProvider<T, String> {

public static final Integer DEFAULT_PAGE_LENGTH = 10;

Expand Down Expand Up @@ -174,7 +174,7 @@ public void setFilter(final String filterText) {
@Override
public void updateSelection(final Set<String> selectedItemKeys, final Set<String> deselectedItemKeys, final boolean sortingNeeded) {
ComboBoxMultiselect.this.updateSelection(getItemsForSelectionChange(selectedItemKeys), getItemsForSelectionChange(deselectedItemKeys), true,
sortingNeeded);
sortingNeeded);
}

private Set<T> getItemsForSelectionChange(final Set<String> keys) {
Expand Down Expand Up @@ -248,6 +248,10 @@ public void clear(final String filter) {

private String currentFilterText;

private boolean showSelectedOnTop;

private boolean orderByCaption;

private SerializableConsumer<String> filterSlot = filter -> {
// Just ignore when neither setDataProvider nor setItems has been called
};
Expand Down Expand Up @@ -308,6 +312,9 @@ public ComboBoxMultiselect(final String caption, final Collection<T> options) {
* Initialize the ComboBoxMultiselect with default settings and register client to server RPC implementation.
*/
private void init() {
this.showSelectedOnTop = true;
this.orderByCaption = true;

registerRpc(this.rpc);
registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent));

Expand Down Expand Up @@ -611,7 +618,7 @@ public NewItemHandler getNewItemHandler() {
@Override
public Registration addValueChangeListener(final HasValue.ValueChangeListener<Set<T>> listener) {
return addSelectionListener(event -> listener
.valueChange(new ValueChangeEvent<>(event.getComponent(), this, event.getOldValue(), event.isUserOriginated())));
.valueChange(new ValueChangeEvent<>(event.getComponent(), this, event.getOldValue(), event.isUserOriginated())));
}

@Override
Expand Down Expand Up @@ -672,7 +679,7 @@ protected T readItem(final Element child, final Set<T> selected, final DesignCon
}
else {
throw new IllegalStateException(String.format("Don't know how " + "to set style using current style generator '%s'", styleGenerator.getClass()
.getName()));
.getName()));
}
}
return item;
Expand Down Expand Up @@ -708,18 +715,30 @@ protected <F> SerializableConsumer<F> internalSetDataProvider(final DataProvider
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 flag is set to true check if one of the two objects to compare is selected.
//If so, set the selected one above the not selected.
//If this flag is false, do nothing and leave them as they are.
if (showSelectedOnTop) {
final boolean selected1 = this.sortingSelection.contains(o1);
final boolean selected2 = this.sortingSelection.contains(o2);

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

//If no one of the both entries is selected and this flag is set, sort them by their caption.
//If flag is not set, leave the order as it is passed through the setItems(...) method.
if (orderByCaption) {
return getItemCaptionGenerator().apply(o1)
.compareToIgnoreCase(getItemCaptionGenerator().apply(o2));
}

return getItemCaptionGenerator().apply(o1)
.compareToIgnoreCase(getItemCaptionGenerator().apply(o2));
//If no sorting was done, return 0 to change nothing in the order
return 0;
});
}

Expand All @@ -739,9 +758,9 @@ protected <F> SerializableConsumer<F> internalSetDataProvider(final DataProvider
*/
public void setDataProvider(final FetchItemsCallback<T> fetchItems, final SerializableToIntFunction<String> sizeCallback) {
setDataProvider(new CallbackDataProvider<>(q -> fetchItems.fetchItems(q.getFilter()
.orElse(""), q.getOffset(), q.getLimit()),
.orElse(""), q.getOffset(), q.getLimit()),
q -> sizeCallback.applyAsInt(q.getFilter()
.orElse(""))));
.orElse(""))));
}

/**
Expand Down Expand Up @@ -876,6 +895,7 @@ private void updateSelection(final SerializableConsumer<Collection<T>> handler,

fireEvent(new MultiSelectionEvent<>(this, oldSelection, userOriginated));

getDataCommunicator().reset();
getDataProvider().refreshAll();
}

Expand Down Expand Up @@ -961,4 +981,12 @@ public void setClearButtonCaption(final String clearButtonCaption) {
public void setSelectAllButtonCaption(final String selectAllButtonCaption) {
getState().selectAllButtonCaption = selectAllButtonCaption;
}

public void showSelectedOnTop(final boolean showSelectedOnTop) {
this.showSelectedOnTop = showSelectedOnTop;
}

public void orderByCaption(final boolean orderByCaption) {
this.orderByCaption = orderByCaption;
}
}
2 changes: 1 addition & 1 deletion vaadin-combobox-multiselect-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>org.vaadin.addons</groupId>
<artifactId>vaadin-combobox-multiselect-demo</artifactId>
<packaging>war</packaging>
<version>2.9-SNAPSHOT</version>
<version>2.9</version>
<name>ComboBoxMultiselect Add-on Demo</name>

<prerequisites>
Expand Down