-
Notifications
You must be signed in to change notification settings - Fork 5
/
TodoApp.java
353 lines (301 loc) · 14.1 KB
/
TodoApp.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
package org.genericsystem.todomvc;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.common.Generic;
import org.genericsystem.defaults.tools.BidirectionalBinding;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.ReactorStatics;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.annotations.BindAction;
import org.genericsystem.reactor.annotations.Children;
import org.genericsystem.reactor.annotations.DependsOnModel;
import org.genericsystem.reactor.annotations.DirectSelect;
import org.genericsystem.reactor.annotations.ForEach;
import org.genericsystem.reactor.annotations.SetText;
import org.genericsystem.reactor.annotations.StyleClass;
import org.genericsystem.reactor.annotations.Switch;
import org.genericsystem.reactor.annotations.TagName;
import org.genericsystem.reactor.appserver.ApplicationServer;
import org.genericsystem.reactor.context.ContextAction.CANCEL;
import org.genericsystem.reactor.context.ContextAction.FLUSH;
import org.genericsystem.reactor.context.ObservableListExtractor.SUBINSTANCES;
import org.genericsystem.reactor.context.TagSwitcher;
import org.genericsystem.reactor.gscomponents.FlexDiv;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlButton;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlDiv;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlH1;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlHyperLink;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlInputText;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlLabel;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlLi;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlSpan;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlUl;
import org.genericsystem.reactor.gscomponents.RootTagImpl;
import org.genericsystem.reactor.gscomponents.TagImpl;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyDiv2;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyDiv2.MyHtmlUl;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyDiv2.MyHtmlUl.MyHtmlLi;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyDiv2.MyHtmlUl.MyHtmlLi.MyHtmlDiv2;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyDiv2.MyHtmlUl.MyHtmlLi.MyHtmlDiv2.MyHtmlButton2;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyDiv2.MyHtmlUl.MyHtmlLi.MyHtmlDiv2.MyHtmlCheckBox;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyDiv2.MyHtmlUl.MyHtmlLi.MyHtmlDiv2.MyHtmlLabel;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHeader;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHeader.MyHtmlH1;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHeader.MyHtmlInputText;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1.MyHtmlDiv3;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1.MyHtmlDiv3.MyHtmlButton;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1.MyHtmlDiv3.MyHtmlSpan;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1.MyHtmlDiv3.MyHtmlSpan.MyHtmlStrong;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1.MyHtmlDiv3.MyHtmlUl2;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1.MyHtmlDiv3.MyHtmlUl2.MyHtmlLi2;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1.MyHtmlDiv3.MyHtmlUl2.MyHtmlLi2.MyHtmlHyperLink;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1.MyHtmlDiv3.MyHtmlUl2.MyHtmlLi3;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1.MyHtmlDiv3.MyHtmlUl2.MyHtmlLi3.MyHtmlHyperLink2;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1.MyHtmlDiv3.MyHtmlUl2.MyHtmlLi4;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyDiv.MyHtmlFooter1.MyHtmlDiv3.MyHtmlUl2.MyHtmlLi4.MyHtmlHyperLink3;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyHtmlFooter2;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyHtmlFooter2.MyHtmlDiv4;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyHtmlFooter2.MyHtmlDiv4.MyHtmlButton3;
import org.genericsystem.todomvc.TodoApp.MyHtmlDiv.MyHtmlFooter2.MyHtmlDiv4.MyHtmlButton4;
import org.genericsystem.todomvc.Todos.Completed;
import io.reactivex.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableObjectValue;
import javafx.beans.value.ObservableValue;
/**
* @author Nicolas Feybesse
*
*/
@DependsOnModel({ Todos.class, Completed.class })
@Children({ MyHtmlDiv.class })
public class TodoApp extends RootTagImpl {
public static final String FILTER_MODE = "mode";
public static final String COMPLETED = "completed";
public static void main(String[] mainArgs) {
ApplicationServer.startSimpleGenericApp(mainArgs, TodoApp.class, "/todo2/");
}
protected static Property<Predicate<ObservableValue<Generic>>> getModeProperty(Tag tag, Context model) {
return tag.getContextProperty(FILTER_MODE, model);
}
@Override
public void init() {
addContextAttribute(FILTER_MODE, c -> new SimpleObjectProperty<Predicate<ObservableValue<Generic>>>(ALL));
}
public static class StateFilter implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
Generic todo = context.getGeneric();
ObservableValue<Generic> completed = todo.getObservableHolder(todo.getRoot().find(Completed.class));
Property<Predicate<ObservableValue<Generic>>> mode = getModeProperty(tag, context);
return Bindings.createBooleanBinding(() -> mode.getValue().test(completed), completed, mode);
}
}
static Predicate<ObservableValue<Generic>> ALL = o -> true;
static Predicate<ObservableValue<Generic>> ACTIVE = completedObs -> {
return completedObs.getValue() != null ? Boolean.FALSE.equals(completedObs.getValue().getValue()) : true;
};
static Predicate<ObservableValue<Generic>> COMPLETE = ACTIVE.negate();
static Predicate<Generic> completed = todo -> {
Generic completed = todo.getHolder(todo.getRoot().find(Completed.class));
return completed != null && Boolean.TRUE.equals(completed.getValue());
};
protected static Snapshot<Generic> getTodos(Context context) {
return context.find(Todos.class).getSubInstances();
}
protected static Snapshot<Generic> getCompleted(Context context) {
return context.find(Completed.class).getSubInstances();
}
@Children({ MyDiv.class, MyHtmlFooter2.class })
public static class MyHtmlDiv extends HtmlDiv {
@StyleClass("todoapp")
@Children({ MyHeader.class, MyDiv2.class, MyHtmlFooter1.class })
public static class MyDiv extends FlexDiv {
@StyleClass("header")
@Children({ MyHtmlH1.class, MyHtmlInputText.class })
@TagName(TagName.HEADER)
public static class MyHeader extends TagImpl {
public static class MyHtmlH1 extends HtmlH1 {
@Override
public void init() {
setText("todos");
}
}
@StyleClass("new-todo")
public static class MyHtmlInputText extends HtmlInputText {
@Override
public void init() {
addAttribute("placeholder", "What needs to be done?");
bindAction(model -> {
String value = getDomNodeAttributes(model).get("value");
if (value != null && !value.isEmpty())
model.find(Todos.class).addInstance(value);
getDomNodeAttributes(model).put("value", null);
});
}
}
}
@StyleClass("main")
@Children(MyHtmlUl.class)
public static class MyDiv2 extends FlexDiv {
@StyleClass("todo-list")
@Children(MyHtmlLi.class)
@DirectSelect(Todos.class)
public static class MyHtmlUl extends HtmlUl {
@Children(MyHtmlDiv2.class)
@ForEach(SUBINSTANCES.class)
@Switch(StateFilter.class)
public static class MyHtmlLi extends HtmlLi {
@Override
public void init() {
addContextAttribute(COMPLETED, model -> {
Generic completed = model.getGeneric().getHolder(model.getGeneric().getRoot().find(Completed.class));
return new SimpleBooleanProperty(completed != null && Boolean.TRUE.equals(completed.getValue()) ? true : false);
});
bindOptionalStyleClass(COMPLETED, COMPLETED);
}
@StyleClass("view")
@Children({ MyHtmlCheckBox.class, MyHtmlLabel.class, MyHtmlButton2.class })
public static class MyHtmlDiv2 extends HtmlDiv {
@StyleClass("toggle")
@TagName(value = TagName.INPUT, type = TagName.CHECKBOX)
public static class MyHtmlCheckBox extends TagImpl {
@Override
public void init() {
addPrefixBinding(todo -> {
Property<Boolean> completedProperty = getContextProperty(COMPLETED, todo);
ObservableValue<Generic> completed = todo.getGeneric().getObservableHolder(todo.getGeneric().getRoot().find(Completed.class));
Property<Generic> completedGenericProperty = new SimpleObjectProperty(completed.getValue());
completed.addListener((ov, v, nv) -> completedGenericProperty.setValue(nv));
BidirectionalBinding.bind(completedProperty, completedGenericProperty, b -> todo.getGeneric().isAlive() ? todo.getGeneric().setHolder(todo.find(Completed.class), b) : null,
g -> g == null ? false : (Boolean) g.getValue());
});
addStyleClass("toggle");
addPrefixBinding(todo -> {
if (Boolean.TRUE.equals(getContextObservableValue(COMPLETED, todo).getValue())) {
getDomNodeAttributes(todo).put(ReactorStatics.CHECKED, ReactorStatics.CHECKED);
}
});
bindOptionalBiDirectionalAttribute(COMPLETED, ReactorStatics.CHECKED, ReactorStatics.CHECKED);
}
}
public static class MyHtmlLabel extends HtmlLabel {
@Override
public void init() {
bindText();
}
}
@StyleClass("destroy")
public static class MyHtmlButton2 extends HtmlButton {
@Override
public void init() {
bindAction(Context::remove);
}
}
}
}
}
}
@StyleClass("footer")
@Children(MyHtmlDiv3.class)
@TagName(TagName.FOOTER)
public static class MyHtmlFooter1 extends TagImpl {
@Override
public void init() {
// Broken
// bindOptionalStyleClass("hide", "hasNoTodo", model -> Bindings.createBooleanBinding(() -> getTodos(this, model).size() == 0 ? true : false, getTodos(this, model)));
}
@Children({ MyHtmlSpan.class, MyHtmlUl2.class, MyHtmlButton.class })
public static class MyHtmlDiv3 extends HtmlDiv {
@StyleClass("todo-count")
@Children(MyHtmlStrong.class)
public static class MyHtmlSpan extends HtmlSpan {
@TagName(TagName.STRONG)
public static class MyHtmlStrong extends TagImpl {
@Override
public void init() {
bindText(model -> Observable.combineLatest(getTodos(model).setOnChanged(), getCompleted(model).setOnChanged(), (todos, comp) ->
todos.stream().filter(g -> !completed.test(g)).collect(Collectors.toList())).map(list -> list.size())
.map(size -> size + " item" + (size > 1 ? "s" : "") + " left"));
}
}
}
@StyleClass("filters")
@Children({ MyHtmlLi2.class, MyHtmlLi3.class, MyHtmlLi4.class })
public static class MyHtmlUl2 extends HtmlUl {
@Children(MyHtmlHyperLink.class)
public static class MyHtmlLi2 extends HtmlLi {
@SetText("All")
public static class MyHtmlHyperLink extends HtmlHyperLink {
@Override
public void init() {
bindAction(model -> getModeProperty(this, model).setValue(ALL));
bindOptionalStyleClass("selected", "allMode", model -> Bindings.equal((ObservableObjectValue<?>) getModeProperty(this, model), ALL));
}
}
}
@Children(MyHtmlHyperLink2.class)
public static class MyHtmlLi3 extends HtmlLi {
@SetText("Actives")
public static class MyHtmlHyperLink2 extends HtmlHyperLink {
@Override
public void init() {
bindAction(model -> getModeProperty(this, model).setValue(ACTIVE));
bindOptionalStyleClass("selected", "activeMode", model -> Bindings.equal((ObservableObjectValue<?>) getModeProperty(this, model), ACTIVE));
}
}
}
@Children(MyHtmlHyperLink3.class)
public static class MyHtmlLi4 extends HtmlLi {
@SetText("Completes")
public static class MyHtmlHyperLink3 extends HtmlHyperLink {
@Override
public void init() {
bindAction(model -> getModeProperty(this, model).setValue(COMPLETE));
bindOptionalStyleClass("selected", "completeMode", model -> Bindings.equal((ObservableObjectValue<?>) getModeProperty(this, model), COMPLETE));
}
}
}
}
@StyleClass("clear-completed")
public static class MyHtmlButton extends HtmlButton {
@Override
public void init() {
bindAction(model -> getTodos(model).filter(completed).toList().forEach(Generic::remove));
bindText(model -> getCompleted(model).setOnChanged()
.map(instances -> instances.stream().filter(g -> Boolean.TRUE.equals(g.getValue())).collect(Collectors.toList()))
.map(list -> list.size()).map(size -> "Clear completed (" + size + ")"));
// Broken
// bindOptionalStyleClass("hide", "hasNoCompleted", model -> Bindings.createBooleanBinding(() -> getCompletedTodos(model).size() == 0 ? true : false, getCompletedTodos(model)));
}
}
}
}
}
@Children(MyHtmlDiv4.class)
@TagName(TagName.FOOTER)
public static class MyHtmlFooter2 extends TagImpl {
@StyleClass("save-cancel")
@Children({ MyHtmlButton3.class, MyHtmlButton4.class })
@TagName(TagName.FOOTER)
public static class MyHtmlDiv4 extends TagImpl {
@StyleClass("save")
@SetText("Save")
@BindAction(FLUSH.class)
public static class MyHtmlButton3 extends HtmlButton {
}
@StyleClass("cancel")
@SetText("Cancel")
@BindAction(CANCEL.class)
public static class MyHtmlButton4 extends HtmlButton {
}
}
}
}
}