-
Notifications
You must be signed in to change notification settings - Fork 5
/
GSEditor.java
452 lines (370 loc) · 19.3 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
package org.genericsystem.reactor.gs3;
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.annotations.ForEach;
import org.genericsystem.reactor.annotations.Parent;
import org.genericsystem.reactor.annotations.ReactorDependencies;
import org.genericsystem.reactor.annotations.Select;
import org.genericsystem.reactor.gs.GSCheckBoxWithValue;
import org.genericsystem.reactor.gs.GSCheckBoxWithValue.GSCheckBoxEditor;
import org.genericsystem.reactor.gs.GSDiv;
import org.genericsystem.reactor.gs.GSInputTextWithConversion;
import org.genericsystem.reactor.gs.GSInputTextWithConversion.GSInputTextEditorWithConversion;
import org.genericsystem.reactor.gs.GSSelect.CompositeSelectWithEmptyEntry;
import org.genericsystem.reactor.gs.GSSelect.InstanceCompositeSelect;
import org.genericsystem.reactor.gs3.FlexStyle.RowFlexStyle;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellAdder.BooleanHolderAdder.CheckboxContainerAddDiv.BooleanHolderAdderInput;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellAdder.BooleanHolderAdder.CheckboxContainerAddDiv.BooleanHolderAdditionLink;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellAdder.HolderAdder.HolderAdderInput;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellAdder.HolderAdder.HolderAdditionLink;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellAdder.LinkAdder.ComponentAdder.ComponentAdderSelect;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellEditor.BooleanHolderEditor.CheckboxContainerDiv.BooleanHolderEditorInput;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellEditor.HolderEditor.HolderEditorInput;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellEditor.LinkEditor.ComponentEditor.DirectRelationComponentEditor;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellEditor.LinkEditor.ComponentEditor.ReversedRelationDisplayer;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.AttributeEditionColumn.SubcellEditor.RemovalLink;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceAttributeEditor.MultiCheckbox.CheckboxLabel.Checkbox;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.InstanceEdition.InstanceNameEditorDiv.InstanceNameEditor;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.LinkTitles.InstanceType;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.LinkTitles.InstanceType.TypeNameDisplayer;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.LinkTitles.TypeAttribute.AttributeName.AttributeNameDisplayer;
import org.genericsystem.reactor.gs3.GSEditor.EditorContent.LinkTitles.TypeAttribute.RelationName.ComponentName.ComponentNameDisplayer;
import org.genericsystem.reactor.gs3.GSEditor.EditorTitle.EditorTitleContent;
import org.genericsystem.reactor.gstag.HtmlH2;
import org.genericsystem.reactor.gstag.HtmlHyperLink;
import org.genericsystem.reactor.gstag.HtmlLabel;
import org.genericsystem.reactor.gstag.HtmlLabel.GSLabelDisplayer;
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 org.genericsystem.reactor.modelproperties.ComponentsDefaults;
import org.genericsystem.reactor.modelproperties.ConvertedValueDefaults;
import org.genericsystem.reactor.modelproperties.SelectionDefaults;
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 })
public class GSEditor extends CompositeTagImpl implements RowFlexStyle {
public GSEditor() {
super();
}
public GSEditor(Tag parent) {
super(parent);
}
// No automatic enclosing class parent for this GSEditor extention
public static class HorizontalGSEditor extends GSEditor implements ColumnFlexStyle, SelectionDefaults {
public HorizontalGSEditor(Tag parent) {
super(parent);
}
@Override
public void style() {
ColumnFlexStyle.super.style();
}
}
@Parent(GSEditor.class)
// Main title.
public static class EditorTitle extends GSDiv implements FlexStyle.TitleStyle {
public static class EditorTitleContent extends HtmlH2 {
@Override
public void init() {
setStringExtractor(StringExtractor.TYPE_INSTANCE_EXTRACTOR);
bindText();
}
}
}
// Content.
@Parent(GSEditor.class)
public static class EditorContent extends GSDiv implements FlexStyle.EditorContentStyle {
// Line/column with the names of the attributes and components of relations.
public static class LinkTitles extends GSDiv implements FlexStyle.LinkTitlesStyle {
public static class InstanceType extends GSDiv implements FlexStyle.TitleLineCellStyle {
@Override
public void init() {
select(gs -> gs[1]);
}
public static class TypeNameDisplayer extends GSLabelDisplayer {
}
}
@ForEach(ATTRIBUTES_OF_INSTANCES.class)
public static class TypeAttribute extends GSDiv implements FlexStyle.RowFlexStyle {
@Select(STRICT_ATTRIBUTE_SELECTOR.class)
public static class AttributeName extends GSDiv implements FlexStyle.TitleLineCellStyle {
public static class AttributeNameDisplayer extends GSLabelDisplayer {
}
}
@Select(RELATION_SELECTOR.class)
public static class RelationName extends GSDiv implements FlexStyle.RowFlexStyle {
@ForEach(OTHER_COMPONENTS_2.class)
public static class ComponentName extends GSDiv implements FlexStyle.TitleLineCellStyle {
public static class ComponentNameDisplayer extends GSLabelDisplayer {
}
}
}
}
}
// Edition itself.
public static class InstanceEdition extends GSDiv implements FlexStyle.ReversedFlexStyle {
// Edition of the name of the instance.
public static class InstanceNameEditorDiv extends GSDiv implements FlexStyle.SubCellEditorStyle {
public static class InstanceNameEditor extends GSInputTextEditorWithConversion implements FullSizeStyle {
}
}
// Edition of the holders/links.
public static class InstanceAttributeEditor extends GSDiv implements FlexStyle.AttributeEditorStyle {
@Override
public void init() {
forEach(ObservableListExtractor.ATTRIBUTES_OF_INSTANCES);
}
// Multiple checkboxes : for binary relations without the singular constraint.
public static class MultiCheckbox extends GSDiv implements FlexStyle.MultiCheckboxStyle {
@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 CheckboxLabel extends HtmlLabel implements FlexStyle.CheckboxLabelStyle {
@Override
public void init() {
bindText();
forEach(gs -> ObservableListExtractor.SUBINSTANCES.apply(ObservableListExtractor.COMPONENTS.apply(gs).filtered(g -> !g.equals(gs[2])).stream().toArray(Generic[]::new)));
}
public static class Checkbox extends GSCheckBoxWithValue implements CheckboxStyle {
@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.
public static class AttributeEditionColumn extends GenericColumn implements FlexStyle.AttributeEditionColumnStyle {
@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 GSDiv implements FlexStyle.SubcellEditorContainerStyle {
@Override
public void init() {
forEach(ObservableListExtractor.HOLDERS);
}
// Edition of non-boolean holders.
public static class HolderEditor extends GSDiv implements FlexStyle.SubCellEditorStyle {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() < 2 && !Boolean.class.equals(gs[0].getInstanceValueClassConstraint()) ? gs[0] : null);
}
public static class HolderEditorInput extends GSInputTextEditorWithConversion implements FullSizeStyle {
}
}
// Edition of boolean holders.
public static class BooleanHolderEditor extends GSDiv implements FlexStyle.SubCellEditorStyle {
@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 GSDiv implements FlexStyle.CenteredFlex {
@Parent(CheckboxContainerDiv.class)
public static class BooleanHolderEditorInput extends GSCheckBoxEditor {
}
}
}
// Edition of links.
public static class LinkEditor extends GSDiv implements ComponentsDefaults, FlexStyle.RowFlexStyle {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() >= 2 ? gs[0] : null);
createComponentsListProperty();
}
public static class ComponentEditor extends GSDiv implements ComponentEditorStyle {
@Override
public void init() {
forEach((ObservableListExtractor) gs -> ObservableListExtractor.COMPONENTS.apply(gs).filtered(g -> !g.equals(gs[2])));
}
// TODO: Finish decomposition of InstanceCompositeSelect.
public static class DirectRelationComponentEditor extends InstanceCompositeSelect implements FullSizeStyle {
@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 HtmlHyperLink implements ActionLinkStyle {
@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 GSDiv implements FlexStyle.SubcellEditorContainerStyle {
@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 GSDiv implements FlexStyle.SubCellEditorStyle {
@Override
public void init() {
select(gs -> gs[0].getComponents().size() < 2 && !Boolean.class.equals(gs[0].getInstanceValueClassConstraint()) ? gs[0] : null);
}
public static class HolderAdderInput extends GSInputTextWithConversion implements FullSizeStyle {
@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, (ConvertedValueDefaults) find(HolderAdderInput.class)));
}
}
}
// Addition of boolean holders.
public static class BooleanHolderAdder extends GSDiv implements FlexStyle.SubCellEditorStyle {
@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 GSDiv implements FlexStyle.CenteredFlex {
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, (ConvertedValueDefaults) find(BooleanHolderAdderInput.class)));
}
}
}
}
// Addition of links.
public static class LinkAdder extends GSDiv implements ComponentsDefaults, FlexStyle.RowFlexStyle {
@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 GSDiv implements ComponentEditorStyle {
@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)
public static class ComponentAdderSelect extends CompositeSelectWithEmptyEntry implements FullSizeStyle {
@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 HtmlHyperLink implements ActionLinkStyle {
@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);
}
}
}
}
}
}
}