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

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

added filtering of values in all values panel

File size: 6.0 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.FieldValueOrderSelector;
20import eu.clarin.cmdi.vlo.pojo.FieldValuesOrder;
21import eu.clarin.cmdi.vlo.wicket.provider.FacetFieldValuesProvider;
22import java.util.Collection;
23import java.util.Collections;
24import org.apache.solr.client.solrj.response.FacetField;
25import org.apache.wicket.ajax.AjaxRequestTarget;
26import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
27import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
28import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
29import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
30import org.apache.wicket.markup.html.WebMarkupContainer;
31import org.apache.wicket.markup.html.basic.Label;
32import org.apache.wicket.markup.html.form.DropDownChoice;
33import org.apache.wicket.markup.html.form.Form;
34import org.apache.wicket.markup.html.form.TextField;
35import org.apache.wicket.markup.html.link.Link;
36import org.apache.wicket.markup.html.panel.GenericPanel;
37import org.apache.wicket.markup.repeater.Item;
38import org.apache.wicket.markup.repeater.data.DataView;
39import org.apache.wicket.model.CompoundPropertyModel;
40import org.apache.wicket.model.IModel;
41import org.apache.wicket.model.Model;
42import org.apache.wicket.model.PropertyModel;
43
44/**
45 * A panel that shows all available values for a selected facet. Supports two
46 * ordering modes (by name or result count) and dynamic filtering.
47 *
48 * @author twagoo
49 */
50public abstract class AllFacetValuesPanel extends GenericPanel<FacetField> {
51
52    private final FacetFieldValuesProvider valuesProvider;
53    private final WebMarkupContainer valuesContainer;
54    private IModel<String> filterModel = new Model<String>();
55
56    public AllFacetValuesPanel(String id, IModel<FacetField> model) {
57        super(id, model);
58        // create a provider that shows all values and is sorted by name by default
59        valuesProvider = new FacetFieldValuesProvider(model, Integer.MAX_VALUE, FieldValueOrderSelector.NAME_SORT) {
60
61            @Override
62            protected IModel<String> getFilterModel() {
63                // filters the values
64                return filterModel;
65            }
66
67        };
68
69        // create a container for the values to allow for AJAX updates
70        valuesContainer = new WebMarkupContainer("facetValuesContainer");
71        valuesContainer.setOutputMarkupId(true);
72        add(valuesContainer);
73
74        // create the view of the actual values
75        final DataView<FacetField.Count> valuesView = createValuesView("facetValue");
76        valuesContainer.add(valuesView);
77
78        // create the form for selection sort option and entering filter string
79        final Form optionsForm = createOptionsForm("options");
80        optionsForm.setOutputMarkupId(true);
81        add(optionsForm);
82    }
83
84    private DataView<FacetField.Count> createValuesView(String id) {
85        return new DataView<FacetField.Count>(id, valuesProvider) {
86
87            @Override
88            protected void populateItem(final Item<FacetField.Count> item) {
89                item.setDefaultModel(new CompoundPropertyModel<FacetField.Count>(item.getModel()));
90
91                // link to select an individual facet value
92                final Link selectLink = new AjaxFallbackLink("facetSelect") {
93
94                    @Override
95                    public void onClick(AjaxRequestTarget target) {
96                        // call callback
97                        onValuesSelected(
98                                item.getModelObject().getFacetField().getName(),
99                                // for now only single values can be selected
100                                Collections.singleton(item.getModelObject().getName()),
101                                target);
102                    }
103                };
104                item.add(selectLink);
105
106                // 'name' field from Count (name of value)
107                selectLink.add(new Label("name"));
108
109                // 'count' field from Count (document count for value)
110                item.add(new Label("count"));
111            }
112        };
113    }
114
115    private Form createOptionsForm(String id) {
116        final Form options = new Form(id);
117        final DropDownChoice<SortParam<FieldValuesOrder>> sortSelect
118                = new FieldValueOrderSelector("sort", new PropertyModel<SortParam<FieldValuesOrder>>(valuesProvider, "sort"));
119        sortSelect.add(new OnChangeAjaxBehavior() {
120
121            @Override
122            protected void onUpdate(AjaxRequestTarget target) {
123                target.add(options);
124                target.add(valuesContainer);
125            }
126        });
127        options.add(sortSelect);
128
129        final TextField filterField = new TextField("filter", filterModel);
130        filterField.add(new AjaxFormComponentUpdatingBehavior("keyup") {
131
132            @Override
133            protected void onUpdate(AjaxRequestTarget target) {
134                target.add(valuesContainer);
135            }
136        });
137        options.add(filterField);
138        return options;
139    }
140
141    /**
142     * Callback triggered when values have been selected on this facet
143     *
144     * @param facet name of the facet this panel represents
145     * @param values selected values
146     * @param target Ajax target allowing for a partial update. May be null
147     * (fallback)!
148     */
149    protected abstract void onValuesSelected(String facet, Collection<String> values, AjaxRequestTarget target);
150}
Note: See TracBrowser for help on using the repository browser.