source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/FacetsPanel.java @ 4540

Last change on this file since 4540 was 4540, checked in by twagoo, 10 years ago

Added a panel for facets with selected valeus that allow for deselection. FacetsPanel? adds either an instance of this or of the value select panel depending on whether an existing selection is present.

File size: 4.6 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.components;
18
19import eu.clarin.cmdi.vlo.pojo.FacetSelection;
20import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
21import eu.clarin.cmdi.vlo.service.FacetFieldsService;
22import eu.clarin.cmdi.vlo.wicket.provider.FacetFieldsDataProvider;
23import java.util.Collection;
24import java.util.HashSet;
25import org.apache.solr.client.solrj.response.FacetField;
26import org.apache.wicket.ajax.AjaxRequestTarget;
27import org.apache.wicket.markup.html.panel.Panel;
28import org.apache.wicket.markup.repeater.Item;
29import org.apache.wicket.markup.repeater.data.DataView;
30import org.apache.wicket.model.IModel;
31import org.apache.wicket.model.Model;
32import 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 */
45public 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}
Note: See TracBrowser for help on using the repository browser.