Ive the foolowing code. I need when user hovers over field he gets description. I ve got it but when I add another field description keeps the same for all others.
public class TokenField<TOKEN extends Tokenizable, ITEM> extends CustomField<Collection> implements HasValue<Collection> {
private ExtTokenField field;
private ComboBox comboBox;
private Function<ITEM, TOKEN> tokenConverter = f → (TOKEN) f; //this for adding into token field
//This is to cheat List used in ExtTokenField
private Collection originalTokenData;
private Map<Long, TOKEN> identifierToTokenizable = new HashMap<>();
private Function<String, TOKEN> stringToTokenConverter;
private Supplier newModelSupplier = ArrayList::new;
public TokenField(String caption) {
super();
setCaption(caption);
field = new ExtTokenField();
comboBox = new ComboBoxBuilder<ITEM, ITEM>()
.withInputPrompt("TokenField.TypeHereToAdd.Message")
.build();
this.comboBox.setNewItemProvider((newItem) -> {
if (stringToTokenConverter != null) {
TOKEN tokenizable = this.stringToTokenConverter.apply(newItem);
this.addNewToken(tokenizable, this.comboBox);
field.setDescription(tokenizable.getStringValue());
}
return Optional.empty();
});
comboBox.addValueChangeListener(getComboBoxValueChange(comboBox));
field.setInputField(comboBox);
field.addTokenAction(new DeleteTokenAction());
field.setDescription(comboBox.getDescription());
// addValueChangeListener(getTokenValueChange());
}
private void addNewToken(Tokenizable item, ComboBox comboBox) {
if (item != null) {
this.addTokenizable(item);
comboBox.setValue((Object)null);
comboBox.clear();
this.field.setDescription(item.getStringValue());
}
}
public void setTextInputAllowed(Function<String, TOKEN> stringToTokenConverter) {
this.comboBox.setTextInputAllowed(true);
this.stringToTokenConverter = stringToTokenConverter;
}
@Override
protected Component initContent() {
return field;
}
public ComboBox getComboBox() {
return comboBox;
}
public void addTokenizable(Tokenizable tokenizable) {
if (!identifierToTokenizable.containsKey(tokenizable.getIdentifier())) {
identifierToTokenizable.put(tokenizable.getIdentifier(), (TOKEN) tokenizable);
originalTokenData.add((TOKEN) tokenizable);
}
field.addTokenizable(tokenizable);
field.setDescription(tokenizable.getStringValue());
}
public void removeTokenizable(Tokenizable tokenizable) {
identifierToTokenizable.remove(tokenizable.getIdentifier());
originalTokenData.remove((TOKEN) tokenizable);
field.removeTokenizable(tokenizable);
}
@Override
public Registration addValueChangeListener(ValueChangeListener<Collection> listener) {
return field.addValueChangeListener((ValueChangeListener) listener);
}
private ValueChangeListener getComboBoxValueChange(ComboBox comboBox) {
return event → {
ITEM item = event.getSource().getValue();
if (item != null) {
addTokenizable(tokenConverter.apply(item));
comboBox.setValue(null);
}
};
}
@Override
protected void doSetValue(Collection value) {
originalTokenData = value != null ? value : newModelSupplier.get();
identifierToTokenizable.clear();
originalTokenData.forEach(f → identifierToTokenizable.put(f.getIdentifier(), f));
field.setValue(new ArrayList<>(originalTokenData));
if (!CollectionUtils.isEmpty(value)){
field.setDescription(value.stream().findAny().get().getStringValue());
}
}
@Override
public Collection getValue() {
return originalTokenData;
}
@Override
public void setReadOnly(boolean readOnly) {
super.setReadOnly(readOnly);
field.setReadOnly(readOnly);
}
@Override
public Collection getEmptyValue() {
return newModelSupplier.get();
}
public void setTokenConverter(Function<ITEM, TOKEN> tokenConverter) {
this.tokenConverter = tokenConverter;
}
public void setNewModelSupplier(Supplier newModelSupplier) {
this.newModelSupplier = newModelSupplier;
}
public Supplier getNewModelSupplier() {
return newModelSupplier;
}
public class DeleteTokenAction extends TokenizableAction {
public DeleteTokenAction() {
super(“delete”, 2147483647, FontAwesome.CLOSE);
}
public void onClick(Tokenizable tokenizable) {
removeTokenizable(tokenizable);
}
}
}