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 com.google.common.collect.ImmutableSet; |
---|
20 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
21 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
22 | import eu.clarin.cmdi.vlo.pojo.ExpansionState; |
---|
23 | import eu.clarin.cmdi.vlo.pojo.FacetSelection; |
---|
24 | import eu.clarin.cmdi.vlo.pojo.FacetSelectionType; |
---|
25 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
26 | import eu.clarin.cmdi.vlo.wicket.model.FacetSelectionModel; |
---|
27 | import eu.clarin.cmdi.vlo.wicket.model.ToggleModel; |
---|
28 | import eu.clarin.cmdi.vlo.wicket.panels.ExpandablePanel; |
---|
29 | import java.util.Collection; |
---|
30 | import org.apache.wicket.MarkupContainer; |
---|
31 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
32 | import org.apache.wicket.ajax.form.OnChangeAjaxBehavior; |
---|
33 | import org.apache.wicket.markup.html.WebMarkupContainer; |
---|
34 | import org.apache.wicket.markup.html.basic.Label; |
---|
35 | import org.apache.wicket.markup.html.form.CheckBox; |
---|
36 | import org.apache.wicket.markup.html.form.Form; |
---|
37 | import org.apache.wicket.model.IModel; |
---|
38 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
39 | |
---|
40 | /** |
---|
41 | * A panel showing advanced search options: |
---|
42 | * <ul> |
---|
43 | * <li>selection of records that support FCS</li> |
---|
44 | * <li>selection of records that are based on either CMDI or OLAC</li> |
---|
45 | * </ul> |
---|
46 | * |
---|
47 | * @author twagoo |
---|
48 | */ |
---|
49 | public abstract class AdvancedSearchOptionsPanel extends ExpandablePanel<QueryFacetsSelection> { |
---|
50 | |
---|
51 | @SpringBean |
---|
52 | private VloConfig config; |
---|
53 | |
---|
54 | /** |
---|
55 | * The fields that this panel provides options for |
---|
56 | */ |
---|
57 | public final static Collection<String> OPTIONS_FIELDS = ImmutableSet.of( |
---|
58 | FacetConstants.FIELD_HAS_PART_COUNT, |
---|
59 | FacetConstants.FIELD_SEARCH_SERVICE); |
---|
60 | |
---|
61 | public AdvancedSearchOptionsPanel(String id, IModel<QueryFacetsSelection> model) { |
---|
62 | super(id, model); |
---|
63 | final Form options = new Form("options"); |
---|
64 | |
---|
65 | final CheckBox fcsCheck = createFieldNotEmptyOption("fcs", FacetConstants.FIELD_SEARCH_SERVICE); |
---|
66 | options.add(fcsCheck); |
---|
67 | |
---|
68 | final MarkupContainer collectionsSection = new WebMarkupContainer("collectionsSection"); |
---|
69 | final CheckBox collectionCheck = createFieldNotEmptyOption("collection", FacetConstants.FIELD_HAS_PART_COUNT); |
---|
70 | collectionsSection.add(collectionCheck); |
---|
71 | collectionsSection.setVisible(config.isProcessHierarchies()); |
---|
72 | options.add(collectionsSection); |
---|
73 | |
---|
74 | add(options); |
---|
75 | } |
---|
76 | |
---|
77 | private CheckBox createFieldNotEmptyOption(String id, String fieldName) { |
---|
78 | // create a model for the selection state of the facet |
---|
79 | final IModel<FacetSelection> facetModel = new FacetSelectionModel(getModel(), fieldName); |
---|
80 | // wrap in a toggle model that allows switching between a null selection and a 'not empty' selection |
---|
81 | final ToggleModel<FacetSelection> toggleModel = new ToggleModel<>(facetModel, null, new FacetSelection(FacetSelectionType.NOT_EMPTY)); |
---|
82 | |
---|
83 | final CheckBox checkBox = new CheckBox(id, toggleModel); |
---|
84 | checkBox.add(new OnChangeAjaxBehavior() { |
---|
85 | |
---|
86 | @Override |
---|
87 | protected void onUpdate(AjaxRequestTarget target) { |
---|
88 | selectionChanged(target); |
---|
89 | } |
---|
90 | }); |
---|
91 | // should initially be epxanded if one of the options was selected |
---|
92 | if (toggleModel.getObject()) { |
---|
93 | getExpansionModel().setObject(ExpansionState.EXPANDED); |
---|
94 | } |
---|
95 | return checkBox; |
---|
96 | } |
---|
97 | |
---|
98 | @Override |
---|
99 | protected Label createTitleLabel(String id) { |
---|
100 | return new Label(id, "Search options"); |
---|
101 | } |
---|
102 | |
---|
103 | protected abstract void selectionChanged(AjaxRequestTarget target); |
---|
104 | |
---|
105 | } |
---|