source: vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/panels/search/FacetPanel.java @ 6248

Last change on this file since 6248 was 6248, checked in by Twan Goosen, 9 years ago

'title' attribute now added to actual html elements, not volatile wicket containers

Reduced some logging :)

File size: 6.1 KB
Line 
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 */
17package eu.clarin.cmdi.vlo.wicket.panels.search;
18
19import eu.clarin.cmdi.vlo.wicket.model.SolrFieldDescriptionModel;
20import eu.clarin.cmdi.vlo.pojo.ExpansionState;
21import eu.clarin.cmdi.vlo.pojo.FacetFieldSelection;
22import eu.clarin.cmdi.vlo.pojo.FacetSelection;
23import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
24import eu.clarin.cmdi.vlo.wicket.model.SolrFieldNameModel;
25import eu.clarin.cmdi.vlo.wicket.panels.ExpandablePanel;
26import java.util.Collection;
27import java.util.HashSet;
28import org.apache.solr.client.solrj.response.FacetField;
29import org.apache.wicket.ajax.AjaxRequestTarget;
30import org.apache.wicket.behavior.AttributeAppender;
31import org.apache.wicket.markup.html.basic.Label;
32import org.apache.wicket.model.IModel;
33import org.apache.wicket.model.PropertyModel;
34import org.slf4j.Logger;
35import 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 */
45public 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        facetValuesPanel.setVisible(!valuesSelected);
84        selectedFacetPanel.setVisible(valuesSelected);
85
86        // hide this entire panel is no values are selectable
87        setVisible(!isHideIfNoValues() || valuesSelected || getModelObject().getFacetField().getValueCount() > 0);
88    }
89
90    /**
91     *
92     * @return whether the panel should be hidden if no values are present given
93     * the current selection and query
94     */
95    protected boolean isHideIfNoValues() {
96        return true;
97    }
98
99    private FacetValuesPanel createFacetValuesPanel(String id, int subListSize) {
100        return new FacetValuesPanel(id,
101                new PropertyModel<FacetField>(getModel(), "facetField"),
102                new PropertyModel<QueryFacetsSelection>(getModel(), "selection"), subListSize) {
103                    @Override
104                    public void onValuesSelected(String facet, FacetSelection value, AjaxRequestTarget target) {
105                        // A value has been selected on this facet's panel, update the model!
106                        FacetPanel.this.getModelObject().getSelection().selectValues(facet, value);
107                        if (target != null) {
108                            // reload entire page for now
109                            selectionChanged(target);
110                        }
111                    }
112                };
113    }
114
115    private SelectedFacetPanel createSelectedFacetPanel(String id) {
116        return new SelectedFacetPanel(id, getModel()) {
117            @Override
118            public void onValuesUnselected(String facet, Collection<String> valuesRemoved, AjaxRequestTarget target) {
119                final QueryFacetsSelection selection = getModelObject().getSelection();
120
121                // Values have been removed, calculate remainder
122                final FacetSelection facetSelection = selection.getSelectionValues(facet);
123                final Collection<String> currentSelection = facetSelection.getValues();
124                final Collection<String> newSelection = new HashSet<String>(currentSelection);
125                newSelection.removeAll(valuesRemoved);
126
127                // Update model (keep selection type)
128                selection.selectValues(facet, new FacetSelection(facetSelection.getSelectionType(), newSelection));
129
130                // collapse after removal
131                // TODO: should be removed, but then list of values
132                // does not seem to update correctly
133                expansionStateModel.setObject(ExpansionState.COLLAPSED);
134
135                if (target != null) {
136                    // reload entire page for now
137                    selectionChanged(target);
138                }
139            }
140        };
141    }
142
143    protected abstract void selectionChanged(AjaxRequestTarget target);
144
145}
Note: See TracBrowser for help on using the repository browser.