diff options
author | Artur Signell <artur@vaadin.com> | 2014-09-24 19:20:43 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-09-30 12:39:03 +0000 |
commit | 34934d1055d2451a3633d4380648ce661da17ce7 (patch) | |
tree | 9dfdb3d39bd7beabde441eb7389c566e55a0e855 /server/src | |
parent | afbf79cfecbea444b3276e7ef7c83719364be025 (diff) | |
download | vaadin-framework-34934d1055d2451a3633d4380648ce661da17ce7.tar.gz vaadin-framework-34934d1055d2451a3633d4380648ce661da17ce7.zip |
String <-> Enum converter which produces a human readable string (#14756)
Change-Id: I3a825f52a43daea3172ced23bc510118376e76cb
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java | 2 | ||||
-rw-r--r-- | server/src/com/vaadin/data/util/converter/StringToEnumConverter.java | 97 |
2 files changed, 99 insertions, 0 deletions
diff --git a/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java b/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java index fdf858a528..26613c5d02 100644 --- a/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java +++ b/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java @@ -110,6 +110,8 @@ public class DefaultConverterFactory implements ConverterFactory { return new StringToBooleanConverter(); } else if (Date.class.isAssignableFrom(sourceType)) { return new StringToDateConverter(); + } else if (Enum.class.isAssignableFrom(sourceType)) { + return new StringToEnumConverter(); } else { return null; } diff --git a/server/src/com/vaadin/data/util/converter/StringToEnumConverter.java b/server/src/com/vaadin/data/util/converter/StringToEnumConverter.java new file mode 100644 index 0000000000..a1328d831c --- /dev/null +++ b/server/src/com/vaadin/data/util/converter/StringToEnumConverter.java @@ -0,0 +1,97 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.data.util.converter; + +import java.util.EnumSet; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to an {@link Enum} and back. + * <p> + * Designed to provide nice human readable strings for {@link Enum} classes + * where the constants are named SOME_UPPERCASE_WORDS. Will not necessarily work + * correctly for other cases. + * </p> + * + * @author Vaadin Ltd + * @since + */ +public class StringToEnumConverter implements Converter<String, Enum> { + + @Override + public Enum convertToModel(String value, Class<? extends Enum> targetType, + Locale locale) throws ConversionException { + if (value == null) { + return null; + } + if (locale == null) { + locale = Locale.getDefault(); + } + + // Foo -> FOO + // Foo bar -> FOO_BAR + String result = value.replace(" ", "_").toUpperCase(locale); + try { + return Enum.valueOf(targetType, result); + } catch (IllegalArgumentException ee) { + // There was no match. Try to compare the available values to see if + // the constant is using something else than all upper case + + EnumSet<?> set = EnumSet.allOf(targetType); + for (Enum e : set) { + if (e.name().toUpperCase(locale).equals(result)) { + return e; + } + } + + // Fallback did not work either, re-throw original exception so user + // knows what went wrong + throw new ConversionException(ee); + } + } + + @Override + public String convertToPresentation(Enum value, + Class<? extends String> targetType, Locale locale) + throws ConversionException { + if (value == null) { + return null; + } + if (locale == null) { + locale = Locale.getDefault(); + } + + String enumString = value.toString(); + // FOO -> Foo + // FOO_BAR -> Foo bar + // _FOO -> _foo + String result = enumString.substring(0, 1).toUpperCase(locale); + result += enumString.substring(1).toLowerCase(locale).replace('_', ' '); + + return result; + } + + @Override + public Class<Enum> getModelType() { + return Enum.class; + } + + @Override + public Class<String> getPresentationType() { + return String.class; + } + +} |