-
Notifications
You must be signed in to change notification settings - Fork 5
/
Controller.java
263 lines (210 loc) · 7.63 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
package org.genericsystem.reactor.gscomponents;
import java.util.Map.Entry;
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 javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableIntegerValue;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
public class Controller {
public static final String CONTROLLER = "controller";
private final Tag containerTag;
private final Class<? extends TagImpl> firstClass;
private final Property<Class<? extends Tag>> classProperty;
private final ObservableMap<Tag, SwitchStep> switcherSteps = FXCollections.observableHashMap();
public static void initialize(Tag tag, Class<? extends TagImpl> clazz) {
tag.createNewInitializedProperty(CONTROLLER, context -> new Controller(tag, clazz));
}
public static Controller get(Tag tag, Context context) {
return tag.<Controller> getProperty(CONTROLLER, context).getValue();
}
public Controller(Tag containerTag, Class<? extends TagImpl> firstClass) {
this.containerTag = containerTag;
this.firstClass = firstClass;
classProperty = new SimpleObjectProperty<>(firstClass);
}
public Property<Class<? extends Tag>> getClassProperty() {
return classProperty;
}
public SwitchStep getSwitchStep(Tag tag) {
Tag tag_ = tag;
SwitchStep result = null;
while (result == null && !containerTag.equals(tag_)) {
result = switcherSteps.get(tag_);
tag_ = tag_.getParent();
}
return result;
}
public SwitchStep addSwitchStep(Tag tag, ObservableIntegerValue observableSize, Class<? extends TagImpl> nextClass, String prevText, String nextText) {
SwitchStep result = new SwitchStep(tag, observableSize, nextClass, prevText, nextText);
switcherSteps.put(tag, result);
return result;
}
public ObservableValue<String> prevText(Tag tag) {
return getSwitchStep(tag).prevText();
}
public ObservableValue<String> nextText(Tag tag) {
return getSwitchStep(tag).nextText();
}
public void previous(Tag tag) {
getSwitchStep(tag).previous();
}
public void next(Tag tag) {
getSwitchStep(tag).next();
}
public ObservableValue<Boolean> hasPrev(Tag tag) {
return getSwitchStep(tag).hasPrev();
}
public ObservableValue<Boolean> hasNext(Tag tag) {
return getSwitchStep(tag).hasNext();
}
public SwitchStep getStep(Class<? extends TagImpl> clazz) {
for (Entry<Tag, SwitchStep> entry : switcherSteps.entrySet())
if (entry.getKey().getClass().equals(clazz))
return entry.getValue();
return null;
}
public SwitchStep getPreviousStep(Class<? extends Tag> clazz) {
for (Entry<Tag, SwitchStep> entry : switcherSteps.entrySet())
if (entry.getValue().getNextClass().equals(clazz) && !entry.getValue().getNextClass().equals(entry.getValue().getTag().getClass()))
return entry.getValue();
return null;
}
public ObservableValue<String> countText(Tag tag) {
Tag realTag = getSwitchStep(tag).getTag();
SimpleIntegerProperty indexProperty = getSwitchStep(tag).getIndexProperty();
return Bindings.createStringBinding(() -> {
int size = 0;
SwitchStep step = getStep(firstClass);
while (step != null) {
if (realTag.equals(step.tag))
break;
size += step.getObservableSize().intValue();
step = step.getNextStep();
}
return "Step : " + Integer.toString(indexProperty.get() + size + 1);
}, switcherSteps, indexProperty);
}
public class SwitchStep {
private final Tag tag;
private final Class<? extends TagImpl> nextClass;
private final SimpleIntegerProperty indexProperty = new SimpleIntegerProperty(0);
private final ObservableIntegerValue observableSize;
private final ObservableValue<Boolean> hasPrev;
private final ObservableValue<Boolean> hasNext;
private final ObservableValue<String> prevText;
private final ObservableValue<String> nextText;
public SwitchStep(Tag tag, ObservableIntegerValue observableSize, Class<? extends TagImpl> nextClass, String prevText, String nextText) {
this.tag = tag;
this.observableSize = observableSize;
this.nextClass = nextClass;
this.hasPrev = Bindings.createBooleanBinding(() -> {
return getPreviousStep(tag.getClass()) != null || (getIndexProperty().get() > 0);
}, getIndexProperty());
this.hasNext = Bindings.createBooleanBinding(() -> {
return !tag.getClass().equals(nextClass) || (getIndexProperty().get() + 1 < getObservableSize().get());
}, getIndexProperty(), getObservableSize());
this.prevText = Bindings.createStringBinding(() -> {
return prevText;
}, getIndexProperty(), getObservableSize());
this.nextText = Bindings.createStringBinding(() -> {
return nextText;
}, getIndexProperty(), getObservableSize());
}
public Object getNextClass() {
return nextClass;
}
public SwitchStep getNextStep() {
return !tag.getClass().equals(nextClass) ? getStep(nextClass) : null;
}
public SimpleIntegerProperty getIndexProperty() {
return indexProperty;
}
public ObservableIntegerValue getObservableSize() {
return observableSize;
}
public void previous() {
if (indexProperty.get() > 0)
indexProperty.set(indexProperty.get() - 1);
else
classProperty.setValue(getPreviousStep(tag.getClass()).getTag().getClass());
}
private Tag getTag() {
return tag;
}
public void next() {
if (indexProperty.get() + 1 < observableSize.get())
indexProperty.set(indexProperty.get() + 1);
else
classProperty.setValue(nextClass);
}
public ObservableValue<Boolean> hasPrev() {
return hasPrev;
}
public ObservableValue<Boolean> hasNext() {
return hasNext;
}
public ObservableValue<String> prevText() {
return prevText;
}
public ObservableValue<String> nextText() {
return nextText;
}
}
public static class MainSwitcher implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
Property<Class<? extends Tag>> classProperty = Controller.get(tag, context).getClassProperty();
return Bindings.createBooleanBinding(() -> tag.getClass().equals(classProperty.getValue()), classProperty);
}
}
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 ObservableValue<String> apply(Context context, Tag tag) {
return Controller.get(tag, context).countText(tag);
}
}
public static class PrevTextBinding implements TextBinding {
@Override
public ObservableValue<String> apply(Context context, Tag tag) {
return Controller.get(tag, context).prevText(tag);
}
}
public static class NextTextBinding implements TextBinding {
@Override
public ObservableValue<String> apply(Context context, Tag tag) {
return Controller.get(tag, context).nextText(tag);
}
}
public static class PrevSwitcher implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return Controller.get(tag, context).hasPrev(tag);
}
}
public static class NextSwitcher implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
return Controller.get(tag, context).hasNext(tag);
}
}
}