-
Notifications
You must be signed in to change notification settings - Fork 5
/
HtmlDomNode.java
382 lines (321 loc) · 14.1 KB
/
HtmlDomNode.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
package org.genericsystem.reactor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.genericsystem.defaults.tools.TransformationObservableList;
import org.genericsystem.reactor.FilteredChildren.FilteredChildContexts;
import org.genericsystem.reactor.FilteredChildren.FilteredTagChildren;
import org.genericsystem.reactor.contextproperties.ActionDefaults;
import org.genericsystem.reactor.contextproperties.SelectionDefaults;
import io.reactivex.disposables.CompositeDisposable;
import io.vertx.core.json.JsonObject;
import javafx.beans.binding.BooleanExpression;
import javafx.beans.property.Property;
import javafx.beans.value.ChangeListener;
import javafx.collections.ListChangeListener;
import javafx.collections.MapChangeListener;
import javafx.collections.SetChangeListener;
public class HtmlDomNode {
static int count = 0;
private final String id;
private HtmlDomNode parent;
private Tag tag;
private Context context;
private boolean destroyed = false;
final Consumer<Tag> tagAdder = tagAdder();
private final CompositeDisposable disposables = new CompositeDisposable();
private Map<Tag, Integer> sizeBySubTag = new IdentityHashMap<Tag, Integer>() {
private static final long serialVersionUID = 6725720602283055930L;
@Override
public Integer get(Object key) {
Integer size = super.get(key);
if (size == null)
put((Tag) key, size = 0);
return size;
};
};
private ListChangeListener<Tag> tagListener = change -> {
while (change.next()) {
if (change.wasRemoved())
change.getRemoved().forEach(childTag -> context.removeTag(childTag));
if (change.wasAdded())
change.getAddedSubList().forEach(tagAdder::accept);
}
};
public HtmlDomNode(HtmlDomNode parent, Context context, Tag tag) {
this.id = String.format("%010d", Integer.parseInt(this.hashCode() + "")).substring(0, 10);
this.parent = parent;
this.tag = tag;
this.context = context;
tag.getRootTag().initDomNode(this);
}
protected <BETWEEN> void init(int index) {
context.register(this);
if (parent != null)
insertChild(index);
// Add the “opaque” class if the generic contained in the context is in the cache (unsaved).
if (parent != null) {
BooleanExpression parentInCache = parent.getModelContext().isInCache();
BooleanExpression inCache = context.isInCache();
tag.bindOptionalStyleClass("opaque", "isInCache", context, inCache.and(parentInCache.not()));
}
for (Consumer<Context> binding : tag.getPreFixedBindings())
context.getCache().safeExecute(() -> binding.accept(context));
assert (!context.containsAttribute(tag, "filteredChildren"));
FilteredTagChildren filteredChildren = new FilteredTagChildren(tag, context);
tag.addContextAttribute("filteredChildren", context, filteredChildren);
for (Tag childTag : filteredChildren.filteredList)
tagAdder.accept(childTag);
filteredChildren.filteredList.addListener(tagListener);
for (Consumer<Context> binding : tag.getPostFixedBindings())
context.getCache().safeExecute(() -> binding.accept(context));
}
void destroy() {
// System.out.println("Attempt to destroy : " + getId());
assert !destroyed : "Node : " + getId();
destroyed = true;
((FilteredTagChildren) tag.getContextAttribute("filteredChildren", context)).filteredList.removeListener(tagListener);
for (Tag childTag : tag.getObservableChildren()) {
childTag.getMetaBindingProperty().removeListener(metaBindingListeners.get(childTag));
if (childTag.getMetaBinding() != null && context.getSubContexts(childTag) != null)
((FilteredChildContexts<?>) childTag.getContextAttribute("filteredContexts", context)).transformationListSubContexts.unbind();
}
disposables.dispose();
tag.getDomNodeTextProperty(context).removeListener(textListener);
tag.getDomNodeStyles(context).removeListener(stylesListener);
tag.getDomNodeAttributes(context).removeListener(attributesListener);
tag.getDomNodeStyleClasses(context).removeListener(styleClassesListener);
getRootHtmlDomNode().remove(getId());
if (parent != null)
parent.decrementSize(tag);
}
public static interface Sender {
public void send(String message);
}
public CompositeDisposable getDisposables() {
return disposables;
}
public List<HtmlDomNode> getChildren() {
List<HtmlDomNode> result = new ArrayList<>();
List<Tag> subTags = tag.getObservableChildren();
for (Tag subTag : subTags) {
if (subTag.getMetaBinding() == null)
result.add(context.getHtmlDomNode(subTag));
else
for (Context subContext : context.getSubContexts(subTag))
result.add(subContext.getHtmlDomNode(subTag));
}
return result;
}
private <BETWEEN> Consumer<Tag> tagAdder() {
return childTag -> {
Property<MetaBinding<BETWEEN>> metaBinding = childTag.getMetaBindingProperty();
metaBinding.addListener(metaBindingListeners.get(childTag));
updateMetaBinding(childTag, metaBinding.getValue());
};
}
private <BETWEEN> void updateMetaBinding(Tag childTag, MetaBinding<BETWEEN> metaBinding) {
if (metaBinding != null) {
if (context.getSubContexts(childTag) == null) {
FilteredChildContexts<BETWEEN> subContexts = new FilteredChildContexts<>(metaBinding, childTag, context);
childTag.addContextAttribute("filteredContexts", context, subContexts);
context.setSubContexts(childTag, new TransformationObservableList<Context, Context>(subContexts.filteredSubContexts, (i, subContext) -> {
childTag.createNode(this, subContext).init(computeIndex(i, childTag));
return subContext;
}, subContext -> subContext.removeTag(childTag)));
}
} else if (context.getHtmlDomNode(childTag) == null)
childTag.createNode(this, context).init(computeIndex(0, childTag));
}
private int computeIndex(int indexInChildren, Tag childElement) {
for (Tag child : context.getRootContext().getObservableChildren(tag)) {
if (child == childElement)
return indexInChildren;
indexInChildren += sizeBySubTag.get(child);
}
return indexInChildren;
}
public Context getModelContext() {
return context;
}
protected RootHtmlDomNode getRootHtmlDomNode() {
return parent.getRootHtmlDomNode();
}
void insertChild(int index) {
parent.incrementSize(tag);
sendAdd(index);
getRootHtmlDomNode().add(getId(), this);
}
private void incrementSize(Tag child) {
sizeBySubTag.put(child, sizeBySubTag.get(child) + 1);
}
private void decrementSize(Tag child) {
int size = sizeBySubTag.get(child) - 1;
assert size >= 0;
if (size == 0)
sizeBySubTag.remove(child);// remove map if empty
else
sizeBySubTag.put(child, size);
}
public Sender getSender() {
return parent.getSender();
}
private final MapChangeListener<String, String> stylesListener = change -> {
if (!change.wasAdded() || change.getValueAdded() == null || change.getValueAdded().equals(""))
sendMessage(new JsonObject().put(ReactorStatics.MSG_TYPE, ReactorStatics.REMOVE_STYLE).put(ReactorStatics.ID, getId()).put(ReactorStatics.STYLE_PROPERTY, change.getKey()));
else if (change.wasAdded())
sendMessage(new JsonObject().put(ReactorStatics.MSG_TYPE, ReactorStatics.ADD_STYLE).put(ReactorStatics.ID, getId()).put(ReactorStatics.STYLE_PROPERTY, change.getKey()).put(ReactorStatics.STYLE_VALUE, change.getValueAdded()));
};
private final MapChangeListener<String, String> attributesListener = change -> {
if (!change.wasAdded() || change.getValueAdded() == null || change.getValueAdded().equals(""))
sendMessage(new JsonObject().put(ReactorStatics.MSG_TYPE, ReactorStatics.REMOVE_ATTRIBUTE).put(ReactorStatics.ID, getId()).put(ReactorStatics.ATTRIBUTE_NAME, change.getKey()));
else if (change.wasAdded())
sendMessage(new JsonObject().put(ReactorStatics.MSG_TYPE, ReactorStatics.ADD_ATTRIBUTE).put(ReactorStatics.ID, getId()).put(ReactorStatics.ATTRIBUTE_NAME, change.getKey()).put(ReactorStatics.ATTRIBUTE_VALUE, change.getValueAdded()));
};
private final SetChangeListener<String> styleClassesListener = change -> {
if (change.wasAdded())
sendMessage(new JsonObject().put(ReactorStatics.MSG_TYPE, ReactorStatics.ADD_STYLECLASS).put(ReactorStatics.ID, getId()).put(ReactorStatics.STYLECLASS, change.getElementAdded()));
else
sendMessage(new JsonObject().put(ReactorStatics.MSG_TYPE, ReactorStatics.REMOVE_STYLECLASS).put(ReactorStatics.ID, getId()).put(ReactorStatics.STYLECLASS, change.getElementRemoved()));
};
private final ChangeListener<String> textListener = (o, old,
newValue) -> sendMessage(new JsonObject().put(ReactorStatics.MSG_TYPE, ReactorStatics.UPDATE_TEXT).put(ReactorStatics.ID, getId()).put(ReactorStatics.TEXT_CONTENT, newValue != null ? newValue : ""));
private final Map<Tag, ChangeListener<MetaBinding<?>>> metaBindingListeners = new HashMap<Tag, ChangeListener<MetaBinding<?>>>() {
private static final long serialVersionUID = 6179552869588758790L;
@Override
public ChangeListener<MetaBinding<?>> get(Object key) {
ChangeListener<MetaBinding<?>> listener = super.get(key);
if (listener == null && key instanceof Tag) {
Tag childTag = (Tag) key;
put(childTag, listener = (o, ov, nv) -> {
context.removeTag(childTag);
updateMetaBinding(childTag, nv);
});
}
return listener;
}
};
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(ReactorStatics.MSG_TYPE, ReactorStatics.UPDATE_SELECTION).put(ReactorStatics.ID, getId()).put(ReactorStatics.SELECTED_INDEX, newValue != null ? newValue : 0));
};
public ChangeListener<Number> getIndexListener() {
return indexListener;
}
public ChangeListener<String> getTextListener() {
return textListener;
}
public MapChangeListener<String, String> getStylesListener() {
return stylesListener;
}
public MapChangeListener<String, String> getAttributesListener() {
return attributesListener;
}
public SetChangeListener<String> getStyleClassesListener() {
return styleClassesListener;
}
public void sendAdd(int index) {
JsonObject jsonObj = new JsonObject().put(ReactorStatics.MSG_TYPE, ReactorStatics.ADD);
jsonObj.put(ReactorStatics.PARENT_ID, getParentId());
jsonObj.put(ReactorStatics.ID, id);
jsonObj.put(ReactorStatics.TAG_HTML, getTag().getTag());
jsonObj.put(ReactorStatics.NEXT_ID, index);
// System.out.println(jsonObj.encodePrettily());
sendMessage(jsonObj);
}
public void sendRemove() {
sendMessage(new JsonObject().put(ReactorStatics.MSG_TYPE, ReactorStatics.REMOVE).put(ReactorStatics.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());
getSender().send(jsonObj.encode());
}
public String getId() {
return id;
}
public String getParentId() {
return parent.getId();
}
public Tag getTag() {
return tag;
}
public void handleMessage(JsonObject json) {
}
public String toHTMLString() {
String tagText = tag.getTag();
String classes = tag.getDomNodeStyleClasses(context).stream().collect(Collectors.joining(" "));
classes = ("section".equals(tagText) || "div".equals(tagText) || "header".equals(tagText) || "footer".equals(tagText)) ? classes += " adding" : classes;
String styles = tag.getDomNodeStyles(context).entrySet().stream().map(m -> m.getKey() + ": " + m.getValue()).collect(Collectors.joining("; "));
String tagAttributes = tag.getDomNodeAttributes(context).entrySet().stream().filter(m -> m.getValue() != null && !m.getValue().isEmpty()).map(m -> m.getKey() + "=\"" + m.getValue() + "\"").collect(Collectors.joining(""));
String body = "\n<" + tagText + " id=\"" + id + "\"";
if (!classes.equals(""))
body += " class=\"" + classes + "\"";
if (!styles.equals(""))
body += " style=\"" + styles + "\"";
body += tagAttributes + ">";
for (HtmlDomNode node : getChildren())
body += node.toHTMLString();
String tagValue = tag.getDomNodeTextProperty(context).getValue();
if (tagValue != null)
body += tagValue;
body += "</" + tagText + ">\n";
return body;
}
public static class HtmlDomNodeAction extends HtmlDomNode {
public HtmlDomNodeAction(HtmlDomNode parent, Context context, Tag tag) {
super(parent, context, tag);
}
@Override
public void handleMessage(JsonObject json) {
((ActionDefaults) getTag()).getActionProperty(getModelContext()).getValue().accept(new Object());
}
}
public static class HtmlDomNodeCheckbox extends HtmlDomNode {
public HtmlDomNodeCheckbox(HtmlDomNode parent, Context context, Tag tag) {
super(parent, context, tag);
}
@Override
public void handleMessage(JsonObject json) {
getTag().getDomNodeAttributes(getModelContext()).put(ReactorStatics.CHECKED, json.getBoolean(ReactorStatics.CHECKED) ? ReactorStatics.CHECKED : "");
}
}
public static class HtmlDomNodeInputText extends HtmlDomNode {
public HtmlDomNodeInputText(HtmlDomNode parent, Context context, Tag tag) {
super(parent, context, tag);
}
@Override
public void handleMessage(JsonObject json) {
super.handleMessage(json);
if (ReactorStatics.ADD.equals(json.getString(ReactorStatics.MSG_TYPE))) {
Property<Consumer<Object>> action = ((ActionDefaults) getTag()).getActionProperty(getModelContext());
if (action != null)
action.getValue().accept(new Object());
}
if (ReactorStatics.UPDATE.equals(json.getString(ReactorStatics.MSG_TYPE)))
getTag().getDomNodeAttributes(getModelContext()).put("value", json.getString(ReactorStatics.TEXT_CONTENT));
}
}
public static class HtmlDomNodeSelect extends HtmlDomNode {
public HtmlDomNodeSelect(HtmlDomNode parent, Context context, Tag tag) {
super(parent, context, tag);
}
@Override
public void handleMessage(JsonObject json) {
if (ReactorStatics.UPDATE.equals(json.getString(ReactorStatics.MSG_TYPE))) {
((SelectionDefaults) getTag()).getSelectionIndex(getModelContext()).setValue(json.getInteger(ReactorStatics.SELECTED_INDEX));
}
}
}
}