GlazedLists FilterList

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

GlazedLists FilterList

lumo
i wrote a simple program to test FilterList

i created a class (Item)
then create a list of these items.
on top of this list i create a filterList and a MatcherEditor (which reads an filter value from my main class)
this is working fine when i set the filter before i create the list.
(obviously because the matchereditor does not get any information about changes on this filter.)

how can i notify the fillterlist to re-filter the list?


thanks in advance!

here the simple program:
------------------------------------------------------
import java.util.List;

import org.eclipse.emf.common.util.BasicEList;

import test.Item;
import test.TestFactory;
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.ObservableElementList;
import ca.odell.glazedlists.ObservableElementList.Connector;
import ca.odell.glazedlists.TransformedList;
import ca.odell.glazedlists.matchers.Matcher;
import ca.odell.glazedlists.matchers.MatcherEditor;

public class Test {
        private List<Item> items = new BasicEList<Item>();
        private FilterList<Item> filteredList;
        private String familyName = null;

        public Test() {
                addItem("Hahn", "Franz");
                addItem("Mustermann", "Max");
                addItem("Mustermann", "Iris");
                addItem("Mustermann", "David");
                addItem("Hahn", "Irmgard");
        }

        public void setFamilyNameFilter(String filter) {
                this.familyName = filter;
        }

        public String getFamilyNameFilter() {
                return this.familyName;
        }

        private void createFilterList() {
                EventList<Item> eventList = new BasicEventList<Item>();
                TransformedList<Item, Item> objectList = GlazedLists
                                .threadSafeList(eventList);
                ObservableElementList<Item> observableElementList = new ObservableElementList<Item>(
                                objectList,
                                (Connector<? super Item>) GlazedLists.beanConnector(Item.class));
                MatcherEditor<Item> matcherEditor = new MatcherEditor<Item>() {
                        private Matcher<Item> m;

                        @Override
                        public void removeMatcherEditorListener(Listener<Item> l) {
                                System.out.println("removing listener " + l);
                        }

                        @Override
                        public Matcher<Item> getMatcher() {
                                m = new Matcher<Item>() {
                                        @Override
                                        public boolean matches(Item lv) {
                                                if (getFamilyNameFilter() == null) {
                                                        return true;
                                                }
                                                return lv.getFamilyName().equalsIgnoreCase(
                                                                getFamilyNameFilter());
                                        }
                                };
                                return m;
                        }

                        @Override
                        public void addMatcherEditorListener(Listener<Item> l) {
                                System.out.println("adding listener " + l);
                        }
                };

                FilterList<Item> filterList = new FilterList<Item>(
                                observableElementList, matcherEditor);
                filterList.addAll(items);

                this.filteredList = filterList;
        }

        private void print() {
                for (Item item : items) {
                        System.out.println(String.format("(%d) %s, %s", item.getId(),
                                        item.getFamilyName(), item.getFirstName()));
                }
        }

        private void printFiltered() {
                for (Item item : filteredList) {
                        System.out.println(String.format("(%d) %s, %s", item.getId(),
                                        item.getFamilyName(), item.getFirstName()));
                }
        }

        private Item addItem(String familyName, String firstName) {
                Item i = TestFactory.eINSTANCE.createItem();
                i.setId(items.size());
                i.setFamilyName(familyName);
                i.setFirstName(firstName);
                items.add(i);
                return i;
        }

