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.markup.html.panel.Panel; |
---|
26 | import org.apache.wicket.model.IModel; |
---|
27 | import org.apache.wicket.model.PropertyModel; |
---|
28 | |
---|
29 | /** |
---|
30 | * |
---|
31 | * @author twagoo |
---|
32 | */ |
---|
33 | public class FacetPanel extends Panel { |
---|
34 | |
---|
35 | private final IModel<FacetSelection> model; |
---|
36 | |
---|
37 | private final SelectedFacetPanel selectedFacetPanel; |
---|
38 | private final FacetValuesPanel facetValuesPanel; |
---|
39 | |
---|
40 | public FacetPanel(String id, IModel<FacetSelection> model) { |
---|
41 | super(id, model); |
---|
42 | this.model = model; |
---|
43 | |
---|
44 | // panel showing values for selection |
---|
45 | facetValuesPanel = createFacetValuesPanel("facetValues"); |
---|
46 | add(facetValuesPanel); |
---|
47 | |
---|
48 | // panel showing current selection, allowing for deselection |
---|
49 | selectedFacetPanel = createSelectedFacetPanel("facetSelection"); |
---|
50 | add(selectedFacetPanel); |
---|
51 | } |
---|
52 | |
---|
53 | @Override |
---|
54 | protected void onConfigure() { |
---|
55 | super.onConfigure(); |
---|
56 | |
---|
57 | final boolean valuesSelected = !model.getObject().getFacetValues().isEmpty(); |
---|
58 | facetValuesPanel.setVisible(!valuesSelected); |
---|
59 | selectedFacetPanel.setVisible(valuesSelected); |
---|
60 | } |
---|
61 | |
---|
62 | private FacetValuesPanel createFacetValuesPanel(String id) { |
---|
63 | return new FacetValuesPanel(id,new PropertyModel<FacetField>(model, "facetField")) { |
---|
64 | @Override |
---|
65 | public void onValuesSelected(String facet, Collection<String> value, AjaxRequestTarget target) { |
---|
66 | // A value has been selected on this facet's panel, |
---|
67 | // update the model! |
---|
68 | model.getObject().getSelection().selectValues(facet, value); |
---|
69 | if (target != null) { |
---|
70 | // reload entire page for now |
---|
71 | target.add(getPage()); |
---|
72 | } |
---|
73 | } |
---|
74 | }; |
---|
75 | } |
---|
76 | |
---|
77 | private SelectedFacetPanel createSelectedFacetPanel(String id) { |
---|
78 | return new SelectedFacetPanel(id, model) { |
---|
79 | @Override |
---|
80 | public void onValuesUnselected(String facet, Collection<String> valuesRemoved, AjaxRequestTarget target) { |
---|
81 | final QueryFacetsSelection selection = model.getObject().getSelection(); |
---|
82 | // Values have been removed, calculate remainder |
---|
83 | final Collection<String> currentSelection = selection.getSelectionValues(facet); |
---|
84 | final Collection<String> newSelection = new HashSet<String>(currentSelection); |
---|
85 | newSelection.removeAll(valuesRemoved); |
---|
86 | // Update model |
---|
87 | selection.selectValues(facet, newSelection); |
---|
88 | if (target != null) { |
---|
89 | // reload entire page for now |
---|
90 | target.add(getPage()); |
---|
91 | } |
---|
92 | } |
---|
93 | }; |
---|
94 | } |
---|
95 | |
---|
96 | } |
---|