-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tag.java
487 lines (404 loc) · 18.8 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
package org.genericsystem.reactor;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.common.Generic;
import org.genericsystem.reactor.context.ForEachExtractor;
import org.genericsystem.reactor.context.TagSwitcher;
import org.genericsystem.reactor.contextproperties.ActionDefaults;
import org.genericsystem.reactor.contextproperties.AttributesDefaults;
import org.genericsystem.reactor.contextproperties.DisplayDefaults;
import org.genericsystem.reactor.contextproperties.GSBuilderDefaults;
import org.genericsystem.reactor.contextproperties.GenericStringDefaults;
import org.genericsystem.reactor.contextproperties.SelectionDefaults;
import org.genericsystem.reactor.contextproperties.StepperDefaults;
import org.genericsystem.reactor.contextproperties.StyleClassesDefaults;
import org.genericsystem.reactor.contextproperties.StylesDefaults;
import org.genericsystem.reactor.contextproperties.TextPropertyDefaults;
import org.genericsystem.reactor.contextproperties.UserRoleDefaults;
import org.genericsystem.reactor.gscomponents.TagImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.reactivex.Observable;
import javafx.beans.property.Property;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.util.StringConverter;
/**
* @author Nicolas Feybesse
*
* @param <N>
*/
public interface Tag extends TagNode, ActionDefaults, SelectionDefaults, StepperDefaults, GSBuilderDefaults, TextPropertyDefaults, StylesDefaults, AttributesDefaults, StyleClassesDefaults, GenericStringDefaults, DisplayDefaults, UserRoleDefaults {
public static final Logger log = LoggerFactory.getLogger(Tag.class);
public String getTag();
public void setTag(String tagName);
public Class<? extends HtmlDomNode> getDomNodeClass();
public void setDomNodeClass(Class<? extends HtmlDomNode> domNodeClass);
public List<Consumer<Context>> getPreFixedBindings();
public List<Consumer<Context>> getPostFixedBindings();
public <BETWEEN> MetaBinding<BETWEEN> getMetaBinding();
public <BETWEEN> Property<MetaBinding<BETWEEN>> getMetaBindingProperty();
public <BETWEEN> void setMetaBinding(MetaBinding<BETWEEN> metaBinding);
@Override
default void addPrefixBinding(Consumer<Context> consumer) {
getPreFixedBindings().add(consumer);
}
@Override
default void addPostfixBinding(Consumer<Context> consumer) {
getPostFixedBindings().add(consumer);
}
default void bindOptionalStyleClass(String styleClass, String propertyName) {
addPrefixBinding(context -> bindOptionalStyleClass(styleClass, propertyName, context));
}
default void bindOptionalStyleClass(String styleClass, String propertyName, Context context) {
ObservableValue<Boolean> optional = getContextObservableValue(propertyName, context);
Set<String> styleClasses = getDomNodeStyleClasses(context);
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));
}
default void bindOptionalStyleClass(String styleClass, String modelPropertyName, Function<Context, ObservableValue<Boolean>> applyOnModel) {
addContextAttribute(modelPropertyName, applyOnModel);
bindOptionalStyleClass(styleClass, modelPropertyName);
}
default void bindOptionalStyleClass(String styleClass, String modelPropertyName, Context context, ObservableValue<Boolean> obs) {
context.addContextAttribute(this, modelPropertyName, obs);
bindOptionalStyleClass(styleClass, modelPropertyName, context);
}
default void forEach2(Function<Context, Observable<Snapshot<Generic>>> applyOnModel) {
setMetaBinding(MetaBinding.forEachMetaBinding(applyOnModel));
}
default void forEach(ForEachExtractor observableListExtractor) {
forEach2(model -> observableListExtractor.apply(model.getGenerics()));
}
default void forEach(Tag parent) {
forEach(gs -> parent.getObservableListExtractor().apply(gs));
}
default void select(Function<Generic[], Generic> genericSupplier) {
forEach((ForEachExtractor) gs -> {
Generic generic = genericSupplier.apply(gs);
return Observable.just(generic != null ? Snapshot.singleton(generic) : Snapshot.empty());
});
}
default void select_(Function<Context, Observable<Snapshot<Context>>> applyOnModel) {
setMetaBinding(MetaBinding.selectMetaBinding(applyOnModel));
}
default void select__(Function<Context, Observable<Optional<Context>>> applyOnModelContext) {
select_(context -> applyOnModelContext.apply(context).map(opt -> opt.isPresent() ? Snapshot.singleton(opt.get()) : Snapshot.empty()));
}
default void select(Class<?> genericClass) {
forEach((ForEachExtractor) gs -> Observable.just(Snapshot.singleton(gs[0].getRoot().find(genericClass))));
}
default ForEachExtractor getObservableListExtractor() {
return ForEachExtractor.SUBINSTANCES;
}
@Override
default <T> T getContextAttribute(String attributeName, Context context) {
class AttributeComputer {
T getAttribute(String attributeName, Context[] contexts) {
Tag tag = Tag.this;
while (tag != null && contexts[0] != null) {
if (contexts[0].containsAttribute(tag, attributeName))
return contexts[0].<T> getAttribute(tag, attributeName);
if (tag.getMetaBinding() != null && contexts[0].getHtmlDomNode(tag.getParent()) == null)
contexts[0] = contexts[0].getParent();
tag = tag.getParent();
}
return null;
}
}
return new AttributeComputer().getAttribute(attributeName, new Context[] { context });
}
@Override
default <T> Property<T> getContextProperty(String propertyName, Context context) {
return getContextAttribute(propertyName, context);
}
@Override
default <T> ObservableValue<T> getContextObservableValue(String propertyName, Context context) {
return getContextAttribute(propertyName, context);
}
@Override
default <T> T getInheritedContextAttribute(String attributeName, Context[] context, Tag[] tag) {
while (tag[0] != null && context[0] != null) {
if (tag[0].getMetaBinding() != null && context[0].getHtmlDomNode(tag[0].getParent()) == null)
context[0] = context[0].getParent();
tag[0] = tag[0].getParent();
if (context[0] != null && context[0].containsAttribute(tag[0], attributeName))
return context[0].<T> getAttribute(tag[0], attributeName);
}
return null;
}
@Override
default <T extends Serializable> void addContextPropertyChangeListener(String propertyName, BiConsumer<Context, T> listener) {
addPrefixBinding(context -> {
ObservableValue<T> observable = getContextObservableValue(propertyName, context);
observable.addListener((o, old, nva) -> listener.accept(context, nva));
});
}
/**
* Creates a new context attribute with the given name and value. Throws an exception if an attribute with this name already exists.
*
* @throws IllegalStateException
* If an attribute with this name already exists.
*/
@Override
default <T> void addContextAttribute(String attributeName, Function<Context, T> applyOnModel) {
addPrefixBinding(context -> context.addContextAttribute(this, attributeName, applyOnModel.apply(context)));
}
/**
* Creates a new context attribute for the given context with the given name and value. Throws an exception if an attribute with this name already exists.
*
* @throws IllegalStateException
* If an attribute with this name already exists.
*/
@Override
default <T> void addContextAttribute(String attributeName, Context context, T value) {
context.addContextAttribute(this, attributeName, value);
}
/**
* Sets the attribute with the given name to the given value. If the attribute does not exist yet, it is created, otherwise it is replaced.
*/
@Override
default <T> void setContextAttribute(String attributeName, Context context, T value) {
context.setContextAttribute(this, attributeName, value);
}
/**
* Sets the attribute with the given name to the given value. If the attribute does not exist yet, it is created, otherwise it is replaced.
*/
@Override
default <T> void setContextAttribute(String attributeName, Function<Context, T> getValue) {
addPrefixBinding(context -> setContextAttribute(attributeName, context, getValue.apply(context)));
}
/**
* Sets the value of a context attribute that is a property. If no attribute with the given name exists, one is created, otherwise the existing property’s value is replaced.
*
* @param attributeName
* The attribute’s name.
* @param context
* The context to use.
* @param value
* The value of the property contained in the attribute.
*/
@Override
default <T> void setContextPropertyValue(String attributeName, Context context, T value) {
context.setContextPropertyValue(this, attributeName, value);
}
/**
* Sets the value of a context attribute that is a property. If no attribute with the given name exists, one is created, otherwise the existing property’s value is replaced.
*
* @param attributeName
* The attribute’s name.
* @param getValue
* The method to apply to the context to retrieve the value to use.
*/
@Override
default <T> void setContextPropertyValue(String attributeName, Function<Context, T> getValue) {
addPrefixBinding(context -> setContextPropertyValue(attributeName, context, getValue.apply(context)));
}
default <T> void setInheritedContextPropertyValue(String attributeName, Context context, T value) {
Property<T> contextProperty = getContextProperty(attributeName, context);
if (contextProperty == null)
setContextPropertyValue(attributeName, context, value);
else
contextProperty.setValue(value);
}
/**
* Creates a new context attribute with the given name containing an empty property. Throws an exception if an attribute with this name already exists.
*/
@Override
default void createNewContextProperty(String propertyName) {
addPrefixBinding(context -> context.createNewContextProperty(this, propertyName));
}
/**
* Creates a new context attribute with the given name containing an empty property. Throws an exception if an attribute with this name already exists.
*/
default void createNewContextProperty(String propertyName, Context context) {
context.createNewContextProperty(this, propertyName);
}
/**
* Creates a new context attribute with the given name containing a property with the given value. Throws an exception if an attribute with this name already exists.
*/
@Override
default <T> void createNewInitializedProperty(String propertyName, Context context, T initialValue) {
context.createNewContextProperty(this, propertyName);
getContextProperty(propertyName, context).setValue(initialValue);
}
/**
* Creates a new context attribute with the given name containing a property with the value obtained by applying getInitialValue to the context. Throws an exception if an attribute with this name already exists.
*/
@Override
default <T> void createNewInitializedProperty(String propertyName, Function<Context, T> getInitialValue) {
addPrefixBinding(context -> createNewInitializedProperty(propertyName, context, getInitialValue.apply(context)));
}
default void bindMapElement(String name, String propertyName, Function<Context, 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 = getContextObservableValue(propertyName, model);
observable.addListener(listener);
map.put(name, observable.getValue());
});
}
default void bindBiDirectionalMapElement(String propertyName, String name, Function<Context, ObservableMap<String, String>> getMap) {
bindBiDirectionalMapElement(propertyName, name, getMap, ReactorStatics.STRING_CONVERTERS.get(String.class));
}
default <T extends Serializable> void bindBiDirectionalMapElement(String propertyName, String name, Function<Context, ObservableMap<String, String>> getMap, StringConverter<T> stringConverter) {
bindBiDirectionalMapElement(propertyName, name, getMap, model -> stringConverter);
}
default <T extends Serializable> void bindBiDirectionalMapElement(String propertyName, String name, Function<Context, ObservableMap<String, String>> getMap, Function<Context, 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 = getContextProperty(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.info("Conversion exception: {}.", ignore.toString());
}
});
map.put(name, stringConverter.toString(observable.getValue()));
});
}
default void addStyle(String propertyName, String value) {
addPrefixBinding(context -> addStyle(context, propertyName, value));
}
@Override
default void addStyle(Context context, String propertyName, String value) {
getDomNodeStyles(context).put(propertyName, value);
}
default void bindStyle(String style, String modelPropertyName) {
bindMapElement(style, modelPropertyName, model -> getDomNodeStyles(model));
}
default void bindStyle(String style, String propertyName, Function<Context, ObservableValue<String>> applyOnModel) {
addContextAttribute(propertyName, applyOnModel);
bindMapElement(style, propertyName, model -> getDomNodeStyles(model));
}
default void addStyleClasses(String... styleClasses) {
addPrefixBinding(model -> getDomNodeStyleClasses(model).addAll(Arrays.asList(styleClasses)));
}
default void addStyleClass(String styleClass) {
addPrefixBinding(model -> getDomNodeStyleClasses(model).add(styleClass));
}
default void addStyleClass(Context context, String styleClass) {
getDomNodeStyleClasses(context).add(styleClass);
}
default void addAttribute(String attributeName, String value) {
addPrefixBinding(model -> getDomNodeAttributes(model).put(attributeName, value));
}
default void addAttribute(Context context, String attributeName, String value) {
getDomNodeAttributes(context).put(attributeName, value);
}
default void removeStyleClass(Context context, String styleClass) {
getDomNodeStyleClasses(context).remove(styleClass);
}
default void removeStyleClass(String styleClass) {
addPrefixBinding(context -> getDomNodeStyleClasses(context).remove(styleClass));
}
default void bindAttribute(String attributeName, String propertyName) {
bindMapElement(attributeName, propertyName, model -> getDomNodeAttributes(model));
}
default void bindAttribute(String attributeName, String propertyName, Function<Context, ObservableValue<String>> applyOnModel) {
addContextAttribute(propertyName, applyOnModel);
bindMapElement(attributeName, propertyName, model -> getDomNodeAttributes(model));
}
default void bindBiDirectionalAttribute(String propertyName, String attributeName) {
bindBiDirectionalMapElement(propertyName, attributeName, model -> getDomNodeAttributes(model));
}
default <T extends Serializable> void bindBiDirectionalAttribute(String propertyName, String attributeName, StringConverter<T> stringConverter) {
bindBiDirectionalMapElement(propertyName, attributeName, model -> getDomNodeAttributes(model), stringConverter);
}
default <T extends Serializable> void bindBiDirectionalAttribute(String propertyName, String attributeName, Function<Context, StringConverter<T>> getStringConverter) {
bindBiDirectionalMapElement(propertyName, attributeName, model -> getDomNodeAttributes(model), getStringConverter);
}
default void bindOptionalBiDirectionalAttribute(String propertyName, String attributeName, String attributeValue) {
bindOptionalBiDirectionalAttribute(propertyName, attributeName, attributeValue, null);
}
default 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);
}
});
}
default HtmlDomNode createNode(HtmlDomNode parent, Context context) {
try {
return getDomNodeClass().getConstructor(HtmlDomNode.class, Context.class, Tag.class).newInstance(parent, context, this);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new IllegalStateException(e);
}
};
default <T extends TagImpl> TagImpl createChild(Class<T> clazz) {
T result = null;
try {
result = clazz.newInstance();
} catch (IllegalAccessException | InstantiationException e) {
throw new IllegalStateException(e);
}
result.initTag(this);
return result;
}
default void initTag(Tag parent) {
setParent(parent);
setTagNode(getRootTag().buildTagNode(this));
getRootTag().processAnnotations(this);
init();
parent.getObservableChildren().add(this);
}
@Override
public ObservableList<Tag> getObservableChildren();
public TagNode getTagNode();
public void setTagNode(TagNode tagNode);
default void init() {
}
public <COMPONENT extends Tag> COMPONENT getParent();
public void setParent(Tag parent);
default RootTag getRootTag() {
return getParent().getRootTag();
}
public ObservableList<TagSwitcher> getObservableSwitchers();
public void addSwitcher(TagSwitcher switcher);
default String defaultToString() {
return getTag() + " " + getClass().getName();
}
default <T extends Tag> T find(Class<T> tagClass) {
return find(tagClass, 0);
}
default <T extends Tag> T find(Class<T> tagClass, int pos) {
int posFound = 0;
for (Tag child : getObservableChildren()) {
if (tagClass.isAssignableFrom(child.getClass())) {
if (posFound == pos)
return (T) child;
posFound++;
}
}
throw new IllegalStateException("No tag corresponding to class " + tagClass.getSimpleName() + " found, position " + pos + ".");
}
}