-
Notifications
You must be signed in to change notification settings - Fork 45
/
FormDescriptionEditorGroupComponent.java
112 lines (97 loc) · 5.88 KB
/
FormDescriptionEditorGroupComponent.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
/*******************************************************************************
* Copyright (c) 2022, 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.formdescriptioneditors.components;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.emf.ecore.util.ComposedSwitch;
import org.eclipse.emf.ecore.util.Switch;
import org.eclipse.sirius.components.forms.GroupDisplayMode;
import org.eclipse.sirius.components.forms.components.ToolbarActionComponent;
import org.eclipse.sirius.components.forms.components.ToolbarActionComponentProps;
import org.eclipse.sirius.components.forms.components.WidgetComponent;
import org.eclipse.sirius.components.forms.components.WidgetComponentProps;
import org.eclipse.sirius.components.forms.description.AbstractWidgetDescription;
import org.eclipse.sirius.components.forms.description.ButtonDescription;
import org.eclipse.sirius.components.forms.elements.GroupElementProps;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.IComponent;
import org.eclipse.sirius.components.representations.VariableManager;
import org.eclipse.sirius.components.view.form.GroupDescription;
/**
* The component used to render the form description editor group.
*
* @author arichard
*/
public class FormDescriptionEditorGroupComponent implements IComponent {
private static final String AQL_PREFIX = "aql:";
private final FormDescriptionEditorGroupComponentProps props;
private final Switch<AbstractWidgetDescription> converter;
public FormDescriptionEditorGroupComponent(FormDescriptionEditorGroupComponentProps props) {
this.props = props;
List<Switch<AbstractWidgetDescription>> widgetConverters = this.props.getCustomWidgetConverterProviders().stream()
.map(provider -> provider.getWidgetConverter(props.getFormDescriptionEditorDescription(), props.getVariableManager()))
.toList();
Collection<Switch<AbstractWidgetDescription>> switches = new ArrayList<>();
switches.add(new ViewFormDescriptionEditorConverterSwitch(props.getFormDescriptionEditorDescription(), props.getVariableManager(), new ComposedSwitch<>(widgetConverters)));
switches.addAll(widgetConverters);
this.converter = new ComposedSwitch<>(switches);
}
@Override
public Element render() {
VariableManager variableManager = this.props.getVariableManager();
var groupDescription = variableManager.get(VariableManager.SELF, GroupDescription.class).get();
String id = this.props.getFormDescriptionEditorDescription().getTargetObjectIdProvider().apply(variableManager);
String label = this.getGroupLabel(groupDescription, "Group");
List<Element> childrenWidgets = new ArrayList<>();
groupDescription.getToolbarActions().forEach(viewToolbarActionDescription -> {
VariableManager childVariableManager = variableManager.createChild();
childVariableManager.put(VariableManager.SELF, viewToolbarActionDescription);
AbstractWidgetDescription toolbarActionDescription = this.converter.doSwitch(viewToolbarActionDescription);
if (toolbarActionDescription instanceof ButtonDescription) {
ToolbarActionComponentProps toolbarActionComponentProps = new ToolbarActionComponentProps(childVariableManager, (ButtonDescription) toolbarActionDescription);
childrenWidgets.add(new Element(ToolbarActionComponent.class, toolbarActionComponentProps));
}
});
groupDescription.getWidgets().forEach(viewWidgetDescription -> {
VariableManager childVariableManager = variableManager.createChild();
childVariableManager.put(VariableManager.SELF, viewWidgetDescription);
AbstractWidgetDescription widgetDescription = this.converter.doSwitch(viewWidgetDescription);
WidgetComponentProps widgetComponentProps = new WidgetComponentProps(childVariableManager, widgetDescription, this.props.getWidgetDescriptors());
childrenWidgets.add(new Element(WidgetComponent.class, widgetComponentProps));
});
GroupElementProps.Builder groupElementPropsBuilder = GroupElementProps.newGroupElementProps(id)
.label(label)
.displayMode(this.getGroupDisplayMode(groupDescription))
.children(childrenWidgets);
if (groupDescription.getBorderStyle() != null) {
groupElementPropsBuilder.borderStyle(new ContainerBorderStyleProvider(groupDescription.getBorderStyle()).build());
}
return new Element(GroupElementProps.TYPE, groupElementPropsBuilder.build());
}
public String getGroupLabel(org.eclipse.sirius.components.view.form.GroupDescription groupDescription, String defaultLabel) {
String widgetLabel = defaultLabel;
String name = groupDescription.getName();
String labelExpression = groupDescription.getLabelExpression();
if (labelExpression != null && !labelExpression.isBlank() && !labelExpression.startsWith(AQL_PREFIX)) {
widgetLabel = labelExpression;
} else if (name != null && !name.isBlank()) {
widgetLabel = name;
}
return widgetLabel;
}
private GroupDisplayMode getGroupDisplayMode(GroupDescription viewGroupDescription) {
org.eclipse.sirius.components.view.form.GroupDisplayMode viewDisplayMode = viewGroupDescription.getDisplayMode();
return GroupDisplayMode.valueOf(viewDisplayMode.getLiteral());
}
}