-
Notifications
You must be signed in to change notification settings - Fork 5
/
GSLinkEditor.java
95 lines (80 loc) · 3.72 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
package org.genericsystem.reactor.gs;
import java.util.List;
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.GSButton;
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);
createNewProperty(ReactorStatics.COMPONENTS);
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 GSButton(this) {
{
addStyle("justify-content", "center");
addStyle("align-items", "center");
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 -> ((GSInstanceCreator) parent.getParent().getParent()).getLinksValues().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);
new GSButton(this) {
{
addStyle("justify-content", "center");
addStyle("align-items", "center");
addStyle("height", "100%");
setText("+");
bindAttribute(ReactorStatics.DISABLED, ReactorStatics.DISABLED, 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() ? ReactorStatics.DISABLED : "";
}, ((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());
model.getGenerics()[3].setHolder(model.getGeneric(), null, selectedGenerics.stream().toArray(Generic[]::new));
selectedComponents.stream().forEach(sel -> sel.setValue(null));
} catch (RollbackException e) {
e.printStackTrace();
}
});
}
};
}
}
}