Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix options databinding by adding elements if they are not in the initial map #3539

Merged
merged 6 commits into from
Dec 17, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public MapBasedEditorViewModel(String fieldName, AutoCompleteSuggestionProvider<
@Override
public StringConverter<T> getStringConverter() {
return new StringConverter<T>() {

@Override
public String toString(T object) {
if (object == null) {
Expand All @@ -38,7 +39,11 @@ public T fromString(String string) {
if (string == null) {
return null;
} else {
return getItemMap().getOrDefault(string, null);

if (getItemMap().get(string) == null) {
getItemMap().put(string, (T) string);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks!
According to the documentation

a string converter property is provided to allow for developers to specify how to translate a users string into an object of type T, such that the value property may contain it. By default the converter simply returns the String input as the user typed it, which therefore assumes that the type of the editable ComboBox is String.

So you are right that the string converter should return something non-null even when the text is not recognized. However, there is no need to add it to the item map. Moreover, we need to handle the case when T is not a string type. Thus I would propose to create a new protected method that converts an unknown string to a T object. If not overridden, it should try to cast to T as in your code above (and throw an exception with details how to properly implement it if casting was not successful).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now added a method for checking.
We also need such kind for dates to allow custom values which we currently don't support. All input in the field will be cleared if not parseable

}
return getItemMap().get(string);
}
}
};
Expand Down