source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/panels/FacetValuesPanel.java @ 4661

Last change on this file since 4661 was 4661, checked in by Twan Goosen, 10 years ago

Package structure: separated solr services from other services and moved all panels into new wicket.panels package

File size: 3.6 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.panels;
18
19import eu.clarin.cmdi.vlo.wicket.components.SolrFieldNameLabel;
20import eu.clarin.cmdi.vlo.wicket.provider.FacetFieldValuesProvider;
21import java.util.Collection;
22import java.util.Collections;
23import org.apache.solr.client.solrj.response.FacetField;
24import org.apache.solr.client.solrj.response.FacetField.Count;
25import org.apache.wicket.ajax.AjaxRequestTarget;
26import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
27import org.apache.wicket.markup.html.basic.Label;
28import org.apache.wicket.markup.html.link.Link;
29import org.apache.wicket.markup.html.panel.Panel;
30import org.apache.wicket.markup.repeater.Item;
31import org.apache.wicket.markup.repeater.data.DataView;
32import org.apache.wicket.model.CompoundPropertyModel;
33import org.apache.wicket.model.IModel;
34import org.apache.wicket.model.PropertyModel;
35
36/**
37 * A panel representing a single facet and its selectable values
38 *
39 * @author twagoo
40 */
41public abstract class FacetValuesPanel extends Panel {
42
43    private final int maxNumberOfFacetsToShow = 10; //TODO: get from config
44
45    public FacetValuesPanel(String id, IModel<FacetField> model) {
46        super(id, model);
47
48        // add title
49        add(new SolrFieldNameLabel("title", new PropertyModel<String>(model, "name")));
50
51        // provider that extracts values and counts from FacetField
52        final FacetFieldValuesProvider valuesProvider = new FacetFieldValuesProvider(model, maxNumberOfFacetsToShow);
53        add(new DataView<Count>("facetValues", valuesProvider) {
54
55            @Override
56            protected void populateItem(final Item<Count> item) {
57                addFacetValue(item);
58            }
59        });
60    }
61
62    private void addFacetValue(final Item<Count> item) {
63        item.setDefaultModel(new CompoundPropertyModel<Count>(item.getModel()));
64
65        // link to select an individual facet value
66        final Link selectLink = new AjaxFallbackLink("facetSelect") {
67
68            @Override
69            public void onClick(AjaxRequestTarget target) {
70                // call callback
71                onValuesSelected(
72                        item.getModelObject().getFacetField().getName(),
73                        // for now only single values can be selected
74                        Collections.singleton(item.getModelObject().getName()),
75                        target);
76            }
77        };
78        item.add(selectLink);
79
80        // 'name' field from Count (name of value)
81        selectLink.add(new Label("name"));
82        // 'count' field from Count (document count for value)
83        selectLink.add(new Label("count"));
84    }
85
86    /**
87     * Callback triggered when values have been selected on this facet
88     *
89     * @param facet name of the facet this panel represents
90     * @param values selected values
91     * @param target Ajax target allowing for a partial update. May be null
92     * (fallback)!
93     */
94    protected abstract void onValuesSelected(String facet, Collection<String> values, AjaxRequestTarget target);
95
96}
Note: See TracBrowser for help on using the repository browser.