Skip to content

Commit

Permalink
[285] Use Spring dependency injection in the compatibility layer
Browse files Browse the repository at this point in the history
Bug: #285
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
  • Loading branch information
sbegaudeau committed Apr 21, 2021
1 parent d4f1d3d commit a30afb8
Show file tree
Hide file tree
Showing 22 changed files with 272 additions and 603 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import org.eclipse.sirius.diagram.description.NodeMapping;
import org.eclipse.sirius.diagram.description.style.NodeStyleDescription;
import org.eclipse.sirius.diagram.description.AbstractNodeMapping;
import org.eclipse.sirius.diagram.description.ContainerMapping;
import org.eclipse.sirius.diagram.description.style.WorkspaceImageDescription;
import org.eclipse.sirius.viewpoint.description.style.BasicLabelStyleDescription;
import org.eclipse.sirius.viewpoint.description.style.StyleFactory;
Expand All @@ -39,52 +39,51 @@
import org.eclipse.sirius.web.interpreter.AQLInterpreter;
import org.eclipse.sirius.web.representations.VariableManager;
import org.eclipse.sirius.web.services.api.objects.IEditService;
import org.springframework.stereotype.Service;

/**
* This class is used to convert a Sirius NodeMapping to an Sirius Web NodeDescription.
* This class is used to convert a Sirius AbstractNodeMapping to an Sirius Web NodeDescription.
*
* @author sbegaudeau
*/
public class NodeMappingConverter {
@Service
public class AbstractNodeMappingConverter {

private final IObjectService objectService;

private final IEditService editService;

private final AQLInterpreter interpreter;

private final IIdentifierProvider identifierProvider;

private final ISemanticCandidatesProviderFactory semanticCandidatesProviderFactory;

private final IModelOperationHandlerSwitchProvider modelOperationHandlerSwitchProvider;

private final LabelStyleDescriptionConverter labelStyleDescriptionConverter;

public NodeMappingConverter(IObjectService objectService, IEditService editService, AQLInterpreter interpreter, IIdentifierProvider identifierProvider,
public AbstractNodeMappingConverter(IObjectService objectService, IEditService editService, IIdentifierProvider identifierProvider,
ISemanticCandidatesProviderFactory semanticCandidatesProviderFactory, IModelOperationHandlerSwitchProvider modelOperationHandlerSwitchProvider) {
this.objectService = Objects.requireNonNull(objectService);
this.editService = Objects.requireNonNull(editService);
this.interpreter = Objects.requireNonNull(interpreter);
this.identifierProvider = Objects.requireNonNull(identifierProvider);
this.semanticCandidatesProviderFactory = Objects.requireNonNull(semanticCandidatesProviderFactory);
this.modelOperationHandlerSwitchProvider = Objects.requireNonNull(modelOperationHandlerSwitchProvider);
this.labelStyleDescriptionConverter = new LabelStyleDescriptionConverter(this.interpreter, this.objectService);
}

public NodeDescription convert(NodeMapping nodeMapping, Map<UUID, NodeDescription> id2NodeDescriptions) {
NodeStyleDescriptionProvider nodeStyleDescriptionProvider = new NodeStyleDescriptionProvider(this.interpreter, nodeMapping);
public NodeDescription convert(AbstractNodeMapping abstractNodeMapping, AQLInterpreter interpreter, Map<UUID, NodeDescription> id2NodeDescriptions) {
LabelStyleDescriptionConverter labelStyleDescriptionConverter = new LabelStyleDescriptionConverter(interpreter, this.objectService);

Function<VariableManager, org.eclipse.sirius.viewpoint.description.style.LabelStyleDescription> abstractNodeMappingDescriptionProvider = new LabelStyleDescriptionProvider(interpreter,
abstractNodeMapping);

Function<VariableManager, LabelStyleDescription> labelStyleDescriptionProvider = variableManager -> {
NodeStyleDescription styleDescription = nodeStyleDescriptionProvider.getNodeStyleDescription(variableManager);
org.eclipse.sirius.viewpoint.description.style.LabelStyleDescription styleDescription = abstractNodeMappingDescriptionProvider.apply(variableManager);
BasicLabelStyleDescription basicLabelStyleDescription = Optional.ofNullable(styleDescription).map(BasicLabelStyleDescription.class::cast).orElse(this.getDefaultLabelStyle());
return this.labelStyleDescriptionConverter.convert(basicLabelStyleDescription);
return labelStyleDescriptionConverter.convert(basicLabelStyleDescription);
};

Function<VariableManager, String> labelExpressionProvider = variableManager -> {
NodeStyleDescription styleDescription = nodeStyleDescriptionProvider.getNodeStyleDescription(variableManager);
String labelExpression = Optional.ofNullable(styleDescription).map(NodeStyleDescription::getLabelExpression).orElse(""); //$NON-NLS-1$
return new StringValueProvider(this.interpreter, labelExpression).apply(variableManager);
org.eclipse.sirius.viewpoint.description.style.LabelStyleDescription styleDescription = abstractNodeMappingDescriptionProvider.apply(variableManager);
String labelExpression = Optional.ofNullable(styleDescription).map(BasicLabelStyleDescription::getLabelExpression).orElse(""); //$NON-NLS-1$
return new StringValueProvider(interpreter, labelExpression).apply(variableManager);
};

Function<VariableManager, String> labelIdProvider = variableManager -> {
Expand All @@ -93,7 +92,7 @@ public NodeDescription convert(NodeMapping nodeMapping, Map<UUID, NodeDescriptio
};

// @formatter:off
LabelDescription labelDescription = LabelDescription.newLabelDescription(this.identifierProvider.getIdentifier(nodeMapping) + LabelDescription.LABEL_SUFFIX)
LabelDescription labelDescription = LabelDescription.newLabelDescription(this.identifierProvider.getIdentifier(abstractNodeMapping) + LabelDescription.LABEL_SUFFIX)
.idProvider(labelIdProvider)
.textProvider(labelExpressionProvider)
.styleDescriptionProvider(labelStyleDescriptionProvider)
Expand All @@ -110,45 +109,46 @@ public NodeDescription convert(NodeMapping nodeMapping, Map<UUID, NodeDescriptio
return variableManager.get(VariableManager.SELF, Object.class).map(this.objectService::getLabel).orElse(null);
};
Function<VariableManager, String> typeProvider = variableManager -> {
NodeStyleDescription nodeStyle = nodeStyleDescriptionProvider.getNodeStyleDescription(variableManager);
if (nodeStyle instanceof WorkspaceImageDescription) {
org.eclipse.sirius.viewpoint.description.style.LabelStyleDescription style = abstractNodeMappingDescriptionProvider.apply(variableManager);
if (style instanceof WorkspaceImageDescription) {
return NodeType.NODE_IMAGE;
}
return NodeType.NODE_RECTANGLE;
};

Function<VariableManager, INodeStyle> styleProvider = new NodeMappingStyleProvider(this.interpreter, nodeMapping);
Function<VariableManager, INodeStyle> styleProvider = new AbstractNodeMappingStyleProvider(interpreter, abstractNodeMapping);
AbstractNodeMappingSizeProvider sizeProvider = new AbstractNodeMappingSizeProvider(interpreter, abstractNodeMapping);

NodeMappingSizeProvider nodeMappingSizeProvider = new NodeMappingSizeProvider(this.interpreter, nodeMapping);
String domainClass = abstractNodeMapping.getDomainClass();
String semanticCandidatesExpression = abstractNodeMapping.getSemanticCandidatesExpression();
String preconditionExpression = abstractNodeMapping.getPreconditionExpression();
Function<VariableManager, List<Object>> semanticElementsProvider = this.semanticCandidatesProviderFactory.getSemanticCandidatesProvider(interpreter, domainClass, semanticCandidatesExpression,
preconditionExpression);

String domainClass = nodeMapping.getDomainClass();
String semanticCandidatesExpression = nodeMapping.getSemanticCandidatesExpression();
String preconditionExpression = nodeMapping.getPreconditionExpression();
Function<VariableManager, List<Object>> semanticElementsProvider = this.semanticCandidatesProviderFactory.getSemanticCandidatesProvider(this.interpreter, domainClass,
semanticCandidatesExpression, preconditionExpression);
List<NodeDescription> childNodeDescriptions = this.getChildNodeDescriptions(abstractNodeMapping, interpreter, id2NodeDescriptions);

// @formatter:off
List<NodeDescription> borderNodeDescriptions = nodeMapping.getBorderedNodeMappings().stream()
.map(borderNodeMapping -> new NodeMappingConverter(this.objectService, this.editService, this.interpreter, this.identifierProvider, this.semanticCandidatesProviderFactory, this.modelOperationHandlerSwitchProvider).convert(borderNodeMapping, id2NodeDescriptions))
List<NodeDescription> borderNodeDescriptions = abstractNodeMapping.getBorderedNodeMappings().stream()
.map(borderNodeMapping -> this.convert(borderNodeMapping, interpreter, id2NodeDescriptions))
.collect(Collectors.toList());
// @formatter:on

ToolConverter toolConverter = new ToolConverter(this.interpreter, this.editService, this.modelOperationHandlerSwitchProvider);
var deleteHandler = toolConverter.createDeleteToolHandler(nodeMapping.getDeletionDescription());
var labelEditHandler = toolConverter.createDirectEditToolHandler(nodeMapping.getLabelDirectEdit());
ToolConverter toolConverter = new ToolConverter(interpreter, this.editService, this.modelOperationHandlerSwitchProvider);
var deleteHandler = toolConverter.createDeleteToolHandler(abstractNodeMapping.getDeletionDescription());
var labelEditHandler = toolConverter.createDirectEditToolHandler(abstractNodeMapping.getLabelDirectEdit());

// @formatter:off
NodeDescription description = NodeDescription.newNodeDescription(UUID.fromString(this.identifierProvider.getIdentifier(nodeMapping)))
NodeDescription description = NodeDescription.newNodeDescription(UUID.fromString(this.identifierProvider.getIdentifier(abstractNodeMapping)))
.typeProvider(typeProvider)
.targetObjectIdProvider(semanticTargetIdProvider)
.targetObjectKindProvider(semanticTargetKindProvider)
.targetObjectLabelProvider(semanticTargetLabelProvider)
.semanticElementsProvider(semanticElementsProvider)
.labelDescription(labelDescription)
.styleProvider(styleProvider)
.sizeProvider(nodeMappingSizeProvider)
.sizeProvider(sizeProvider)
.borderNodeDescriptions(borderNodeDescriptions)
.childNodeDescriptions(new ArrayList<>())
.childNodeDescriptions(childNodeDescriptions)
.labelEditHandler(labelEditHandler)
.deleteHandler(deleteHandler)
.build();
Expand All @@ -159,6 +159,29 @@ public NodeDescription convert(NodeMapping nodeMapping, Map<UUID, NodeDescriptio
return description;
}

private List<NodeDescription> getChildNodeDescriptions(AbstractNodeMapping abstractNodeMapping, AQLInterpreter interpreter, Map<UUID, NodeDescription> id2NodeDescriptions) {
// @formatter:off
List<NodeDescription> childNodeDescriptions = new ArrayList<>();

if (abstractNodeMapping instanceof ContainerMapping) {
ContainerMapping containerMapping = (ContainerMapping) abstractNodeMapping;
List<NodeDescription> childNodeMappingDescriptions = containerMapping.getSubNodeMappings().stream()
.map(childNodeMapping -> this.convert(childNodeMapping, interpreter, id2NodeDescriptions))
.collect(Collectors.toList());

List<NodeDescription> childContainerMappingDescriptions = containerMapping.getSubContainerMappings().stream()
.map(childContainerMapping -> this.convert(childContainerMapping, interpreter, id2NodeDescriptions))
.collect(Collectors.toList());

childNodeDescriptions.addAll(childNodeMappingDescriptions);
childNodeDescriptions.addAll(childContainerMappingDescriptions);
}
// @formatter:on

return childNodeDescriptions;

}

private BasicLabelStyleDescription getDefaultLabelStyle() {
var labelStyle = StyleFactory.eINSTANCE.createBasicLabelStyleDescription();
labelStyle.setLabelExpression(""); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
import java.util.Objects;
import java.util.function.Function;

import org.eclipse.sirius.diagram.description.NodeMapping;
import org.eclipse.sirius.diagram.description.style.NodeStyleDescription;
import org.eclipse.sirius.diagram.description.AbstractNodeMapping;
import org.eclipse.sirius.diagram.description.style.FlatContainerStyleDescription;
import org.eclipse.sirius.diagram.description.style.SquareDescription;
import org.eclipse.sirius.viewpoint.description.style.LabelStyleDescription;
import org.eclipse.sirius.web.diagrams.Size;
import org.eclipse.sirius.web.interpreter.AQLInterpreter;
import org.eclipse.sirius.web.interpreter.Result;
Expand All @@ -28,33 +29,30 @@
*
* @author fbarbin
*/
public class NodeMappingSizeProvider implements Function<VariableManager, Size> {
public class AbstractNodeMappingSizeProvider implements Function<VariableManager, Size> {
/**
* Inherit from Sirius Desktop. Currently, the size specified in the VSM is multiplied by 10.
*/
private static final int SIZE_FACTOR = 10;

private final AQLInterpreter interpreter;

private final NodeMapping nodeMapping;
private final AbstractNodeMapping abstractNodeMapping;

public NodeMappingSizeProvider(AQLInterpreter interpreter, NodeMapping nodeMapping) {
public AbstractNodeMappingSizeProvider(AQLInterpreter interpreter, AbstractNodeMapping abstractNodeMapping) {
this.interpreter = Objects.requireNonNull(interpreter);
this.nodeMapping = Objects.requireNonNull(nodeMapping);
this.abstractNodeMapping = Objects.requireNonNull(abstractNodeMapping);
}

@Override
public Size apply(VariableManager variableManager) {
NodeStyleDescription nodeStyleDescription = new NodeStyleDescriptionProvider(this.interpreter, this.nodeMapping).getNodeStyleDescription(variableManager);
return this.getNodeSize(variableManager, nodeStyleDescription);
}
LabelStyleDescription labelStyleDescription = new LabelStyleDescriptionProvider(this.interpreter, this.abstractNodeMapping).apply(variableManager);

private Size getNodeSize(VariableManager variableManager, NodeStyleDescription nodeStyleDescription) {
Size size = Size.of(0, 0);
if (nodeStyleDescription instanceof SquareDescription) {
SquareDescription squareDescription = (SquareDescription) nodeStyleDescription;
Integer width = squareDescription.getWidth() * SIZE_FACTOR;
Integer height = squareDescription.getHeight() * SIZE_FACTOR;
Size size = Size.UNDEFINED;
if (labelStyleDescription instanceof SquareDescription) {
SquareDescription squareDescription = (SquareDescription) labelStyleDescription;
int width = squareDescription.getWidth() * SIZE_FACTOR;
int height = squareDescription.getHeight() * SIZE_FACTOR;

// If the initial width and/or height have not been set by the specifier, we interpret the size computation
// expression to set the width and/or height
Expand All @@ -71,7 +69,23 @@ private Size getNodeSize(VariableManager variableManager, NodeStyleDescription n
}
}
size = Size.of(width, height);
} else if (labelStyleDescription instanceof FlatContainerStyleDescription) {
int width = -1;
int height = -1;
FlatContainerStyleDescription flatContainerStyleDescription = (FlatContainerStyleDescription) labelStyleDescription;
Result result = this.interpreter.evaluateExpression(variableManager.getVariables(), flatContainerStyleDescription.getWidthComputationExpression());
int computedWidth = result.asInt().getAsInt();
if (computedWidth > 0) {
width = computedWidth * SIZE_FACTOR;
}
result = this.interpreter.evaluateExpression(variableManager.getVariables(), flatContainerStyleDescription.getHeightComputationExpression());
int computedHeight = result.asInt().getAsInt();
if (computedHeight > 0) {
height = computedHeight * SIZE_FACTOR;
}
size = Size.of(width, height);
}
return size;
}

}
Loading

0 comments on commit a30afb8

Please sign in to comment.