-
Notifications
You must be signed in to change notification settings - Fork 5
/
Model.java
140 lines (115 loc) · 4.61 KB
/
Model.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
134
135
136
137
138
139
140
package org.genericsystem.reactor;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.collections.ObservableSet;
import org.genericsystem.defaults.tools.TransformationObservableList;
import org.genericsystem.reactor.HtmlDomNode.SelectableHtmlDomNode;
/**
* @author Nicolas Feybesse
*
*/
public class Model {
private Model parent;
private Map<Tag<?>, ViewContext<?>> viewContextsMap = new LinkedHashMap<>();
private Map<Tag<?>, ObservableList<Model>> subModelsMap = new HashMap<>();
private Map<Tag<?>, Map<String, ObservableValue<?>>> propertiesMap = new HashMap<>();
public Model(Model parent) {
this.parent = parent;
}
public Model getParent() {
return this.parent;
}
@SuppressWarnings("unchecked")
public <SUBMODEL extends Model> ObservableList<SUBMODEL> getSubContexts(Tag<SUBMODEL> tag) {
return (ObservableList<SUBMODEL>) subModelsMap.get(tag);
}
public boolean containsProperty(Tag<?> tag, String propertyName) {
return propertiesMap.containsKey(tag) ? propertiesMap.get(tag).containsKey(propertyName) : false;
}
public void createNewProperty(Tag<?> tag, String propertyName) {
assert viewContextsMap.keySet().contains(tag);
if (getProperties(tag).containsKey(propertyName))
throw new IllegalStateException("Unable to create an already used property : " + propertyName);
getProperties(tag).put(propertyName, new SimpleObjectProperty<>());
}
private Map<String, ObservableValue<?>> getProperties(Tag<?> tag) {
Map<String, ObservableValue<?>> properties = propertiesMap.get(tag);
if (properties == null) {
assert viewContextsMap.keySet().contains(tag);
propertiesMap.put(tag, properties = new HashMap<String, ObservableValue<?>>());
}
return properties;
}
@SuppressWarnings("unchecked")
<T> ObservableValue<T> getObservableValue(Tag<?> tag, String propertyName) {
return (ObservableValue<T>) getProperties(tag).get(propertyName);
}
@SuppressWarnings("unchecked")
<T> Property<T> getProperty(Tag<?> tag, String propertyName) {
return (Property<T>) getProperties(tag).get(propertyName);
}
public Collection<Map<String, ObservableValue<?>>> getPropertiesMaps() {
return propertiesMap.values();
}
protected void storeProperty(Tag<?> tag, String propertyName, ObservableValue<?> value) {
assert viewContextsMap.keySet().contains(tag);
if (getProperties(tag).containsKey(propertyName))
throw new IllegalStateException("Unable to store an already used property : " + propertyName);
getProperties(tag).put(propertyName, value);
}
public List<Model> subContexts() {
return subModelsMap.values().stream().flatMap(list -> list.stream()).collect(Collectors.toList());
}
@SuppressWarnings("unchecked")
<SUBMODEL extends Model> void setSubContexts(Tag<?> element, ObservableList<SUBMODEL> subContexts) {
assert subModelsMap.get(element) == null;
subModelsMap.put(element, (ObservableList<Model>) subContexts);
}
public void register(ViewContext<?> viewContext) {
ViewContext<?> previous = viewContextsMap.put(viewContext.getTag(), viewContext);
assert previous == null;
}
public void destroy() {
viewContextsMap.values().iterator().next().getNode().sendRemove();
internalDestroy();
}
public boolean destroyed = false;
public void internalDestroy() {
// System.out.println("InternalDestroy : " + this);
assert !destroyed;
destroyed = true;
for (ViewContext<?> viewContext : viewContextsMap.values()) {
viewContext.destroy();
}
for (ObservableList<Model> subModels : subModelsMap.values()) {
((TransformationObservableList<?, ?>) subModels).unbind();
for (Model subModel : subModels)
subModel.internalDestroy();
}
subModelsMap = new HashMap<>();
viewContextsMap = new LinkedHashMap<>();
propertiesMap = new HashMap<>();
}
@SuppressWarnings("unchecked")
public <M extends Model> ViewContext<M> getViewContext(Tag<?> element) {
return (ViewContext<M>) viewContextsMap.get(element);
}
public ObservableSet<String> getObservableStyleClasses(Tag<?> element) {
return getViewContext(element).getNode().getStyleClasses();
}
public ObservableMap<String, String> getObservableAttributes(Tag<?> element) {
return getViewContext(element).getNode().getAttributes();
}
public Property<Number> getSelectionIndex(Tag<?> element) {
return getViewContext(element).<SelectableHtmlDomNode> getNode().getSelectionIndex();
}
}