-
Notifications
You must be signed in to change notification settings - Fork 5
/
SelectionDefaults.java
133 lines (113 loc) · 5.78 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
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
package org.genericsystem.reactor.contextproperties;
import java.util.Optional;
import java.util.stream.Collectors;
import org.genericsystem.common.Generic;
import org.genericsystem.defaults.tools.BidirectionalBinding;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.context.StringExtractor;
import javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.beans.value.WeakChangeListener;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
/**
* @author Nicolas Feybesse
*
*/
public interface SelectionDefaults extends ContextProperty {
public static final String SELECTION = "selection";
public static final String UPDATED_GENERIC = "updatedGeneric";
public static final String SELECTION_INDEX = "selectionIndex";
public static final String SELECTION_SHIFT = "selectionShift";
public static final String SELECTION_STRING = "selectionString";
public static final String CONTEXTS_LISTENER = "contextsListener";
public static final String UPDATED_GENERIC_LISTENER = "updatedGenericListener";
default void createSelectionProperty() {
createNewContextProperty(SELECTION);
addContextAttribute(SELECTION_STRING,
model -> Bindings.createStringBinding(() -> StringExtractor.SIMPLE_CLASS_EXTRACTOR.apply(getSelectionProperty(model).getValue() != null ? getSelectionProperty(model).getValue().getGeneric() : null), getSelectionProperty(model)));
addContextAttribute(SELECTION_INDEX, model -> {
Property<Integer> index = new SimpleObjectProperty<>();
index.addListener(new WeakChangeListener<>(model.getHtmlDomNode((Tag) this).getIndexListener()));
return index;
});
createNewContextProperty(UPDATED_GENERIC);
}
default Property<Context> getSelectionProperty(Context context) {
return getContextProperty(SELECTION, context);
}
default Property<Generic> getUpdatedGenericProperty(Context context) {
return getContextProperty(UPDATED_GENERIC, context);
}
default Property<Integer> getSelectionIndex(Context context) {
return getContextProperty(SELECTION_INDEX, context);
}
default void setSelectionShift(int shift) {
createNewInitializedProperty(SELECTION_SHIFT, context -> shift);
}
default int getSelectionShift(Context context) {
Property<Integer> selectionShiftProperty = getContextProperty(SELECTION_SHIFT, context);
return selectionShiftProperty != null ? selectionShiftProperty.getValue() : 0;
}
default ObservableValue<String> getSelectionString(Context context) {
return getContextObservableValue(SELECTION_STRING, context);
}
default ListChangeListener<Context> getContextsListenerProperty(Context context) {
return getContextAttribute(CONTEXTS_LISTENER, context);
}
default ChangeListener<Generic> getUpdatedGenericListenerProperty(Context context) {
return getContextAttribute(UPDATED_GENERIC_LISTENER, context);
}
default void bindBiDirectionalSelection(Tag subElement) {
addPostfixBinding(modelContext -> {
ObservableList<Context> subContexts = modelContext.getSubContexts(subElement);
Generic selectedGeneric = modelContext.getGeneric();
Optional<Context> selectedModel = subContexts.stream().filter(sub -> selectedGeneric.equals(sub.getGeneric())).findFirst();
Property<Context> selection = getSelectionProperty(modelContext);
int selectionShift = getSelectionShift(modelContext);
selection.setValue(selectedModel.isPresent() ? selectedModel.get() : null);
Property<Integer> selectionIndex = getSelectionIndex(modelContext);
BidirectionalBinding.bind(selectionIndex, selection, number -> number.intValue() - selectionShift >= 0 ? (Context) subContexts.get(number.intValue() - selectionShift) : null, genericModel -> subContexts.indexOf(genericModel) + selectionShift);
subContexts.addListener((ListChangeListener<Context>) change -> {
if (selection != null) {
Integer newIndex = subContexts.indexOf(selection.getValue()) + selectionShift;
if (newIndex != selectionIndex.getValue())
selectionIndex.setValue(newIndex);
}
});
});
}
default void bindSelection(Tag subElement, Context context) {
ObservableList<Context> subContexts = context.getSubContexts(subElement);
Property<Context> selection = getSelectionProperty(context);
Property<Generic> updatedGeneric = getUpdatedGenericProperty(context);
setContextAttribute(CONTEXTS_LISTENER, context, (ListChangeListener<Context>) change -> {
if (selection.getValue() != null)
while (change.next())
if (change.wasRemoved() && !change.wasAdded() && change.getRemoved().stream().map(c -> c.getGeneric()).collect(Collectors.toList()).contains(selection.getValue().getGeneric()))
selection.setValue(null);
});
setContextAttribute(UPDATED_GENERIC_LISTENER, context, (ChangeListener<Generic>) (o, v, nv) -> {
Optional<? extends Context> updatedContext = subContexts.stream().filter(m -> m.getGeneric().equals(nv)).findFirst();
if (updatedContext.isPresent() && selection.getValue() == null)
selection.setValue(updatedContext.get());
});
if (selection != null && updatedGeneric != null) {
subContexts.addListener(getContextsListenerProperty(context));
updatedGeneric.addListener(getUpdatedGenericListenerProperty(context));
}
}
default void unbindSelection(Tag subElement, Context context) {
ObservableList<Context> subContexts = context.getSubContexts(subElement);
Property<Context> selection = getSelectionProperty(context);
Property<Generic> updatedGeneric = getUpdatedGenericProperty(context);
if (selection != null && updatedGeneric != null) {
subContexts.removeListener(getContextsListenerProperty(context));
updatedGeneric.removeListener(getUpdatedGenericListenerProperty(context));
}
}
}