reading table selected items after filter changed - SWT

View: New views
5 Messages — Rating Filter:   Alert me  

reading table selected items after filter changed - SWT

by Matt P :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have a FilterList feeding an EventTableViewer for a SWT table.  The filterlist is connected to a TextWidgetMatcherEditor and a Text widget.   Everything works as normal.

I also have a detail area on my UI, so that when there is an item selected in the table, the details of that item is shown in the detail area.  

The problem is when there is an item already selected in the table, and then I type stuff into the filter text widget, the selected item possibly will disappear from the table.  At this point I would like to then clear the detail area since the corresponding selected item is no longer visible in the table.

So I thought I would add a listener to the fitlerList.  

                filterList.addListEventListener( new ListEventListener<Employee>() {
                        public void listChanged(ListEvent<Employee> event) {
                                //what do I do here to determine if the selected item is now not in this list?
                        }
                });


However I am getting thoroughly confused on how the listChanged() method  and the ListEvents are supposed to work.  I can't find much in the docs or searching to solve this...
Is there a better way of doing this?

Thanks

re: reading table selected items after filter changed - SWT

by Kevin Day-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
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:
 

public class LastSelectedElementCalculation<E> extends AbstractCalculation<E> implements ListSelectionListener{

    private final EventSelectionModel<E> eventSelectionModel;
    private final EventList<E> source;
    private final E defaultValue;
   
    public LastSelectedElementCalculation(EventSelectionModel<E> eventSelectionModel, EventList<E> source, E defaultValue) {
        super(defaultValue);
        this.eventSelectionModel = eventSelectionModel;
        this.eventSelectionModel.addListSelectionListener(this);
        this.source = source;
        this.defaultValue = defaultValue;
       
        recalculate();
    }

    public void dispose() {
        eventSelectionModel.removeListSelectionListener(this);
    }

    private void recalculate(){
        final E oldValue = getValue();
        boolean selected = eventSelectionModel.isSelectedIndex(eventSelectionModel.getAnchorSelectionIndex());
        setValue(selected ? source.get(eventSelectionModel.getAnchorSelectionIndex()) : defaultValue);
        final E newValue = getValue();
        fireValueChange(oldValue, newValue);
    }
   
    public void valueChanged(ListSelectionEvent e) {
        recalculate();
    }


}

 
----------------------- Original Message -----------------------
  
From: Matt P blyefd@...
Cc: 
Date: Fri, 22 May 2009 10:41:51 -0700 (PDT)
Subject: reading table selected items after filter changed - SWT
  

Hi,

I have a FilterList feeding an EventTableViewer for a SWT table.  The
filterlist is connected to a TextWidgetMatcherEditor and a Text widget.  
Everything works as normal.

I also have a detail area on my UI, so that when there is an item selected
in the table, the details of that item is shown in the detail area.  

The problem is when there is an item already selected in the table, and then
I type stuff into the filter text widget, the selected item possibly will
disappear from the table.  At this point I would like to then clear the
detail area since the corresponding selected item is no longer visible in
the table.

So I thought I would add a listener to the fitlerList.  

        filterList.addListEventListener( new ListEventListener<Employee>() {
            public void list Changed(ListEvent<Employee> event) {
                //what do I do here to determine if the selected item is now not in this
list?
            }
        });


However I am getting thoroughly confused on how the listChanged() method
and the ListEvents are supposed to work.  I can't find much in the docs or
searching to solve this...
Is there a better way of doing this?

Thanks
--
View this message in context: http://www.nabble.com/reading-table-selected-items-after-filter-changed---SWT-tp23675106p23675106.html
Sent from the GlazedLists - User mailing list archive at Nabble.com.


---------- -----------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


--------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@...

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

by Matt P :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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:
 

re[3]: reading table selected items after filter changed - SWT

by Kevin Day-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Cool.
 
As an FYI, there is already a Calculation implementation that will get you the first element of any list (see Calculations.elementAt() ).
 
That works great if your list is single selection.
 
My funky code is useful for providing an intuitive experience in the multi-select case - but, as you say, it does rely on the list selection model.  I suppose another (diametrically opposite) option would be to include Calculations.oneElement() in the mix and not show any detail view if multiple items are selected.
 
- K
 
----------------------- Original Message -----------------------
  
From: Matt P blyefd@...
Cc: 
Date: Fri, 22 May 2009 13:14:02 -0700 (PDT)
Subject: Re: re: reading table selected items after filter changed - SWT
  

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 sel ected
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&nbsp;EventSelectionModel
> in SWT).&nbsp; I then bind the calculation result to the model that drives
> the detail&nbsp;view.&nbsp; This approach is a lot more declaritive than
> processin g list events - it makes the view and model&nbsp;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&nbsp;will be used to drive the detail view).&nbsp; Our users find
> that to be more intuitive.
> &nbsp;
> Here's a calculation that will give the last selected element (or a
> default value if there is no selected element).&nbsp; This is specific to
> EventSelectModel, so you'll need to adapt it to SWT:
> &nbsp;
>
>

--
View this message in context: http://w ww.nabble.com/reading-table-selected-items-after-filter-changed---SWT-tp23675106p23676652.html
Sent from the GlazedLists - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


--------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@...

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

by Matt P :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Doh!.  I should have looked at that Calculations class more carefully before implementing the same thing!
Yes, my list is single selection.

thanks


matt



Kevin Day wrote:
Cool.
 
As an FYI, there is already a Calculation implementation that will get you the first element of any list (see Calculations.elementAt() ).
 
That works great if your list is single selection.
 
My funky code is useful for providing an intuitive experience in the multi-select case - but, as you say, it does rely on the list selection model.  I suppose another (diametrically opposite) option would be to include Calculations.oneElement() in the mix and not show any detail view if multiple items are selected.
 
- K