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 eu.clarin.cmdi.vlo.service.FacetFieldsService; |
---|
22 | import eu.clarin.cmdi.vlo.wicket.provider.FacetFieldsDataProvider; |
---|
23 | import java.util.Collection; |
---|
24 | import java.util.HashSet; |
---|
25 | import org.apache.solr.client.solrj.response.FacetField; |
---|
26 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
27 | import org.apache.wicket.markup.html.panel.Panel; |
---|
28 | import org.apache.wicket.markup.repeater.Item; |
---|
29 | import org.apache.wicket.markup.repeater.data.DataView; |
---|
30 | import org.apache.wicket.model.IModel; |
---|
31 | import org.apache.wicket.model.Model; |
---|
32 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
33 | |
---|
34 | /** |
---|
35 | * A panel representing a group of facets. |
---|
36 | * |
---|
37 | * For each facet present (retrieved from the injected |
---|
38 | * {@link FacetFieldsService}, a panel is added. This is either a |
---|
39 | * {@link FacetValuesPanel}, allowing for selection of facet values, or a |
---|
40 | * {@link SelectedFacetPanel} representing a facet with selected values, |
---|
41 | * allowing for deselection of these values. |
---|
42 | * |
---|
43 | * @author twagoo |
---|
44 | */ |
---|
45 | public class FacetsPanel extends Panel { |
---|
46 | |
---|
47 | @SpringBean |
---|
48 | private FacetFieldsService facetFieldsService; |
---|
49 | private final IModel<QueryFacetsSelection> model; |
---|
50 | |
---|
51 | public FacetsPanel(final String id, IModel<QueryFacetsSelection> model) { |
---|
52 | super(id, model); |
---|
53 | this.model = model; |
---|
54 | |
---|
55 | add(new DataView<FacetField>("facets", new FacetFieldsDataProvider(facetFieldsService, model)) { |
---|
56 | |
---|
57 | @Override |
---|
58 | protected void populateItem(Item<FacetField> item) { |
---|
59 | createFacetPanel(item); |
---|
60 | } |
---|
61 | }); |
---|
62 | } |
---|
63 | |
---|
64 | private void createFacetPanel(Item<FacetField> item) { |
---|
65 | // Is there a selection for this facet? |
---|
66 | final IModel<FacetField> facetFieldModel = item.getModel(); |
---|
67 | final String facetName = facetFieldModel.getObject().getName(); |
---|
68 | final Collection<String> selectionValues = model.getObject().getSelectionValues(facetName); |
---|
69 | |
---|
70 | // Show different panel, depending on selected values |
---|
71 | if (selectionValues == null || selectionValues.isEmpty()) { |
---|
72 | // No values selected, show value selection panel |
---|
73 | item.add(createFacetValuesPanel(facetFieldModel)); |
---|
74 | } else { |
---|
75 | // Values selected, show selected values panel (with option to remove) |
---|
76 | item.add(createSelectedFacetPanel(facetName)); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | private FacetValuesPanel createFacetValuesPanel(final IModel<FacetField> facetFieldModel) { |
---|
81 | return new FacetValuesPanel("facet", facetFieldModel) { |
---|
82 | |
---|
83 | @Override |
---|
84 | public void onValuesSelected(String facet, Collection<String> value, AjaxRequestTarget target) { |
---|
85 | // A value has been selected on this facet's panel, |
---|
86 | // update the model! |
---|
87 | model.getObject().selectValues(facet, value); |
---|
88 | |
---|
89 | if (target != null) { |
---|
90 | // reload entire page for now |
---|
91 | target.add(getPage()); |
---|
92 | } |
---|
93 | } |
---|
94 | }; |
---|
95 | } |
---|
96 | |
---|
97 | private SelectedFacetPanel createSelectedFacetPanel(String facetName) { |
---|
98 | final FacetSelection selection = new FacetSelection(facetName, model); |
---|
99 | return new SelectedFacetPanel("facet", new Model(selection)) { |
---|
100 | |
---|
101 | @Override |
---|
102 | public void onValuesUnselected(String facet, Collection<String> valuesRemoved, AjaxRequestTarget target) { |
---|
103 | // Values have been removed, calculate remainder |
---|
104 | final Collection<String> currentSelection = model.getObject().getSelectionValues(facet); |
---|
105 | final Collection<String> newSelection = new HashSet<String>(currentSelection); |
---|
106 | newSelection.removeAll(valuesRemoved); |
---|
107 | |
---|
108 | // Update model |
---|
109 | model.getObject().selectValues(facet, newSelection); |
---|
110 | |
---|
111 | if (target != null) { |
---|
112 | // reload entire page for now |
---|
113 | target.add(getPage()); |
---|
114 | } |
---|
115 | } |
---|
116 | }; |
---|
117 | } |
---|
118 | } |
---|