-
Notifications
You must be signed in to change notification settings - Fork 5
/
HtmlDomNode.java
230 lines (186 loc) · 7.87 KB
/
HtmlDomNode.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package org.genericsystem.reactor;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.json.JsonObject;
import java.util.function.Consumer;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.WeakChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableSet;
import javafx.collections.SetChangeListener;
import javafx.collections.WeakSetChangeListener;
public class HtmlDomNode {
static int count = 0;
static final String MSG_TYPE = "msgType";
static final String ADD = "A";
private static final String UPDATE = "U";
static final String REMOVE = "R";
static final String UPDATE_TEXT = "UT";
private static final String UPDATE_SELECTION = "US";
static final String ADD_STYLECLASS = "AC";
static final String REMOVE_STYLECLASS = "RC";
static final String ADD_STYLE = "AS";
static final String REMOVE_STYLE = "RS";
static final String ADD_ATTRIBUTE = "AA";
static final String REMOVE_ATTRIBUTE = "RA";
static final String PARENT_ID = "parentId";
public static final String ID = "nodeId";
static final String NEXT_ID = "nextId";
static final String STYLE_PROPERTY = "styleProperty";
static final String STYLE_VALUE = "styleValue";
static final String ATTRIBUTE_NAME = "attributeName";
static final String ATTRIBUTE_VALUE = "attributeValue";
static final String STYLECLASS = "styleClass";
static final String TEXT_CONTENT = "textContent";
static final String TAG_HTML = "tagHtml";
private static final String ELT_TYPE = "eltType";
private final String id;
private final String parentId;
ViewContext<?> viewContext;
private final ObservableSet<String> styleClasses = FXCollections.observableSet();
private final MapChangeListener<String, String> stylesListener = change -> {
if (!change.wasAdded() || change.getValueAdded() == null || change.getValueAdded().equals(""))
sendMessage(new JsonObject().put(MSG_TYPE, REMOVE_STYLE).put(ID, getId()).put(STYLE_PROPERTY, change.getKey()));
else if (change.wasAdded())
sendMessage(new JsonObject().put(MSG_TYPE, ADD_STYLE).put(ID, getId()).put(STYLE_PROPERTY, change.getKey()).put(STYLE_VALUE, change.getValueAdded()));
};
private final MapChangeListener<String, String> attributesListener = change -> {
if (!change.wasAdded() || change.getValueAdded() == null || change.getValueAdded().equals(""))
sendMessage(new JsonObject().put(MSG_TYPE, REMOVE_ATTRIBUTE).put(ID, getId()).put(ATTRIBUTE_NAME, change.getKey()));
else if (change.wasAdded())
sendMessage(new JsonObject().put(MSG_TYPE, ADD_ATTRIBUTE).put(ID, getId()).put(ATTRIBUTE_NAME, change.getKey()).put(ATTRIBUTE_VALUE, change.getValueAdded()));
};
private final SetChangeListener<String> styleClassesListener = change -> {
if (change.wasAdded())
sendMessage(new JsonObject().put(MSG_TYPE, ADD_STYLECLASS).put(ID, getId()).put(STYLECLASS, change.getElementAdded()));
else
sendMessage(new JsonObject().put(MSG_TYPE, REMOVE_STYLECLASS).put(ID, getId()).put(STYLECLASS, change.getElementRemoved()));
};
private final ChangeListener<String> textListener = (o, old, newValue) -> sendMessage(new JsonObject().put(MSG_TYPE, UPDATE_TEXT).put(ID, getId()).put(TEXT_CONTENT, newValue != null ? newValue : ""));
public ChangeListener<String> getTextListener() {
return textListener;
}
public MapChangeListener<String, String> getStylesListener() {
return stylesListener;
}
public MapChangeListener<String, String> getAttributesListener() {
return attributesListener;
}
public HtmlDomNode(String parentId) {
assert parentId != null;
this.parentId = parentId;
this.id = String.format("%010d", Integer.parseInt(this.hashCode() + "")).substring(0, 10);
styleClasses.addListener(new WeakSetChangeListener<>(styleClassesListener));
}
public void sendAdd(int index) {
JsonObject jsonObj = new JsonObject().put(MSG_TYPE, ADD);
jsonObj.put(PARENT_ID, parentId);
jsonObj.put(ID, id);
jsonObj.put(TAG_HTML, viewContext.getTag().getTag());
jsonObj.put(NEXT_ID, index);
fillJson(jsonObj);
// System.out.println(jsonObj.encodePrettily());
sendMessage(jsonObj);
}
public JsonObject fillJson(JsonObject jsonObj) {
return null;
}
public void sendRemove() {
sendMessage(new JsonObject().put(MSG_TYPE, REMOVE).put(ID, id));
// System.out.println(new JsonObject().put(MSG_TYPE, REMOVE).put(ID, id).encodePrettily());
}
public void sendMessage(JsonObject jsonObj) {
jsonObj.put("count", count++);
// if (jsonObj.getString(MSG_TYPE).equals(ADD) || jsonObj.getString(MSG_TYPE).equals(REMOVE))
// System.out.println(jsonObj.encodePrettily());
getWebSocket().writeFinalTextFrame(jsonObj.encode());
}
public ServerWebSocket getWebSocket() {
return viewContext.getWebSocket();
}
public ObservableSet<String> getStyleClasses() {
return styleClasses;
}
public String getId() {
return id;
}
public void handleMessage(JsonObject json) {
}
public static class ActionHtmlNode extends HtmlDomNode {
public ActionHtmlNode(String parentId) {
super(parentId);
}
private final Property<Consumer<Object>> actionProperty = new SimpleObjectProperty<>();
public Property<Consumer<Object>> getActionProperty() {
return actionProperty;
}
@Override
public void handleMessage(JsonObject json) {
getActionProperty().getValue().accept(new Object());
}
}
public static class SelectableHtmlDomNode extends ActionHtmlNode {
private static final String SELECTED_INDEX = "selectedIndex";
private Property<Number> selectionIndex = new SimpleIntegerProperty();
private final ChangeListener<Number> indexListener = (o, old, newValue) -> {
// System.out.println(new JsonObject().put(MSG_TYPE, UPDATE_SELECTION).put(ID, getId()).put(SELECTED_INDEX, newValue != null ? newValue : 0)
// .encodePrettily());
sendMessage(new JsonObject().put(MSG_TYPE, UPDATE_SELECTION).put(ID, getId()).put(SELECTED_INDEX, newValue != null ? newValue : 0));
};
public SelectableHtmlDomNode(String parentId) {
super(parentId);
selectionIndex.addListener(new WeakChangeListener<>(indexListener));
}
public Property<Number> getSelectionIndex() {
return selectionIndex;
}
@Override
public void handleMessage(JsonObject json) {
if (UPDATE.equals(json.getString(MSG_TYPE))) {
getSelectionIndex().setValue(json.getInteger(SELECTED_INDEX));
// System.out.println("Selected index : " + getSelectionIndex().getValue());
}
}
}
public static class InputTextHtmlDomNode extends HtmlDomNode {
private final Property<Consumer<Object>> enterProperty = new SimpleObjectProperty<>();
public InputTextHtmlDomNode(String parentId) {
super(parentId);
}
@Override
public JsonObject fillJson(JsonObject jsonObj) {
super.fillJson(jsonObj);
return jsonObj.put("type", "text");
}
@Override
public void handleMessage(JsonObject json) {
if (ADD.equals(json.getString(MSG_TYPE)))
getEnterProperty().getValue().accept(new Object());
if (UPDATE.equals(json.getString(MSG_TYPE)))
viewContext.getTag().getDomNodeAttributes(viewContext.getModelContext()).put(ReactorStatics.VALUE, json.getString(TEXT_CONTENT));
}
public Property<Consumer<Object>> getEnterProperty() {
return enterProperty;
}
}
public static class InputCheckHtmlDomNode extends HtmlDomNode {
private final String type;
public InputCheckHtmlDomNode(String parentId, String type) {
super(parentId);
this.type = type;
}
@Override
public JsonObject fillJson(JsonObject jsonObj) {
super.fillJson(jsonObj);
return jsonObj.put("type", type);
}
@Override
public void handleMessage(JsonObject json) {
if ("checkbox".equals(json.getString(ELT_TYPE)))
viewContext.getTag().getDomNodeAttributes(viewContext.getModelContext()).put(ReactorStatics.CHECKED, json.getBoolean(ReactorStatics.CHECKED) ? ReactorStatics.CHECKED : "");
}
}
}