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