Open
Description
Describe your motivation
For select editor I want to use options of any type and do not want to handle String
options in ItemUpdater
. Instead I want to use a label generator for the options.
Describe the solution you'd like
I use the approach used for enums in EditColumnConfigurator
and generalize it.
/**
* Configures the column to have a select editor with the given item updater and options.
*
* @param itemUpdater the callback function that is called when item is changed. It receives two arguments:
* item and newValue.
* @param optionLabelGenerator used to get the string representation for each option
* @param options options provided for the select editor
* @param <O> the type of options
* @return the configured column
*/
public <O> Column<T> select(
ItemUpdater<T, O> itemUpdater,
Function<O, String> optionLabelGenerator,
Collection<O> options)
{
Map<String, O> optionValues = new HashMap<>();
List<String> optionLabels = new ArrayList<>();
for (O option : options)
{
String optionLabel = optionLabelGenerator.apply(option);
if (optionValues.containsKey(optionLabel))
{
throw new IllegalArgumentException("Options "
+ optionValues.get(optionLabel) + " and " + option
+ " both have the same string representation: "
+ optionLabel);
}
optionValues.put(optionLabel, option);
optionLabels.add(optionLabel);
}
ItemUpdater<T, String> wrapper = (item, newOptionLabel) ->
itemUpdater.accept(item, optionValues.get(newOptionLabel));
return select(wrapper, optionLabels);
}
Describe alternatives you've considered
#7409 is a solution only for enum type, but with a bigger interface, and is included in this solution.
Additional context
No response