summaryrefslogtreecommitdiffstats
path: root/documentation/articles/CreatingYourOwnConverterForString.asciidoc
blob: 3cb98e81fae0da1ef3397b473abc67f48dee042d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
---
title: Creating Your Own Converter For String
order: 5
layout: page
---

[[creating-your-own-converter-for-string-mytype-conversion]]
= Creating your own converter for String - MyType conversion

If you have custom types that you want to represent using the built in
field components, you can easily create your own converter to take care
of converting between your own type and the native data type of the
field.

A sample custom type, in this case a Name object with separate fields
for first and last name.

[source,java]
....
public class Name {
  private String firstName;
  private String lastName;

  public Name(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}
....

A converter for the name, assuming the parts are separated with a space
and that there are only two parts of a name.

[source,java]
....
public class StringToNameConverter implements Converter<String, Name> {
  public Name convertToModel(String text, Locale locale)
      throws ConversionException {
    if (text == null) {
      return null;
    }
    String[] parts = text.split(" ");
    if (parts.length != 2) {
      throw new ConversionException("Can not convert text to a name: " + text);
    }
    return new Name(parts[0], parts[1]);
  }

  public String convertToPresentation(Name name, Locale locale)
      throws ConversionException {
    if (name == null) {
      return null;
    } else {
      return name.getFirstName() + " " + name.getLastName();
    }
  }

  public Class<Name> getModelType() {
    return Name.class;
  }

  public Class<String> getPresentationType() {
    return String.class;
  }
}
....

Hooking up the Name type and its Converter to a TextField can then be
done like this

[source,java]
....
Name name = new Name("Rudolph", "Reindeer");

final TextField textField = new TextField("Name");
textField.setConverter(new StringToNameConverter());
textField.setConvertedValue(name);

addComponent(textField);
addComponent(new Button("Submit value", new ClickListener() {
  public void buttonClick(ClickEvent event) {
    try {
      Name name = (Name) textField.getConvertedValue();
      Notification.show(
              "First name: " + name.getFirstName() +
              "<br />Last name: " + name.getLastName());
    } catch (ConversionException e) {
      Notification.show(e.getCause().getMessage());
    }
  }
}));
....