contribution offer: HierarchicalList

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

contribution offer: HierarchicalList

lumo
Hello!

first of all, thanks to anyone working on GlazedLists! you do a great job.

in my application i needed an HierarchicalList, but i was not able to find anything like this.
(let me know if i just wasted time and this is already possible with GlazedLists from stock)
therefore i implemented one based on GlazedLists (FilterList and UniqueList) quick and dirty.

today i refactured the code and made it more general to be able to contribute to glazedlists.

if one of the committers of GlazedLists reads this and is interested in, please leave me a message.

here a small sample program which shows its useage:

import hierarchical.glazedlists.HierarchicalContentProvider;
import hierarchical.glazedlists.HierarchicalList;

import java.util.ArrayList;
import java.util.List;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.UniqueList;

public class TestHierarchicalList {
        public static void main(String[] args) {
                // create an EventList of Songs
                final EventList<Song> songs = new BasicEventList<Song>();
                songs.add(new Song("Metallica", "Of Wolf And Man", "Metal"));
                songs.add(new Song("Eminem", "Without Me", "Rap"));
                songs.add(new Song("Britney Spears", "Oops I did it Again", "Pop"));
                songs.add(new Song("Michael Jackson", "Thriller", "Pop"));
                songs.add(new Song("Megadeth", "Rust In Peace", "Metal"));
                songs.add(new Song("MC Hammer", "U Can't Touch This", "Rap"));

                HierarchicalList<Song> hierarchicalList = new HierarchicalList<Song>(
                                songs, new HierarchicalSongContentProvider());
               
                List filter = new ArrayList();
                filter.add("Metal");
                filter.add("Metallica");
                hierarchicalList.setFilters(filter);
                printAll(hierarchicalList);
        }
       
        public static void printAll(HierarchicalList<Song> list) {
                for (int i = 0; i < list.getHierarchyCount(); i++) {
                        UniqueList<Song> hl = list.getHierarchy(i);
                        System.out.println("filtered HierarchicalList(" + i
                                        + ") with filter: " + list.getFilter(i));
                        System.out.println(hl.toString().replace("[", "\t")
                                        .replace(", ", ",\n\t").replace("]", ""));
                }
        }

        private static class HierarchicalSongContentProvider implements
                        HierarchicalContentProvider<Song> {
                @Override
                public Object getHierarchy(Song element, int hierarchy) {
                        switch (hierarchy) {
                        case 0:
                                return element.getGenre();
                        case 1:
                                return element.getArtist();
                        case 2:
                                return element.getName();
                        }
                        return element;
                }

                @Override
                public int getHierarchyCount() {
                        return 3;
                }
        }
}

and this is the generated output (to proof its working)

filtered HierarchicalList(0) with filter: []
        MC Hammer - U Can't Touch This,
        Megadeth - Rust In Peace,
        Michael Jackson - Thriller,
        Britney Spears - Oops I did it Again,
        Eminem - Without Me,
        Metallica - Of Wolf And Man
filtered HierarchicalList(1) with filter: [Metal]
        Megadeth - Rust In Peace,
        Metallica - Of Wolf And Man
filtered HierarchicalList(2) with filter: [Metal, Metallica]
        Metallica - Of Wolf And Man
Reply | Threaded
Open this post in threaded view
|

Re: contribution offer: HierarchicalList

James Lemieux
GL includes SeparatorList (one level of hierarchy for grouping) and TreeList (more general, multi-level hierarchy) transforms already.

James


On Thu, Feb 28, 2013 at 2:11 AM, lumo <[hidden email]> wrote:
Hello!

first of all, thanks to anyone working on GlazedLists! you do a great job.

in my application i needed an HierarchicalList, but i was not able to find
anything like this.
(let me know if i just wasted time and this is already possible with
GlazedLists from stock)
therefore i implemented one based on GlazedLists (FilterList and UniqueList)
quick and dirty.

today i refactured the code and made it more general to be able to
contribute to glazedlists.

*if one of the committers of GlazedLists reads this and is interested in,
please leave me a message.*

*here a small sample program which shows its useage:*

