-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tag.java
323 lines (271 loc) · 12.3 KB
/
Tag.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package org.genericsystem.reactor;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import org.genericsystem.api.core.ApiStatics;
import org.genericsystem.reactor.HtmlDomNode.RootHtmlDomNode;
import org.genericsystem.reactor.modelproperties.AttributesDefaults;
import org.genericsystem.reactor.modelproperties.StyleClassesDefaults;
import org.genericsystem.reactor.modelproperties.StylesDefaults;
import org.genericsystem.reactor.modelproperties.TextPropertyDefaults;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.vertx.core.http.ServerWebSocket;
import javafx.beans.property.Property;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.util.StringConverter;
/**
* @author Nicolas Feybesse
*
* @param <N>
*/
public abstract class Tag<M extends Model> implements TextPropertyDefaults<M>, StylesDefaults<M>, AttributesDefaults<M>, StyleClassesDefaults<M> {
private static final Logger log = LoggerFactory.getLogger(Tag.class);
private final String tag;
private MetaBinding<?> metaBinding;
// private Function<Model, ObservableList<?>> metaBinding;
// private BiFunction<Model, ?, M> modelBuilder;
private final List<BiConsumer<Model, HtmlDomNode>> preFixedBindings = new ArrayList<>();
private final List<BiConsumer<Model, HtmlDomNode>> postFixedBindings = new ArrayList<>();
private final Tag parent;
private final ObservableList<Tag<?>> children = FXCollections.observableArrayList();
@Override
public String toString() {
return tag + " " + getClass().getName();
}
protected Tag(Tag<?> parent, String tag) {
this.tag = tag;
this.parent = parent;
if (parent != null)
parent.getObservableChildren().add(this);
}
public String getTag() {
return tag;
}
protected List<BiConsumer<Model, HtmlDomNode>> getPreFixedBindings() {
return preFixedBindings;
}
protected List<BiConsumer<Model, HtmlDomNode>> getPostFixedBindings() {
return postFixedBindings;
}
@SuppressWarnings("unchecked")
protected <BETWEEN> MetaBinding<BETWEEN> getMetaBinding() {
return (MetaBinding<BETWEEN>) metaBinding;
}
protected <BETWEEN> void setMetaBinding(MetaBinding<BETWEEN> metaBinding) {
if (this.metaBinding != null)
throw new IllegalStateException("MetaBinding already defined");
this.metaBinding = metaBinding;
}
@Override
public void addPrefixBinding(Consumer<M> consumer) {
preFixedBindings.add((modelContext, node) -> consumer.accept((M) modelContext));
}
@Override
public void addPostfixBinding(Consumer<M> consumer) {
postFixedBindings.add((modelContext, node) -> consumer.accept((M) modelContext));
}
public void bindOptionalStyleClass(String styleClass, String propertyName) {
addPrefixBinding(modelContext -> {
ObservableValue<Boolean> optional = getObservableValue(propertyName, modelContext);
Set<String> styleClasses = getDomNodeStyleClasses(modelContext);
Consumer<Boolean> consumer = bool -> {
if (Boolean.TRUE.equals(bool))
styleClasses.add(styleClass);
else
styleClasses.remove(styleClass);
};
consumer.accept(optional.getValue());
optional.addListener((o, ov, nv) -> consumer.accept(nv));
});
}
public void bindOptionalStyleClass(String styleClass, String modelPropertyName, Function<M, ObservableValue<Boolean>> applyOnModel) {
storeProperty(modelPropertyName, applyOnModel);
bindOptionalStyleClass(styleClass, modelPropertyName);
}
protected <BETWEEN> void forEach(Function<Model, ObservableList<BETWEEN>> applyOnModel, BiFunction<Model, BETWEEN, Model> modelBuilder) {
setMetaBinding(new MetaBinding<BETWEEN>(applyOnModel, modelBuilder));
}
@Override
public <T> Property<T> getProperty(String propertyName, Model model) {
return getProperty(propertyName, new Model[] { model });
}
private <T> Property<T> getProperty(String propertyName, Model[] model) {
Tag<?> tag = this;
while (tag != null && model[0] != null) {
if (model[0].containsProperty(tag, propertyName))
return model[0].getProperty(tag, propertyName);
if (tag.metaBinding != null && model[0].getHtmlDomNode(tag.getParent()) == null)
model[0] = model[0].getParent();
tag = tag.getParent();
}
return null;
}
@Override
public <T> Property<T> getInheritedProperty(String propertyName, Model[] model, Tag<?>[] tag) {
while (tag != null && model[0] != null) {
if (tag[0].metaBinding != null && model[0].getHtmlDomNode(tag[0].getParent()) == null)
model[0] = model[0].getParent();
tag[0] = tag[0].getParent();
if (model[0] != null && model[0].containsProperty(tag[0], propertyName))
return model[0].getProperty(tag[0], propertyName);
}
return null;
}
@Override
public <T> ObservableValue<T> getObservableValue(String propertyName, Model model) {
return getObservableValue(propertyName, new Model[] { model });
}
private <T> ObservableValue<T> getObservableValue(String propertyName, Model[] model) {
Tag<?> tag = this;
while (tag != null && model[0] != null) {
if (model[0].containsProperty(tag, propertyName))
return model[0].getObservableValue(tag, propertyName);
if (tag.metaBinding != null && model[0].getHtmlDomNode(tag.getParent()) == null)
model[0] = model[0].getParent();
tag = tag.getParent();
}
return null;
}
private void bindMapElement(String name, String propertyName, Function<Model, Map<String, String>> getMap) {
addPrefixBinding(model -> {
Map<String, String> map = getMap.apply(model);
ChangeListener<String> listener = (o, old, newValue) -> map.put(name, newValue);
ObservableValue<String> observable = getObservableValue(propertyName, model);
observable.addListener(listener);
map.put(name, observable.getValue());
});
}
private void bindBiDirectionalMapElement(String propertyName, String name, Function<Model, ObservableMap<String, String>> getMap) {
bindBiDirectionalMapElement(propertyName, name, getMap, ApiStatics.STRING_CONVERTERS.get(String.class));
}
private <T extends Serializable> void bindBiDirectionalMapElement(String propertyName, String name, Function<Model, ObservableMap<String, String>> getMap, StringConverter<T> stringConverter) {
bindBiDirectionalMapElement(propertyName, name, getMap, model -> stringConverter);
}
private <T extends Serializable> void bindBiDirectionalMapElement(String propertyName, String name, Function<Model, ObservableMap<String, String>> getMap, Function<M, StringConverter<T>> getStringConverter) {
addPrefixBinding(modelContext -> {
ObservableMap<String, String> map = getMap.apply(modelContext);
StringConverter<T> stringConverter = getStringConverter.apply(modelContext);
ChangeListener<T> listener = (o, old, newValue) -> map.put(name, stringConverter.toString(newValue));
Property<T> observable = getProperty(propertyName, modelContext);
observable.addListener(listener);
map.addListener((MapChangeListener<String, String>) c -> {
if (!name.equals(c.getKey()))
return;
try {
observable.setValue(c.wasAdded() ? stringConverter.fromString(c.getValueAdded()) : null);
} catch (Exception ignore) {
log.warn("Conversion exception : " + ignore.getMessage());
}
});
map.put(name, stringConverter.toString(observable.getValue()));
});
}
public <T extends Serializable> void addPropertyChangeListener(String propertyName, BiConsumer<M, T> listener) {
addPrefixBinding(modelContext -> {
ObservableValue<T> observable = getObservableValue(propertyName, modelContext);
observable.addListener((o, old, nva) -> listener.accept(modelContext, nva));
});
}
@Override
public void createNewProperty(String propertyName) {
addPrefixBinding(modelContext -> modelContext.createNewProperty(this, propertyName));
}
@Override
public <T> void createNewInitializedProperty(String propertyName, M model, Function<M, T> getInitialValue) {
model.createNewProperty(this, propertyName);
getProperty(propertyName, model).setValue(getInitialValue.apply(model));
}
@Override
public <T> void createNewInitializedProperty(String propertyName, Function<M, T> getInitialValue) {
createNewProperty(propertyName);
initProperty(propertyName, getInitialValue);
}
@Override
public <T> void initProperty(String propertyName, Function<M, T> getInitialValue) {
addPrefixBinding(modelContext -> getProperty(propertyName, modelContext).setValue(getInitialValue.apply(modelContext)));
}
@Override
public <T> void storeProperty(String propertyName, Function<M, ObservableValue<T>> applyOnModel) {
addPrefixBinding(modelContext -> modelContext.storeProperty(this, propertyName, applyOnModel.apply(modelContext)));
}
@Override
public <T> void storeProperty(String propertyName, M model, Function<M, ObservableValue<T>> applyOnModel) {
model.storeProperty(this, propertyName, applyOnModel.apply(model));
}
public void addStyle(String propertyName, String value) {
addPrefixBinding(model -> getDomNodeStyles(model).put(propertyName, value));
}
public void bindStyle(String style, String modelPropertyName) {
bindMapElement(style, modelPropertyName, model -> getDomNodeStyles(model));
}
public void bindStyle(String style, String propertyName, Function<M, ObservableValue<String>> applyOnModel) {
storeProperty(propertyName, applyOnModel);
bindMapElement(style, propertyName, model -> getDomNodeStyles(model));
}
public void addStyleClasses(String... styleClasses) {
addPrefixBinding(model -> getDomNodeStyleClasses(model).addAll(Arrays.asList(styleClasses)));
}
public void addStyleClass(String styleClass) {
addPrefixBinding(model -> getDomNodeStyleClasses(model).add(styleClass));
}
public void addAttribute(String attributeName, String value) {
addPrefixBinding(model -> getDomNodeAttributes(model).put(attributeName, value));
}
public void bindAttribute(String attributeName, String propertyName) {
bindMapElement(attributeName, propertyName, model -> getDomNodeAttributes(model));
}
public void bindAttribute(String attributeName, String propertyName, Function<M, ObservableValue<String>> applyOnModel) {
storeProperty(propertyName, applyOnModel);
bindMapElement(attributeName, propertyName, model -> getDomNodeAttributes(model));
}
public void bindBiDirectionalAttribute(String propertyName, String attributeName) {
bindBiDirectionalMapElement(propertyName, attributeName, model -> getDomNodeAttributes(model));
}
public <T extends Serializable> void bindBiDirectionalAttribute(String propertyName, String attributeName, StringConverter<T> stringConverter) {
bindBiDirectionalMapElement(propertyName, attributeName, model -> getDomNodeAttributes(model), stringConverter);
}
public <T extends Serializable> void bindBiDirectionalAttribute(String propertyName, String attributeName, Function<M, StringConverter<T>> getStringConverter) {
bindBiDirectionalMapElement(propertyName, attributeName, model -> getDomNodeAttributes(model), getStringConverter);
}
public void bindOptionalBiDirectionalAttribute(String propertyName, String attributeName, String attributeValue) {
bindOptionalBiDirectionalAttribute(propertyName, attributeName, attributeValue, null);
}
public void bindOptionalBiDirectionalAttribute(String propertyName, String attributeName, String attributeValue, String attributeValueFalse) {
bindBiDirectionalMapElement(propertyName, attributeName, model -> getDomNodeAttributes(model), new StringConverter<Boolean>() {
@Override
public String toString(Boolean bool) {
return Boolean.TRUE.equals(bool) ? attributeValue : attributeValueFalse;
}
@Override
public Boolean fromString(String string) {
return attributeValue.equals(string);
}
});
}
protected abstract HtmlDomNode createNode(String parentId);
protected ObservableList<Tag<?>> getObservableChildren() {
return children;
}
@SuppressWarnings("unchecked")
public <COMPONENT extends Tag<?>> COMPONENT getParent() {
return (COMPONENT) parent;
}
public static interface RootTag<M extends Model> {
default RootHtmlDomNode<M> init(M rootModelContext, String rootId, ServerWebSocket webSocket) {
return new RootHtmlDomNode<M>(rootModelContext, this, rootId, webSocket);
}
}
}