-
Notifications
You must be signed in to change notification settings - Fork 5
/
ExtendedRootTag.java
337 lines (290 loc) · 13.6 KB
/
ExtendedRootTag.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
package org.genericsystem.reactor.gscomponents;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.api.core.TagAnnotation;
import org.genericsystem.api.core.annotations.Components;
import org.genericsystem.api.core.annotations.Dependencies;
import org.genericsystem.api.core.annotations.InstanceClass;
import org.genericsystem.api.core.annotations.Meta;
import org.genericsystem.api.core.annotations.SystemGeneric;
import org.genericsystem.api.core.annotations.constraints.InstanceValueClassConstraint;
import org.genericsystem.api.core.annotations.constraints.NoInheritance;
import org.genericsystem.api.core.annotations.constraints.PropertyConstraint;
import org.genericsystem.api.core.annotations.value.ClassGenericValue;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
import org.genericsystem.reactor.AnnotationsManager;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.ExtendedAnnotationsManager;
import org.genericsystem.reactor.HtmlDomNode.RootHtmlDomNode;
import org.genericsystem.reactor.HtmlDomNode.Sender;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.TagNode;
import org.genericsystem.reactor.annotations.Children;
import org.genericsystem.reactor.annotations.Style;
import org.genericsystem.reactor.gscomponents.ExtendedRootTag.TagType.TagAnnotationAttribute;
import org.genericsystem.reactor.gscomponents.ExtendedRootTag.TagType.TagAnnotationContentAttribute;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import javafx.beans.binding.MapBinding;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.collections.ObservableSet;
import javafx.collections.WeakListChangeListener;
public class ExtendedRootTag extends RootTagImpl {
private final Root engine;
// To avoid duplicate work.
private final Map<Class<?>, GTag> storedClasses = new HashMap<>();
private final ListChangeListener<? super GTagAnnotationContent> listener = c -> {
while (c.next()) {
if (c.wasRemoved())
doStyle(c.getRemoved().stream(), ON_REMOVE);
if (c.wasAdded())
doStyle(c.getAddedSubList().stream(), ON_ADD);
}
};
private static BiConsumer<ObservableSet<GTagAnnotation>, GTagAnnotation> ON_ADD = (styles, gTagAnnotation) -> {
styles.add(gTagAnnotation);
};
private static BiConsumer<ObservableSet<GTagAnnotation>, GTagAnnotation> ON_REMOVE = (styles, gTagAnnotation) -> {
styles.remove(gTagAnnotation);
};
private void doStyle(Stream<? extends GTagAnnotationContent> streamToConsum, BiConsumer<ObservableSet<GTagAnnotation>, GTagAnnotation> action) {
streamToConsum.forEach(valueGeneric -> {
GTagAnnotation gTagAnnotation = valueGeneric.getBaseComponent();
TagAnnotation tagAnnotation = gTagAnnotation.getValue();
Class<?>[] path = tagAnnotation.getPath();
Class<?> targetTagClass = path.length == 0 ? (Class<?>) gTagAnnotation.getBaseComponent().getValue() : path[path.length - 1];
if (Style.class.equals(tagAnnotation.getAnnotationClass())) {
Set<Tag> concernedTags = searchTags(this, targetTagClass, tagAnnotation.getPath(), tagAnnotation.getPos());
concernedTags.forEach(tag -> action.accept(((GenericTagNode) tag.getTagNode()).tagAnnotations, gTagAnnotation));
}
});
}
private Set<Tag> searchTags(Tag subTree, Class<?> targetTagClass, Class<?>[] path, int[] pos) {
Set<Tag> foundTags = new HashSet<>();
if (targetTagClass.equals(subTree.getClass()) && AnnotationsManager.posMatches(pos, path, subTree))
foundTags.add(subTree);
subTree.getObservableChildren().forEach(child -> foundTags.addAll(searchTags(child, targetTagClass, path, pos)));
return foundTags;
}
public ExtendedRootTag(Root engine) {
this.engine = engine;
storedClasses.put(TagImpl.class, engine.find(GTag.class));
storeClass(this.getClass());
annotationsManager = new ExtendedAnnotationsManager(getClass());
createSubTree();
}
@Override
protected void initRoot() {
}
@Override
public RootHtmlDomNode init(Context rootModelContext, String rootId, Sender send) {
return new RootHtmlDomNode(rootModelContext, this, rootId, send) {
private final ObservableList<GTagAnnotationContent> annotationContentInstances;
{
annotationContentInstances = (ObservableList) engine.find(TagAnnotationContentAttribute.class).getObservableSubInstances();
annotationContentInstances.addListener(new WeakListChangeListener<>(listener));
}
};
}
public Root getEngine() {
return engine;
}
@Override
public TagNode buildTagNode(Tag tag) {
return new GenericTagNode(tag);
}
static long start;
static long time;
static long total;
private GTag storeClass(Class<?> clazz) {
if (!TagImpl.class.isAssignableFrom(clazz))
return getEngine().find(GTag.class);
GTag result = storedClasses.get(clazz);
if (result != null)
return result;
Generic parentGeneric = storeClass(clazz.getSuperclass());
result = (GTag) parentGeneric.getMeta().setInstance(parentGeneric, clazz);
storedClasses.put(clazz, result);
for (Children childrenAnnotation : clazz.getAnnotationsByType(Children.class)) {
result.setChildrenAnnotation(childrenAnnotation);
for (Class<?> childClass : childrenAnnotation.value())
storeClass(childClass);
}
for (Style styleAnnotation : clazz.getAnnotationsByType(Style.class))
result.setStyleAnnotation(styleAnnotation);
getEngine().getCurrentCache().flush();
return result;
}
// Called only by addStyle, not used to treat @Style annotations.
// Use the most precise path and pos possible to make sure that this style applies to the given tag and nothing else,
// so the generic annotations created here are on the root tag’s class.
@Override
public void processStyle(Tag tag, String name, String value) {
Deque<Class<?>> path = new ArrayDeque<>();
Deque<Integer> pos = new ArrayDeque<>();
Tag current = tag;
while (current != null && current.getParent() != null) {
path.push(current.getClass());
pos.push(AnnotationsManager.position(current, current.getClass()));
current = current.getParent();
}
GTag gTag = storedClasses.get(current.getClass());
GTagAnnotation styleAnnotation = (GTagAnnotation) gTag.setHolder(engine.find(TagAnnotationAttribute.class), new TagAnnotation(Style.class, path.stream().toArray(Class<?>[]::new), pos.stream().mapToInt(i -> i).toArray(), name));
((GenericTagNode) tag.getTagNode()).tagAnnotations.add(styleAnnotation);
styleAnnotation.setHolder(engine.find(TagAnnotationContentAttribute.class), new JsonObject().put("value", value).encodePrettily());
}
private static <T> T getTagClass(Tag tag, String className) {
try {
return (T) Class.forName(className);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Class " + className + " not found in children of " + tag.getClass().getSimpleName());
}
}
public class GenericTagNode extends SimpleTagNode {
private ObservableSet<GTagAnnotation> tagAnnotations = FXCollections.observableSet(new HashSet<GTagAnnotation>() {
private static final long serialVersionUID = 1887068618788935803L;
@Override
public boolean add(GTagAnnotation annotation) {
Optional<GTagAnnotation> equivAnnotation = this.stream().filter(gta -> gta.getValue().equivs(annotation.getValue())).findAny();
if (equivAnnotation.isPresent()) {
GTagAnnotation overriddenAnnotation = equivAnnotation.get();
if (overriddenAnnotation.getValue().getPath().length <= annotation.getValue().getPath().length) {
remove(overriddenAnnotation);
} else
// Found an annotation applying to this tag that’s more precise than the new annotation,
// so the new annotation is not added to the Set.
return false;
}
return super.add(annotation);
}
});
public GenericTagNode(Tag tag) {
Deque<Class<?>> classesToResult = new ArrayDeque<>();
// Retrieve all applying annotations.
Tag current = tag;
while (current != null) {
Set<GTagAnnotation> annotationsFound = selectAnnotations(current.getClass(), classesToResult, tag);
tagAnnotations.addAll(annotationsFound);
classesToResult.push(current.getClass());
current = current.getParent();
}
// Build children if there are any.
Optional<GTagAnnotation> childrenAnnotation = tagAnnotations.stream().filter(ta -> Children.class.equals(ta.getValue().getAnnotationClass())).findFirst();
if (childrenAnnotation.isPresent())
new JsonObject((String) childrenAnnotation.get().getHolder(engine.find(TagAnnotationContentAttribute.class)).getValue()).getJsonArray("value").stream().map(className -> getTagClass(tag, (String) className))
.forEach(childClass -> getObservableChildren().add(createChild(tag, (Class<? extends TagImpl>) childClass)));
}
@SuppressWarnings("unchecked")
private Set<GTagAnnotation> selectAnnotations(Class<?> annotatedClass, Deque<Class<?>> classesToResult, Tag tag) {
Set<GTagAnnotation> annotationsFound = new HashSet<>();
Generic tagClass = storedClasses.get(annotatedClass);
Snapshot<GTagAnnotation> annotations = (Snapshot) tagClass.getHolders(engine.find(TagAnnotationAttribute.class));
for (GTagAnnotation annotation : annotations) {
Class<?>[] path = annotation.getValue().getPath();
int[] pos = annotation.getValue().getPos();
Class<?> annotationClass = annotation.getValue().getAnnotationClass();
if (pos.length != 0 && pos.length != path.length)
throw new IllegalStateException("The annotation " + annotationClass.getSimpleName() + " contains a path and an array of class positions of different lengths. path: "
+ Arrays.asList(path).stream().map(c -> c.getSimpleName()).collect(Collectors.toList()) + ", positions: " + Arrays.stream(pos).boxed().collect(Collectors.toList()) + " found on class " + annotatedClass.getSimpleName());
if (AnnotationsManager.isAssignableFrom(Arrays.asList(path), new ArrayList<>(classesToResult)) && AnnotationsManager.posMatches(pos, path, tag)) {
annotationsFound.add(annotation);
}
}
return annotationsFound;
}
@Override
public ObservableMap<String, String> buildObservableStyles() {
return new MapBinding<String, String>() {// don't transmit successive invalidations
{
bind(tagAnnotations);
}
@Override
protected ObservableMap<String, String> computeValue() {
ObservableMap<String, String> styles = FXCollections.observableHashMap();
for (GTagAnnotation tagAnnotation : tagAnnotations.stream().filter(gta -> Style.class.equals(gta.getValue().getAnnotationClass())).collect(Collectors.toList())) {
GTagAnnotationContent annotationContent = (GTagAnnotationContent) tagAnnotation.getComposites().filter(g -> engine.find(TagAnnotationContentAttribute.class).equals(g.getMeta())).first();
if (annotationContent != null)
styles.put(tagAnnotation.getValue().getName(), new JsonObject(annotationContent.getValue()).getString("value"));
}
return styles;
}
};
}
}
@SystemGeneric
@InstanceClass(GTag.class)
@Dependencies({ TagAnnotationAttribute.class })
@InstanceValueClassConstraint(Class.class)
public static interface TagType extends Generic {
@SystemGeneric
@Components(TagType.class)
@InstanceValueClassConstraint(TagAnnotation.class)
@Dependencies({ TagAnnotationContentAttribute.class })
@InstanceClass(GTagAnnotation.class)
public static interface TagAnnotationAttribute extends Generic {
}
@SystemGeneric
@Components(TagAnnotationAttribute.class)
@InstanceValueClassConstraint(String.class)
@PropertyConstraint
@NoInheritance
@InstanceClass(GTagAnnotationContent.class)
public static interface TagAnnotationContentAttribute extends Generic {
}
}
@SystemGeneric
@Meta(TagType.class)
@ClassGenericValue(TagImpl.class)
public static interface GTag extends Generic {
@Override
default public Class<?> getValue() {
return (Class<?>) Generic.super.getValue();
}
default void setChildrenAnnotation(Children annotation) {
GTagAnnotation gTagAnnotation = (GTagAnnotation) setHolder(getRoot().find(TagAnnotationAttribute.class), new TagAnnotation(Children.class, annotation.path(), annotation.pos()));
gTagAnnotation.setHolder(getRoot().find(TagAnnotationContentAttribute.class), new JsonObject().put("value", new JsonArray(Arrays.asList(annotation.value()))).encodePrettily());
}
default void setStyleAnnotation(Style annotation) {
GTagAnnotation gStyleAnnotation = (GTagAnnotation) setHolder(getRoot().find(TagAnnotationAttribute.class), new TagAnnotation(Style.class, annotation.path(), annotation.pos(), annotation.name()));
gStyleAnnotation.setHolder(getRoot().find(TagAnnotationContentAttribute.class), new JsonObject().put("value", annotation.value()).encodePrettily());
}
}
public static interface GTagAnnotation extends Generic {
@Override
default public GTag getBaseComponent() {
return (GTag) Generic.super.getBaseComponent();
}
@Override
default public TagAnnotation getValue() {
return (TagAnnotation) Generic.super.getValue();
}
}
public static interface GTagAnnotationContent extends Generic {
@Override
default public GTagAnnotation getBaseComponent() {
return (GTagAnnotation) Generic.super.getBaseComponent();
}
@Override
default public String getValue() {
return (String) Generic.super.getValue();
}
default public JsonObject getJSonValue() {
return new JsonObject(getValue());
}
}
}