-
Notifications
You must be signed in to change notification settings - Fork 5
/
Controller.java
286 lines (229 loc) · 8.25 KB
/
Controller.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
package org.genericsystem.reactor.gscomponents;
import java.util.Map.Entry;
import org.genericsystem.defaults.tools.RxJavaHelpers;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.context.ContextAction;
import org.genericsystem.reactor.context.TagSwitcher;
import org.genericsystem.reactor.context.TextBinding;
import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.ReplaySubject;
import io.reactivex.subjects.Subject;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
public class Controller {
public static final String CONTROLLER = "controller";
private final Tag containerTag;
private final Subject<Class<? extends Tag>> classProperty;
// Emits +1 when going to the next element and -1 when going to the previous element,
// so that the sum of emitted elements is the current index.
private final Subject<Integer> globalIncs = ReplaySubject.create();
private final ObservableMap<Tag, StepsStep> steps = FXCollections.observableHashMap();
private Property<Boolean> activeProperty = new SimpleBooleanProperty(true);
public static void initialize(Tag tag, Class<? extends TagImpl> firstClass) {
tag.setContextAttribute(CONTROLLER, c -> new Controller(tag, firstClass));
}
public static Controller get(Tag tag, Context context) {
return tag.getContextAttribute(CONTROLLER, context);
}
public Controller(Tag containerTag, Class<? extends TagImpl> firstClass) {
this.containerTag = containerTag;
classProperty = BehaviorSubject.create();
classProperty.onNext(firstClass);
}
public Observable<Class<? extends Tag>> getClassProperty() {
return classProperty.hide();
}
public Property<Boolean> getActiveProperty() {
return activeProperty;
}
public void setActiveProperty(boolean active) {
activeProperty.setValue(active);
}
public StepsStep getStep(Tag tag) {
Tag tag_ = tag;
StepsStep result = null;
while (result == null && !containerTag.equals(tag_)) {
result = steps.get(tag_);
tag_ = tag_.getParent();
}
return result;
}
public StepsStep addStep(Tag tag, Observable<Integer> observableSize, Class<? extends TagImpl> nextClass, String prevText, String nextText) {
StepsStep result = new StepsStep(tag, observableSize, nextClass, prevText, nextText);
steps.put(tag, result);
return result;
}
public Observable<String> prevText(Tag tag) {
return getStep(tag).prevText();
}
public Observable<String> nextText(Tag tag) {
return getStep(tag).nextText();
}
public void previous(Tag tag) {
getStep(tag).prev();
}
public void next(Tag tag) {
getStep(tag).next();
}
public Observable<Boolean> hasPrev(Tag tag) {
return getStep(tag).hasPrev();
}
public Observable<Boolean> hasNext(Tag tag) {
return getStep(tag).hasNext();
}
public StepsStep getStep(Class<? extends TagImpl> clazz) {
for (Entry<Tag, StepsStep> entry : steps.entrySet())
if (entry.getKey().getClass().equals(clazz))
return entry.getValue();
return null;
}
public StepsStep getPreviousStep(Class<? extends Tag> clazz) {
for (Entry<Tag, StepsStep> entry : steps.entrySet())
if (entry.getValue().getNextClass().equals(clazz) && !entry.getValue().getNextClass().equals(entry.getValue().getTag().getClass()))
return entry.getValue();
return null;
}
public Observable<String> countText(Tag tag) {
return globalIncs.hide().scan(0, (sum, curr) -> sum + curr).map(ind -> "Step: " + Integer.toString(ind + 1));
}
public class StepsStep {
private final Tag tag;
private final Class<? extends TagImpl> nextClass;
// Used to indicate when to increase/decrease the index or go to the previous/next class.
private final Subject<Integer> inc = ReplaySubject.create();
private final Observable<Integer> observableSize;
private final Observable<Integer> indexProperty;
private final Observable<Boolean> hasPrev;
private final Observable<Boolean> hasNext;
private final Observable<String> prevText;
private final Observable<String> nextText;
public StepsStep(Tag tag, Observable<Integer> observableSize, Class<? extends TagImpl> nextClass, String prevText, String nextText) {
this.tag = tag;
this.observableSize = observableSize;
indexProperty = observableSize.switchMap(size -> inc.hide().scan(0, (curr, inc) -> {
int newIndex = curr + inc;
if (newIndex >= 0 && newIndex < size)
return newIndex;
else {
if (inc == -1)
classProperty.onNext(getPreviousStep(tag.getClass()).getTag().getClass());
else
classProperty.onNext(nextClass);
return curr;
}
})).distinctUntilChanged().replay().refCount();
this.nextClass = nextClass;
this.hasPrev = Observable.combineLatest(getIndexProperty(), RxJavaHelpers.valuesOf(activeProperty),
(index, active) -> active && (getPreviousStep(tag.getClass()) != null || (index.intValue() > 0)));
this.hasNext = Observable.combineLatest(getIndexProperty(), RxJavaHelpers.valuesOf(activeProperty), observableSize,
(index, active, size) -> active && (!tag.getClass().equals(nextClass) || (index.intValue() + 1 < size)));
this.prevText = Observable.just(prevText);
this.nextText = Observable.just(nextText);
}
private Tag getTag() {
return tag;
}
public Object getNextClass() {
return nextClass;
}
public StepsStep getNextStep() {
return !tag.getClass().equals(nextClass) ? getStep(nextClass) : null;
}
public Observable<Integer> getIndexProperty() {
return indexProperty;
}
public Observable<Integer> getObservableSize() {
return observableSize;
}
public void prev() {
if (activeProperty.getValue()) {
inc.onNext(-1);
globalIncs.onNext(-1);
}
}
public void next() {
if (activeProperty.getValue()) {
inc.onNext(1);
globalIncs.onNext(1);
}
}
public Observable<Boolean> hasPrev() {
return hasPrev;
}
public Observable<Boolean> hasNext() {
return hasNext;
}
public Observable<String> prevText() {
return prevText;
}
public Observable<String> nextText() {
return nextText;
}
}
public static class MainSwitcher implements TagSwitcher {
@Override
public Observable<Boolean> apply(Context context, Tag tag) {
Controller controller = Controller.get(tag, context);
Property<Boolean> activeProperty = controller.getActiveProperty();
return Observable.combineLatest(RxJavaHelpers.valuesOf(activeProperty), controller.getClassProperty(), (active, clazz) -> !active || tag.getClass().equals(clazz));
}
}
public static class PrevAction implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Controller.get(tag, context).previous(tag);
}
}
public static class NextAction implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Controller.get(tag, context).next(tag);
}
}
public static class CountTextBinding implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
return Controller.get(tag, context).countText(tag);
}
}
public static class PrevTextBinding implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
return Controller.get(tag, context).prevText(tag);
}
}
public static class NextTextBinding implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
return Controller.get(tag, context).nextText(tag);
}
}
public static class CountTextSwitcher implements TagSwitcher {
@Override
public Observable<Boolean> apply(Context context, Tag tag) {
return RxJavaHelpers.valuesOf(Controller.get(tag, context).activeProperty);
}
}
public static class PrevSwitcher implements TagSwitcher {
@Override
public Observable<Boolean> apply(Context context, Tag tag) {
return Controller.get(tag, context).hasPrev(tag);
}
}
public static class NextSwitcher implements TagSwitcher {
@Override
public Observable<Boolean> apply(Context context, Tag tag) {
return Controller.get(tag, context).hasNext(tag);
}
}
public static class LastSwitcher implements TagSwitcher {
@Override
public Observable<Boolean> apply(Context context, Tag tag) {
return Controller.get(tag, context).hasNext(tag).map(bool -> !bool);
}
}
}