-
Notifications
You must be signed in to change notification settings - Fork 5
/
GSCompositeDiv.java
298 lines (272 loc) · 13.6 KB
/
GSCompositeDiv.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
package org.genericsystem.reactor.az3;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.function.Consumer;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.annotations.DirectSelect;
import org.genericsystem.reactor.annotations.ForEach;
import org.genericsystem.reactor.annotations.ForEach.ChildForEach;
import org.genericsystem.reactor.annotations.ReactorDependencies;
import org.genericsystem.reactor.annotations.ReactorDependencies.ChildReactorDependencies;
import org.genericsystem.reactor.annotations.Select;
import org.genericsystem.reactor.annotations.Select.ChildSelect;
import org.genericsystem.reactor.annotations.Styles.AlignItems;
import org.genericsystem.reactor.annotations.Styles.BackgroundColor;
import org.genericsystem.reactor.annotations.Styles.ChildAlignItems;
import org.genericsystem.reactor.annotations.Styles.ChildBackgroundColor;
import org.genericsystem.reactor.annotations.Styles.ChildColor;
import org.genericsystem.reactor.annotations.Styles.ChildFlex;
import org.genericsystem.reactor.annotations.Styles.ChildFlexDirection;
import org.genericsystem.reactor.annotations.Styles.ChildFlexWrap;
import org.genericsystem.reactor.annotations.Styles.ChildGenericValueBackgroundColor;
import org.genericsystem.reactor.annotations.Styles.ChildHeight;
import org.genericsystem.reactor.annotations.Styles.ChildJustifyContent;
import org.genericsystem.reactor.annotations.Styles.ChildKeepFlexDirection;
import org.genericsystem.reactor.annotations.Styles.ChildMarginBottom;
import org.genericsystem.reactor.annotations.Styles.ChildMarginRight;
import org.genericsystem.reactor.annotations.Styles.ChildOverflow;
import org.genericsystem.reactor.annotations.Styles.ChildReverseFlexDirection;
import org.genericsystem.reactor.annotations.Styles.ChildStyle;
import org.genericsystem.reactor.annotations.Styles.ChildWidth;
import org.genericsystem.reactor.annotations.Styles.Color;
import org.genericsystem.reactor.annotations.Styles.Flex;
import org.genericsystem.reactor.annotations.Styles.FlexDirectionStyle;
import org.genericsystem.reactor.annotations.Styles.FlexWrap;
import org.genericsystem.reactor.annotations.Styles.GenericValueBackgroundColor;
import org.genericsystem.reactor.annotations.Styles.Height;
import org.genericsystem.reactor.annotations.Styles.JustifyContent;
import org.genericsystem.reactor.annotations.Styles.KeepFlexDirection;
import org.genericsystem.reactor.annotations.Styles.MarginBottom;
import org.genericsystem.reactor.annotations.Styles.MarginRight;
import org.genericsystem.reactor.annotations.Styles.Overflow;
import org.genericsystem.reactor.annotations.Styles.ReverseFlexDirection;
import org.genericsystem.reactor.annotations.Styles.Style;
import org.genericsystem.reactor.annotations.Styles.Width;
import org.genericsystem.reactor.az.GSDiv;
import org.genericsystem.reactor.az.GSTagImpl;
import org.genericsystem.reactor.model.StringExtractor;
import org.genericsystem.reactor.modelproperties.GenericStringDefaults;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
public class GSCompositeDiv extends GSDiv implements Tag {
public static final Logger log = LoggerFactory.getLogger(Tag.class);
private final HashMap<Class<? extends Tag>, Tag> nodes = new LinkedHashMap<Class<? extends Tag>, Tag>() {
private static final long serialVersionUID = -6835018021862236920L;
@Override
public Tag get(Object key) {
Class<? extends Tag> searchClass = (Class<? extends Tag>) key;
Tag tag = super.get(searchClass);
if (tag == null)
for (Class<? extends Tag> clazz : keySet()) {
if (searchClass.isAssignableFrom(clazz)) {
if (tag == null) {
tag = super.get(clazz);
if (!searchClass.equals(GSCompositeDiv.this.getClass()))
System.out.println("Search : " + searchClass.getSimpleName() + " find polymorphic class : " + GSCompositeDiv.this.getClass().getSimpleName());
else
break;
} else
System.out.println("Warning : Found several results for class : " + searchClass.getSimpleName() + " on : " + GSCompositeDiv.this.getClass().getSimpleName() + " exact paths for them : " + searchClass + " " + getClass());
}
}
return tag;
};
};
public GSCompositeDiv() {
}
public GSCompositeDiv(Tag parent) {
super(parent);
processAnnotations(this);
initComposite();
init();
}
private void initComposite() {
nodes.put(getClass(), this);
processAnnotation(ReactorDependencies.class, this, annotation -> {
// System.out.println("Declaring classes : " + Arrays.toString(getClass().getDeclaredClasses()));
// System.out.println("ReactorDependencies : " + Arrays.toString(deps.value()));
for (Class<? extends GSTagImpl> clazz : ((ReactorDependencies) annotation).value())
find(clazz);
});
if (getParent() != null) {
processDecorateAnnotation(ChildReactorDependencies.class, this, annotation -> {
for (Class<? extends GSTagImpl> childClass : ((ChildReactorDependencies) annotation).value())
find(childClass);
});
}
for (Tag tag : nodes.values())
tag.postfix();
}
@Override
public <T extends Tag> T find(Class<T> tagClass) {
T result = (T) nodes.get(tagClass);
if (result == null) {
try {
result = tagClass.newInstance();
} catch (IllegalAccessException | InstantiationException e) {
throw new IllegalStateException(e);
}
((GSTagImpl) result).setParent(this);
processAnnotations(result);
if (GSCompositeDiv.class.isAssignableFrom(tagClass))
((GSCompositeDiv) result).initComposite();
result.init();
nodes.put(tagClass, result);
}
return result;
}
private static <T extends Tag> void processAnnotations(Tag result) {
if (result.getParent() != null) {
processDecorateAnnotation(ChildSelect.class, result, annotation -> {
try {
result.select(((ChildSelect) annotation).value().newInstance().get());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
});
processDecorateAnnotation(ChildForEach.class, result, annotation -> {
try {
result.forEach(((ChildForEach) annotation).value().newInstance().get());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
});
processDecorateAnnotation(ChildStyle.class, result, annotation -> result.addStyle(((ChildStyle) annotation).name(), ((ChildStyle) annotation).value()));
processDecorateAnnotation(ChildFlexDirection.class, result, annotation -> {
if (GSDiv.class.isAssignableFrom(result.getClass()))
((GSDiv) result).setDirection(((ChildFlexDirection) annotation).value());
else
log.warn("Warning: FlexDirection is applicable only to GSDiv extensions.");
});
processDecorateAnnotation(ChildKeepFlexDirection.class, result, annotation -> {
if (GSDiv.class.isAssignableFrom(result.getClass()))
((GSDiv) result).keepDirection();
else
log.warn("Warning: KeepFlexDirection is applicable only to GSDiv extensions.");
});
processDecorateAnnotation(ChildReverseFlexDirection.class, result, annotation -> {
if (GSDiv.class.isAssignableFrom(result.getClass()))
((GSDiv) result).reverseDirection();
else
log.warn("Warning: ReverseFlexDirection is applicable only to GSDiv extensions.");
});
processDecorateAnnotation(ChildGenericValueBackgroundColor.class, result, annotation -> result.addPrefixBinding(modelContext -> result.addStyle(modelContext, "background-color",
"Color".equals(StringExtractor.SIMPLE_CLASS_EXTRACTOR.apply(modelContext.getGeneric().getMeta())) ? ((GenericStringDefaults) result).getGenericStringProperty(modelContext).getValue() : "#e5ed00")));
processDecorateStyleAnnotation(ChildFlex.class, result, "flex");
processDecorateStyleAnnotation(ChildFlexWrap.class, result, "flex-wrap");
processDecorateStyleAnnotation(ChildBackgroundColor.class, result, "background-color");
processDecorateStyleAnnotation(ChildAlignItems.class, result, "align-items");
processDecorateStyleAnnotation(ChildJustifyContent.class, result, "justify-content");
processDecorateStyleAnnotation(ChildOverflow.class, result, "overflow");
processDecorateStyleAnnotation(ChildColor.class, result, "color");
processDecorateStyleAnnotation(ChildMarginRight.class, result, "margin-right");
processDecorateStyleAnnotation(ChildMarginBottom.class, result, "margin-bottom");
processDecorateStyleAnnotation(ChildHeight.class, result, "height");
processDecorateStyleAnnotation(ChildWidth.class, result, "width");
}
processAnnotation(ForEach.class, result, annotation -> {
try {
result.forEach(((ForEach) annotation).value().newInstance().get());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
});
processAnnotation(Select.class, result, annotation -> {
try {
result.select(((Select) annotation).value().newInstance().get());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
});
processAnnotation(FlexDirectionStyle.class, result, annotation -> {
if (GSDiv.class.isAssignableFrom(result.getClass()))
((GSDiv) result).setDirection(((FlexDirectionStyle) annotation).value());
else
log.warn("Warning: FlexDirectionStyle is applicable only to GSDiv extensions.");
});
processAnnotation(KeepFlexDirection.class, result, annotation -> {
if (GSDiv.class.isAssignableFrom(result.getClass()))
((GSDiv) result).keepDirection();
else
log.warn("Warning: KeepFlexDirection is applicable only to GSDiv extensions.");
});
processAnnotation(ReverseFlexDirection.class, result, annotation -> {
if (GSDiv.class.isAssignableFrom(result.getClass()))
((GSDiv) result).reverseDirection();
else
log.warn("Warning: ReverseFlexDirection is applicable only to GSDiv extensions.");
});
processAnnotation(DirectSelect.class, result, annotation -> result.select(((DirectSelect) annotation).value()));
processAnnotation(GenericValueBackgroundColor.class, result, annotation -> result.addPrefixBinding(modelContext -> result.addStyle(modelContext, "background-color",
"Color".equals(StringExtractor.SIMPLE_CLASS_EXTRACTOR.apply(modelContext.getGeneric().getMeta())) ? ((GenericStringDefaults) result).getGenericStringProperty(modelContext).getValue() : "#e5ed00")));
processAnnotation(Style.class, result, annotation -> result.addStyle(((Style) annotation).propertyName(), ((Style) annotation).propertyValue()));
processStyleAnnotation(BackgroundColor.class, result, "background-color");
processStyleAnnotation(FlexWrap.class, result, "flex-wrap");
processStyleAnnotation(Flex.class, result, "flex");
processStyleAnnotation(AlignItems.class, result, "align-items");
processStyleAnnotation(JustifyContent.class, result, "justify-content");
processStyleAnnotation(Overflow.class, result, "overflow");
processStyleAnnotation(Color.class, result, "color");
processStyleAnnotation(MarginRight.class, result, "margin-right");
processStyleAnnotation(MarginBottom.class, result, "margin-bottom");
processStyleAnnotation(Height.class, result, "height");
processStyleAnnotation(Width.class, result, "width");
}
private static <T extends Tag> void processAnnotation(Class<? extends Annotation> annotationClass, Tag result, Consumer<Annotation> consumer) {
Annotation[] annotations = result.getClass().getAnnotationsByType(annotationClass);
for (Annotation annotation : annotations)
consumer.accept(annotation);
}
private static <T extends Tag> void processStyleAnnotation(Class<? extends Annotation> annotationClass, Tag result, String propertyName) {
processAnnotation(annotationClass, result, annotation -> {
try {
result.addStyle(propertyName, (String) annotation.annotationType().getDeclaredMethod("value").invoke(annotation));
} catch (Exception e) {
throw new IllegalStateException(e);
}
});
}
private static boolean isAssignableFrom(List<Class<?>> list1, List<Class<?>> list2) {
if (list1.size() != list2.size())
return false;
for (int i = 0; i < list1.size(); i++)
if (!list1.get(i).isAssignableFrom(list2.get(i)))
return false;
return true;
}
private static <T extends Tag> void processDecorateAnnotation(Class<? extends Annotation> annotationClass, Tag result, Consumer<Annotation> consumer) {
List<Class<?>> classesToResult = new ArrayList<>();
classesToResult.add(result.getClass());
Tag current = result.getParent();
Annotation applyingAnnotation = null;
while (current != null) {
Annotation[] annotations = current.getClass().getAnnotationsByType(annotationClass);
for (Annotation annotation : annotations)
try {
Class<?>[] decorate = (Class<?>[]) annotation.annotationType().getDeclaredMethod("decorate").invoke(annotation);
if (isAssignableFrom(Arrays.asList(decorate), classesToResult))
applyingAnnotation = annotation;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new IllegalStateException(e);
}
classesToResult.add(0, current.getClass());
current = current.getParent();
}
if (applyingAnnotation != null)
consumer.accept(applyingAnnotation);
}
private static <T extends Tag> void processDecorateStyleAnnotation(Class<? extends Annotation> annotationClass, Tag result, String propertyName) {
processDecorateAnnotation(annotationClass, result, annotation -> {
try {
result.addStyle(propertyName, (String) annotation.annotationType().getDeclaredMethod("value").invoke(annotation));
} catch (Exception e) {
throw new IllegalStateException(e);
}
});
}
}