-
Notifications
You must be signed in to change notification settings - Fork 5
/
SwitchChildDiv.java
185 lines (148 loc) · 7.28 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
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.ReadOnlyBooleanWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ObservableValue;
@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 PrevAction implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
if (tag.getParent().getMetaBinding() == null) {
SwitchChildDiv.Prev prev = tag.getParent().getClass().getAnnotation(SwitchChildDiv.Prev.class);
tag.getProperty(Switcher.SELECTED_CLASS, context).setValue(prev.value());
} else {
Property<Map<Tag, SimpleIntegerProperty>> property = tag.getParent().<Map<Tag, SimpleIntegerProperty>> getProperty(Switcher.TAG_INDEX_MAP, context);
Map<Tag, SimpleIntegerProperty> map = property.getValue();
SimpleIntegerProperty indexProperty = map.get(tag.getParent());
if (indexProperty.get() != 0)
indexProperty.set(indexProperty.get() - 1);
else {
SwitchChildDiv.Prev prev = tag.getParent().getClass().getAnnotation(SwitchChildDiv.Prev.class);
tag.getProperty(Switcher.SELECTED_CLASS, context).setValue(prev.value());
}
}
}
}
public static class NextAction implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
if (tag.getParent().getMetaBinding() == null) {
SwitchChildDiv.Next next = tag.getParent().getClass().getAnnotation(SwitchChildDiv.Next.class);
tag.getProperty(Switcher.SELECTED_CLASS, context).setValue(next.value());
} else {
Property<Map<Tag, SimpleIntegerProperty>> property = tag.getParent().<Map<Tag, SimpleIntegerProperty>> getProperty(Switcher.TAG_INDEX_MAP, context);
Map<Tag, SimpleIntegerProperty> map = property.getValue();
SimpleIntegerProperty indexProperty = map.get(tag.getParent());
if (indexProperty.get() != 1)
indexProperty.set(indexProperty.get() + 1);
else {
SwitchChildDiv.Next next = tag.getParent().getClass().getAnnotation(SwitchChildDiv.Next.class);
tag.getProperty(Switcher.SELECTED_CLASS, context).setValue(next.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);
return new ReadOnlyStringWrapper(prev.text() + (tag.getParent().getMetaBinding() != null ? "#" + tag.getParent().<Map<Tag, SimpleIntegerProperty>> getProperty(Switcher.TAG_INDEX_MAP, context).getValue().get(tag.getParent()).get() : ""));
}
}
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);
return new ReadOnlyStringWrapper(
next != null ? next.text() + (tag.getParent().getMetaBinding() != null ? "#" + tag.getParent().<Map<Tag, SimpleIntegerProperty>> getProperty(Switcher.TAG_INDEX_MAP, context).getValue().get(tag.getParent()).get() : "") : ">>");
}
}
public static class PrevSwitcher implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
if (tag.getParent().getMetaBinding() == null)
return new ReadOnlyBooleanWrapper(tag.getParent().getClass().getAnnotation(SwitchChildDiv.Prev.class) != null);
else {
Property<Map<Tag, SimpleIntegerProperty>> property = tag.getParent().<Map<Tag, SimpleIntegerProperty>> getProperty(Switcher.TAG_INDEX_MAP, context);
Map<Tag, SimpleIntegerProperty> map = property.getValue();
SimpleIntegerProperty indexProperty = map.get(tag.getParent());
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) {
if (tag.getParent().getMetaBinding() == null)
return new ReadOnlyBooleanWrapper(tag.getParent().getClass().getAnnotation(SwitchChildDiv.Next.class) != null);
else {
Property<Map<Tag, SimpleIntegerProperty>> property = tag.getParent().<Map<Tag, SimpleIntegerProperty>> getProperty(Switcher.TAG_INDEX_MAP, context);
Map<Tag, SimpleIntegerProperty> map = property.getValue();
SimpleIntegerProperty indexProperty = map.get(tag.getParent());
return Bindings.createBooleanBinding(() -> indexProperty.get() != 1 || tag.getParent().getClass().getAnnotation(SwitchChildDiv.Next.class) != null, indexProperty);
}
}
}
}