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.components; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.pojo.FacetSelection; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
21 | import java.util.Collection; |
---|
22 | import java.util.HashSet; |
---|
23 | import org.apache.solr.client.solrj.response.FacetField; |
---|
24 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
25 | import org.apache.wicket.behavior.Behavior; |
---|
26 | import org.apache.wicket.markup.html.panel.Panel; |
---|
27 | import org.apache.wicket.model.IModel; |
---|
28 | import org.apache.wicket.model.PropertyModel; |
---|
29 | |
---|
30 | /** |
---|
31 | * Panel that displays a single facet based on the current query/value |
---|
32 | * selection. Two children will be generated: a {@link FacetValuesPanel} and a |
---|
33 | * {@link SelectedFacetPanel}. One of them is set visible (at {@link |
---|
34 | * #onConfigure()}), depending on whether this facet has selected values. |
---|
35 | * |
---|
36 | * @author twagoo |
---|
37 | */ |
---|
38 | public class FacetPanel extends Panel { |
---|
39 | |
---|
40 | private final IModel<FacetSelection> model; |
---|
41 | |
---|
42 | private final SelectedFacetPanel selectedFacetPanel; |
---|
43 | private final FacetValuesPanel facetValuesPanel; |
---|
44 | |
---|
45 | public FacetPanel(String id, IModel<FacetSelection> model) { |
---|
46 | super(id, model); |
---|
47 | this.model = model; |
---|
48 | |
---|
49 | // panel showing values for selection |
---|
50 | facetValuesPanel = createFacetValuesPanel("facetValues"); |
---|
51 | add(facetValuesPanel); |
---|
52 | |
---|
53 | // panel showing current selection, allowing for deselection |
---|
54 | selectedFacetPanel = createSelectedFacetPanel("facetSelection"); |
---|
55 | add(selectedFacetPanel); |
---|
56 | } |
---|
57 | |
---|
58 | @Override |
---|
59 | protected void onConfigure() { |
---|
60 | super.onConfigure(); |
---|
61 | |
---|
62 | final boolean valuesSelected = !model.getObject().getFacetValues().isEmpty(); |
---|
63 | facetValuesPanel.setVisible(!valuesSelected); |
---|
64 | selectedFacetPanel.setVisible(valuesSelected); |
---|
65 | } |
---|
66 | |
---|
67 | private FacetValuesPanel createFacetValuesPanel(String id) { |
---|
68 | return new FacetValuesPanel(id, new PropertyModel<FacetField>(model, "facetField")) { |
---|
69 | @Override |
---|
70 | public void onValuesSelected(String facet, Collection<String> value, AjaxRequestTarget target) { |
---|
71 | // A value has been selected on this facet's panel, |
---|
72 | // update the model! |
---|
73 | model.getObject().getSelection().selectValues(facet, value); |
---|
74 | if (target != null) { |
---|
75 | // reload entire page for now |
---|
76 | target.add(getPage()); |
---|
77 | } |
---|
78 | } |
---|
79 | }; |
---|
80 | } |
---|
81 | |
---|
82 | private SelectedFacetPanel createSelectedFacetPanel(String id) { |
---|
83 | return new SelectedFacetPanel(id, model) { |
---|
84 | @Override |
---|
85 | public void onValuesUnselected(String facet, Collection<String> valuesRemoved, AjaxRequestTarget target) { |
---|
86 | final QueryFacetsSelection selection = model.getObject().getSelection(); |
---|
87 | // Values have been removed, calculate remainder |
---|
88 | final Collection<String> currentSelection = selection.getSelectionValues(facet); |
---|
89 | final Collection<String> newSelection = new HashSet<String>(currentSelection); |
---|
90 | newSelection.removeAll(valuesRemoved); |
---|
91 | // Update model |
---|
92 | selection.selectValues(facet, newSelection); |
---|
93 | if (target != null) { |
---|
94 | // reload entire page for now |
---|
95 | target.add(getPage()); |
---|
96 | } |
---|
97 | } |
---|
98 | }; |
---|
99 | } |
---|
100 | |
---|
101 | } |
---|