        public static void main(String[] args) {
                Test t = new Test();
                t.createFilterList();
                t.setFamilyNameFilter("Mustermann");
                t.print();
                System.out.println("---");
                t.printFiltered();
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: GlazedLists FilterList

Rob Eden
If you are filtering based on a value that changes, you need to implement MatcherEditor, not just Matcher. That will give you listeners to which you can fire a notification of the update. Extending AbstractMatcherEditor makes it pretty simple. So, just fire an event when your string value changes.

Rob

On Feb 26, 2013, at 5:57 AM, lumo <[hidden email]> wrote:

> i wrote a simple program to test FilterList
>
> i created a class (Item)
> then create a list of these items.
> on top of this list i create a filterList and a MatcherEditor (which reads
> an filter value from my main class)
> this is working fine when i set the filter before i create the list.
> (obviously because the matchereditor does not get any information about
> changes on this filter.)
>
> how can i notify the fillterlist to re-filter the list?
>
>
> thanks in advance!
>
> here the simple program:
> ------------------------------------------------------
> import java.util.List;
>
> import org.eclipse.emf.common.util.BasicEList;
>
> import test.Item;
> import test.TestFactory;
> import ca.odell.glazedlists.BasicEventList;
> import ca.odell.glazedlists.EventList;
> import ca.odell.glazedlists.FilterList;
> import ca.odell.glazedlists.GlazedLists;
> import ca.odell.glazedlists.ObservableElementList;
> import ca.odell.glazedlists.ObservableElementList.Connector;
> import ca.odell.glazedlists.TransformedList;
> import ca.odell.glazedlists.matchers.Matcher;
> import ca.odell.glazedlists.matchers.MatcherEditor;
>
> public class Test {
> private List<Item> items = new BasicEList<Item>();
> private FilterList<Item> filteredList;
> private String familyName = null;
>
> public Test() {
> addItem("Hahn", "Franz");
> addItem("Mustermann", "Max");
> addItem("Mustermann", "Iris");
> addItem("Mustermann", "David");
> addItem("Hahn", "Irmgard");
> }
>
> public void setFamilyNameFilter(String filter) {
> this.familyName = filter;
> }
>
> public String getFamilyNameFilter() {
> return this.familyName;
> }
>
> private void createFilterList() {
> EventList<Item> eventList = new BasicEventList<Item>();
> TransformedList<Item, Item> objectList = GlazedLists
> .threadSafeList(eventList);
> ObservableElementList<Item> observableElementList = new
> ObservableElementList<Item>(
> objectList,
> (Connector<? super Item>) GlazedLists.beanConnector(Item.class));
> MatcherEditor<Item> matcherEditor = new MatcherEditor<Item>() {
> private Matcher<Item> m;
>
> @Override
> public void removeMatcherEditorListener(Listener<Item> l) {
> System.out.println("removing listener " + l);
> }
>
> @Override
> public Matcher<Item> getMatcher() {
> m = new Matcher<Item>() {
> @Override
> public boolean matches(Item lv) {
> if (getFamilyNameFilter() == null) {
> return true;
> }
> return lv.getFamilyName().equalsIgnoreCase(
> getFamilyNameFilter());
> }
> };
> return m;
> }
>
> @Override
> public void addMatcherEditorListener(Listener<Item> l) {
> System.out.println("adding listener " + l);
> }
> };
>
> FilterList<Item> filterList = new FilterList<Item>(
> observableElementList, matcherEditor);
> filterList.addAll(items);
>
> this.filteredList = filterList;
> }
>
> private void print() {
> for (Item item : items) {
> System.out.println(String.format("(%d) %s, %s", item.getId(),
> item.getFamilyName(), item.getFirstName()));
> }
> }
>
> private void printFiltered() {
> for (Item item : filteredList) {
> System.out.println(String.format("(%d) %s, %s", item.getId(),
> item.getFamilyName(), item.getFirstName()));
> }
> }
>
> private Item addItem(String familyName, String firstName) {
> Item i = TestFactory.eINSTANCE.createItem();
> i.setId(items.size());
> i.setFamilyName(familyName);
> i.setFirstName(firstName);
> items.add(i);
> return i;
> }
>
> public static void main(String[] args) {
> Test t = new Test();
> t.createFilterList();
> t.setFamilyNameFilter("Mustermann");
> t.print();
> System.out.println("---");
> t.printFiltered();
> }
> }
>
>
>
> --
> View this message in context: http://glazedlists.1045722.n5.nabble.com/GlazedLists-FilterList-tp5709877.html
> Sent from the GlazedLists - User mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: GlazedLists FilterList

Rob Eden
Whoops, sorry read your test a little closer. Looks like you are sort of implementing MatcherEditor, but you're not actually implementing the listener methods or firing updates. Extends AbstractMatcherEditor and call fireChanged(…) with the new matcher when your string value changes.

Rob

On Feb 26, 2013, at 11:40 AM, Rob Eden <[hidden email]> wrote:

> If you are filtering based on a value that changes, you need to implement MatcherEditor, not just Matcher. That will give you listeners to which you can fire a notification of the update. Extending AbstractMatcherEditor makes it pretty simple. So, just fire an event when your string value changes.
>
> Rob
>
> On Feb 26, 2013, at 5:57 AM, lumo <[hidden email]> wrote:
>
>> i wrote a simple program to test FilterList
>>
>> i created a class (Item)
>> then create a list of these items.
>> on top of this list i create a filterList and a MatcherEditor (which reads
>> an filter value from my main class)
>> this is working fine when i set the filter before i create the list.
>> (obviously because the matchereditor does not get any information about
>> changes on this filter.)
>>
>> how can i notify the fillterlist to re-filter the list?
>>
>>
>> thanks in advance!
>>
>> here the simple program:
>> ------------------------------------------------------
>> import java.util.List;
>>
>> import org.eclipse.emf.common.util.BasicEList;
>>
>> import test.Item;
>> import test.TestFactory;
>> import ca.odell.glazedlists.BasicEventList;
>> import ca.odell.glazedlists.EventList;
>> import ca.odell.glazedlists.FilterList;
>> import ca.odell.glazedlists.GlazedLists;
>> import ca.odell.glazedlists.ObservableElementList;
>> import ca.odell.glazedlists.ObservableElementList.Connector;
>> import ca.odell.glazedlists.TransformedList;
>> import ca.odell.glazedlists.matchers.Matcher;
>> import ca.odell.glazedlists.matchers.MatcherEditor;
>>
>> public class Test {
>> private List<Item> items = new BasicEList<Item>();
>> private FilterList<Item> filteredList;
>> private String familyName = null;
>>
>> public Test() {
>> addItem("Hahn", "Franz");
>> addItem("Mustermann", "Max");
>> addItem("Mustermann", "Iris");
>> addItem("Mustermann", "David");
>> addItem("Hahn", "Irmgard");
>> }
>>
>> public void setFamilyNameFilter(String filter) {
>> this.familyName = filter;
>> }
>>
>> public String getFamilyNameFilter() {
>> return this.familyName;
>> }
>>
>> private void createFilterList() {
>> EventList<Item> eventList = new BasicEventList<Item>();
>> TransformedList<Item, Item> objectList = GlazedLists
>> .threadSafeList(eventList);
>> ObservableElementList<Item> observableElementList = new
>> ObservableElementList<Item>(
>> objectList,
>> (Connector<? super Item>) GlazedLists.beanConnector(Item.class));
>> MatcherEditor<Item> matcherEditor = new MatcherEditor<Item>() {
>> private Matcher<Item> m;
>>
>> @Override
>> public void removeMatcherEditorListener(Listener<Item> l) {
>> System.out.println("removing listener " + l);
>> }
>>
>> @Override
>> public Matcher<Item> getMatcher() {
>> m = new Matcher<Item>() {
>> @Override
>> public boolean matches(Item lv) {
>> if (getFamilyNameFilter() == null) {
>> return true;
>> }
>> return lv.getFamilyName().equalsIgnoreCase(
>> getFamilyNameFilter());
>> }
>> };
>> return m;
>> }
>>
>> @Override
>> public void addMatcherEditorListener(Listener<Item> l) {
>> System.out.println("adding listener " + l);
>> }
>> };
>>
>> FilterList<Item> filterList = new FilterList<Item>(
>> observableElementList, matcherEditor);
>> filterList.addAll(items);
>>
>> this.filteredList = filterList;
>> }
>>
>> private void print() {
>> for (Item item : items) {
>> System.out.println(String.format("(%d) %s, %s", item.getId(),
>> item.getFamilyName(), item.getFirstName()));
>> }
>> }
>>
>> private void printFiltered() {
>> for (Item item : filteredList) {
>> System.out.println(String.format("(%d) %s, %s", item.getId(),
>> item.getFamilyName(), item.getFirstName()));
>> }
>> }
>>
>> private Item addItem(String familyName, String firstName) {
>> Item i = TestFactory.eINSTANCE.createItem();
>> i.setId(items.size());
>> i.setFamilyName(familyName);
>> i.setFirstName(firstName);
>> items.add(i);
>> return i;
>> }
>>
>> public static void main(String[] args) {
>> Test t = new Test();
>> t.createFilterList();
>> t.setFamilyNameFilter("Mustermann");
>> t.print();
>> System.out.println("---");
>> t.printFiltered();
>> }
>> }
>>
>>
>>
>> --
>> View this message in context: http://glazedlists.1045722.n5.nabble.com/GlazedLists-FilterList-tp5709877.html
>> Sent from the GlazedLists - User mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: GlazedLists FilterList

lumo
thanks for your second reply :)

i looked at this abstract class, and decided to take its parent (AbstractMatchereEditorListenerSupport)

here is my resulting class:

import test.Item;
import ca.odell.glazedlists.matchers.AbstractMatcherEditorListenerSupport;
import ca.odell.glazedlists.matchers.Matcher;

public class ItemMatcher extends AbstractMatcherEditorListenerSupport<Item> {

