source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/SearchResultsPanel.java @ 4579

Last change on this file since 4579 was 4579, checked in by twagoo, 10 years ago

Added single facet panel for collection selection

File size: 5.5 KB
Line 
1/*
2 * Copyright (C) 2014 CLARIN
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17package eu.clarin.cmdi.vlo.wicket.components;
18
19import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
20import eu.clarin.cmdi.vlo.service.SolrDocumentService;
21import eu.clarin.cmdi.vlo.wicket.provider.SolrDocumentProvider;
22import java.util.Arrays;
23import java.util.List;
24import org.apache.solr.common.SolrDocument;
25import org.apache.wicket.ajax.AjaxRequestTarget;
26import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
27import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
28import org.apache.wicket.markup.html.basic.Label;
29import org.apache.wicket.markup.html.form.DropDownChoice;
30import org.apache.wicket.markup.html.form.Form;
31import org.apache.wicket.markup.html.navigation.paging.IPageableItems;
32import org.apache.wicket.markup.html.panel.Panel;
33import org.apache.wicket.markup.repeater.Item;
34import org.apache.wicket.markup.repeater.data.DataView;
35import org.apache.wicket.markup.repeater.data.IDataProvider;
36import org.apache.wicket.model.AbstractReadOnlyModel;
37import org.apache.wicket.model.IModel;
38import org.apache.wicket.model.PropertyModel;
39import org.apache.wicket.spring.injection.annot.SpringBean;
40
41/**
42 * Panel that has a data view on the current search results
43 *
44 * @author twagoo
45 */
46public class SearchResultsPanel extends Panel {
47
48    public static final List<Long> ITEMS_PER_PAGE_OPTIONS = Arrays.asList(5L, 10L, 25L, 50L, 100L);
49
50    @SpringBean
51    private SolrDocumentService documentService;
52    private final IDataProvider<SolrDocument> solrDocumentProvider;
53    private final DataView<SolrDocument> resultsView;
54
55    public SearchResultsPanel(String id, IModel<QueryFacetsSelection> model) {
56        super(id, model);
57        solrDocumentProvider = new SolrDocumentProvider(documentService, model);
58
59        // data view for search results
60        resultsView = new DataView<SolrDocument>("resultItem", solrDocumentProvider, 10) {
61
62            @Override
63            protected void populateItem(Item<SolrDocument> item) {
64                // single result item
65                item.add(new SearchResultItemPanel("resultItemDetails", item.getModel()));
66            }
67        };
68        add(resultsView);
69
70        // pagination navigators
71        add(new AjaxPagingNavigator("pagingTop", resultsView));
72        add(new AjaxPagingNavigator("pagingBottom", resultsView));
73
74        // total result counter
75        add(createResultCount("resultCount"));
76
77        // page result indicater
78        add(createResultPageIndicator("resultPageIndicator", resultsView));
79
80        // form to select number of results per page
81        add(createResultPageSizeForm("resultPageSizeForm", resultsView));
82
83        //For Ajax updating of search results
84        setOutputMarkupId(true);
85    }
86
87    /**
88     * Gets called on each request before render
89     */
90    @Override
91    protected void onConfigure() {
92        super.onConfigure();
93       
94        // only show pagination navigators if there's more than one page
95        final boolean showPaging = resultsView.getPageCount() > 1;
96        this.get("pagingTop").setVisible(showPaging);
97        this.get("pagingBottom").setVisible(showPaging);
98    }
99
100    private Label createResultCount(String id) {
101        final IModel<String> resultCountModel = new AbstractReadOnlyModel<String>() {
102
103            @Override
104            public String getObject() {
105                return String.format("%d results", solrDocumentProvider.size());
106            }
107        };
108        return new Label(id, resultCountModel);
109    }
110
111    private Label createResultPageIndicator(String id, final IPageableItems resultsView) {
112        IModel<String> indicatorModel = new AbstractReadOnlyModel<String>() {
113
114            @Override
115            public String getObject() {
116                final long firstShown = 1 + resultsView.getCurrentPage() * resultsView.getItemsPerPage();
117                final long lastShown = Math.min(resultsView.getItemCount(), firstShown + resultsView.getItemsPerPage() - 1);
118                return String.format("Showing %d to %d", firstShown, lastShown);
119            }
120        };
121        return new Label(id, indicatorModel);
122    }
123
124    private Form createResultPageSizeForm(String id, final IPageableItems resultsView) {
125        final Form resultPageSizeForm = new Form(id);
126
127        final DropDownChoice<Long> pageSizeDropDown
128                = new DropDownChoice<Long>("resultPageSize",
129                        // bind to items per page property of pageable
130                        new PropertyModel<Long>(resultsView, "itemsPerPage"),
131                        ITEMS_PER_PAGE_OPTIONS);
132        pageSizeDropDown.add(new AjaxFormComponentUpdatingBehavior("onchange") {
133
134            @Override
135            protected void onUpdate(AjaxRequestTarget target) {
136                target.add(SearchResultsPanel.this);
137            }
138        });
139        resultPageSizeForm.add(pageSizeDropDown);
140
141        return resultPageSizeForm;
142    }
143
144}
Note: See TracBrowser for help on using the repository browser.