-
Notifications
You must be signed in to change notification settings - Fork 5
/
RootTag.java
325 lines (280 loc) · 12.6 KB
/
RootTag.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
package org.genericsystem.reactor;
import java.lang.reflect.InvocationTargetException;
import java.util.function.Function;
import org.genericsystem.defaults.tools.BindingsTools;
import org.genericsystem.reactor.HtmlDomNode.HtmlDomNodeAction;
import org.genericsystem.reactor.HtmlDomNode.HtmlDomNodeCheckbox;
import org.genericsystem.reactor.HtmlDomNode.HtmlDomNodeInputText;
import org.genericsystem.reactor.HtmlDomNode.HtmlDomNodeSelect;
import org.genericsystem.reactor.HtmlDomNode.RootHtmlDomNode;
import org.genericsystem.reactor.HtmlDomNode.Sender;
import org.genericsystem.reactor.context.ContextAction;
import org.genericsystem.reactor.context.ObservableContextSelector;
import org.genericsystem.reactor.context.ObservableListExtractor;
import org.genericsystem.reactor.context.ObservableListExtractor.NO_FOR_EACH;
import org.genericsystem.reactor.context.ObservableListExtractorFromContext;
import org.genericsystem.reactor.context.ObservableValueSelector;
import org.genericsystem.reactor.context.StringExtractor;
import org.genericsystem.reactor.context.TagSwitcher;
import org.genericsystem.reactor.context.TextBinding;
import org.genericsystem.reactor.context.TextBinding.GENERIC_STRING;
import org.genericsystem.reactor.contextproperties.ActionDefaults;
import org.genericsystem.reactor.contextproperties.FlexDirectionDefaults;
import org.genericsystem.reactor.contextproperties.GenericStringDefaults;
import org.genericsystem.reactor.contextproperties.SelectionDefaults;
import org.genericsystem.reactor.gscomponents.Controller;
import org.genericsystem.reactor.gscomponents.Controller.MainSwitcher;
import org.genericsystem.reactor.gscomponents.Controller.StepsStep;
import org.genericsystem.reactor.gscomponents.FlexDirection;
import org.genericsystem.reactor.gscomponents.TagImpl;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.ListBinding;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public interface RootTag extends Tag {
default RootHtmlDomNode init(Context rootModelContext, String rootId, Sender send) {
return new RootHtmlDomNode(rootModelContext, this, rootId, send);
}
@Override
default RootTag getRootTag() {
return this;
}
AnnotationsManager getAnnotationsManager();
default void processAnnotations(Tag tag) {
getAnnotationsManager().processAnnotations(tag);
}
TagNode buildTagNode(Tag child);
default void processChildren(Tag tag, Class<? extends TagImpl>[] classes) {
for (Class<? extends TagImpl> clazz : classes)
tag.createChild(clazz);
}
default void processStyle(Tag tag, String name, String value) {
tag.addPrefixBinding(context -> processStyle(tag, context, name, value));
}
default void processStyle(Tag tag, Context context, String name, String value) {
tag.addStyle(context, name, value);
}
default void processAttribute(Tag tag, String name, String value) {
tag.addPrefixBinding(context -> processAttribute(tag, context, name, value));
}
default void processAttribute(Tag tag, Context context, String name, String value) {
tag.addAttribute(context, name, value);
}
default void processGenericValueBackgroundColor(Tag tag, String value) {
tag.addPrefixBinding(context -> processGenericValueBackgroundColor(tag, context, value));
}
default void processGenericValueBackgroundColor(Tag tag, Context context, String value) {
tag.addStyle(context, "background-color", "Color".equals(StringExtractor.SIMPLE_CLASS_EXTRACTOR.apply(context.getGeneric().getMeta())) ? ((GenericStringDefaults) tag).getGenericStringProperty(context).getValue() : value);
}
default void processFlexDirectionStyle(Tag tag, FlexDirection flexDirection) {
tag.addPrefixBinding(context -> processFlexDirectionStyle(tag, context, flexDirection));
}
default void processFlexDirectionStyle(Tag tag, Context context, FlexDirection flexDirection) {
if (FlexDirectionDefaults.class.isAssignableFrom(tag.getClass()))
((FlexDirectionDefaults) tag).setDirection(context, flexDirection);
else
log.warn("Warning: FlexDirection is applicable only to classes implementing FlexDirectionDefaults.");
}
default void processReverseFlexDirection(Tag tag) {
tag.addPrefixBinding(context -> processReverseFlexDirection(tag, context));
}
default void processReverseFlexDirection(Tag tag, Context context) {
if (FlexDirectionDefaults.class.isAssignableFrom(tag.getClass()))
((FlexDirectionDefaults) tag).reverseDirection(context);
else
log.warn("Warning: ReverseFlexDirection is applicable only to classes implementing FlexDirectionDefaults.");
}
default void processKeepFlexDirection(Tag tag) {
tag.addPrefixBinding(context -> processKeepFlexDirection(tag, context));
}
default void processKeepFlexDirection(Tag tag, Context context) {
if (FlexDirectionDefaults.class.isAssignableFrom(tag.getClass()))
((FlexDirectionDefaults) tag).keepDirection(context);
else
log.warn("Warning: KeepFlexDirection is applicable only to classes implementing FlexDirectionDefaults.");
}
default void processStyleClass(Tag tag, String[] classes) {
tag.addPrefixBinding(context -> processStyleClass(tag, context, classes));
}
default void processStyleClass(Tag tag, Context context, String[] classes) {
for (String sc : classes)
tag.addStyleClass(context, sc);
}
default void processSetText(Tag tag, Class<?>[] path, String[] texts) {
tag.addPrefixBinding(context -> processSetText(tag, context, path, texts));
}
default void processSetText(Tag tag, Context context, Class<?>[] path, String[] texts) {
if (texts.length == 1)
tag.setText(context, texts[0]);
else
tag.setText(context, texts[AnnotationsManager.position(tag, path[path.length - 1])]);
}
default void processBindText(Tag tag, Class<? extends TextBinding> value) {
tag.addPrefixBinding(context -> processBindText(tag, context, value));
}
default void processBindText(Tag tag, Context context, Class<? extends TextBinding> value) {
if (GENERIC_STRING.class.equals(value))
tag.bindText(context);
else
try {
tag.bindText(context, value.newInstance().apply(context, tag));
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
default void processBindAction(Tag tag, Class<? extends ContextAction>[] value) {
tag.addPrefixBinding(context -> processBindAction(tag, context, value));
}
default void processBindAction(Tag tag, Context context, Class<? extends ContextAction>[] value) {
if (ActionDefaults.class.isAssignableFrom(tag.getClass()))
((ActionDefaults) tag).bindAction(context, context_ -> {
for (Class<? extends ContextAction> classValue : value)
try {
classValue.newInstance().accept(context_, tag);
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
});
else
log.warn("BindAction is applicable only to tags implementing ActionDefaults.");
}
default void processSetStringExtractor(Tag tag, Class<? extends StringExtractor> value) {
tag.addPrefixBinding(context -> processSetStringExtractor(tag, context, value));
}
default void processSetStringExtractor(Tag tag, Context context, Class<? extends StringExtractor> value) {
try {
tag.setStringExtractor(context, value.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
default void processSelect(Tag tag, Class<?> value) {
if (ObservableValueSelector.class.isAssignableFrom(value))
try {
tag.select((ObservableValueSelector) value.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
else
throw new IllegalStateException("Select value must implement ObservableValueSelector. Given class: " + value.getName());
}
default void processSelectContext(Tag tag, Class<?> value) {
if (ObservableContextSelector.class.isAssignableFrom(value))
tag.select__(context -> {
try {
return ((ObservableContextSelector) value.newInstance()).apply(context, tag);
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
});
else
throw new IllegalStateException("SelectContext value must implement ObservableContextSelector. Given class: " + value.getName());
}
default void processForEach(Tag tag, Class<?> value) {
if (ObservableListExtractor.class.isAssignableFrom(value))
try {
if (!NO_FOR_EACH.class.equals(value))
tag.forEach((ObservableListExtractor) value.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
else
throw new IllegalStateException("ForEach value must implement ObservableListExtractor. Given class: " + value.getName());
}
default void processForEachContext(Tag tag, Class<?> value) {
if (ObservableListExtractorFromContext.class.isAssignableFrom(value))
tag.forEach2(context -> {
try {
return ((ObservableListExtractorFromContext) value.getDeclaredConstructor().newInstance()).apply(context, tag);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new IllegalStateException(e);
}
});
else
throw new IllegalStateException("ForEach value must implement ObservableListExtractorFromContext. Given class: " + value.getName());
}
default void processDirectSelect(Tag tag, Class<?>[] path, Class<?>[] selects) {
if (selects.length == 1)
tag.select(selects[0]);
else
tag.select(selects[AnnotationsManager.position(tag, path[path.length - 1])]);
}
default void processSwitch(Tag tag, Class<? extends TagSwitcher>[] value) {
try {
for (Class<? extends TagSwitcher> switcher : value)
tag.addSwitcher(switcher.newInstance());
} catch (IllegalAccessException | InstantiationException e) {
throw new IllegalStateException(e);
}
}
default void processBindSelection(Tag tag, Context context, Class<? extends TagImpl> value, int valuePos) {
if (SelectionDefaults.class.isAssignableFrom(tag.getClass()))
((SelectionDefaults) tag).bindSelection(tag.find(value, valuePos), context);
else
log.warn("BindSelection is applicable only to a class implementing SelectionDefaults.");
}
default void processBindSelection(Tag tag, Class<? extends TagImpl> value, int valuePos) {
tag.addPostfixBinding(context -> processBindSelection(tag, context, value, valuePos));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
default void processStep(Tag tag, Class<? extends TagImpl> next, String prevText, String nextText) {
if (tag.getMetaBinding() == null) {
tag.addPrefixBinding(context -> {
Controller controller = Controller.get(tag, context);
controller.addStep(tag, new SimpleIntegerProperty(1), next, prevText, nextText);
});
} else {
Function<Context, ObservableList<Object>> contextOl = tag.getMetaBinding().getBetweenChildren();
tag.getMetaBinding().setBetweenChildren(context -> {
ObservableList ol = contextOl.apply(context);
Controller controller = Controller.get(tag, context);
StepsStep subSteps = controller.getStep(tag);
if (subSteps == null)
subSteps = controller.addStep(tag, Bindings.size(ol), next, prevText, nextText);
Property<Boolean> activeProperty = controller.getActiveProperty();
SimpleIntegerProperty indexProperty = subSteps.getIndexProperty();
return BindingsTools.transmitSuccessiveInvalidations(new ListBinding() {
{
bind(indexProperty, activeProperty);
}
@Override
protected ObservableList computeValue() {
if (!activeProperty.getValue())
return ol;
return (indexProperty.get() >= 0) && (indexProperty.get() < ol.size()) ? FXCollections.singletonObservableList(ol.get(indexProperty.get())) : FXCollections.emptyObservableList();
}
});
});
}
tag.getRootTag().processSwitch(tag, new Class[] { MainSwitcher.class });
}
default void processTagName(Tag tag, String tagName, String type) {
tag.setTag(tagName);
if ("input".equals(tagName))
tag.addAttribute("type", type);
switch (tagName.toLowerCase()) {
case "input" :
switch (type.toLowerCase()) {
case "checkbox" :
case "radio" :
tag.setDomNodeClass(HtmlDomNodeCheckbox.class);
break;
default :
tag.setDomNodeClass(HtmlDomNodeInputText.class);
}
break;
case "datalist" :
case "select" :
tag.setDomNodeClass(HtmlDomNodeSelect.class);
break;
case "button" :
case "a" :
tag.setDomNodeClass(HtmlDomNodeAction.class);
break;
}
}
default void initDomNode(HtmlDomNode domNode) {
}
}