-
Notifications
You must be signed in to change notification settings - Fork 5
/
GSInstanceBuilder.java
196 lines (170 loc) · 7.73 KB
/
GSInstanceBuilder.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
package org.genericsystem.reactor.gs3;
import java.io.Serializable;
import java.util.List;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.genericsystem.common.Generic;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.ReactorStatics;
import org.genericsystem.reactor.annotations.Parent;
import org.genericsystem.reactor.annotations.ReactorDependencies;
import org.genericsystem.reactor.annotations.Styles.Flex;
import org.genericsystem.reactor.annotations.Styles.Height;
import org.genericsystem.reactor.annotations.Styles.ReverseFlexDirection;
import org.genericsystem.reactor.annotations.Styles.Width;
import org.genericsystem.reactor.gs.GSCheckBoxWithValue;
import org.genericsystem.reactor.gs.GSInputTextWithConversion;
import org.genericsystem.reactor.gs3.GSCellDiv.ButtonDiv;
import org.genericsystem.reactor.gs3.GSCellDiv.CenteredFlexDiv;
import org.genericsystem.reactor.gs3.GSCellDiv.GSComponentEditorDiv;
import org.genericsystem.reactor.gs3.GSCellDiv.GSSubcellEditorDiv;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellAdder.LinkAdder.ComponentAdder.ComponentAdderSelect;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellEditor.LinkEditor;
import org.genericsystem.reactor.gs3.GSInstanceBuilder.AddButtonDiv.AddButton;
import org.genericsystem.reactor.gs3.GSInstanceBuilder.BuilderCell.BooleanHolderBuilder.CheckboxContainerBuildDiv.BooleanHolderBuilderInput;
import org.genericsystem.reactor.gs3.GSInstanceBuilder.BuilderCell.HolderBuilder.HolderBuilderInput;
import org.genericsystem.reactor.gs3.GSInstanceBuilder.BuilderCell.LinkBuilder.ComponentBuilder.ComponentBuilderSelect;
import org.genericsystem.reactor.gs3.GSInstanceBuilder.InstanceNameBuilder.InstanceNameBuilderInput;
import org.genericsystem.reactor.gstag.HtmlButton;
import org.genericsystem.reactor.model.ObservableListExtractor;
import org.genericsystem.reactor.modelproperties.ConvertedValueDefaults;
import org.genericsystem.reactor.modelproperties.GSBuilderDefaults;
import javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
import javafx.beans.value.ObservableValue;
@Parent(GSTable.class)
@ReactorDependencies({ InstanceNameBuilderInput.class, HolderBuilderInput.class, BooleanHolderBuilderInput.class, ComponentBuilderSelect.class, AddButton.class })
@Flex("1")
@ReverseFlexDirection
public class GSInstanceBuilder extends GSCompositeDiv implements GSBuilderDefaults {
public GSInstanceBuilder() {
super();
}
// public GSInstanceBuilder(Tag parent) {
// super(parent, GSInstanceBuilder.class);
// }
@Override
public void init() {
createHoldersMapProperty();
createComponentsMapProperty();
createInvalidListProperty();
}
// For the creation of the instance’s value.
@Parent(GSInstanceBuilder.class)
public static class InstanceNameBuilder extends GSSubcellEditorDiv {
@Parent(InstanceNameBuilder.class)
@Flex("1")
@Height("100%")
@Width("100%")
public static class InstanceNameBuilderInput extends GSInputTextWithConversion {
}
}
// Creation of holders/links.
@Parent(GSInstanceBuilder.class)
public static class BuilderCell extends GSSubcellEditorDiv {
@Override
public void init() {
forEach(ObservableListExtractor.ATTRIBUTES_OF_TYPE);
}
// Creation of non-boolean holders.
@Parent(BuilderCell.class)
public static class HolderBuilder extends GSSubcellEditorDiv {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() < 2 && !Boolean.class.equals(gs[0].getInstanceValueClassConstraint()) ? gs[0] : null);
}
@Parent(HolderBuilder.class)
@Flex("1")
@Height("100%")
@Width("100%")
public static class HolderBuilderInput extends GSInputTextWithConversion implements GSBuilderDefaults {
@Override
public void init() {
addPrefixBinding(model -> {
if (getHoldersMapProperty(model) != null)
getHoldersMapProperty(model).getValue().put(model.getGeneric(), getConvertedValueProperty(model));
if (getInvalidListProperty(model) != null)
getInvalidListProperty(model).getValue().add(getInvalidObservable(model));
});
}
}
}
// Creation of boolean holders.
@Parent(BuilderCell.class)
public static class BooleanHolderBuilder extends GSSubcellEditorDiv {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() < 2 && Boolean.class.equals(gs[0].getInstanceValueClassConstraint()) ? gs[0] : null);
}
@Parent(BooleanHolderBuilder.class)
public static class CheckboxContainerBuildDiv extends CenteredFlexDiv {
@Parent(CheckboxContainerBuildDiv.class)
public static class BooleanHolderBuilderInput extends GSCheckBoxWithValue implements GSBuilderDefaults {
@Override
public void init() {
addPrefixBinding(model -> {
if (getHoldersMapProperty(model) != null)
getHoldersMapProperty(model).getValue().put(model.getGeneric(), getConvertedValueProperty(model));
});
}
}
}
}
// Creation of links.
@Parent(BuilderCell.class)
public static class LinkBuilder extends LinkEditor implements org.genericsystem.reactor.modelproperties.GSBuilderDefaults {
@Override
public void init() {
super.init();
addPostfixBinding(model -> {
if (getComponentsMapProperty(model) != null)
getComponentsMapProperty(model).getValue().put(model.getGeneric(), getComponentsProperty(model).getValue());
});
}
@Parent(LinkBuilder.class)
public static class ComponentBuilder extends GSComponentEditorDiv {
@Override
public void init() {
forEach((ObservableListExtractor) gs -> ObservableListExtractor.COMPONENTS.apply(gs).filtered(g -> !g.equals(gs[1])));
}
@Parent(ComponentBuilder.class)
public static class ComponentBuilderSelect extends ComponentAdderSelect {
}
}
}
}
// “Add” button.
@Parent(GSInstanceBuilder.class)
public static class AddButtonDiv extends ButtonDiv {
@Parent(AddButtonDiv.class)
@Flex("1")
@Height("100%")
@Width("100%")
public static class AddButton extends HtmlButton {
@Override
public void init() {
setText("Add");
bindAttribute(ReactorStatics.DISABLED, ReactorStatics.DISABLED,
model -> Bindings.createStringBinding(
() -> Boolean.TRUE.equals(getInvalidListProperty(model).getValue().stream().map(input -> input.getValue()).filter(bool -> bool != null).reduce(false, (a, b) -> a || b)) ? ReactorStatics.DISABLED : "",
getInvalidListProperty(model).getValue().stream().toArray(ObservableValue[]::new)));
bindAction(model -> {
ConvertedValueDefaults input = (ConvertedValueDefaults) find(InstanceNameBuilderInput.class);
Generic newInstance = model.getGeneric().setInstance(input.getConvertedValueProperty(model).getValue());
for (Entry<Generic, Property<Serializable>> entry : getHoldersMapProperty(model).getValue().entrySet())
if (entry.getValue().getValue() != null) {
newInstance.setHolder(entry.getKey(), entry.getValue().getValue());
entry.getValue().setValue(null);
}
for (Entry<Generic, List<Property<Context>>> entry : getComponentsMapProperty(model).getValue().entrySet()) {
List<Generic> selectedGenerics = entry.getValue().stream().filter(obs -> obs.getValue() != null).map(obs -> obs.getValue().getGeneric()).filter(gen -> gen != null).collect(Collectors.toList());
if (!selectedGenerics.isEmpty() && selectedGenerics.size() + 1 == entry.getKey().getComponents().size())
newInstance.setHolder(entry.getKey(), null, selectedGenerics.stream().toArray(Generic[]::new));
entry.getValue().stream().forEach(sel -> sel.setValue(null));
}
input.getConvertedValueProperty(model).setValue(null);
});
}
}
}
}