-
Notifications
You must be signed in to change notification settings - Fork 5
/
GSInputTextWithConversion.java
92 lines (81 loc) · 3.41 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
package org.genericsystem.reactor.gs;
import java.io.Serializable;
import org.genericsystem.api.core.ApiStatics;
import org.genericsystem.common.Generic;
import org.genericsystem.reactor.ReactorStatics;
import org.genericsystem.reactor.gstag.HtmlInputText;
import org.genericsystem.reactor.model.GenericModel;
import org.genericsystem.reactor.modelproperties.SelectionDefaults;
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<T extends Serializable> extends HtmlInputText {
static final Logger log = LoggerFactory.getLogger(GSInputTextWithConversion.class);
public GSInputTextWithConversion(GSTag parent) {
super(parent);
addStyle("width", "100%");
addStyle("height", "100%");
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 (value == null || value.trim().isEmpty())
return required;
try {
getConverter(model).fromString(value);
return false;
} catch (Exception e) {
return true;
}
}, model.getObservableAttributes(this)));
bindOptionalStyleClass(ReactorStatics.INVALID, ReactorStatics.INVALID);
bindBiDirectionalAttributeOnEnter(ReactorStatics.VALUE, ReactorStatics.VALUE);
}
private void bindBiDirectionalAttributeOnEnter(String propertyName, String attributeName) {
bindAction(model -> {
try {
getProperty(propertyName, model).setValue(getConverter(model).fromString(model.getObservableAttributes(this).get(attributeName)));
} catch (Exception ignore) {
log.warn("Conversion exception : " + ignore.getMessage());
}
});
addPrefixBinding(model -> {
ChangeListener listener = (o, old, newValue) -> model.getObservableAttributes(this).put(attributeName, getConverter(model).toString((T) newValue));
getProperty(propertyName, model).addListener(listener);
});
}
public StringConverter<T> getConverter(GenericModel model) {
Class<?> clazz = model.getGeneric().getInstanceValueClassConstraint();
if (clazz == null)
clazz = String.class;
return ApiStatics.STRING_CONVERTERS.get(clazz);
}
public static class GSInputTextEditorWithConversion<T extends Serializable> extends GSInputTextWithConversion<T> implements SelectionDefaults {
public GSInputTextEditorWithConversion(GSTag parent) {
super(parent);
initProperty(ReactorStatics.VALUE, model -> model.getGeneric().getValue());
addPropertyChangeListener(ReactorStatics.VALUE, (model, nva) -> {
if (nva != null) {
Generic updatedGeneric = model.getGeneric().updateValue(nva);
Property<Generic> genericProperty = getUpdatedGenericProperty(model);
if (genericProperty != null)
genericProperty.setValue(updatedGeneric);
}
});
}
@Override
public StringConverter<T> 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);
}
}
}