how to handle empty selection in a jface bound combobox?

Posted by guido on Stack Overflow See other posts from Stack Overflow or by guido
Published on 2012-09-27T02:17:03Z Indexed on 2012/09/27 3:37 UTC
Read the original article Hit count: 192

I am developing a search dialog in my eclipse-rcp application.

In the search dialog I have a combobox as follows:

comboImp = new CCombo(grpColSpet, SWT.BORDER | SWT.READ_ONLY);
comboImp.setBounds(556, 46, 184, 27);
comboImpViewer = new ComboViewer(comboImp);
comboImpViewer.setContentProvider(new ArrayContentProvider());
comboImpViewer.setInput(ImpContentProvider.getInstance().getImps());
comboImpViewer.setLabelProvider(new LabelProvider() {
    @Override
    public String getText(Object element) {
        return ((Imp)element).getImpName();
    }
});

Imp is a database entity, ManyToOne to the main entity which is searched, and ImpContentProvider is the model class which speaks to embedded sqlite database via jpa/hibernate.

This combobox is supposed to contain all instances of Imp, but to also let empty selection; it's value is bound to a service bean as follows:

IObservableValue comboImpSelectionObserveWidget = 
    ViewersObservables.observeSingleSelection(comboImpViewer);
IObservableValue filterByImpObserveValue = 
    BeansObservables.observeValue(searchPrep, "imp");
bindingContext.bindValue(comboImpSelectionObserveWidget, filterByImpObserveValue
    , null, null);

As soon as the user clicks on the combo, a selection (first element) is made: I can see the call to a selectionlistener i added on the viewer. My question is:

  • after a selection has been made, how do I let the user change his mind and have an empty selection in the combobox? should I add a "fake" empty instance of Imp to the List returned by the ImpContentProvider? or should I implement an alternative to ArrayContentProvider?

and one additional related question is:

  • why calling deselectAll() and clearSelection() on the combo does NOT set a null value to the bound bean?

© Stack Overflow or respective owner

Related posts about java

Related posts about data-binding