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.JavaScriptResources; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.ExpansionState; |
---|
21 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
22 | import eu.clarin.cmdi.vlo.service.solr.FacetFieldsService; |
---|
23 | import eu.clarin.cmdi.vlo.wicket.model.FacetExpansionStateModel; |
---|
24 | import eu.clarin.cmdi.vlo.wicket.model.FacetFieldModel; |
---|
25 | import eu.clarin.cmdi.vlo.wicket.model.FacetFieldSelectionModel; |
---|
26 | import java.util.HashMap; |
---|
27 | import java.util.List; |
---|
28 | import java.util.Map; |
---|
29 | import org.apache.solr.client.solrj.response.FacetField; |
---|
30 | import org.apache.wicket.Component; |
---|
31 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
32 | import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxFallbackLink; |
---|
33 | import org.apache.wicket.markup.head.CssHeaderItem; |
---|
34 | import org.apache.wicket.markup.head.IHeaderResponse; |
---|
35 | import org.apache.wicket.markup.head.JavaScriptHeaderItem; |
---|
36 | import org.apache.wicket.markup.html.WebMarkupContainer; |
---|
37 | import org.apache.wicket.markup.html.list.ListItem; |
---|
38 | import org.apache.wicket.markup.html.list.ListView; |
---|
39 | import org.apache.wicket.markup.html.panel.GenericPanel; |
---|
40 | import org.apache.wicket.model.IModel; |
---|
41 | import org.apache.wicket.model.util.MapModel; |
---|
42 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
43 | |
---|
44 | /** |
---|
45 | * A panel representing a group of facets. |
---|
46 | * |
---|
47 | * For each facet present in the provided list model, a {@link FacetPanel} is |
---|
48 | * added to the a list view. |
---|
49 | * |
---|
50 | * @author twagoo |
---|
51 | */ |
---|
52 | public abstract class FacetsPanel extends GenericPanel<List<FacetField>> { |
---|
53 | |
---|
54 | @SpringBean |
---|
55 | private FacetFieldsService facetFieldsService; |
---|
56 | private MapModel<String, ExpansionState> expansionModel; |
---|
57 | |
---|
58 | /** |
---|
59 | * |
---|
60 | * @param id component id |
---|
61 | * @param facetsModel model that provides the list of facets to show in this |
---|
62 | * panel |
---|
63 | * @param selectionModel model representing the current query/value |
---|
64 | * selection state |
---|
65 | */ |
---|
66 | public FacetsPanel(final String id, final IModel<List<FacetField>> facetsModel, final IModel<QueryFacetsSelection> selectionModel) { |
---|
67 | super(id, facetsModel); |
---|
68 | |
---|
69 | final Map<String, ExpansionState> expansionStateMap = new HashMap<String, ExpansionState>(); |
---|
70 | expansionModel = new MapModel<String, ExpansionState>(expansionStateMap); |
---|
71 | |
---|
72 | final ListView<FacetField> facetsView = new ListView<FacetField>("facets", facetsModel) { |
---|
73 | |
---|
74 | @Override |
---|
75 | protected void populateItem(ListItem<FacetField> item) { |
---|
76 | // Create a facet field model which does a lookup by name, |
---|
77 | // making it dynamic in case the selection and therefore |
---|
78 | // set of available values changes |
---|
79 | // TODO: Limit to number of items shown while keeping 'more' function? |
---|
80 | final IModel<FacetField> facetFieldModel = new FacetFieldModel(facetFieldsService, item.getModelObject(), selectionModel); |
---|
81 | item.add( |
---|
82 | new FacetPanel("facet", |
---|
83 | new FacetFieldSelectionModel(facetFieldModel, selectionModel), |
---|
84 | new FacetExpansionStateModel(facetFieldModel, expansionModel)) { |
---|
85 | |
---|
86 | @Override |
---|
87 | protected void selectionChanged(AjaxRequestTarget target) { |
---|
88 | FacetsPanel.this.selectionChanged(target); |
---|
89 | } |
---|
90 | } |
---|
91 | ); |
---|
92 | } |
---|
93 | }; |
---|
94 | // facet list is not dynamic, so reuse items |
---|
95 | facetsView.setReuseItems(true); |
---|
96 | add(facetsView); |
---|
97 | |
---|
98 | // links to expand, collapse or deselect all facets |
---|
99 | add(createBatchLinks("batchLinks", selectionModel)); |
---|
100 | } |
---|
101 | |
---|
102 | private Component createBatchLinks(String id, final IModel<QueryFacetsSelection> selectionModel) { |
---|
103 | final WebMarkupContainer links = new WebMarkupContainer(id); |
---|
104 | links.add(new IndicatingAjaxFallbackLink("expandAll") { |
---|
105 | |
---|
106 | @Override |
---|
107 | public void onClick(AjaxRequestTarget target) { |
---|
108 | setAllFacetsExpansionState(ExpansionState.EXPANDED); |
---|
109 | selectionChanged(target); |
---|
110 | } |
---|
111 | }); |
---|
112 | links.add(new IndicatingAjaxFallbackLink("collapseAll") { |
---|
113 | |
---|
114 | @Override |
---|
115 | public void onClick(AjaxRequestTarget target) { |
---|
116 | setAllFacetsExpansionState(ExpansionState.COLLAPSED); |
---|
117 | selectionChanged(target); |
---|
118 | } |
---|
119 | }); |
---|
120 | links.add(new IndicatingAjaxFallbackLink("deselectAll") { |
---|
121 | |
---|
122 | @Override |
---|
123 | public void onClick(AjaxRequestTarget target) { |
---|
124 | String query = selectionModel.getObject().getQuery(); |
---|
125 | |
---|
126 | selectionModel.setObject(new QueryFacetsSelection(query)); |
---|
127 | selectionChanged(target); |
---|
128 | } |
---|
129 | |
---|
130 | @Override |
---|
131 | protected void onConfigure() { |
---|
132 | super.onConfigure(); |
---|
133 | final Map selection = selectionModel.getObject().getSelection(); |
---|
134 | setVisible(selection != null && !selection.isEmpty()); |
---|
135 | } |
---|
136 | }); |
---|
137 | return links; |
---|
138 | } |
---|
139 | |
---|
140 | private void setAllFacetsExpansionState(final ExpansionState state) { |
---|
141 | final Map<String, ExpansionState> expansionMap = expansionModel.getObject(); |
---|
142 | for (FacetField facet : getModelObject()) { |
---|
143 | expansionMap.put(facet.getName(), state); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | @Override |
---|
148 | public void detachModels() { |
---|
149 | super.detachModels(); |
---|
150 | expansionModel.detach(); |
---|
151 | } |
---|
152 | |
---|
153 | @Override |
---|
154 | public void renderHead(IHeaderResponse response) { |
---|
155 | // JQuery UI for tooltips |
---|
156 | response.render(CssHeaderItem.forReference(JavaScriptResources.getJQueryUICSS())); |
---|
157 | response.render(JavaScriptHeaderItem.forReference(JavaScriptResources.getJQueryUIJS())); |
---|
158 | response.render(JavaScriptHeaderItem.forReference(JavaScriptResources.getSyntaxHelpJS())); |
---|
159 | |
---|
160 | } |
---|
161 | |
---|
162 | protected abstract void selectionChanged(AjaxRequestTarget target); |
---|
163 | } |
---|