Figure 1. This version of the createPod method adds a filter to the Movies Pod. It is used in Chapter 6001 public Node createPod(Document document, Map<String,Object> contexts) {
002 try{
003 PodBuilder moviesPod =
004 PodBuilder.newPod(document, PODTYPE.MYFAVMOVIES);
005 moviesPod.setTitle("My Favourite Movies");
006
007 MoviesDB moviesDB = new MoviesDB();
008
009 // Create the configuration for the drop down filter.
010 RendererConfig filterRenderer =
011 new RendererConfig(RendererConfigType.DOMAIN, "CT_CHOICE");
012
013 // Create the PodFitler
014 PodFilter genreFilter =
015 new PodFilter("genre", document, filterRenderer);
016
017 // Create genre list
018 HashMap<String, String> genres = new HashMap<String, String>();
019 genres.put("all", "- All -");
020 genres.put("horror", "Horror");
021 genres.put("drama", "Drama");
022 genres.put("romance", "Romance");
023 genres.put("comedy", "Comedy");
024 genres.put("action", "Action");
025
026 // Create the options and selections using the ChoiceBuilder.
027 ChoiceBuilder choices =
028 ChoiceBuilder.newInstance(genres, document);
029
030 // Return the last saved selection for the filter with id "genre".
031 Node genreSelectionNode =
032 getPodFilterById(PODTYPE.MYFAVMOVIES, "genre", document);
033
034 // Convert the Node to an ArrayList.
035 ArrayList<String> selectedGenres =
036 PodFilter.convertSelectionsNodeToArrayList(genreSelectionNode);
037
038 // Create a default genre selection.
039 if (selectedGenres.isEmpty()){
040 selectedGenres.add("all");
041 }
042 choices.addSelection(selectedGenres.get(0));
043 choices.setTypeOfDisplay("listdropdown");
044
045 genreFilter.addFilter(choices.getWidgetRootNode());
046
047 // Add a filter label
048 genreFilter.addFilterLabel("Genre");
049 genreFilter.addCSSClasses("genre-filter");
050 moviesPod.addFilter(genreFilter);
051
052
053 Collection<MoviesDB.Movie> favMovieCollection =
054 moviesDB.getAllMovies();
055 Iterator<MoviesDB.Movie> movieList =
056 favMovieCollection.iterator();
057
058 // Create the list
059 ListBuilder myFavouriteMovies =
060 ListBuilder.createList(1, document);
061
062 int row = 1;
063 while(movieList.hasNext()) {
064 Movie movie = movieList.next();
065 String movieName = movie.title;
066 String selectedGenre = selectedGenres.get(0);
067 if (selectedGenre.equals(movie.genre)
068 || selectedGenre.equals("all")){
069
070 myFavouriteMovies.addRow();
071 myFavouriteMovies.addEntry(1, row++, movieName);
072 }
073 }
074
075 RendererConfig contentRenderer = new RendererConfig(
076 RendererConfigType.STYLE, "single-list");
077 moviesPod.addContent(myFavouriteMovies, contentRenderer);
078
079 return moviesPod.getWidgetRootNode();
080 }catch(Exception e){
081 throw new RuntimeException(e);
082 }
083 }