-
Notifications
You must be signed in to change notification settings - Fork 5
/
GSInputTextWithConversion.java
98 lines (82 loc) · 3.86 KB
/
GSInputTextWithConversion.java
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
package org.genericsystem.reactor.gs;
import java.io.Serializable;
import java.util.function.Function;
import org.genericsystem.api.core.ApiStatics;
import org.genericsystem.reactor.ReactorStatics;
import org.genericsystem.reactor.gstag.GSInputText;
import org.genericsystem.reactor.model.GenericModel;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
import javafx.beans.value.ChangeListener;
import javafx.util.StringConverter;
public class GSInputTextWithConversion extends GSInputText {
static final Logger log = LoggerFactory.getLogger(GSInputTextWithConversion.class);
public GSInputTextWithConversion(GSTag parent) {
super(parent);
addStyle("width", "100%");
addStyle("height", "100%");
createNewProperty(ReactorStatics.CONVERTER);
initProperty(ReactorStatics.CONVERTER, model -> getConverter(model));
createNewProperty(ReactorStatics.VALUE);
storeProperty(ReactorStatics.INVALID, model -> Bindings.createBooleanBinding(() -> {
boolean required = model.getGeneric().isRequiredConstraintEnabled(ApiStatics.BASE_POSITION);
String value = model.getObservableAttributes(this).get(ReactorStatics.VALUE);
if (required && (value == null || value.trim().isEmpty()))
return true;
try {
((StringConverter) getProperty(ReactorStatics.CONVERTER, model).getValue()).fromString(value);
return false;
} catch (Exception e) {
return true;
}
}, model.getObservableAttributes(this)));
bindOptionalStyleClass(ReactorStatics.INVALID, ReactorStatics.INVALID);
bindBiDirectionalAttributeOnEnter(ReactorStatics.VALUE, ReactorStatics.VALUE, model -> (StringConverter) getProperty(ReactorStatics.CONVERTER, model).getValue());
}
private void bindBiDirectionalAttributeOnEnter(String propertyName, String attributeName) {
bindBiDirectionalAttributeOnEnter(propertyName, attributeName, ApiStatics.STRING_CONVERTERS.get(String.class));
}
private <T extends Serializable> void bindBiDirectionalAttributeOnEnter(String propertyName, String attributeName, StringConverter<T> stringConverter) {
bindBiDirectionalAttributeOnEnter(propertyName, attributeName, model -> stringConverter);
}
private <T extends Serializable> void bindBiDirectionalAttributeOnEnter(String propertyName, String attributeName, Function<GenericModel, StringConverter<T>> getStringConverter) {
bindAction(model -> {
Property observable = getProperty(propertyName, model);
StringConverter stringConverter = getStringConverter.apply(model);
String attributeValue = model.getObservableAttributes(this).get(attributeName);
try {
observable.setValue(stringConverter.fromString(attributeValue));
} catch (Exception ignore) {
log.warn("Conversion exception : " + ignore.getMessage());
}
});
addPrefixBinding(model -> {
StringConverter stringConverter = getStringConverter.apply(model);
ChangeListener listener = (o, old, newValue) -> model.getObservableAttributes(this).put(attributeName, stringConverter.toString(newValue));
Property observable = getProperty(propertyName, model);
observable.addListener(listener);
});
}
public StringConverter<?> getConverter(GenericModel model) {
Class<?> clazz = model.getGeneric().getMeta().getInstanceValueClassConstraint();
if (clazz == null) {
if (model.getGeneric().getValue() != null)
clazz = model.getGeneric().getValue().getClass();
else
clazz = String.class;
}
return ApiStatics.STRING_CONVERTERS.get(clazz);
}
public static class GSInputTextEditorWithConversion extends GSInputTextWithConversion {
public GSInputTextEditorWithConversion(GSTag parent) {
super(parent);
initProperty(ReactorStatics.VALUE, model -> model.getGeneric().getValue());
bindActionToValueChangeListener(ReactorStatics.VALUE, (model, nva) -> {
if (nva != null)
model.getGeneric().updateValue(nva);
});
}
}
}