FilterList and ListEvent

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

FilterList and ListEvent

naugler
Hello. New and enthusiastic user here. I apologize if this has been answered, I couldn't find a clear answer anywhere.

I have an EventList, and a FilterList that transforms that EventList. I have added a ListEventListener to the FilterList to receive ListEvent s on that FilterList.

***What ListEvent s am I supposed to see on the FilterList? I was hoping to see only ListEvent s that pertain to the FilterList itself, but I think I am seeing all ListEvent s on the underlying EventList.

I know at some point you must be using these ListEvents in a filtered manner, for swing models and such, but looking at the ListEventModel I am unable to come up with a clear idea of the context of the ListEvent.

Thank you in advance for any and all responses.
Reply | Threaded
Open this post in threaded view
|

Re: FilterList and ListEvent

James Lemieux
Dear Mr. Naugler,

Your assumption is actually the correct one. Your ListEventListener attached to the FilterList is only receiving ListEvents describing changes to the FilterList *after* the FilterList has reacted to them.

What is the purpose of your custom ListEventListener? Just to view the ListEvents coming from FilterList? Or does it contain real logic for your application?

James


On Tue, Dec 4, 2012 at 7:12 AM, naugler <[hidden email]> wrote:
Hello. New and enthusiastic user here. I apologize if this has been answered,
I couldn't find a clear answer anywhere.

I have an EventList, and a FilterList that transforms that EventList. I have
added a ListEventListener to the FilterList to receive ListEvent s on that
FilterList.

***What ListEvent s am I supposed to see on the FilterList? I was hoping to
see only ListEvent s that pertain to the FilterList itself, but I think I am
seeing all ListEvent s on the underlying EventList.

I know at some point you must be using these ListEvents in a filtered
manner, for swing models and such, but looking at the ListEventModel I am
unable to come up with a clear idea of the context of the ListEvent.

Thank you in advance for any and all responses.



--
View this message in context: http://glazedlists.1045722.n5.nabble.com/FilterList-and-ListEvent-tp5709802.html
Sent from the GlazedLists - User mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: FilterList and ListEvent

naugler
I attempted to write a test case to demonstrate my confusion, and ended up proving your claim. I guess I should have started there...

I was originally listening for ListEvent s to build a matching list of renderables on a map. I am sure there are glazedlists tricks for syncing or writing my own transformation class to accomplish something quite like this, but I'm not there yet.

Again, thank you for your help.

If anyone wants the test case, I'll post it here. It's a nice small FilterList demo:

package test;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.event.ListEvent;
import ca.odell.glazedlists.event.ListEventListener;
import ca.odell.glazedlists.matchers.Matcher;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Main {
    
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,400);
        frame.setLayout(new BorderLayout());
        final JTextArea textArea = new JTextArea();
        JButton button = new JButton("add random element");
        
        //set up lists
        final EventList<Integer> ints = new BasicEventList<Integer>();
        Matcher<Integer> posMatcher = new Matcher<Integer>() {
            @Override
            public boolean matches(Integer item) {
                return (item>0);
            }
        };
        Matcher<Integer> negMatcher = new Matcher<Integer>() {
            @Override
            public boolean matches(Integer item) {
                return (item<0);
            }
        };
        FilterList<Integer> pos = new FilterList<Integer>(ints, posMatcher);
        FilterList<Integer> neg = new FilterList<Integer>(ints, negMatcher);
        
        //add listeners
        pos.addListEventListener(new ListEventListener<Integer>() {
            @Override
            public void listChanged(ListEvent<Integer> listChanges) {
                    textArea.append("Positive "+listChanges.toString()+"\n");
            }
        });
        neg.addListEventListener(new ListEventListener<Integer>() {
            @Override
            public void listChanged(ListEvent<Integer> listChanges) {
                    textArea.append("Negative "+listChanges.toString()+"\n");
            }
        });
        
        //set up actions
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                Random r = new Random();
                int i = r.nextInt();
                textArea.append("Adding "+i+" to base list...\n");
                ints.add(i);
            }
            
        });
        
        frame.add(textArea, BorderLayout.NORTH);
        frame.add(button, BorderLayout.SOUTH);
        frame.setVisible(true);
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: FilterList and ListEvent

James Lemieux
The glazedlists trick for syncing an EventList with a Map is.... <drumroll>....

GlazedLists.syncEventListToMap(...).

Simply give it this static method the source EventList and a Function that will generate keys for each item in your source list and you're in business. No need to sync anything yourself. The Map is capable of write-through operations, so calling mutating methods will actually alter the backing EventList.

James


On Wed, Dec 5, 2012 at 6:05 AM, naugler <[hidden email]> wrote:
I attempted to write a test case to demonstrate my confusion, and ended up
proving your claim. I guess I should have started there...

I was originally listening for ListEvent s to build a matching list of
renderables on a map. I am sure there are glazedlists tricks for syncing or
writing my own transformation class to accomplish something quite like this,
but I'm not there yet.

Again, thank you for your help.

If anyone wants the test case, I'll post it here. It's a nice small
FilterList demo:





--
View this message in context: http://glazedlists.1045722.n5.nabble.com/FilterList-and-ListEvent-tp5709802p5709806.html
Sent from the GlazedLists - User mailing list archive at Nabble.com.