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.search; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.wicket.model.SolrFieldDescriptionModel; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.ExpansionState; |
---|
21 | import eu.clarin.cmdi.vlo.pojo.FacetFieldSelection; |
---|
22 | import eu.clarin.cmdi.vlo.pojo.FacetSelection; |
---|
23 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
24 | import eu.clarin.cmdi.vlo.wicket.model.SolrFieldNameModel; |
---|
25 | import eu.clarin.cmdi.vlo.wicket.panels.ExpandablePanel; |
---|
26 | import java.util.Collection; |
---|
27 | import java.util.HashSet; |
---|
28 | import org.apache.solr.client.solrj.response.FacetField; |
---|
29 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
30 | import org.apache.wicket.behavior.AttributeAppender; |
---|
31 | import org.apache.wicket.markup.html.basic.Label; |
---|
32 | import org.apache.wicket.model.IModel; |
---|
33 | import org.apache.wicket.model.PropertyModel; |
---|
34 | import org.slf4j.Logger; |
---|
35 | import org.slf4j.LoggerFactory; |
---|
36 | |
---|
37 | /** |
---|
38 | * Panel that displays a single facet based on the current query/value |
---|
39 | * selection. Two children will be generated: a {@link FacetValuesPanel} and a |
---|
40 | * {@link SelectedFacetPanel}. One of them is set visible (at {@link |
---|
41 | * #onConfigure()}), depending on whether this facet has selected values. |
---|
42 | * |
---|
43 | * @author twagoo |
---|
44 | */ |
---|
45 | public abstract class FacetPanel extends ExpandablePanel<FacetFieldSelection> { |
---|
46 | private final static Logger logger = LoggerFactory.getLogger(FacetPanel.class); |
---|
47 | |
---|
48 | private final IModel<ExpansionState> expansionStateModel; |
---|
49 | |
---|
50 | private final SelectedFacetPanel selectedFacetPanel; |
---|
51 | private final FacetValuesPanel facetValuesPanel; |
---|
52 | |
---|
53 | public FacetPanel(String id, IModel<FacetFieldSelection> selectionModel, IModel<ExpansionState> expansionState) { |
---|
54 | this(id, selectionModel, expansionState, 0); |
---|
55 | } |
---|
56 | |
---|
57 | public FacetPanel(String id, IModel<FacetFieldSelection> selectionModel, IModel<ExpansionState> expansionState, int subListSize) { |
---|
58 | super(id, selectionModel, expansionState); |
---|
59 | this.expansionStateModel = expansionState; |
---|
60 | |
---|
61 | // panel showing values for selection |
---|
62 | facetValuesPanel = createFacetValuesPanel("facetValues", subListSize); |
---|
63 | add(facetValuesPanel); |
---|
64 | |
---|
65 | // panel showing current selection, allowing for deselection |
---|
66 | selectedFacetPanel = createSelectedFacetPanel("facetSelection"); |
---|
67 | add(selectedFacetPanel); |
---|
68 | } |
---|
69 | |
---|
70 | @Override |
---|
71 | protected Label createTitleLabel(String id) { |
---|
72 | final IModel<String> facetNameModel = new PropertyModel<>(getModel(), "facetField.name"); |
---|
73 | final Label label = new Label(id, new SolrFieldNameModel(facetNameModel)); |
---|
74 | label.add(new AttributeAppender("title", new SolrFieldDescriptionModel(facetNameModel))); |
---|
75 | return label; |
---|
76 | } |
---|
77 | |
---|
78 | @Override |
---|
79 | protected void onConfigure() { |
---|
80 | super.onConfigure(); |
---|
81 | |
---|
82 | final boolean valuesSelected = !getModelObject().getFacetValues().isEmpty(); |
---|
83 | //show both for multi values |
---|
84 | facetValuesPanel.setVisible(valuesSelected); |
---|
85 | selectedFacetPanel.setVisible(valuesSelected); |
---|
86 | |
---|
87 | // hide this entire panel is no values are selectable |
---|
88 | setVisible(!isHideIfNoValues() || valuesSelected || getModelObject().getFacetField().getValueCount() > 0); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * |
---|
93 | * @return whether the panel should be hidden if no values are present given |
---|
94 | * the current selection and query |
---|
95 | */ |
---|
96 | protected boolean isHideIfNoValues() { |
---|
97 | return true; |
---|
98 | } |
---|
99 | |
---|
100 | private FacetValuesPanel createFacetValuesPanel(String id, int subListSize) { |
---|
101 | return new FacetValuesPanel(id, |
---|
102 | new PropertyModel<FacetField>(getModel(), "facetField"), |
---|
103 | new PropertyModel<QueryFacetsSelection>(getModel(), "selection"), subListSize) { |
---|
104 | @Override |
---|
105 | public void onValuesSelected(String facet, FacetSelection value, AjaxRequestTarget target) { |
---|
106 | // A value has been selected on this facet's panel, update the model! |
---|
107 | FacetPanel.this.getModelObject().getSelection().selectValues(facet, value); |
---|
108 | if (target != null) { |
---|
109 | // reload entire page for now |
---|
110 | selectionChanged(target); |
---|
111 | } |
---|
112 | } |
---|
113 | }; |
---|
114 | } |
---|
115 | |
---|
116 | private SelectedFacetPanel createSelectedFacetPanel(String id) { |
---|
117 | return new SelectedFacetPanel(id, getModel()) { |
---|
118 | @Override |
---|
119 | public void onValuesUnselected(String facet, Collection<String> valuesRemoved, AjaxRequestTarget target) { |
---|
120 | final QueryFacetsSelection selection = getModelObject().getSelection(); |
---|
121 | |
---|
122 | // Values have been removed, calculate remainder |
---|
123 | final FacetSelection facetSelection = selection.getSelectionValues(facet); |
---|
124 | final Collection<String> currentSelection = facetSelection.getValues(); |
---|
125 | final Collection<String> newSelection = new HashSet<String>(currentSelection); |
---|
126 | newSelection.removeAll(valuesRemoved); |
---|
127 | |
---|
128 | // Update model (keep selection type) |
---|
129 | selection.selectValues(facet, new FacetSelection(facetSelection.getSelectionType(), newSelection)); |
---|
130 | |
---|
131 | // collapse after removal |
---|
132 | // TODO: should be removed, but then list of values |
---|
133 | // does not seem to update correctly |
---|
134 | expansionStateModel.setObject(ExpansionState.COLLAPSED); |
---|
135 | |
---|
136 | if (target != null) { |
---|
137 | // reload entire page for now |
---|
138 | selectionChanged(target); |
---|
139 | } |
---|
140 | } |
---|
141 | }; |
---|
142 | } |
---|
143 | |
---|
144 | protected abstract void selectionChanged(AjaxRequestTarget target); |
---|
145 | |
---|
146 | } |
---|