Dear Team, I found something glitchy (bug!?) in the component. The Use C

Dear Team,

I found something glitchy (bug!?) in the component.

The Use Case is, I have 2 MultiselectComboBox. The available values of 2nd depends on selected values of 1st.

I added a valueChangeListner for 1st MsCB by which I get all selected values of 1st and add a filter to 2nd MsCB dataprovider.

Then I make a intersection set of existing selected values of 2nd MsCB and selected 1 MsCB and set it back to 2nd MsCB.

This works perfectly well unless intersected values are empty. In the odd case mentioned, the values existing on 2nd MsCB won’t change even after value changes in 1st MsCB.

I’m making sure I’m not using null to setValue. And tried with setValue with MsCB.getEmptyValue() also, but doesn’t seem to work.

Tried with version: 2.0.0, 2.4.1, and 2.5.0 all are resulting in same glitch.

MultiselectComboBox<ABC> first = new MultiselectComboBox<>();
MultiselectComboBox<ABC> second = new MultiselectComboBox<>();

List<ABC> fullList = getDataFromAPI(); // 

first.setItems(fullList);
second.setItems(fullList);

first.addValueChangeListener(event -> {
	logger.debug("Triggered valueChangeListnerX for first with values: " + event.getValue());
	if(event.getValue().isEmpty){
			first.setPlaceholder("All");
		} else {
			first.setPlaceholder("Choose..");
		}
});

second.addValueChangeListener(event -> {
	logger.debug("Triggered valueChangeListnerY for second with values: " + event.getValue());
	if(event.getValue().isEmpty){
			second.setPlaceholder("All");
			second.setCompactMode(false);
		} else {
			second.setCompactMode(true);
		}
});

first.addValueChangeListener(event -> {
	logger.debug("Triggered valueChangeListnerZ for first with values: " + event.getValue());
	Set<ABC> availableForSelection = new HashSet<>();
	if (first.getValue().isEmpty()) {
		availableForSelection = new HashSet<ABC>(fullList);
	} else {
		availableForSelection = first.getValue();
	}

	Set<ABC> existingSelectionAtSecond = second.getValue();
	existingSelectionAtSecond.retainAll(availableForSelection);
	second.setValue(existingSelectionAtSecond);
	// Above line doesn't trigger valuChangeListner for second!
	
	Map<Long, ABC> availableForSelectionIdMap = availableForSelection.stream()
				.collect(Collectors.toMap(i -> i.getId(), i -> i));

	@SuppressWarnings("unchecked")
	ListDataProvider<ABC> dataProvider = (ListDataProvider<ABC>) second.getDataProvider();
	dataProvider.setFilter(value -> {
		return availableForSelectionIdMap.containsKey(value.getId());
	});
});

Hi Mohan,

Thanks for the detailed example. Could you please share the code for the the ABC class or say whether you override hashCode and equals in that class?

Also, if possible, it would help if you share a minimal reproducible example that will help investigate this potential issue easier.

BR,

Goran

Hi Goran,

The class ABC would look something like below;

public class ABC implements Serializable {

  /**
   *
   */
  private static final long serialVersionUID = -8182082074838675004L;

  private Long id;
  private String name;
  
  // getters and setters
  // toSting
}

hashcode and equals are not overrided.

Regards,
Mohan

Hi,

Thanks for the info. I suggest you override hashCode and equals, as I suspect the issue is likely due to that and not related to the component itself.

BR,

Goran

Hi Goran,

The issue is arising only when the setValue(--) has empty set. I don’t really think if it’s because of hashCode and equals.
Please try the above code sample once and let me know if you can reproduce the issue.

Regards,
Mohan