-
Notifications
You must be signed in to change notification settings - Fork 5
/
CompositeTagImpl.java
182 lines (166 loc) · 7.16 KB
/
CompositeTagImpl.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
package org.genericsystem.reactor.gs3;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
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.ForEach.ParentForEach;
import org.genericsystem.reactor.annotations.Parent;
import org.genericsystem.reactor.annotations.ReactorDependencies;
import org.genericsystem.reactor.annotations.Select;
import org.genericsystem.reactor.annotations.Style;
import org.genericsystem.reactor.annotations.Style.AlignItems;
import org.genericsystem.reactor.annotations.Style.BackgroundColor;
import org.genericsystem.reactor.annotations.Style.ChildFlexDirection;
import org.genericsystem.reactor.annotations.Style.Color;
import org.genericsystem.reactor.annotations.Style.Flex;
import org.genericsystem.reactor.annotations.Style.FlexDirection;
import org.genericsystem.reactor.annotations.Style.FlexWrap;
import org.genericsystem.reactor.annotations.Style.JustifyContent;
import org.genericsystem.reactor.annotations.Style.Overflow;
import org.genericsystem.reactor.annotations.Style.ParentFlexDirection;
import org.genericsystem.reactor.gs.GSDiv;
import org.genericsystem.reactor.gs.GSTagImpl;
public class CompositeTagImpl extends GSDiv implements Tag {
private final HashMap<Class<? extends GSTagImpl>, GSTagImpl> nodes = new LinkedHashMap<Class<? extends GSTagImpl>, GSTagImpl>() {
private static final long serialVersionUID = -6835018021862236920L;
@Override
public GSTagImpl get(Object key) {
Class<? extends GSTagImpl> searchClass = (Class<? extends GSTagImpl>) key;
GSTagImpl tag = super.get(searchClass);
if (tag == null)
for (Class<? extends GSTagImpl> clazz : keySet()) {
if (searchClass.isAssignableFrom(clazz)) {
if (tag == null) {
tag = super.get(clazz);
System.out.println("search : " + searchClass.getSimpleName() + " find polymorphic class : " + CompositeTagImpl.this.getClass().getSimpleName());
} else
System.out.println("Warning : Found several results for class : " + searchClass.getSimpleName() + " on : " + CompositeTagImpl.this.getClass().getSimpleName() + " exact paths for them : " + searchClass + " " + getClass());
}
}
return tag;
};
};
public CompositeTagImpl() {
super();
nodes.put(getClass(), this);
ReactorDependencies deps = getClass().getAnnotation(ReactorDependencies.class);
if (deps != null) {
System.out.println("Declaring classes : " + Arrays.toString(getClass().getDeclaredClasses()));
System.out.println("ReactorDependencies : " + Arrays.toString(deps.value()));
for (Class<? extends GSTagImpl> clazz : deps.value())
find(clazz);
}
for (Tag tag : nodes.values())
tag.postfix();
}
public CompositeTagImpl(Tag parent) {
super(parent);
init();
style();
nodes.put(getClass(), this);
ReactorDependencies deps = getClass().getAnnotation(ReactorDependencies.class);
if (deps != null)
for (Class<? extends GSTagImpl> clazz : deps.value())
find(clazz);
for (Tag tag : nodes.values())
tag.postfix();
}
private static Class<? extends GSTagImpl> getParentTagClass(Class<? extends GSTagImpl> tagClass) {
Parent parent = tagClass.getAnnotation(Parent.class);
if (parent != null)
return parent.value();
Class<? extends GSTagImpl> enclosing = (Class<? extends GSTagImpl>) tagClass.getEnclosingClass();
if (enclosing != null && !enclosing.isAssignableFrom(tagClass))
return enclosing;
return null;
}
@Override
public GSTagImpl find(Class<? extends GSTagImpl> tagClass) {
GSTagImpl result = nodes.get(tagClass);
if (result == null) {
try {
result = tagClass.newInstance();
} catch (IllegalAccessException | InstantiationException e) {
throw new IllegalStateException();
}
Class<? extends GSTagImpl> parentClass = getParentTagClass(tagClass);
result.setParent(parentClass != null ? find(parentClass) : this);
processAnnotations(tagClass, result);
result.init();
result.style();
nodes.put(tagClass, result);
}
return result;
}
private static void processAnnotations(Class<? extends GSTagImpl> tagClass, Tag result) {
ParentForEach parentForEach = tagClass.getAnnotation(ParentForEach.class);
if (parentForEach == null) {
ForEach forEach = tagClass.getAnnotation(ForEach.class);
if (forEach != null) {
try {
result.forEach(forEach.value().newInstance().get());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
} else {
ChildForEach childForEach = result.getParent().getClass().getAnnotation(ChildForEach.class);
if (childForEach != null) {
try {
result.forEach(childForEach.value()[parentForEach.pos()].newInstance().get());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
} else
System.out.println("Warning : unable to find childForEach on : " + result.getParent().getClass().getSimpleName() + " for : " + tagClass.getSimpleName());
}
Select select = tagClass.getAnnotation(Select.class);
if (select != null) {
try {
result.select(select.value().newInstance().get());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
ParentFlexDirection parentFlexDirection = tagClass.getAnnotation(ParentFlexDirection.class);
if (parentFlexDirection == null) {
FlexDirection flexDirection = tagClass.getAnnotation(FlexDirection.class);
if (flexDirection != null)
result.addStyle("flex-direction", flexDirection.value());
} else {
ChildFlexDirection childFlexDirection = result.getParent().getClass().getAnnotation(ChildFlexDirection.class);
if (childFlexDirection != null)
result.addStyle("flex-direction", childFlexDirection.value()[parentFlexDirection.pos()]);
}
DirectSelect directSelect = tagClass.getAnnotation(DirectSelect.class);
if (directSelect != null)
result.select(directSelect.value());
BackgroundColor backgroundColor = tagClass.getAnnotation(BackgroundColor.class);
if (backgroundColor != null)
result.addStyle("background-color", backgroundColor.value());
FlexWrap flexWrap = tagClass.getAnnotation(FlexWrap.class);
if (flexWrap != null)
result.addStyle("flex-wrap", flexWrap.value());
Flex flex = tagClass.getAnnotation(Flex.class);
if (flex != null)
result.addStyle("flex", flex.value());
AlignItems alignItems = tagClass.getAnnotation(AlignItems.class);
if (alignItems != null)
result.addStyle("align-items", alignItems.value());
JustifyContent justifyContent = tagClass.getAnnotation(JustifyContent.class);
if (justifyContent != null)
result.addStyle("justify-content", justifyContent.value());
Overflow overlflow = tagClass.getAnnotation(Overflow.class);
if (overlflow != null)
result.addStyle("overflow", overlflow.value());
Color color = tagClass.getAnnotation(Color.class);
if (color != null)
result.addStyle("color", color.value());
Style style = tagClass.getAnnotation(Style.class);
if (style != null)
result.addStyle(style.propertyName(), style.propertyValue());
}
}