I don’t completely understand how the getValue works in components like Table or Tree.
If they are in single select mode, then I get whatever Object corresponds to that row or node, but if it is multiselect? Do I get a list of Objects? And if it’s multiselect but there is only a value selected, do I get a list with one element or I get the value Object?
How do I know if there is more than one selected row or node? Something like this would be a really bad solution for many reasons:
if (table.isMultiSelect())
Collection<Object> values = (Set<Object>) table.getValue();
else
Object value = table.getValue();
Hi,
If you look at the getValue method of the i.e Table :
Incase of single select you will get only one Item of the Type you set the value with.
Incase of multiselect you wil get a List of objects. no matter if none,one or many items are selected.
so your can override the method and always return a collection incase your application requires that.
final Object retValue = super.getValue();
if (isMultiSelect()) {
// If the return value is not a set
if (retValue == null) {
return new HashSet<Object>();
}
if (retValue instanceof Set) {
return Collections.unmodifiableSet((Set<?>) retValue);
} else if (retValue instanceof Collection) {
return new HashSet<Object>((Collection<?>) retValue);
} else {
final Set<Object> s = new HashSet<Object>();
if (items.containsId(retValue)) {
s.add(retValue);
}
return s;
}
} else {
return retValue;
}