import hierarchical.glazedlists.HierarchicalContentProvider;
import hierarchical.glazedlists.HierarchicalList;

import java.util.ArrayList;
import java.util.List;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.UniqueList;

public class TestHierarchicalList {
        public static void main(String[] args) {
                // create an EventList of Songs
                final EventList<Song> songs = new BasicEventList<Song>();
                songs.add(new Song("Metallica", "Of Wolf And Man", "Metal"));
                songs.add(new Song("Eminem", "Without Me", "Rap"));
                songs.add(new Song("Britney Spears", "Oops I did it Again", "Pop"));
                songs.add(new Song("Michael Jackson", "Thriller", "Pop"));
                songs.add(new Song("Megadeth", "Rust In Peace", "Metal"));
                songs.add(new Song("MC Hammer", "U Can't Touch This", "Rap"));

                HierarchicalList<Song> hierarchicalList = new HierarchicalList<Song>(
                                songs, new HierarchicalSongContentProvider());

                List filter = new ArrayList();
                filter.add("Metal");
                filter.add("Metallica");
                hierarchicalList.setFilters(filter);
                printAll(hierarchicalList);
        }

        public static void printAll(HierarchicalList<Song> list) {
                for (int i = 0; i < list.getHierarchyCount(); i++) {
                        UniqueList<Song> hl = list.getHierarchy(i);
                        System.out.println("filtered HierarchicalList(" + i
                                        + ") with filter: " + list.getFilter(i));
                        System.out.println(hl.toString().replace("[", "\t")
                                        .replace(", ", ",\n\t").replace("]", ""));
                }
        }

        private static class HierarchicalSongContentProvider implements
                        HierarchicalContentProvider<Song> {
                @Override
                public Object getHierarchy(Song element, int hierarchy) {
                        switch (hierarchy) {
                        case 0:
                                return element.getGenre();
                        case 1:
                                return element.getArtist();
                        case 2:
                                return element.getName();
                        }
                        return element;
                }

                @Override
                public int getHierarchyCount() {
                        return 3;
                }
        }
}

*and this is the generated output (to proof its working)*

filtered HierarchicalList(0) with filter: []
        MC Hammer - U Can't Touch This,
        Megadeth - Rust In Peace,
        Michael Jackson - Thriller,
        Britney Spears - Oops I did it Again,
        Eminem - Without Me,
        Metallica - Of Wolf And Man
filtered HierarchicalList(1) with filter: [Metal]
        Megadeth - Rust In Peace,
        Metallica - Of Wolf And Man
filtered HierarchicalList(2) with filter: [Metal, Metallica]
        Metallica - Of Wolf And Man




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

Reply | Threaded
Open this post in threaded view
|

Re: contribution offer: HierarchicalList

lumo
dont get me wrong,
but TreeList is not the same.
Hierarchical means you are going from one source list to one output list and you limit your results by applying multiple filters (and you can see the resulting list for each level)

TreeList (@see http://publicobject.com/glazedlistsdeveloper/screencasts/treelist/TreeListExample.java)
creates new Instances of Objects.

mine does not, it only provides you the data you are querying for.

to me this looks like two pairs of shoes
(if not please create a small runnable exampe to proof)

thanks
Reply | Threaded
Open this post in threaded view
|

Re: contribution offer: HierarchicalList

davide.cavestro
lumo wrote
dont get me wrong,
but TreeList is not the same.
Hierarchical means you are going from one source list to one output list and you limit your results by applying multiple filters (and you can see the resulting list for each level)

TreeList (@see http://publicobject.com/glazedlistsdeveloper/screencasts/treelist/TreeListExample.java)
creates new Instances of Objects.

mine does not, it only provides you the data you are querying for.

to me this looks like two pairs of shoes
(if not please create a small runnable exampe to proof)

thanks
Isn't yours hierarchical filtering on a list? To me the term HierarchicalList resemble a List with some additional informations that can be applied on its data in order to see it as a hierarchical structure. And that's the job of TreeList. But maybe the fact that I'm not a native English speaker limits my understanding of what you meant.
Cheers
Davide