-
Notifications
You must be signed in to change notification settings - Fork 5
/
TodoList.java
177 lines (144 loc) · 5.37 KB
/
TodoList.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
package org.genericsystem.todomvc;
import java.util.ArrayList;
import java.util.function.Predicate;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableBooleanValue;
import javafx.beans.value.ObservableNumberValue;
import javafx.beans.value.ObservableObjectValue;
import javafx.beans.value.ObservableStringValue;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
import org.genericsystem.defaults.tools.TransformationObservableList;
import org.genericsystem.reactor.Model;
/**
* @author Nicolas Feybesse
*
*/
@SuppressWarnings("unchecked")
public class TodoList extends Model {
private final Root engine;
private final Property<String> name = new SimpleStringProperty();
private final Property<Predicate<Todo>> mode = new SimpleObjectProperty<>(ALL);
private final ObservableList<Todo> todos;
private final FilteredList<Todo> filtered;
private final ObservableNumberValue completedCount;
private final ObservableNumberValue activeCount;
private final ObservableValue<String> clearButtonText;
private final ObservableValue<Boolean> hasNoCompleted;
private final ObservableBooleanValue hasTodo;
private final ObservableValue<Boolean> hasNoTodo;
private final ObservableValue<Boolean> allMode = Bindings.equal((ObservableObjectValue<Predicate<Todo>>) mode, ALL);
private final ObservableValue<Boolean> activeMode = Bindings.equal((ObservableObjectValue<Predicate<Todo>>) mode, ACTIVE);
private final ObservableValue<Boolean> completedMode = Bindings.equal((ObservableObjectValue<Predicate<Todo>>) mode, COMPLETE);
private final ObservableStringValue items;
private final ObservableStringValue clearCompleted;
private final ObservableStringValue save;
private final ObservableStringValue cancel;
public TodoList(Root engine) {
this.engine = engine;
todos = new TransformationObservableList<>(engine.find(Todos.class).getObservableSubInstances(), g -> new Todo(this, g), todo -> new Observable[] { todo.getCompleted() });
filtered = new FilteredList<>(todos);
filtered.predicateProperty().bind(Bindings.createObjectBinding(() -> mode.getValue(), mode));
completedCount = Bindings.size(todos.filtered(COMPLETE));
activeCount = Bindings.size(todos.filtered(ACTIVE));
clearButtonText = Bindings.createStringBinding(() -> "Clear completed (" + completedCount.getValue() + ")", completedCount);
hasNoCompleted = Bindings.equal(0, completedCount);
hasTodo = Bindings.lessThan(0, Bindings.size(todos));
hasNoTodo = Bindings.not(hasTodo);
items = Bindings.createStringBinding(() -> " " + (activeCount.getValue().intValue() > 1 ? "items" : "item") + " left", activeCount);
clearCompleted = Bindings.createStringBinding(() -> "Clear completed (" + completedCount.getValue().intValue() + ")", completedCount);
save = Bindings.createStringBinding(() -> "Save ");
cancel = Bindings.createStringBinding(() -> "Cancel");
}
public ObservableValue<String> getActiveCount() {
return Bindings.convert(activeCount);
}
public ObservableValue<String> getItems() {
return items;
}
public void create() {
if (getName().getValue() != null) {
Generic todo = engine.find(Todos.class).addInstance(getName().getValue());
System.out.println("Add instance : " + getName().getValue());
name.setValue(null);
}
}
public void showAll() {
mode.setValue(ALL);
}
public void showActive() {
mode.setValue(ACTIVE);
}
public void showCompleted() {
mode.setValue(COMPLETE);
}
public void removeCompleted() {
for (Todo todo : new ArrayList<>(todos.filtered(COMPLETE)))
todo.remove();
}
public void cancel() {
engine.getCurrentCache().clear();
}
public void save() {
engine.getCurrentCache().flush();
}
static Predicate<Todo> ALL = todo -> true;
static Predicate<Todo> ACTIVE = todo -> !todo.getCompleted().getValue();
static Predicate<Todo> COMPLETE = todo -> todo.getCompleted().getValue();
/*********************************************************************************************************************************/
public Root getEngine() {
return engine;
}
public Property<String> getName() {
return name;
}
public Property<Predicate<Todo>> getMode() {
return mode;
}
public ObservableList<Todo> getTodos() {
return todos;
}
public ObservableList<Todo> getFiltered() {
return filtered;
}
public ObservableValue<Number> getCompletedCount() {
return completedCount;
}
public ObservableValue<String> getClearButtonText() {
return clearButtonText;
}
public ObservableValue<Boolean> getHasNoCompleted() {
return hasNoCompleted;
}
public ObservableValue<Boolean> getHasTodo() {
return hasTodo;
}
public ObservableValue<Boolean> getHasNoTodo() {
return hasNoTodo;
}
public ObservableValue<Boolean> getAllMode() {
return allMode;
}
public ObservableValue<Boolean> getActiveMode() {
return activeMode;
}
public ObservableValue<Boolean> getCompletedMode() {
return completedMode;
}
public ObservableValue<String> getClearCompleted() {
return clearCompleted;
};
public ObservableValue<String> getSave() {
return save;
};
public ObservableValue<String> getCancel() {
return cancel;
};
}