-
Notifications
You must be signed in to change notification settings - Fork 5
/
ContextAction.java
86 lines (76 loc) · 4 KB
/
ContextAction.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
package org.genericsystem.reactor.model;
import org.genericsystem.reactor.modelproperties.ConvertedValueDefaults;
import org.genericsystem.reactor.modelproperties.GSBuilderDefaults;
import org.genericsystem.reactor.modelproperties.SelectionDefaults;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import org.genericsystem.common.Generic;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.gscomponents.GSInputTextWithConversion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javafx.beans.property.Property;
public interface ContextAction extends BiConsumer<Context, Tag> {
public static final Logger log = LoggerFactory.getLogger(ContextAction.class);
public static class ADD_HOLDER implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Property<Serializable> convertedValue = ((ConvertedValueDefaults) tag.getParent()).getConvertedValueProperty(context.getParent());
if (convertedValue.getValue() != null) {
Serializable newValue = convertedValue.getValue();
convertedValue.setValue(null);
context.getGenerics()[1].addHolder(context.getGeneric(), newValue);
} else if (Boolean.class.equals(context.getGeneric().getInstanceValueClassConstraint()))
context.getGenerics()[1].addHolder(context.getGeneric(), false);
}
}
public static class REMOVE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.remove();
}
}
public static class SET_SELECTION implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
if (SelectionDefaults.class.isAssignableFrom(tag.getClass()))
((SelectionDefaults) tag).getSelectionProperty(context).setValue(context);
else
log.warn("The SET_SELECTION action can apply only to a tag class implementing SelectionDefaults.");
}
}
public static class CREATE_INSTANCE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
if (GSBuilderDefaults.class.isAssignableFrom(tag.getClass())) {
GSBuilderDefaults buildTag = (GSBuilderDefaults) tag;
ConvertedValueDefaults input = tag.getParent().getParent().find(GSInputTextWithConversion.class);
Generic newInstance = context.getGeneric().setInstance(input.getConvertedValueProperty(context).getValue());
for (Entry<Generic, Property<Serializable>> entry : buildTag.getHoldersMapProperty(context).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 : buildTag.getComponentsMapProperty(context).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));
}
Map<Generic, Map<Generic, Property<Serializable>>> relationMap = buildTag.getMultipleRelationProperty(context).getValue();
for (Entry<Generic, Map<Generic, Property<Serializable>>> entry : relationMap.entrySet())
for (Generic target : entry.getValue().keySet())
newInstance.setHolder(entry.getKey(), null, target);
for (Property<Serializable> convertedProperty : relationMap.values().stream().flatMap(hm -> hm.values().stream()).collect(Collectors.toList()))
convertedProperty.setValue(null);
input.getConvertedValueProperty(context).setValue(null);
} else
log.warn("The CREATE_INSTANCE action can apply only to a tag class implementing GSBuilderDefaults.");
}
}
}