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 | */ |
---|
17 | package eu.clarin.cmdi.vlo.wicket.panels; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.wicket.components.FieldValueOrderSelector; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.FieldValuesOrder; |
---|
21 | import eu.clarin.cmdi.vlo.wicket.provider.FacetFieldValuesProvider; |
---|
22 | import java.util.Collection; |
---|
23 | import java.util.Collections; |
---|
24 | import org.apache.solr.client.solrj.response.FacetField; |
---|
25 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
26 | import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; |
---|
27 | import org.apache.wicket.ajax.form.OnChangeAjaxBehavior; |
---|
28 | import org.apache.wicket.ajax.markup.html.AjaxFallbackLink; |
---|
29 | import org.apache.wicket.extensions.markup.html.repeater.util.SortParam; |
---|
30 | import org.apache.wicket.markup.html.WebMarkupContainer; |
---|
31 | import org.apache.wicket.markup.html.basic.Label; |
---|
32 | import org.apache.wicket.markup.html.form.DropDownChoice; |
---|
33 | import org.apache.wicket.markup.html.form.Form; |
---|
34 | import org.apache.wicket.markup.html.form.TextField; |
---|
35 | import org.apache.wicket.markup.html.link.Link; |
---|
36 | import org.apache.wicket.markup.html.panel.GenericPanel; |
---|
37 | import org.apache.wicket.markup.repeater.Item; |
---|
38 | import org.apache.wicket.markup.repeater.data.DataView; |
---|
39 | import org.apache.wicket.model.CompoundPropertyModel; |
---|
40 | import org.apache.wicket.model.IModel; |
---|
41 | import org.apache.wicket.model.Model; |
---|
42 | import 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 | */ |
---|
50 | public 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 | } |
---|