-
Notifications
You must be signed in to change notification settings - Fork 5
/
TodoApp.java
389 lines (326 loc) · 15.2 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package org.genericsystem.todomvc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import org.genericsystem.common.Generic;
import org.genericsystem.defaults.tools.BidirectionalBinding;
import org.genericsystem.defaults.tools.ObservableListWrapper;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.ReactorStatics;
import org.genericsystem.reactor.annotations.Children;
import org.genericsystem.reactor.annotations.DependsOnModel;
import org.genericsystem.reactor.annotations.StyleClass;
import org.genericsystem.reactor.appserver.ApplicationServer;
import org.genericsystem.reactor.gscomponents.FlexDiv;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlButton;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlCheckBox;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlDiv;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlFooter;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlH1;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlHeader;
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.HtmlStrong;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlUl;
import org.genericsystem.reactor.gscomponents.RootTagImpl;
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 javafx.beans.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;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
/**
* @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 TODOS = "todos";
public static final String FILTERED_TODOS = "filteredTodos";
public static final String ACTIVE_TODOS = "activeTodos";
public static final String COMPLETED = "completed";
public static final String COMPLETED_TODOS = "completedTodos";
public static void main(String[] mainArgs) {
ApplicationServer.startSimpleGenericApp(mainArgs, TodoApp.class, "/todo2/");
}
private Property<Predicate<Generic>> getModeProperty(Context model) {
return getProperty(FILTER_MODE, model);
}
private ObservableList<Generic> getTodos(Context model) {
return this.<ObservableList<Generic>> getProperty(TODOS, model).getValue();
}
private Map<Generic, Observable[]> getExtractors(Context model) {
return this.<Map<Generic, Observable[]>> getProperty("extractorMap", model).getValue();
}
static Predicate<Generic> ALL = null;
static Predicate<Generic> ACTIVE = todo -> {
Generic completed = todo.getHolder(todo.getRoot().find(Completed.class));
return completed != null ? Boolean.FALSE.equals(completed.getValue()) : true;
};
static Predicate<Generic> COMPLETE = ACTIVE.negate();
@Override
public void init() {
createNewInitializedProperty("extractorMap", model -> new HashMap<Generic, Observable[]>() {
private static final long serialVersionUID = -435743147955810836L;
@Override
public Observable[] get(Object key) {
Observable[] result = super.get(key);
if (result == null)
put((Generic) key, result = new Observable[] { ((Generic) key).getObservableHolder(((Generic) key).getRoot().find(Completed.class)) });
return result;
};
});
createNewInitializedProperty(TODOS, model -> new ObservableListWrapper<>(model.find(Todos.class).getObservableSubInstances(), todo -> getExtractors(model).get(todo)));
createNewInitializedProperty(FILTER_MODE, model -> ALL);
createNewInitializedProperty(FILTERED_TODOS, model -> {
FilteredList<Generic> filtered = new FilteredList<>(getTodos(model));
filtered.predicateProperty().bind(getModeProperty(model));
return filtered;
});
createNewInitializedProperty(ACTIVE_TODOS, model -> getTodos(model).filtered(ACTIVE));
createNewInitializedProperty(COMPLETED_TODOS, model -> getTodos(model).filtered(COMPLETE));
}
@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 })
public static class MyHeader extends HtmlHeader {
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)
public static class MyHtmlUl extends HtmlUl {
@Children(MyHtmlDiv2.class)
public static class MyHtmlLi extends HtmlLi {
@Override
public void init() {
storeProperty(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);
});
forEach2(model -> getFilteredTodos(model));
bindOptionalStyleClass(COMPLETED, COMPLETED);
}
private ObservableList<Generic> getFilteredTodos(Context model) {
return this.<ObservableList<Generic>> getProperty(FILTERED_TODOS, model).getValue();
}
@StyleClass("view")
@Children({ MyHtmlCheckBox.class, MyHtmlLabel.class, MyHtmlButton2.class })
public static class MyHtmlDiv2 extends HtmlDiv {
@StyleClass("toggle")
public static class MyHtmlCheckBox extends HtmlCheckBox {
Map<Generic, Observable[]> getExtractors(Context model) {
return this.<Map<Generic, Observable[]>> getProperty("extractorMap", model).getValue();
}
@Override
public void init() {
addPrefixBinding(todo -> {
Property<Boolean> completedProperty = getProperty(COMPLETED, todo);
ObservableValue<Generic> completed = (ObservableValue<Generic>) getExtractors(todo.getParent()).get(todo.getGeneric())[0];
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(getObservableValue(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)
public static class MyHtmlFooter1 extends HtmlFooter {
@Override
public void init() {
bindOptionalStyleClass("hide", "hasNoTodo", model -> Bindings.createBooleanBinding(() -> getTodos(model).size() == 0 ? true : false, getTodos(model)));
}
private ObservableList<Generic> getTodos(Context model) {
return this.<ObservableList<Generic>> getProperty(TODOS, model).getValue();
}
@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 {
public static class MyHtmlStrong extends HtmlStrong {
@Override
public void init() {
bindText(model -> Bindings.createStringBinding(() -> {
int size = getActiveTodos(model).size();
return size + " item" + (size > 1 ? "s" : "") + " left";
}, getActiveTodos(model)));
}
private ObservableList<Generic> getActiveTodos(Context model) {
return this.<ObservableList<Generic>> getProperty(ACTIVE_TODOS, model).getValue();
}
}
}
@StyleClass("filters")
@Children({ MyHtmlLi2.class, MyHtmlLi3.class, MyHtmlLi4.class })
public static class MyHtmlUl2 extends HtmlUl {
@Children(MyHtmlHyperLink.class)
public static class MyHtmlLi2 extends HtmlLi {
public static class MyHtmlHyperLink extends HtmlHyperLink {
@Override
public void init() {
setText("All");
bindAction(model -> getModeProperty(model).setValue(ALL));
bindOptionalStyleClass("selected", "allMode", model -> Bindings.equal((ObservableObjectValue<?>) getModeProperty(model), ALL));
}
private Property<Predicate<Generic>> getModeProperty(Context model) {
return getProperty(FILTER_MODE, model);
}
}
}
@Children(MyHtmlHyperLink2.class)
public static class MyHtmlLi3 extends HtmlLi {
public static class MyHtmlHyperLink2 extends HtmlHyperLink {
@Override
public void init() {
setText("Actives");
bindAction(model -> getModeProperty(model).setValue(ACTIVE));
bindOptionalStyleClass("selected", "activeMode", model -> Bindings.equal((ObservableObjectValue<?>) getModeProperty(model), ACTIVE));
}
private Property<Predicate<Generic>> getModeProperty(Context model) {
return getProperty(FILTER_MODE, model);
}
}
}
@Children(MyHtmlHyperLink3.class)
public static class MyHtmlLi4 extends HtmlLi {
public static class MyHtmlHyperLink3 extends HtmlHyperLink {
@Override
public void init() {
setText("Completes");
bindAction(model -> getModeProperty(model).setValue(COMPLETE));
bindOptionalStyleClass("selected", "completeMode", model -> Bindings.equal((ObservableObjectValue<?>) getModeProperty(model), COMPLETE));
}
private Property<Predicate<Generic>> getModeProperty(Context model) {
return getProperty(FILTER_MODE, model);
}
}
}
}
@StyleClass("clear-completed")
public static class MyHtmlButton extends HtmlButton {
@Override
public void init() {
bindAction(model -> new ArrayList<>(getCompletedTodos(model)).forEach(Generic::remove));
bindText(model -> Bindings.createStringBinding(() -> "Clear completed (" + getCompletedTodos(model).size() + ")", getCompletedTodos(model)));
bindOptionalStyleClass("hide", "hasNoCompleted", model -> Bindings.createBooleanBinding(() -> getCompletedTodos(model).size() == 0 ? true : false, getCompletedTodos(model)));
}
private ObservableList<Generic> getCompletedTodos(Context model) {
return this.<ObservableList<Generic>> getProperty(COMPLETED_TODOS, model).getValue();
}
}
}
}
}
@Children(MyHtmlDiv4.class)
public static class MyHtmlFooter2 extends HtmlFooter {
@StyleClass("save-cancel")
@Children({ MyHtmlButton3.class, MyHtmlButton4.class })
public static class MyHtmlDiv4 extends HtmlFooter {
@StyleClass("save")
public static class MyHtmlButton3 extends HtmlButton {
@Override
public void init() {
setText("Save");
bindAction(Context::flush);
}
}
@StyleClass("cancel")
public static class MyHtmlButton4 extends HtmlButton {
@Override
public void init() {
setText("Cancel");
bindAction(Context::cancel);
}
}
}
}
}
}