        private Matcher<Item> currentMatcher;
        private String familyNameFilter;

        @Override
        public Matcher<Item> getMatcher() {
                if (currentMatcher == null) {
                        currentMatcher = new Matcher<Item>() {
                                @Override
                                public boolean matches(Item lv) {
                                        if (familyNameFilter == null) {
                                                return true;
                                        }
                                        return lv.getFamilyName()
                                                        .equalsIgnoreCase(familyNameFilter);
                                }
                        };
                }
                return currentMatcher;
        }

        public void setFilter(String filter) {
                familyNameFilter = filter;
                fireChangedMatcher(createChangedEvent(getMatcher()));
        }
}

in my Test class i simply use:

        public void setFamilyNameFilter(String filter) {
                this.familyName = filter;
                matcherEditor.setFilter(filter);
        }

which is now working fine!

thanks a lot for your help!
Reply | Threaded
Open this post in threaded view
|

Re: GlazedLists FilterList

lumo
This post has NOT been accepted by the mailing list yet.
In reply to this post by Rob Eden
next question is coming up.
when i change my original list, which is base for the FilterList
eg: i add an item to it.
how can i notify the filterList that the original list changed?
fireChangedMatcher will not fit here...
Reply | Threaded
Open this post in threaded view
|

Re: GlazedLists FilterList

James Lemieux
In reply to this post by lumo
For reference to future peoples reading this thread:

You can watch a screencast on implementing customer MatcherEditors here:

http://publicobject.com/glazedlistsdeveloper/


On Tue, Feb 26, 2013 at 10:25 PM, lumo <[hidden email]> wrote:
thanks for your second reply :)

i looked at this abstract class, and decided to take its parent
(AbstractMatchereEditorListenerSupport)

here is my resulting class:

import test.Item;
import ca.odell.glazedlists.matchers.AbstractMatcherEditorListenerSupport;
import ca.odell.glazedlists.matchers.Matcher;

public class ItemMatcher extends AbstractMatcherEditorListenerSupport<Item>
{

        private Matcher<Item> currentMatcher;
        private String familyNameFilter;

        @Override
        public Matcher<Item> getMatcher() {
                if (currentMatcher == null) {
                        currentMatcher = new Matcher<Item>() {
                                @Override
                                public boolean matches(Item lv) {
                                        if (familyNameFilter == null) {
                                                return true;
                                        }
                                        return lv.getFamilyName()
                                                        .equalsIgnoreCase(familyNameFilter);
                                }
                        };
                }
                return currentMatcher;
        }

        public void setFilter(String filter) {
                familyNameFilter = filter;
                fireChangedMatcher(createChangedEvent(getMatcher()));
        }
}

in my Test class i simply use:

        public void setFamilyNameFilter(String filter) {
                this.familyName = filter;
                matcherEditor.setFilter(filter);
        }

which is now working fine!

thanks a lot for your help!



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