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.

CreatingYourOwnConverterForString.asciidoc 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ---
  2. title: Creating Your Own Converter For String
  3. order: 5
  4. layout: page
  5. ---
  6. [[creating-your-own-converter-for-string-mytype-conversion]]
  7. = Creating your own converter for String - MyType conversion
  8. If you have custom types that you want to represent using the built in
  9. field components, you can easily create your own converter to take care
  10. of converting between your own type and the native data type of the
  11. field.
  12. A sample custom type, in this case a Name object with separate fields
  13. for first and last name.
  14. [source,java]
  15. ....
  16. public class Name {
  17. private String firstName;
  18. private String lastName;
  19. public Name(String firstName, String lastName) {
  20. this.firstName = firstName;
  21. this.lastName = lastName;
  22. }
  23. public String getFirstName() {
  24. return firstName;
  25. }
  26. public void setFirstName(String firstName) {
  27. this.firstName = firstName;
  28. }
  29. public String getLastName() {
  30. return lastName;
  31. }
  32. public void setLastName(String lastName) {
  33. this.lastName = lastName;
  34. }
  35. }
  36. ....
  37. A converter for the name, assuming the parts are separated with a space
  38. and that there are only two parts of a name.
  39. [source,java]
  40. ....
  41. public class StringToNameConverter implements Converter<String, Name> {
  42. public Name convertToModel(String text, Locale locale)
  43. throws ConversionException {
  44. if (text == null) {
  45. return null;
  46. }
  47. String[] parts = text.split(" ");
  48. if (parts.length != 2) {
  49. throw new ConversionException("Can not convert text to a name: " + text);
  50. }
  51. return new Name(parts[0], parts[1]);
  52. }
  53. public String convertToPresentation(Name name, Locale locale)
  54. throws ConversionException {
  55. if (name == null) {
  56. return null;
  57. } else {
  58. return name.getFirstName() + " " + name.getLastName();
  59. }
  60. }
  61. public Class<Name> getModelType() {
  62. return Name.class;
  63. }
  64. public Class<String> getPresentationType() {
  65. return String.class;
  66. }
  67. }
  68. ....
  69. Hooking up the Name type and its Converter to a TextField can then be
  70. done like this
  71. [source,java]
  72. ....
  73. Name name = new Name("Rudolph", "Reindeer");
  74. final TextField textField = new TextField("Name");
  75. textField.setConverter(new StringToNameConverter());
  76. textField.setConvertedValue(name);
  77. addComponent(textField);
  78. addComponent(new Button("Submit value", new ClickListener() {
  79. public void buttonClick(ClickEvent event) {
  80. try {
  81. Name name = (Name) textField.getConvertedValue();
  82. Notification.show(
  83. "First name: " + name.getFirstName() +
  84. "<br />Last name: " + name.getLastName());
  85. } catch (ConversionException e) {
  86. Notification.show(e.getCause().getMessage());
  87. }
  88. }
  89. }));
  90. ....