-
Notifications
You must be signed in to change notification settings - Fork 5
/
SwitchChildDiv.java
195 lines (151 loc) · 7.15 KB
/
SwitchChildDiv.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
package org.genericsystem.reactor.gscomponents;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.annotations.BindAction;
import org.genericsystem.reactor.annotations.BindText;
import org.genericsystem.reactor.annotations.Children;
import org.genericsystem.reactor.annotations.Switch;
import org.genericsystem.reactor.annotations.Switcher;
import org.genericsystem.reactor.context.ContextAction;
import org.genericsystem.reactor.context.TagSwitcher;
import org.genericsystem.reactor.context.TextBinding;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlButton;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlDiv;
import org.genericsystem.reactor.gscomponents.SwitchChildDiv.MainSwitcher;
import javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ObservableIntegerValue;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
@Switch(MainSwitcher.class)
@Children({ HtmlButton.class, HtmlButton.class })
@BindAction(path = HtmlButton.class, pos = { 0 }, value = SwitchChildDiv.PrevAction.class)
@BindAction(path = HtmlButton.class, pos = { 1 }, value = SwitchChildDiv.NextAction.class)
@BindText(path = HtmlButton.class, pos = { 0 }, value = SwitchChildDiv.PrevTextBinding.class)
@BindText(path = HtmlButton.class, pos = { 1 }, value = SwitchChildDiv.NextTextBinding.class)
@Switch(path = HtmlButton.class, pos = { 0 }, value = SwitchChildDiv.PrevSwitcher.class)
@Switch(path = HtmlButton.class, pos = { 1 }, value = SwitchChildDiv.NextSwitcher.class)
public class SwitchChildDiv extends HtmlDiv {
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface First {
Class<? extends TagImpl>[] path() default {};
int[] pos() default {};
String text()
default "<<";
Class<? extends TagImpl> value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface Prev {
Class<? extends TagImpl>[] path() default {};
int[] pos() default {};
String text()
default "<<";
Class<? extends TagImpl> value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface Next {
Class<? extends TagImpl>[] path() default {};
int[] pos() default {};
String text()
default ">>";
Class<? extends TagImpl> value();
}
public static class MainSwitcher implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
Property<?> selectedClassProperty = tag.getProperty(Switcher.SELECTED_CLASS, context);
return Bindings.createBooleanBinding(() -> tag.getClass().equals(selectedClassProperty.getValue()), selectedClassProperty);
}
}
public static class SwitchSubSteps {
private final Property<Class<? extends TagImpl>> classProperty;
private final SimpleIntegerProperty indexProperty = new SimpleIntegerProperty(0);
private final ObservableIntegerValue observableSize;
public SwitchSubSteps(Property<Class<? extends TagImpl>> classProperty) {
this.classProperty = classProperty;
observableSize = new SimpleIntegerProperty(1);
}
public SwitchSubSteps(Property<Class<? extends TagImpl>> classProperty, ObservableList<?> observableList) {
this.classProperty = classProperty;
observableSize = Bindings.size(observableList);
}
public SimpleIntegerProperty getIndexProperty() {
return indexProperty;
}
public ObservableIntegerValue getObservableSize() {
return observableSize;
}
public void previous(Class<? extends TagImpl> previousClass) {
if (indexProperty.get() > 0)
indexProperty.set(indexProperty.get() - 1);
else
classProperty.setValue(previousClass);
}
public void next(Class<? extends TagImpl> nextClass) {
if (indexProperty.get() + 1 < observableSize.get())
indexProperty.set(indexProperty.get() + 1);
else
classProperty.setValue(nextClass);
}
}
public static class PrevAction implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
tag.getParent().<Map<Tag, SwitchSubSteps>> getProperty(Switcher.SWITCHER_MAP, context).getValue().get(tag.getParent()).previous(tag.getParent().getClass().getAnnotation(SwitchChildDiv.Prev.class).value());
}
}
public static class NextAction implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
tag.getParent().<Map<Tag, SwitchSubSteps>> getProperty(Switcher.SWITCHER_MAP, context).getValue().get(tag.getParent()).next(tag.getParent().getClass().getAnnotation(SwitchChildDiv.Next.class).value());
}
}
public static class PrevTextBinding implements TextBinding {
@Override
public ObservableValue<String> apply(Context context, Tag tag) {
SwitchChildDiv.Prev prev = tag.getParent().getClass().getAnnotation(SwitchChildDiv.Prev.class);
if (prev != null) {
SwitchSubSteps switchSubSteps = tag.getParent().<Map<Tag, SwitchSubSteps>> getProperty(Switcher.SWITCHER_MAP, context).getValue().get(tag.getParent());
return new ReadOnlyStringWrapper(prev.text() + "#" + switchSubSteps.getIndexProperty().get() + "/" + switchSubSteps.getObservableSize().get());
} else
return new ReadOnlyStringWrapper("None");
}
}
public static class NextTextBinding implements TextBinding {
@Override
public ObservableValue<String> apply(Context context, Tag tag) {
SwitchChildDiv.Next next = tag.getParent().getClass().getAnnotation(SwitchChildDiv.Next.class);
if (next != null) {
SwitchSubSteps switchSubSteps = tag.getParent().<Map<Tag, SwitchSubSteps>> getProperty(Switcher.SWITCHER_MAP, context).getValue().get(tag.getParent());
return new ReadOnlyStringWrapper(next.text() + "#" + switchSubSteps.getIndexProperty().get() + "/" + switchSubSteps.getObservableSize().get());
} else
return new ReadOnlyStringWrapper("None");
}
}
public static class PrevSwitcher implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
SwitchSubSteps switchSubSteps = tag.getParent().<Map<Tag, SwitchSubSteps>> getProperty(Switcher.SWITCHER_MAP, context).getValue().get(tag.getParent());
SimpleIntegerProperty indexProperty = switchSubSteps.getIndexProperty();
return Bindings.createBooleanBinding(() -> (indexProperty.get() > 0) || tag.getParent().getClass().getAnnotation(SwitchChildDiv.Prev.class) != null, indexProperty);
}
}
public static class NextSwitcher implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
SwitchSubSteps switchSubSteps = tag.getParent().<Map<Tag, SwitchSubSteps>> getProperty(Switcher.SWITCHER_MAP, context).getValue().get(tag.getParent());
SimpleIntegerProperty indexProperty = switchSubSteps.getIndexProperty();
return Bindings.createBooleanBinding(() -> (indexProperty.get() + 1 < switchSubSteps.getObservableSize().get()) || tag.getParent().getClass().getAnnotation(SwitchChildDiv.Next.class) != null, indexProperty);
}
}
}