-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tag.java
566 lines (464 loc) · 21 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
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.defaults.tools.TransformationObservableList;
import org.genericsystem.reactor.html.TextPropertyDefaults;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.json.JsonObject;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.beans.value.WeakChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.collections.ObservableSet;
import javafx.collections.SetChangeListener;
import javafx.collections.WeakMapChangeListener;
import javafx.collections.WeakSetChangeListener;
import javafx.util.StringConverter;
/**
* @author Nicolas Feybesse
*
* @param <N>
*/
public abstract class Tag<M extends Model> implements TextPropertyDefaults<M> {
private static int count = 0;
private static final Logger log = LoggerFactory.getLogger(Tag.class);
private final String tag;
private BiConsumer<Tag<?>, ViewContext<?>> metaBinding;
private final List<BiConsumer<Model, HtmlDomNode>> preFixedBindings = new ArrayList<>();
private final List<BiConsumer<Model, HtmlDomNode>> postFixedBindings = new ArrayList<>();
private final Tag<?> parent;
private final List<Tag<?>> children = new ArrayList<>();
@Override
public String toString() {
return tag + " " + getClass().getName();
}
protected Tag(Tag<?> parent, String tag) {
this.tag = tag;
this.parent = parent;
if (parent != null)
parent.getChildren().add(this);
}
public String getTag() {
return tag;
}
protected List<BiConsumer<Model, HtmlDomNode>> getPreFixedBindings() {
return preFixedBindings;
}
protected List<BiConsumer<Model, HtmlDomNode>> getPostFixedBindings() {
return postFixedBindings;
}
protected BiConsumer<Tag<?>, ViewContext<?>> getMetaBinding() {
return metaBinding;
}
protected void setMetaBinding(BiConsumer<Tag<?>, ViewContext<?>> metaBinding) {
if (this.metaBinding != null)
throw new IllegalStateException("MetaBinding already defined");
this.metaBinding = metaBinding;
}
public ServerWebSocket getWebSocket() {
return getParent().getWebSocket();
}
@Override
public void addPrefixBinding(Consumer<M> consumer) {
preFixedBindings.add((modelContext, node) -> consumer.accept((M) modelContext));
}
public void addPostfixBinding(Consumer<M> consumer) {
postFixedBindings.add((modelContext, node) -> consumer.accept((M) modelContext));
}
protected <NODE extends HtmlDomNode> void addActionBinding(Function<NODE, Property<Consumer<Object>>> applyOnNode, Consumer<M> applyOnModel) {
preFixedBindings.add((modelContext, node) -> applyOnNode.apply((NODE) node).setValue(o -> applyOnModel.accept((M) modelContext)));
}
public void bindOptionalStyleClass(String styleClass, String propertyName) {
addPrefixBinding(modelContext -> {
ObservableValue<Boolean> optional = getObservableValue(propertyName, modelContext);
Set<String> styleClasses = modelContext.getObservableStyleClasses(this);
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);
}
public <MODEL extends Model> void forEach(Function<MODEL, ObservableList<M>> applyOnModel) {
forEach(applyOnModel, (model, subElement) -> {
subElement.parent = model;
return subElement;
});
}
protected <MODEL extends Model, SUBELEMENT> void forEach(Function<MODEL, ObservableList<SUBELEMENT>> applyOnModel, BiFunction<MODEL, SUBELEMENT, M> modelBuilder) {
setMetaBinding((childElement, viewContext) -> {
MODEL model = viewContext.getModelContext();
ObservableList<SUBELEMENT> subElements = applyOnModel.apply(model);
ObservableList<M> subModels = new TransformationObservableList<SUBELEMENT, M>(subElements, (index, subModel) -> {
M resultModel = modelBuilder.apply(model, subModel);
viewContext.createViewContextChild(index, resultModel, childElement);
return resultModel;
}, Model::destroy);
setSubModels(model, childElement, subModels);
});
}
protected <SUBMODEL extends Model> void setSubModels(Model model, Tag<?> child, ObservableList<SUBMODEL> subModels) {
model.setSubContexts(child, subModels);
}
@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].getViewContext(tag.getParent()) == null)
model[0] = model[0].getParent();
tag = tag.getParent();
}
return null;
}
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].getViewContext(tag.getParent()) == null)
model[0] = model[0].getParent();
tag = tag.getParent();
}
return null;
}
public void addSelectionIndex(int value) {
addPrefixBinding(modelContext -> modelContext.getSelectionIndex(this).setValue(value));
}
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));
});
}
public void createNewProperty(String propertyName) {
addPrefixBinding(modelContext -> modelContext.createNewProperty(this, propertyName));
}
public <T> void createNewInitializedProperty(String propertyName, Function<M, T> getInitialValue) {
createNewProperty(propertyName);
initProperty(propertyName, getInitialValue);
}
public <T> void initProperty(String propertyName, Function<M, T> getInitialValue) {
addPrefixBinding(modelContext -> getProperty(propertyName, modelContext).setValue(getInitialValue.apply(modelContext)));
}
public <T> void storeProperty(String propertyName, Function<M, ObservableValue<T>> applyOnModel) {
addPrefixBinding(modelContext -> modelContext.storeProperty(this, propertyName, applyOnModel.apply(modelContext)));
}
@Override
public <T> void storePropertyWithoutCheck(String propertyName, M model, Function<M, ObservableValue<T>> applyOnModel) {
model.storePropertyWithoutCheck(this, propertyName, applyOnModel.apply(model));
}
public void addStyle(String propertyName, String value) {
addPrefixBinding(model -> model.getObservableStyles(this).put(propertyName, value));
}
public void bindStyle(String style, String modelPropertyName) {
bindMapElement(style, modelPropertyName, model -> model.getObservableStyles(this));
}
public void bindStyle(String style, String propertyName, Function<M, ObservableValue<String>> applyOnModel) {
storeProperty(propertyName, applyOnModel);
bindMapElement(style, propertyName, model -> model.getObservableStyles(this));
}
public void addStyleClasses(String... styleClasses) {
addPrefixBinding(model -> model.getObservableStyleClasses(this).addAll(Arrays.asList(styleClasses)));
}
public void addStyleClass(String styleClass) {
addPrefixBinding(model -> model.getObservableStyleClasses(this).add(styleClass));
}
public void addAttribute(String attributeName, String value) {
addPrefixBinding(model -> model.getObservableAttributes(this).put(attributeName, value));
}
public void bindAttribute(String attributeName, String propertyName) {
bindMapElement(attributeName, propertyName, model -> model.getObservableAttributes(this));
}
public void bindAttribute(String attributeName, String propertyName, Function<M, ObservableValue<String>> applyOnModel) {
storeProperty(propertyName, applyOnModel);
bindMapElement(attributeName, propertyName, model -> model.getObservableAttributes(this));
}
public void bindBiDirectionalAttribute(String propertyName, String attributeName) {
bindBiDirectionalMapElement(propertyName, attributeName, model -> model.getObservableAttributes(this));
}
public <T extends Serializable> void bindBiDirectionalAttribute(String propertyName, String attributeName, StringConverter<T> stringConverter) {
bindBiDirectionalMapElement(propertyName, attributeName, model -> model.getObservableAttributes(this), stringConverter);
}
public <T extends Serializable> void bindBiDirectionalAttribute(String propertyName, String attributeName, Function<M, StringConverter<T>> getStringConverter) {
bindBiDirectionalMapElement(propertyName, attributeName, model -> model.getObservableAttributes(this), 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 -> model.getObservableAttributes(this), 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);
}
});
}
@Override
public ViewContext getViewContext(M model) {
return model.getViewContext(this);
}
protected abstract HtmlDomNode createNode(String parentId);
protected List<Tag<?>> getChildren() {
return children;
}
@SuppressWarnings("unchecked")
public <COMPONENT extends Tag<?>> COMPONENT getParent() {
return (COMPONENT) parent;
}
private static final String MSG_TYPE = "msgType";
private static final String ADD = "A";
private static final String UPDATE = "U";
private static final String REMOVE = "R";
private static final String UPDATE_TEXT = "UT";
private static final String UPDATE_SELECTION = "US";
private static final String ADD_STYLECLASS = "AC";
private static final String REMOVE_STYLECLASS = "RC";
private static final String ADD_STYLE = "AS";
private static final String REMOVE_STYLE = "RS";
private static final String ADD_ATTRIBUTE = "AA";
private static final String REMOVE_ATTRIBUTE = "RA";
private static final String PARENT_ID = "parentId";
public static final String ID = "nodeId";
private static final String NEXT_ID = "nextId";
private static final String STYLE_PROPERTY = "styleProperty";
private static final String STYLE_VALUE = "styleValue";
private static final String ATTRIBUTE_NAME = "attributeName";
private static final String ATTRIBUTE_VALUE = "attributeValue";
private static final String STYLECLASS = "styleClass";
private static final String TEXT_CONTENT = "textContent";
private static final String TAG_HTML = "tagHtml";
private static final String ELT_TYPE = "eltType";
public class HtmlDomNode {
private final String id;
private final String parentId;
private final ObservableSet<String> styleClasses = FXCollections.observableSet();
private final ObservableMap<String, String> styles = FXCollections.observableHashMap();
private final ObservableMap<String, String> attributes = FXCollections.observableHashMap();
private final MapChangeListener<String, String> stylesListener = change -> {
if (!change.wasAdded() || change.getValueAdded() == null || change.getValueAdded().equals(""))
sendMessage(new JsonObject().put(MSG_TYPE, REMOVE_STYLE).put(ID, getId()).put(STYLE_PROPERTY, change.getKey()));
else if (change.wasAdded())
sendMessage(new JsonObject().put(MSG_TYPE, ADD_STYLE).put(ID, getId()).put(STYLE_PROPERTY, change.getKey()).put(STYLE_VALUE, change.getValueAdded()));
};
private final MapChangeListener<String, String> attributesListener = change -> {
if (!change.wasAdded() || change.getValueAdded() == null || change.getValueAdded().equals(""))
sendMessage(new JsonObject().put(MSG_TYPE, REMOVE_ATTRIBUTE).put(ID, getId()).put(ATTRIBUTE_NAME, change.getKey()));
else if (change.wasAdded())
sendMessage(new JsonObject().put(MSG_TYPE, ADD_ATTRIBUTE).put(ID, getId()).put(ATTRIBUTE_NAME, change.getKey()).put(ATTRIBUTE_VALUE, change.getValueAdded()));
};
private final SetChangeListener<String> styleClassesListener = change -> {
if (change.wasAdded())
sendMessage(new JsonObject().put(MSG_TYPE, ADD_STYLECLASS).put(ID, getId()).put(STYLECLASS, change.getElementAdded()));
else
sendMessage(new JsonObject().put(MSG_TYPE, REMOVE_STYLECLASS).put(ID, getId()).put(STYLECLASS, change.getElementRemoved()));
};
public ChangeListener<String> getTextListener() {
return (o, old, newValue) -> sendMessage(new JsonObject().put(MSG_TYPE, UPDATE_TEXT).put(ID, getId()).put(TEXT_CONTENT, newValue != null ? newValue : ""));
}
public ObservableMap<String, String> getStyles() {
return styles;
}
public ObservableMap<String, String> getAttributes() {
return attributes;
}
public HtmlDomNode(String parentId) {
assert parentId != null;
this.parentId = parentId;
this.id = String.format("%010d", Integer.parseInt(this.hashCode() + "")).substring(0, 10);
styles.addListener(new WeakMapChangeListener<>(stylesListener));
styleClasses.addListener(new WeakSetChangeListener<>(styleClassesListener));
attributes.addListener(new WeakMapChangeListener<>(attributesListener));
}
public void sendAdd(int index) {
JsonObject jsonObj = new JsonObject().put(MSG_TYPE, ADD);
jsonObj.put(PARENT_ID, parentId);
jsonObj.put(ID, id);
jsonObj.put(TAG_HTML, getTag());
jsonObj.put(NEXT_ID, index);
fillJson(jsonObj);
// System.out.println(jsonObj.encodePrettily());
sendMessage(jsonObj);
}
public JsonObject fillJson(JsonObject jsonObj) {
return null;
}
public void sendRemove() {
sendMessage(new JsonObject().put(MSG_TYPE, REMOVE).put(ID, id));
// System.out.println(new JsonObject().put(MSG_TYPE, REMOVE).put(ID, id).encodePrettily());
}
public void sendMessage(JsonObject jsonObj) {
jsonObj.put("count", count++);
// if (jsonObj.getString(MSG_TYPE).equals(ADD) || jsonObj.getString(MSG_TYPE).equals(REMOVE))
// System.out.println(jsonObj.encodePrettily());
getWebSocket().writeFinalTextFrame(jsonObj.encode());
}
public ObservableSet<String> getStyleClasses() {
return styleClasses;
}
public Property<String> getStyle(String propertyName) {
Property<String> property = new SimpleStringProperty(styles.get(propertyName));
property.addListener((c, o, n) -> styles.put(propertyName, n));
return property;
}
public String getId() {
return id;
}
public void handleMessage(JsonObject json) {
}
}
public class ActionHtmlNode extends HtmlDomNode {
public ActionHtmlNode(String parentId) {
super(parentId);
}
private final Property<Consumer<Object>> actionProperty = new SimpleObjectProperty<>();
public Property<Consumer<Object>> getActionProperty() {
return actionProperty;
}
@Override
public void handleMessage(JsonObject json) {
getActionProperty().getValue().accept(new Object());
}
}
public class SelectableHtmlDomNode extends ActionHtmlNode {
private static final String SELECTED_INDEX = "selectedIndex";
private Property<Number> selectionIndex = new SimpleIntegerProperty();
private final ChangeListener<Number> indexListener = (o, old, newValue) -> {
// System.out.println(new JsonObject().put(MSG_TYPE, UPDATE_SELECTION).put(ID, getId()).put(SELECTED_INDEX, newValue != null ? newValue : 0)
// .encodePrettily());
sendMessage(new JsonObject().put(MSG_TYPE, UPDATE_SELECTION).put(ID, getId()).put(SELECTED_INDEX, newValue != null ? newValue : 0));
};
public SelectableHtmlDomNode(String parentId) {
super(parentId);
selectionIndex.addListener(new WeakChangeListener<>(indexListener));
}
public Property<Number> getSelectionIndex() {
return selectionIndex;
}
@Override
public void handleMessage(JsonObject json) {
if (UPDATE.equals(json.getString(MSG_TYPE))) {
getSelectionIndex().setValue(json.getInteger(SELECTED_INDEX));
// System.out.println("Selected index : " + getSelectionIndex().getValue());
}
}
}
public class InputTextHtmlDomNode extends HtmlDomNode {
private final Property<String> inputString = new SimpleStringProperty();
private final Property<Consumer<Object>> enterProperty = new SimpleObjectProperty<>();
public InputTextHtmlDomNode(String parentId) {
super(parentId);
inputString.addListener(new WeakChangeListener<>(inputListener));
}
private final ChangeListener<String> inputListener = (o, old, newValue) -> {
assert old != newValue;
System.out.println(new JsonObject().put(MSG_TYPE, UPDATE_TEXT).put(ID, getId()).encodePrettily());
sendMessage(fillJson(new JsonObject().put(MSG_TYPE, UPDATE_TEXT).put(ID, getId())));
};
public Property<String> getInputString() {
return inputString;
}
@Override
public JsonObject fillJson(JsonObject jsonObj) {
super.fillJson(jsonObj);
return jsonObj.put("type", "text").put(TEXT_CONTENT, inputString.getValue());
}
@Override
public void handleMessage(JsonObject json) {
if (ADD.equals(json.getString(MSG_TYPE)))
getEnterProperty().getValue().accept(new Object());
if (UPDATE.equals(json.getString(MSG_TYPE)))
getAttributes().put(ReactorStatics.VALUE, json.getString(TEXT_CONTENT));
}
public Property<Consumer<Object>> getEnterProperty() {
return enterProperty;
}
}
public class InputCheckHtmlDomNode extends HtmlDomNode {
private final String type;
public InputCheckHtmlDomNode(String parentId, String type) {
super(parentId);
this.type = type;
}
@Override
public JsonObject fillJson(JsonObject jsonObj) {
super.fillJson(jsonObj);
return jsonObj.put("type", type);
}
@Override
public void handleMessage(JsonObject json) {
if ("checkbox".equals(json.getString(ELT_TYPE)))
getAttributes().put(ReactorStatics.CHECKED, json.getBoolean(ReactorStatics.CHECKED) ? ReactorStatics.CHECKED : "");
}
}
}