-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tag.java
384 lines (316 loc) · 14.4 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
package org.genericsystem.reactor;
import org.genericsystem.reactor.modelproperties.AttributesDefaults;
import org.genericsystem.reactor.modelproperties.DisplayDefaults;
import org.genericsystem.reactor.modelproperties.GenericStringDefaults;
import org.genericsystem.reactor.modelproperties.StyleClassesDefaults;
import org.genericsystem.reactor.modelproperties.StylesDefaults;
import org.genericsystem.reactor.modelproperties.TextPropertyDefaults;
import org.genericsystem.reactor.modelproperties.UserRoleDefaults;
import java.io.Serializable;
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.Consumer;
import java.util.function.Function;
import org.genericsystem.api.core.ApiStatics;
import org.genericsystem.common.Generic;
import org.genericsystem.defaults.tools.BindingsTools;
import org.genericsystem.reactor.model.ObservableListExtractor;
import org.genericsystem.reactor.model.TagSwitcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javafx.beans.binding.ListBinding;
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 interface Tag extends TagNode, TextPropertyDefaults, StylesDefaults, AttributesDefaults, StyleClassesDefaults, GenericStringDefaults, DisplayDefaults, UserRoleDefaults {
public static final Logger log = LoggerFactory.getLogger(Tag.class);
public String getTag();
public List<Consumer<Context>> getPreFixedBindings();
public List<Consumer<Context>> getPostFixedBindings();
public <BETWEEN> MetaBinding<BETWEEN> getMetaBinding();
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(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));
});
}
default void bindOptionalStyleClass(String styleClass, String modelPropertyName, Function<Context, ObservableValue<Boolean>> applyOnModel) {
storeProperty(modelPropertyName, applyOnModel);
bindOptionalStyleClass(styleClass, modelPropertyName);
}
default void forEach2(Function<Context, ObservableList<Generic>> applyOnModel) {
setMetaBinding(MetaBinding.forEachMetaBinding(applyOnModel));
}
default void forEach(ObservableListExtractor 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((ObservableListExtractor) gs -> {
Generic generic = genericSupplier.apply(gs);
return generic != null ? FXCollections.singletonObservableList(generic) : FXCollections.emptyObservableList();
});
}
default void select_(Function<Context, ObservableList<Context>> applyOnModel) {
setMetaBinding(MetaBinding.selectMetaBinding(applyOnModel));
}
default void select__(Function<Context, ObservableValue<Context>> applyOnModelContext) {
// fix probable issues with transmitSuccessiveInvalidations
select_(model -> BindingsTools.transmitSuccessiveInvalidations(new ListBinding<Context>() {
ObservableValue<Context> ov = applyOnModelContext.apply(model);
{
bind(ov);
}
@Override
protected ObservableList<Context> computeValue() {
Context model = ov.getValue();
return model != null ? FXCollections.singletonObservableList(model) : FXCollections.emptyObservableList();
}
}));
}
default void select(Class<?> genericClass) {
forEach((ObservableListExtractor) gs -> FXCollections.singletonObservableList(gs[0].getRoot().find(genericClass)));
}
default ObservableListExtractor getObservableListExtractor() {
return ObservableListExtractor.SUBINSTANCES;
}
@Override
default <T> Property<T> getProperty(String propertyName, Context model) {
class PropertyComputer {
Property<T> getProperty(String propertyName, Context[] models) {
Tag tag = Tag.this;
while (tag != null && models[0] != null) {
if (models[0].containsProperty(tag, propertyName))
return models[0].getProperty(tag, propertyName);
if (tag.getMetaBinding() != null && models[0].getHtmlDomNode(tag.getParent()) == null)
models[0] = models[0].getParent();
tag = tag.getParent();
}
return null;
}
}
return new PropertyComputer().getProperty(propertyName, new Context[] { model });
}
@Override
default <T> Property<T> getInheritedProperty(String propertyName, Context[] model, Tag[] tag) {
while (tag != null && model[0] != null) {
if (tag[0].getMetaBinding() != 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].<T> getProperty(tag[0], propertyName);
}
return null;
}
@Override
default <T> ObservableValue<T> getObservableValue(String propertyName, Context model) {
class ObservableValueComputer {
ObservableValue<T> getObservableValue(String propertyName, Context[] model) {
Tag tag = Tag.this;
while (tag != null && model[0] != null) {
if (model[0].containsProperty(tag, propertyName))
return model[0].<T> getObservableValue(tag, propertyName);
if (tag.getMetaBinding() != null && model[0].getHtmlDomNode(tag.getParent()) == null)
model[0] = model[0].getParent();
tag = tag.getParent();
}
return null;
}
}
return new ObservableValueComputer().getObservableValue(propertyName, new Context[] { model });
}
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 = getObservableValue(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, ApiStatics.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 = 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()));
});
}
@Override
default <T extends Serializable> void addPropertyChangeListener(String propertyName, BiConsumer<Context, T> listener) {
addPrefixBinding(modelContext -> {
ObservableValue<T> observable = getObservableValue(propertyName, modelContext);
observable.addListener((o, old, nva) -> listener.accept(modelContext, nva));
});
}
@Override
default void createNewProperty(String propertyName) {
addPrefixBinding(modelContext -> modelContext.createNewProperty(this, propertyName));
}
@Override
default <T> void createNewInitializedProperty(String propertyName, Context context, Function<Context, T> getInitialValue) {
context.createNewProperty(this, propertyName);
getProperty(propertyName, context).setValue(getInitialValue.apply(context));
}
@Override
default <T> void createNewInitializedProperty(String propertyName, Function<Context, T> getInitialValue) {
createNewProperty(propertyName);
initProperty(propertyName, getInitialValue);
}
@Override
default <T> void initProperty(String propertyName, Function<Context, T> getInitialValue) {
addPrefixBinding(modelContext -> getProperty(propertyName, modelContext).setValue(getInitialValue.apply(modelContext)));
}
@Override
default <T> void storeProperty(String propertyName, Function<Context, ObservableValue<T>> applyOnModel) {
addPrefixBinding(modelContext -> modelContext.storeProperty(this, propertyName, applyOnModel.apply(modelContext)));
}
@Override
default <T> void storeProperty(String propertyName, Context context, Function<Context, ObservableValue<T>> applyOnModel) {
context.storeProperty(this, propertyName, applyOnModel.apply(context));
}
default void addStyle(String propertyName, String value) {
addPrefixBinding(model -> getDomNodeStyles(model).put(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) {
storeProperty(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 bindAttribute(String attributeName, String propertyName) {
bindMapElement(attributeName, propertyName, model -> getDomNodeAttributes(model));
}
default void bindAttribute(String attributeName, String propertyName, Function<Context, ObservableValue<String>> applyOnModel) {
storeProperty(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 modelContext) {
return new HtmlDomNode(parent, modelContext, this);
};
@Override
public ObservableList<Tag> getObservableChildren();
default void init() {
}
public <COMPONENT extends Tag> COMPONENT getParent();
default RootTag getRootTag() {
return getParent().getRootTag();
}
public List<TagSwitcher> getSwitchers();
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 (!tagClass.equals(child.getClass()))
System.out.println("Search : " + tagClass.getSimpleName() + " find polymorphic class : " + child.getClass().getSimpleName());
if (posFound == pos)
return (T) child;
posFound++;
}
}
throw new IllegalStateException("No tag corresponding to class " + tagClass.getSimpleName() + " found, position " + pos + ".");
}
}