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; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.pojo.FacetSelection; |
---|
20 | import java.util.Collection; |
---|
21 | import java.util.Collections; |
---|
22 | import java.util.List; |
---|
23 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
24 | import org.apache.wicket.ajax.markup.html.AjaxFallbackLink; |
---|
25 | import org.apache.wicket.markup.html.basic.Label; |
---|
26 | import org.apache.wicket.markup.html.list.ListItem; |
---|
27 | import org.apache.wicket.markup.html.list.ListView; |
---|
28 | import org.apache.wicket.markup.html.panel.Panel; |
---|
29 | import org.apache.wicket.model.IModel; |
---|
30 | import org.apache.wicket.model.PropertyModel; |
---|
31 | |
---|
32 | /** |
---|
33 | * A panel representing a single facet and its selected values, allowing for |
---|
34 | * deselection |
---|
35 | * |
---|
36 | * @author twagoo |
---|
37 | */ |
---|
38 | public abstract class SelectedFacetPanel extends Panel { |
---|
39 | |
---|
40 | private final IModel<FacetSelection> model; |
---|
41 | |
---|
42 | public SelectedFacetPanel(String id, final IModel<FacetSelection> model) { |
---|
43 | super(id, model); |
---|
44 | this.model = model; |
---|
45 | |
---|
46 | // Facet name becomes title |
---|
47 | add(new Label("facet", new PropertyModel(model, "facetField.name"))); |
---|
48 | // Add removers for all selected values |
---|
49 | add(createSelectionRemovers("facetValueRemover")); |
---|
50 | } |
---|
51 | |
---|
52 | private ListView<String> createSelectionRemovers(String id) { |
---|
53 | // Model of the list of selected values in this facet |
---|
54 | final PropertyModel<List<String>> propertyModel = new PropertyModel<List<String>>(model, "facetValues"); |
---|
55 | // Repeating container of value + unselection links |
---|
56 | final ListView<String> listView = new ListView<String>(id, propertyModel) { |
---|
57 | |
---|
58 | /** |
---|
59 | * Populates an individual value selection remover |
---|
60 | * |
---|
61 | * @param item item to populate |
---|
62 | */ |
---|
63 | @Override |
---|
64 | protected void populateItem(final ListItem<String> item) { |
---|
65 | // A label showing the name of the facet |
---|
66 | item.add(new Label("facetValue", item.getModel())); |
---|
67 | // A link to remove the value selection from this facet |
---|
68 | item.add(new RemoveLink("unselectValue", item.getModel())); |
---|
69 | } |
---|
70 | |
---|
71 | }; |
---|
72 | return listView; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Callback triggered when values have been removed from this facet |
---|
77 | * |
---|
78 | * @param facet name of the facet this panel represents |
---|
79 | * @param valuesRemoved removed values |
---|
80 | * @param target Ajax target allowing for a partial update. May be null |
---|
81 | * (fallback)! |
---|
82 | */ |
---|
83 | protected abstract void onValuesUnselected(String facet, Collection<String> valuesRemoved, AjaxRequestTarget target); |
---|
84 | |
---|
85 | public class RemoveLink extends AjaxFallbackLink { |
---|
86 | |
---|
87 | private final IModel<String> valueModel; |
---|
88 | |
---|
89 | public RemoveLink(String id, IModel<String> valueModel) { |
---|
90 | super(id); |
---|
91 | this.valueModel = valueModel; |
---|
92 | } |
---|
93 | |
---|
94 | @Override |
---|
95 | public void onClick(AjaxRequestTarget target) { |
---|
96 | // Remove a single value |
---|
97 | // Call callback |
---|
98 | onValuesUnselected( |
---|
99 | model.getObject().getFacetField().getName(), |
---|
100 | Collections.singleton(valueModel.getObject()), target); |
---|
101 | } |
---|
102 | |
---|
103 | } |
---|
104 | } |
---|