source: vlo/branches/vlo-3.1/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/FieldValueLabel.java @ 6029

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

merged changes from trunk

File size: 2.6 KB
Line 
1/*
2 * Copyright (C) 2015 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.wicket.provider.FieldValueConverterProvider;
20import eu.clarin.cmdi.vlo.wicket.provider.FieldValueConverterProviderImpl;
21import org.apache.solr.client.solrj.response.FacetField;
22import org.apache.wicket.markup.html.basic.Label;
23import org.apache.wicket.model.IModel;
24import org.apache.wicket.spring.injection.annot.SpringBean;
25import org.apache.wicket.util.convert.IConverter;
26
27/**
28 * Label to be used for facet fields. It will apply the appropriate converter
29 * from {@link FieldValueConverterProviderImpl} based on the field name.
30 *
31 * @author Twan Goosen <twan.goosen@mpi.nl>
32 */
33public class FieldValueLabel extends Label {
34
35    @SpringBean
36    private FieldValueConverterProvider fieldValueConverters;
37
38    private final IModel<String> fieldModel;
39
40    /**
41     *
42     * @param id component id
43     * @param fieldModel model that provides the name of the field
44     */
45    public FieldValueLabel(String id, IModel<String> fieldModel) {
46        super(id);
47        this.fieldModel = fieldModel;
48    }
49
50    /**
51     *
52     * @param id component id
53     * @param model model that provides the field value for this label (which
54     * may get converted on render)
55     * @param fieldModel model that provides the name of the field
56     */
57    public FieldValueLabel(String id, IModel<?> model, IModel<String> fieldModel) {
58        super(id, model);
59        this.fieldModel = fieldModel;
60    }
61
62    @Override
63    public <C> IConverter<C> getConverter(Class<C> type) {
64        if (type == String.class) {
65            final IConverter<String> converter = fieldValueConverters.getConverter(fieldModel.getObject());
66            if (converter != null) {
67                return (IConverter<C>) converter;
68            }
69        }
70        return super.getConverter(type);
71    }
72
73    @Override
74    public void detachModels() {
75        super.detachModels();
76        fieldModel.detach();
77    }
78
79}
Note: See TracBrowser for help on using the repository browser.