-
Notifications
You must be signed in to change notification settings - Fork 5
/
GSLinkEditor.java
99 lines (84 loc) · 3.89 KB
/
GSLinkEditor.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
package org.genericsystem.reactor.gs;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.genericsystem.api.core.exceptions.RollbackException;
import org.genericsystem.common.Generic;
import org.genericsystem.reactor.ReactorStatics;
import org.genericsystem.reactor.gs.GSSingleLinkComponentEditor.GSLinkComponentAdder;
import org.genericsystem.reactor.gs.GSSingleLinkComponentEditor.GSLinkComponentCreator;
import org.genericsystem.reactor.gs.GSSingleLinkComponentEditor.GSLinkComponentEditor;
import org.genericsystem.reactor.gstag.GSHyperLink;
import org.genericsystem.reactor.model.GenericModel;
import javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
public class GSLinkEditor extends GSSection {
protected GSSingleLinkComponentEditor components;
public GSLinkEditor(GSTag parent) {
this(parent, GSLinkComponentEditor::new);
}
public GSLinkEditor(GSTag parent, GSLinkComponentConstructor constructor) {
super(parent, FlexDirection.ROW);
createNewInitializedProperty(ReactorStatics.COMPONENTS, model -> new ArrayList<Property<GenericModel>>());
components = constructor.build(this);
}
@FunctionalInterface
public interface GSLinkComponentConstructor {
GSSingleLinkComponentEditor build(GSTag parent);
}
public static class GSLinkEditorWithRemoval extends GSLinkEditor {
public GSLinkEditorWithRemoval(GSTag parent) {
super(parent);
new GSHyperLink(this) {
{
addStyle("justify-content", "center");
addStyle("text-decoration", "none");
addStyle("height", "100%");
setText("×");
bindAction(GenericModel::remove);
}
};
}
}
public static class GSLinkCreator extends GSLinkEditor {
public GSLinkCreator(GSTag parent) {
this(parent, GSLinkComponentCreator::new);
}
public GSLinkCreator(GSTag parent, GSLinkComponentConstructor constructor) {
super(parent, constructor);
if (parent != null && parent.getParent() != null && parent.getParent().getParent() instanceof GSInstanceCreator)
addPostfixBinding(
model -> ((Map<Generic, List<Property<GenericModel>>>) getProperty(ReactorStatics.COMPONENTS_MAP, model).getValue()).put(model.getGeneric(), (List<Property<GenericModel>>) getProperty(ReactorStatics.COMPONENTS, model).getValue()));
}
}
public static class GSLinkAdder extends GSLinkCreator {
public GSLinkAdder(GSTag parent) {
super(parent, GSLinkComponentAdder::new);
addStyle("height", "100%");
new GSHyperLink(this) {
{
addStyle("justify-content", "center");
addStyle("text-decoration", "none");
addStyle("height", "100%");
setText("+");
bindStyle(ReactorStatics.DISPLAY, ReactorStatics.DISPLAY, model -> Bindings.createStringBinding(() -> {
List<Generic> selectedGenerics = ((List<Property<GenericModel>>) getProperty(ReactorStatics.COMPONENTS, model).getValue()).stream().filter(obs -> obs.getValue() != null).map(obs -> obs.getValue().getGeneric())
.filter(gen -> gen != null).collect(Collectors.toList());
return selectedGenerics.size() + 1 == model.getGeneric().getComponents().size() ? "flex" : "none";
}, ((List<Property<GenericModel>>) getProperty(ReactorStatics.COMPONENTS, model).getValue()).stream().toArray(Property[]::new)));
bindAction(model -> {
try {
List<Property<GenericModel>> selectedComponents = (List<Property<GenericModel>>) getProperty(ReactorStatics.COMPONENTS, model).getValue();
List<Generic> selectedGenerics = selectedComponents.stream().filter(obs -> obs.getValue() != null).map(obs -> obs.getValue().getGeneric()).filter(gen -> gen != null).collect(Collectors.toList());
selectedComponents.stream().forEach(sel -> sel.setValue(null));
model.getGenerics()[1].setHolder(model.getGeneric(), null, selectedGenerics.stream().toArray(Generic[]::new));
} catch (RollbackException e) {
e.printStackTrace();
}
});
}
};
}
}
}