-
Notifications
You must be signed in to change notification settings - Fork 5
/
SelectionDefaults.java
74 lines (62 loc) · 3.06 KB
/
SelectionDefaults.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
package org.genericsystem.reactor.gs;
import java.util.Optional;
import java.util.function.Consumer;
import javafx.beans.property.Property;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import org.genericsystem.common.Generic;
import org.genericsystem.defaults.tools.BidirectionalBinding;
import org.genericsystem.reactor.Model;
import org.genericsystem.reactor.ReactorStatics;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.model.GenericModel;
/**
* @author Nicolas Feybesse
*
*/
public interface SelectionDefaults {
// void addPrefixBinding(Consumer<GenericModel> consumer);
void addPostfixBinding(Consumer<GenericModel> consumer);
void createNewProperty(String propertyName);
<T> Property<T> getProperty(String property, Model model);
default void createSelectionProperty() {
createNewProperty(ReactorStatics.SELECTION);
}
default Property<GenericModel> getSelectionProperty(GenericModel model) {
return getProperty(ReactorStatics.SELECTION, model);
}
default void bindBiDirectionalSelection(Tag<GenericModel> subElement) {
addPostfixBinding(modelContext -> {
ObservableList<GenericModel> subContexts = modelContext.getSubContexts(subElement);
Generic selectedGeneric = modelContext.getGeneric();
Optional<GenericModel> selectedModel = subContexts.stream().filter(sub -> selectedGeneric.equals(sub.getGeneric())).findFirst();
Property<GenericModel> selection = getSelectionProperty(modelContext);// getProperty(ReactorStatics.SELECTION, modelContext);
int selectionShift = getProperty(ReactorStatics.SELECTION_SHIFT, modelContext) != null ? (Integer) getProperty(ReactorStatics.SELECTION_SHIFT, modelContext).getValue() : 0;
selection.setValue(selectedModel.isPresent() ? selectedModel.get() : null);
Property<Number> selectionIndex = getProperty(ReactorStatics.SELECTION_INDEX, modelContext);
BidirectionalBinding.bind(selectionIndex, selection, number -> number.intValue() - selectionShift >= 0 ? (GenericModel) subContexts.get(number.intValue() - selectionShift) : null, genericModel -> subContexts.indexOf(genericModel)
+ selectionShift);
subContexts.addListener((ListChangeListener<GenericModel>) change -> {
if (selection != null) {
Number oldIndex = (Number) getProperty(ReactorStatics.SELECTION_INDEX, modelContext).getValue();
Number newIndex = subContexts.indexOf(selection.getValue()) + selectionShift;
if (newIndex != oldIndex)
this.getProperty(ReactorStatics.SELECTION_INDEX, modelContext).setValue(newIndex);
}
});
});
}
default void bindSelection(Tag<GenericModel> subElement) {
addPostfixBinding(model -> {
ObservableList<GenericModel> subContexts = model.getSubContexts(subElement);
Property<GenericModel> selection = getSelectionProperty(model);
subContexts.addListener((ListChangeListener<GenericModel>) change -> {
if (selection != null)
while (change.next())
if (change.wasRemoved() && !change.wasAdded())
if (change.getRemoved().contains(selection.getValue()))
selection.setValue(null);
});
});
}
}