ref #6169 feature completed: adding new type designation workingsets
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / util / converter / UrlStringConverter.java
1 /**
2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.vaadin.util.converter;
10
11 import java.util.Locale;
12
13 import com.vaadin.data.util.converter.Converter;
14
15 /**
16 * Creates a link from the values in the column.
17 *
18 * @author a.kohlbecker
19 * @since Mar 7, 2017
20 *
21 */
22 public final class UrlStringConverter implements Converter<String, String> {
23
24 private String regex = null;
25 private String replacement = "";
26
27 public UrlStringConverter() {
28
29 }
30
31 /**
32 * @param regex Regular expression for creating an alternative label by string replacement on the value.
33 * This is optional. No string replacement will be done when this is <code>null</code>.
34 */
35 public UrlStringConverter(String regex) {
36 this(regex, null);
37 }
38
39 /**
40 * @param regex Regular expression for creating an alternative label by string replacement on the value.
41 * This is optional. No string replacement will be done when this is <code>null</code>.
42 * @param replacement The replacement to be used with the <code>regex</code>.
43 * Defaults to an empty string when null.
44 */
45 public UrlStringConverter(String regex, String replacement) {
46 this.regex = regex;
47 if(replacement != null){
48 this.replacement = replacement;
49 }
50 }
51
52 @Override
53 public String convertToModel(String value, Class<? extends String> targetType, Locale locale)
54 throws com.vaadin.data.util.converter.Converter.ConversionException {
55 return null;
56 }
57
58 @Override
59 public String convertToPresentation(String value, Class<? extends String> targetType, Locale locale)
60 throws com.vaadin.data.util.converter.Converter.ConversionException {
61 String label = value;
62 if(regex != null){
63 label = label.replace(regex, replacement);
64 }
65 return "<a href=\"" + value + "\" target=\"external\">" + label + "</a>";
66 }
67
68 @Override
69 public Class<String> getModelType() {
70 return String.class;
71 }
72
73 @Override
74 public Class<String> getPresentationType() {
75 return String.class;
76 }
77 }