source: vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/LanguageInfoLink.java @ 6011

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

Link to language info page shown in record fields table
Refs #553

File size: 3.8 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.FacetConstants;
20import eu.clarin.cmdi.vlo.config.VloConfig;
21import java.util.regex.Matcher;
22import java.util.regex.Pattern;
23import org.apache.wicket.AttributeModifier;
24import org.apache.wicket.MarkupContainer;
25import org.apache.wicket.markup.html.WebMarkupContainer;
26import org.apache.wicket.markup.html.panel.GenericPanel;
27import org.apache.wicket.model.AbstractReadOnlyModel;
28import org.apache.wicket.model.IModel;
29import org.apache.wicket.spring.injection.annot.SpringBean;
30
31/**
32 * A panel that has a link and a label for displaying language names with a link
33 * to the language information page. If the value model object matches the
34 * 'language code' syntax ("<code>code:xxx</code>"), it will show a link to the
35 * configured language information page, wrapping around a
36 * {@link FieldValueLabel} to show the language name. In other cases, it falls
37 * back to a plain FieldValueLabel without a link.
38 *
39 * @author Twan Goosen <twan.goosen@mpi.nl>
40 * @see FacetConstants#FIELD_LANGUAGE_CODE
41 * @see VloConfig#getLanguageLinkPrefix()
42 */
43public class LanguageInfoLink extends GenericPanel<String> {
44
45    @SpringBean
46    private VloConfig vloConfig;
47
48    private final static Pattern LANGUAGE_CODE_REGEX = Pattern.compile("code:(.*)");
49    private final MarkupContainer link;
50    private final FieldValueLabel fallbackLabel;
51
52    public LanguageInfoLink(String id, IModel<String> originalValueModel, IModel<String> facetNameModel) {
53        super(id, originalValueModel);
54
55        // link to language info page
56        link = new WebMarkupContainer("link");
57        // make it an external link to the language info page
58        link.add(new AttributeModifier("href", new AbstractReadOnlyModel<String>() {
59
60            @Override
61            public String getObject() {
62                final String languageCode = getLanguageCode();
63                if (languageCode == null) {
64                    return null;
65                } else {
66                    return vloConfig.getLanguageLinkPrefix() + languageCode;
67                }
68            }
69        }));
70       
71        // normal field value label inside (which should convert the language code to a name)
72        link.add(new FieldValueLabel("label", originalValueModel, facetNameModel));
73        add(link);
74
75        // add a standalone label to be shown when the link is not available
76        fallbackLabel = new FieldValueLabel("label", originalValueModel, facetNameModel);
77        add(fallbackLabel);
78    }
79
80    @Override
81    protected void onConfigure() {
82        // when a link can be generated (i.e. wehen a language code is
83        // available) show the link; otherwise show the fallback label
84        final boolean linkAvailable = getLanguageCode() != null;
85        link.setVisible(linkAvailable);
86        fallbackLabel.setVisible(!linkAvailable);
87    }
88
89    private String getLanguageCode() {
90        final Matcher matcher = LANGUAGE_CODE_REGEX.matcher(getModelObject());
91        if (matcher.matches() && matcher.groupCount() == 1) {
92            return matcher.group(1);
93        } else {
94            return null;
95        }
96    }
97}
Note: See TracBrowser for help on using the repository browser.