Is there possibility to set filter like sql “distinct” for ComboBox container data source ?
I have table binded to IndexContainer and filters with ComboBox components that are using same container by setContainerDataSource + setItemCaptionPropertyId and by changing values in those ComboBoxes the filters applying for the IndexContainer (addContainerFilter); In some columns I have many identical fields (for example resellers in the table of client - many clients have a same reseller ), so i need to filter ComboBox data in some way, i can bind to ComboBox specific container with already filtered data, but it would be much more better to use already existing IndexContainer.
You can connect any container to combobox, but there is no “distinct” filtering built in vaadin.
I have not tried it, but you could do a DistinctContainerWrapper extends Container that would implement just the core container API and pass most of the calls back to a wrapped container. Note that you can throw UnsupportedOperationException for all the modifying operations - thus reducing the number of operations to be implemented to just few methods.
public class DistinctContainerWrapper extends IndexedContainer {
/**
*
*/
private static final long serialVersionUID = -7193772499293660624L;
public DistinctContainerWrapper(IndexedContainer src, String itemsrc ){
addContainerProperty(itemsrc, String.class, null);
ArrayList<Object> filter = new ArrayList<Object>();
int id =0;
for(Object index: src.getItemIds()){
Object ob = ((Item)src.getItem(index)).getItemProperty(itemsrc).getValue();
if ( (!filter.contains(ob.toString())) && (ob !=null)){
filter.add(ob.toString());
Item item = addItem(id);
item.getItemProperty(itemsrc).setValue(ob);
id++;
}
}
}
}
but also I need to listen to filter changes of IndexedContainer src (I want to filter values in combo boxes as if they was using the same container ), could I get some kind of listeners for this propose and add him to DistinctContainerWrapper (it is close to fireContentsChange, it possibly should be just a same instance of ValueChangeListener, am I right ? )?
Thanks.