« Return to Thread: reading table selected items after filter changed - SWT

Re: re: reading table selected items after filter changed - SWT

by Matt P :: Rate this Message:

Reply to Author | View in Thread

Thanks Kevin,

For SWT there was a bit of difference with the SWT EventTableViewer (there was no EventSelectionModel type of class).  But eventTableViewer does have a getSelected() method which returns an Event list of selected items.  I created a similar Calculation class which gives the first selected item in the list, as follows:

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.calculation.AbstractCalculation;
import ca.odell.glazedlists.event.ListEvent;
import ca.odell.glazedlists.event.ListEventListener;

public class FirstSelectedElementCalculation<E> extends AbstractCalculation<E> implements ListEventListener<E> {

        private final EventList<E> source;

        public FirstSelectedElementCalculation(EventList<E> source) {
                super( source.size() > 0 ? source.get(0) : null);

                this.source = source;
                this.source.addListEventListener(this);
        }

        public void dispose() {
                this.source.removeListEventListener(this);
        }

        public void listChanged(ListEvent<E> listChanges) {
                final E oldValue = getValue();
                setValue(listChanges.getSourceList().size() > 0 ? listChanges.getSourceList().get(0) : null);
                final E newValue = getValue();
                fireValueChange(oldValue, newValue);
        }

}

Then, I added the following code to link the eventTableViewer selected eventList to this calculation:

new FirstSelectedElementCalculation<Employee> (eventTableViewer.getSelected())
        .addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent event) {
                        //here I bind ((Employee)event.getNewValue()) to my detail view model.
                }
        }
);

 



Kevin Day wrote:

I use a Calculation against the EventSelectionModel (I know that's the Swing world, but there's probably an analog for EventSelectionModel in SWT).  I then bind the calculation result to the model that drives the detail view.  This approach is a lot more declaritive than processing list events - it makes the view and model code nice and clean.
You could use a Calculation that returns the first value in the list selection, but I find that I often want to base my concept of the current selected individual item based on the last element of the list selected (that way if more than one item is picked, the last one actually selected will be used to drive the detail view).  Our users find that to be more intuitive.
 
Here's a calculation that will give the last selected element (or a default value if there is no selected element).  This is specific to EventSelectModel, so you'll need to adapt it to SWT:
 

 « Return to Thread: reading table selected items after filter changed - SWT