You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DesignToStringConverter.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui.declarative.converters;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.util.Locale;
  19. import com.vaadin.data.Result;
  20. import com.vaadin.data.util.converter.Converter;
  21. import com.vaadin.ui.declarative.DesignAttributeHandler;
  22. /**
  23. * Utility class for {@link DesignAttributeHandler} that deals with converting
  24. * various types to string.
  25. *
  26. * @since 7.4
  27. * @author Vaadin Ltd
  28. * @param <TYPE>
  29. * Type of the data being converted.
  30. */
  31. public class DesignToStringConverter<TYPE> implements Converter<String, TYPE> {
  32. private final Class<? extends TYPE> type;
  33. private final String staticMethodName;
  34. /**
  35. * A string that corresponds to how a null value is stored.
  36. */
  37. public static final String NULL_VALUE_REPRESENTATION = "";
  38. /**
  39. * Constructs the converter for a given type. Implicitly requires that a
  40. * static method {@code valueOf(String)} is present in the type to do the
  41. * conversion.
  42. *
  43. * @param type
  44. * Type of values to convert.
  45. */
  46. public DesignToStringConverter(Class<? extends TYPE> type) {
  47. this(type, "valueOf");
  48. }
  49. /**
  50. * Constructs the converter for a given type, giving the name of the public
  51. * static method that does the conversion from String.
  52. *
  53. * @param type
  54. * Type to convert.
  55. * @param staticMethodName
  56. * Method to call when converting from String to this type. This
  57. * must be public and static method that returns an object of
  58. * passed type.
  59. */
  60. public DesignToStringConverter(Class<? extends TYPE> type,
  61. String staticMethodName) {
  62. this.type = type;
  63. this.staticMethodName = staticMethodName;
  64. }
  65. @Override
  66. public Result<TYPE> convertToModel(String value, Locale locale) {
  67. try {
  68. return Result.ok(type
  69. .cast(type.getMethod(this.staticMethodName, String.class)
  70. .invoke(null, value)));
  71. } catch (InvocationTargetException e) {
  72. return Result.error(e.getCause().getMessage());
  73. } catch (Exception e) {
  74. return Result.error(e.getMessage());
  75. }
  76. }
  77. @Override
  78. public String convertToPresentation(TYPE value, Locale locale) {
  79. if (value == null) {
  80. return NULL_VALUE_REPRESENTATION;
  81. } else {
  82. return value.toString();
  83. }
  84. }
  85. }