-
Notifications
You must be signed in to change notification settings - Fork 5
/
GSEditor.java
510 lines (428 loc) · 21 KB
/
GSEditor.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package org.genericsystem.reactor.ca_gscomponents2;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
import org.genericsystem.api.core.ApiStatics;
import org.genericsystem.api.core.exceptions.RollbackException;
import org.genericsystem.common.Generic;
import org.genericsystem.defaults.tools.BindingsTools;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.aa_modelproperties.ComponentsDefaults;
import org.genericsystem.reactor.aa_modelproperties.ConvertedValueDefaults;
import org.genericsystem.reactor.aa_modelproperties.SelectionDefaults;
import org.genericsystem.reactor.annotations.ForEach;
import org.genericsystem.reactor.annotations.Parent;
import org.genericsystem.reactor.annotations.ReactorDependencies;
import org.genericsystem.reactor.annotations.Select;
import org.genericsystem.reactor.annotations.Styles.AlignItems;
import org.genericsystem.reactor.annotations.Styles.Flex;
import org.genericsystem.reactor.annotations.Styles.FlexDirectionStyle;
import org.genericsystem.reactor.annotations.Styles.FlexWrap;
import org.genericsystem.reactor.annotations.Styles.Height;
import org.genericsystem.reactor.annotations.Styles.JustifyContent;
import org.genericsystem.reactor.annotations.Styles.KeepFlexDirection;
import org.genericsystem.reactor.annotations.Styles.ReverseFlexDirection;
import org.genericsystem.reactor.annotations.Styles.Style;
import org.genericsystem.reactor.annotations.Styles.Width;
import org.genericsystem.reactor.ba_htmltag.HtmlH2;
import org.genericsystem.reactor.ba_htmltag.HtmlLabel;
import org.genericsystem.reactor.ba_htmltag.HtmlLabel.GSLabelDisplayer;
import org.genericsystem.reactor.ca_gscomponents.FlexDirection;
import org.genericsystem.reactor.ca_gscomponents.GSCheckBoxWithValue;
import org.genericsystem.reactor.ca_gscomponents.GSCheckBoxWithValue.GSCheckBoxEditor;
import org.genericsystem.reactor.ca_gscomponents.GSDiv;
import org.genericsystem.reactor.ca_gscomponents.GSInputTextWithConversion;
import org.genericsystem.reactor.ca_gscomponents.GSInputTextWithConversion.GSInputTextEditorWithConversion;
import org.genericsystem.reactor.ca_gscomponents.GSSelect.CompositeSelectWithEmptyEntry;
import org.genericsystem.reactor.ca_gscomponents.GSSelect.InstanceCompositeSelect;
import org.genericsystem.reactor.ca_gscomponents2.DivWithTitle.GSTitleDiv;
import org.genericsystem.reactor.ca_gscomponents2.GSCellDiv.CenteredFlexDiv;
import org.genericsystem.reactor.ca_gscomponents2.GSCellDiv.GSActionLink;
import org.genericsystem.reactor.ca_gscomponents2.GSCellDiv.GSComponentEditorDiv;
import org.genericsystem.reactor.ca_gscomponents2.GSCellDiv.GSSubcellEditorDiv;
import org.genericsystem.reactor.ca_gscomponents2.GSCellDiv.GSTitleLineCellDiv;
import org.genericsystem.reactor.ca_gscomponents2.GSCellDiv.SubcellEditorContainerDiv;
import org.genericsystem.reactor.ca_gscomponents2.GSCellDiv.WrappedColumnDiv;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellAdder.BooleanHolderAdder.BooleanHolderAdditionLink;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellAdder.BooleanHolderAdder.CheckboxContainerAddDiv.BooleanHolderAdderInput;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellAdder.HolderAdder.HolderAdderInput;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellAdder.HolderAdder.HolderAdditionLink;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellAdder.LinkAdder.ComponentAdder.ComponentAdderSelect;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellEditor.BooleanHolderEditor.CheckboxContainerDiv.BooleanHolderEditorInput;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellEditor.HolderEditor.HolderEditorInput;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellEditor.LinkEditor.ComponentEditor.DirectRelationComponentEditor;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellEditor.LinkEditor.ComponentEditor.ReversedRelationDisplayer;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellEditor.RemovalLink;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.MultiCheckbox.CheckboxLabel.Checkbox;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.InstanceEdition.InstanceNameEditorDiv.InstanceNameEditor;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.LinkTitles.InstanceType;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.LinkTitles.InstanceType.TypeNameDisplayer;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.LinkTitles.TypeAttribute.AttributeName.AttributeNameDisplayer;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorContent.LinkTitles.TypeAttribute.RelationName.ComponentName.ComponentNameDisplayer;
import org.genericsystem.reactor.ca_gscomponents2.GSEditor.EditorTitle.EditorTitleContent;
import org.genericsystem.reactor.model.ObservableListExtractor;
import org.genericsystem.reactor.model.ObservableListExtractor.ATTRIBUTES_OF_INSTANCES;
import org.genericsystem.reactor.model.ObservableListExtractor.OTHER_COMPONENTS_2;
import org.genericsystem.reactor.model.ObservableValueSelector.RELATION_SELECTOR;
import org.genericsystem.reactor.model.ObservableValueSelector.STRICT_ATTRIBUTE_SELECTOR;
import org.genericsystem.reactor.model.StringExtractor;
import javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
@ReactorDependencies({ EditorTitleContent.class, InstanceType.class, TypeNameDisplayer.class, AttributeNameDisplayer.class, ComponentNameDisplayer.class, InstanceNameEditor.class, Checkbox.class, ReversedRelationDisplayer.class,
DirectRelationComponentEditor.class, BooleanHolderEditorInput.class, HolderEditorInput.class, RemovalLink.class, BooleanHolderAdderInput.class, BooleanHolderAdditionLink.class, HolderAdderInput.class, HolderAdditionLink.class,
ComponentAdderSelect.class })
@Flex("1")
@FlexDirectionStyle(FlexDirection.ROW)
public class GSEditor extends GSDiv {
public GSEditor() {
super();
}
public GSEditor(Tag parent) {
super(parent);
}
// No automatic enclosing class parent for this GSEditor extension
@FlexDirectionStyle(FlexDirection.COLUMN)
public static class HorizontalGSEditor extends GSEditor implements SelectionDefaults {
public HorizontalGSEditor(Tag parent) {
super(parent);
}
}
// Main title.
public static class EditorTitle extends GSTitleDiv {
public static class EditorTitleContent extends HtmlH2 {
@Override
public void init() {
setStringExtractor(StringExtractor.TYPE_INSTANCE_EXTRACTOR);
bindText();
}
}
}
// Content.
@Parent(GSEditor.class)
@KeepFlexDirection
@Flex("1")
@Style(name = "height", value = "100%")
public static class EditorContent extends GSDiv {
// Line/column with the names of the attributes and components of relations.
@ReverseFlexDirection
@Flex("0.3")
public static class LinkTitles extends GSDiv {
public static class InstanceType extends GSTitleLineCellDiv {
@Override
public void init() {
select(gs -> gs[1]);
}
public static class TypeNameDisplayer extends GSLabelDisplayer {
}
}
@ForEach(ATTRIBUTES_OF_INSTANCES.class)
@Flex("1")
@FlexDirectionStyle(FlexDirection.ROW)
public static class TypeAttribute extends GSDiv {
@Select(STRICT_ATTRIBUTE_SELECTOR.class)
public static class AttributeName extends GSTitleLineCellDiv {
public static class AttributeNameDisplayer extends GSLabelDisplayer {
}
}
@Select(RELATION_SELECTOR.class)
@Flex("1")
@FlexDirectionStyle(FlexDirection.ROW)
public static class RelationName extends GSDiv {
@ForEach(OTHER_COMPONENTS_2.class)
public static class ComponentName extends GSTitleLineCellDiv {
public static class ComponentNameDisplayer extends GSLabelDisplayer {
}
}
}
}
}
// Edition itself.
@Flex("1")
@ReverseFlexDirection
public static class InstanceEdition extends GSDiv {
// Edition of the name of the instance.
public static class InstanceNameEditorDiv extends GSSubcellEditorDiv {
@Flex("1")
@Height("100%")
@Width("100%")
public static class InstanceNameEditor extends GSInputTextEditorWithConversion {
}
}
// Edition of the holders/links.
public static class InstanceAttributeEditor extends WrappedColumnDiv {
@Override
public void init() {
forEach(ObservableListExtractor.ATTRIBUTES_OF_INSTANCES);
}
// Multiple checkboxes : for binary relations without the singular constraint.
public static class MultiCheckbox extends WrappedColumnDiv {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() == 2 && !gs[0].isSingularConstraintEnabled(gs[0].getComponents().indexOf(gs[2])) ? gs[0] : null);
}
@Flex("1 0 auto")
@JustifyContent("center")
@AlignItems("center")
@Style(name = "text-align", value = "center")
public static class CheckboxLabel extends HtmlLabel {
@Override
public void init() {
bindText();
forEach(gs -> ObservableListExtractor.SUBINSTANCES.apply(ObservableListExtractor.COMPONENTS.apply(gs).filtered(g -> !g.equals(gs[2])).stream().toArray(Generic[]::new)));
addPrefixBinding(model -> {
if ("Color".equals(StringExtractor.SIMPLE_CLASS_EXTRACTOR.apply(model.getGeneric().getMeta())))
addStyle(model, "background-color", getGenericStringProperty(model).getValue());
});
}
@Style(name = "float", value = "left")
@Style(name = "vertical-align", value = "middle")
@Style(name = "margin", value = "4px")
public static class Checkbox extends GSCheckBoxWithValue {
@Override
public void init() {
initValueProperty(context -> context.getGenerics()[2].getLink(context.getGenerics()[1], context.getGeneric()) != null ? true : false);
storeProperty("exists", context -> {
ObservableValue<Boolean> exists = Bindings.createBooleanBinding(() -> context.getGenerics()[2].getObservableLink(context.getGenerics()[1], context.getGeneric()).getValue() != null ? true : false,
context.getGenerics()[2].getObservableLink(context.getGenerics()[1], context.getGeneric()));
exists.addListener((o, v, nva) -> {
if (!context.isDestroyed())
getConvertedValueProperty(context).setValue(nva);
});
return exists;
});
addConvertedValueChangeListener((context, nva) -> {
if (Boolean.TRUE.equals(nva))
context.getGenerics()[2].setHolder(context.getGenerics()[1], null, context.getGeneric());
if (Boolean.FALSE.equals(nva)) {
Generic link = context.getGenerics()[2].getLink(context.getGenerics()[1], context.getGeneric());
if (link != null)
link.remove();
}
});
}
}
}
}
// Edition of other attributes.
@Flex("1")
@FlexWrap("wrap")
public static class AttributeEditionColumn extends GenericColumn {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() != 2 || gs[0].isSingularConstraintEnabled(gs[0].getComponents().indexOf(gs[2])) ? gs[0] : null);
}
public static class SubcellEditor extends SubcellEditorContainerDiv {
@Override
public void init() {
forEach(ObservableListExtractor.HOLDERS);
}
// Edition of non-boolean holders.
public static class HolderEditor extends GSSubcellEditorDiv {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() < 2 && !Boolean.class.equals(gs[0].getInstanceValueClassConstraint()) ? gs[0] : null);
}
@Flex("1")
@Height("100%")
@Width("100%")
public static class HolderEditorInput extends GSInputTextEditorWithConversion {
}
}
// Edition of boolean holders.
public static class BooleanHolderEditor extends GSSubcellEditorDiv {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() < 2 && Boolean.class.equals(gs[0].getInstanceValueClassConstraint()) ? gs[0] : null);
}
public static class CheckboxContainerDiv extends CenteredFlexDiv {
@Parent(CheckboxContainerDiv.class)
public static class BooleanHolderEditorInput extends GSCheckBoxEditor {
}
}
}
// Edition of links.
@Flex("1")
@FlexDirectionStyle(FlexDirection.ROW)
public static class LinkEditor extends GSDiv implements ComponentsDefaults {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() >= 2 ? gs[0] : null);
createComponentsListProperty();
}
public static class ComponentEditor extends GSComponentEditorDiv {
@Override
public void init() {
forEach((ObservableListExtractor) gs -> ObservableListExtractor.COMPONENTS.apply(gs).filtered(g -> !g.equals(gs[2])));
}
// TODO: Finish decomposition of InstanceCompositeSelect.
@Flex("1")
@Height("100%")
@Width("100%")
public static class DirectRelationComponentEditor extends InstanceCompositeSelect {
@Override
public void init() {
select(gs -> gs[1].isReferentialIntegrityEnabled(gs[1].getComponents().indexOf(gs[0])) ? gs[0] : null);
addPostfixBinding(model -> {
Property<List<Property<Context>>> selectedComponents = getComponentsProperty(model);
if (selectedComponents != null)
selectedComponents.getValue().add(getSelectionProperty(model));
});
}
}
public static class ReversedRelationDisplayer extends GSLabelDisplayer {
@Override
public void init() {
select(gs -> !gs[1].isReferentialIntegrityEnabled(gs[1].getComponents().indexOf(gs[0])) && !gs[0].getLinks(gs[2]).isEmpty() ? gs[0] : null);
}
}
}
}
// Hyperlink to remove a holder/link. Displayed only if there is no required constraint on the given attribute/relation,
// or there is a required constraint but there are at least two holders/links for the given attribute/relation.
public static class RemovalLink extends GSActionLink {
@Override
public void init() {
setText("×");
bindAction(Context::remove);
select__(context -> {
ObservableList<Generic> holders = ObservableListExtractor.HOLDERS.apply(context.getParent().getGenerics());
return BindingsTools.transmitSuccessiveInvalidations(
Bindings.createObjectBinding(() -> (!context.getParent().getGeneric().isRequiredConstraintEnabled(ApiStatics.BASE_POSITION) && holders.size() == 1) || holders.size() > 1 ? context : null, holders));
});
}
}
}
// To add a new holder/link if it’s possible.
public static class SubcellAdder extends SubcellEditorContainerDiv {
@Override
public void init() {
select__(model -> {
ObservableList<Generic> holders = ObservableListExtractor.HOLDERS.apply(model.getGenerics());
return Bindings
.createObjectBinding(
() -> holders.isEmpty() || (model.getGeneric().getComponents().size() < 2 && !model.getGeneric().isPropertyConstraintEnabled())
|| (model.getGeneric().getComponents().size() >= 2 && !model.getGeneric().isSingularConstraintEnabled(ApiStatics.BASE_POSITION)) ? model : null,
ObservableListExtractor.HOLDERS.apply(model.getGenerics()));
});
}
// Addition of non-boolean holders.
public static class HolderAdder extends GSSubcellEditorDiv {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() < 2 && !Boolean.class.equals(gs[0].getInstanceValueClassConstraint()) ? gs[0] : null);
}
@Flex("1")
@Height("100%")
@Width("100%")
public static class HolderAdderInput extends GSInputTextWithConversion {
@Override
public void init() {
addConvertedValueChangeListener((model, nva) -> {
if (nva != null)
model.getGenerics()[1].addHolder(model.getGeneric(), nva);
});
}
}
public static class HolderAdditionLink extends AdditionLink {
@Override
public void postfix() {
bindAction(context -> addHolder(context, find(HolderAdderInput.class)));
}
}
}
// Addition of boolean holders.
public static class BooleanHolderAdder extends GSSubcellEditorDiv {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() < 2 && Boolean.class.equals(gs[0].getInstanceValueClassConstraint()) ? gs[0] : null);
}
public static class CheckboxContainerAddDiv extends CenteredFlexDiv {
public static class BooleanHolderAdderInput extends GSCheckBoxWithValue {
@Override
public void init() {
addConvertedValueChangeListener((model, nva) -> {
if (nva != null)
model.getGenerics()[1].addHolder(model.getGeneric(), nva);
});
}
}
}
public static class BooleanHolderAdditionLink extends AdditionLink {
@Override
public void postfix() {
bindAction(context -> addHolder(context, find(BooleanHolderAdderInput.class)));
}
}
}
// Addition of links.
@Flex("1")
@FlexDirectionStyle(FlexDirection.ROW)
public static class LinkAdder extends GSDiv implements ComponentsDefaults {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() >= 2 ? gs[0] : null);
createComponentsListProperty();
addPostfixBinding(model -> {
Property<List<Property<Context>>> selectedComponents = getComponentsProperty(model);
ChangeListener<Context> listener = (o, v, nva) -> {
List<Generic> selectedGenerics = selectedComponents.getValue().stream().filter(obs -> obs.getValue() != null).map(obs -> obs.getValue().getGeneric()).filter(gen -> gen != null).collect(Collectors.toList());
if (selectedGenerics.size() + 1 == model.getGeneric().getComponents().size()) {
selectedComponents.getValue().stream().forEach(sel -> sel.setValue(null));
try {
model.getGenerics()[1].setHolder(model.getGeneric(), null, selectedGenerics.stream().toArray(Generic[]::new));
} catch (RollbackException e) {
e.printStackTrace();
}
}
};
selectedComponents.getValue().forEach(component -> component.addListener(listener));
});
}
public static class ComponentAdder extends GSComponentEditorDiv {
@Override
public void init() {
forEach((ObservableListExtractor) gs -> ObservableListExtractor.COMPONENTS.apply(gs).filtered(g -> !g.equals(gs[2])));
}
// TODO: Finish decomposition of CompositeSelectWithEmptyEntry.
@Parent(ComponentAdder.class)
@Flex("1")
@Height("100%")
@Width("100%")
public static class ComponentAdderSelect extends CompositeSelectWithEmptyEntry {
@Override
public void init() {
select(gs -> gs[1].isReferentialIntegrityEnabled(gs[1].getComponents().indexOf(gs[0])) ? gs[0] : null);
addPostfixBinding(model -> {
Property<List<Property<Context>>> selectedComponents = getComponentsProperty(model);
if (selectedComponents != null)
selectedComponents.getValue().add(getSelectionProperty(model));
});
}
}
}
}
}
}
// Hyperlink to create the holder. Displayed only for holders, not for links.
public static class AdditionLink extends GSActionLink {
@Override
public void init() {
setText("+");
}
protected void addHolder(Context context, ConvertedValueDefaults tag) {
Property<Serializable> observable = tag.getConvertedValueProperty(context);
if (observable.getValue() != null) {
Serializable newValue = observable.getValue();
observable.setValue(null);
context.getGenerics()[1].addHolder(context.getGeneric(), newValue);
}
}
}
}
}
}
}