Skip to content

Commit

Permalink
[315] Fix conditional style handling for containers and nodes
Browse files Browse the repository at this point in the history
In Sirius-Web representations displayed to users, the label of elements
with mapping 'MyMapping' should be the one of the conditional style.
Some other properties (bold, italic, ...) of the conditional style were
not also taking into account.

Bug: #315
Signed-off-by: Axel RICHARD <axel.richard@obeo.fr>
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
  • Loading branch information
AxelRICHARD authored and sbegaudeau committed Feb 19, 2021
1 parent e7b2a9f commit a574cc6
Show file tree
Hide file tree
Showing 20 changed files with 544 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.eclipse.sirius.diagram.description.ContainerMapping;
import org.eclipse.sirius.diagram.description.style.ContainerStyleDescription;
import org.eclipse.sirius.diagram.description.style.WorkspaceImageDescription;
import org.eclipse.sirius.viewpoint.description.style.BasicLabelStyleDescription;
import org.eclipse.sirius.viewpoint.description.style.StyleFactory;
import org.eclipse.sirius.web.compat.api.IIdentifierProvider;
import org.eclipse.sirius.web.compat.api.IModelOperationHandlerSwitchProvider;
import org.eclipse.sirius.web.compat.api.ISemanticCandidatesProviderFactory;
Expand Down Expand Up @@ -72,34 +73,27 @@ public ContainerMappingConverter(IObjectService objectService, IEditService edit
}

public NodeDescription convert(ContainerMapping containerMapping, Map<UUID, NodeDescription> id2NodeDescriptions) {
// @formatter:off
String labelExpression = Optional.ofNullable(containerMapping.getStyle()).map(ContainerStyleDescription::getLabelExpression).orElse(""); //$NON-NLS-1$
Supplier<LabelStyleDescription> defaultLabelStyleDescription = () -> LabelStyleDescription.newLabelStyleDescription()
.colorProvider(variableManager -> "#000000") //$NON-NLS-1$
.fontSizeProvider(variableManager -> 16)
.italicProvider(variableManager -> false)
.boldProvider(variableManager -> false)
.underlineProvider(variableManager -> false)
.strikeThroughProvider(variableManager -> false)
.iconURLProvider(variableManager -> "") //$NON-NLS-1$
.build();
ContainerStyleDescriptionProvider containerStyleDescriptionProvider = new ContainerStyleDescriptionProvider(this.interpreter, containerMapping);

LabelStyleDescription labelStyleDescription = Optional.ofNullable(containerMapping.getStyle())
.map(this.labelStyleDescriptionConverter::convert)
.orElseGet(defaultLabelStyleDescription);
Function<VariableManager, LabelStyleDescription> labelStyleDescriptionProvider = variableManager -> {
ContainerStyleDescription styleDescription = containerStyleDescriptionProvider.getContainerStyleDescription(variableManager);
BasicLabelStyleDescription basicLabelStyleDescription = Optional.ofNullable(styleDescription).map(BasicLabelStyleDescription.class::cast).orElse(this.getDefaultLabelStyle());
return this.labelStyleDescriptionConverter.convert(basicLabelStyleDescription);
};

Function<VariableManager, String> labelExpressionProvider = variableManager -> {
ContainerStyleDescription styleDescription = containerStyleDescriptionProvider.getContainerStyleDescription(variableManager);
String labelExpression = Optional.ofNullable(styleDescription).map(ContainerStyleDescription::getLabelExpression).orElse(""); //$NON-NLS-1$
return new StringValueProvider(this.interpreter, labelExpression).apply(variableManager);
};

Function<VariableManager, String> labelIdProvider = variableManager -> {
Object parentId = variableManager.getVariables().get(LabelDescription.OWNER_ID);
return String.valueOf(parentId) + LabelDescription.LABEL_SUFFIX;
};
// @formatter:on

// @formatter:off
LabelDescription labelDescription = LabelDescription.newLabelDescription(this.identifierProvider.getIdentifier(containerMapping) + LabelDescription.LABEL_SUFFIX)
.idProvider(labelIdProvider)
.textProvider(new StringValueProvider(this.interpreter, labelExpression))
.styleDescription(labelStyleDescription)
.build();
LabelDescription labelDescription = LabelDescription.newLabelDescription(this.identifierProvider.getIdentifier(containerMapping) + LabelDescription.LABEL_SUFFIX).idProvider(labelIdProvider)
.textProvider(labelExpressionProvider).styleDescriptionProvider(labelStyleDescriptionProvider).build();
// @formatter:on

Function<VariableManager, String> semanticTargetIdProvider = variableManager -> {
Expand All @@ -112,7 +106,8 @@ public NodeDescription convert(ContainerMapping containerMapping, Map<UUID, Node
return variableManager.get(VariableManager.SELF, Object.class).map(this.objectService::getLabel).orElse(null);
};
Function<VariableManager, String> typeProvider = variableManager -> {
if (containerMapping.getStyle() instanceof WorkspaceImageDescription) {
ContainerStyleDescription containerStyle = containerStyleDescriptionProvider.getContainerStyleDescription(variableManager);
if (containerStyle instanceof WorkspaceImageDescription) {
return NodeType.NODE_IMAGE;
}
return NodeType.NODE_RECTANGLE;
Expand Down Expand Up @@ -170,4 +165,11 @@ public NodeDescription convert(ContainerMapping containerMapping, Map<UUID, Node
return description;
}

private BasicLabelStyleDescription getDefaultLabelStyle() {
var labelStyle = StyleFactory.eINSTANCE.createBasicLabelStyleDescription();
labelStyle.setLabelExpression(""); //$NON-NLS-1$
labelStyle.setShowIcon(true);
labelStyle.setLabelSize(16);
return labelStyle;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2020 Obeo.
* Copyright (c) 2019, 2021 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
Expand All @@ -12,11 +12,9 @@
*******************************************************************************/
package org.eclipse.sirius.web.compat.diagrams;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;

import org.eclipse.sirius.diagram.description.ConditionalContainerStyleDescription;
import org.eclipse.sirius.diagram.description.ContainerMapping;
import org.eclipse.sirius.diagram.description.style.ContainerStyleDescription;
import org.eclipse.sirius.diagram.description.style.FlatContainerStyleDescription;
Expand Down Expand Up @@ -46,24 +44,8 @@ public ContainerMappingStyleProvider(AQLInterpreter interpreter, ContainerMappin

@Override
public INodeStyle apply(VariableManager variableManager) {
INodeStyle style = null;

List<ConditionalContainerStyleDescription> conditionnalStyles = this.containerMapping.getConditionnalStyles();
for (ConditionalContainerStyleDescription conditionalStyle : conditionnalStyles) {
String predicateExpression = conditionalStyle.getPredicateExpression();
Result result = this.interpreter.evaluateExpression(variableManager.getVariables(), predicateExpression);
boolean shouldUseStyle = result.asBoolean().orElse(Boolean.FALSE).booleanValue();
if (shouldUseStyle) {
style = this.getNodeStyle(variableManager, conditionalStyle.getStyle());
break;
}
}

if (style == null) {
style = this.getNodeStyle(variableManager, this.containerMapping.getStyle());
}

return style;
ContainerStyleDescription containerStyleDescription = new ContainerStyleDescriptionProvider(this.interpreter, this.containerMapping).getContainerStyleDescription(variableManager);
return this.getNodeStyle(variableManager, containerStyleDescription);
}

private INodeStyle getNodeStyle(VariableManager variableManager, ContainerStyleDescription containerStyleDescription) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2021 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.web.compat.diagrams;

import java.util.List;
import java.util.Objects;

import org.eclipse.sirius.diagram.description.ConditionalContainerStyleDescription;
import org.eclipse.sirius.diagram.description.ContainerMapping;
import org.eclipse.sirius.diagram.description.style.ContainerStyleDescription;
import org.eclipse.sirius.web.interpreter.AQLInterpreter;
import org.eclipse.sirius.web.interpreter.Result;
import org.eclipse.sirius.web.representations.VariableManager;

/**
* Compute the proper style description to use for a container mapping.
*
* @author sbegaudeau
*/
public class ContainerStyleDescriptionProvider {
private final AQLInterpreter interpreter;

private final ContainerMapping containerMapping;

public ContainerStyleDescriptionProvider(AQLInterpreter interpreter, ContainerMapping containerMapping) {
this.interpreter = Objects.requireNonNull(interpreter);
this.containerMapping = Objects.requireNonNull(containerMapping);
}

public ContainerStyleDescription getContainerStyleDescription(VariableManager variableManager) {
ContainerStyleDescription styleDescription = this.containerMapping.getStyle();
List<ConditionalContainerStyleDescription> conditionnalStyles = this.containerMapping.getConditionnalStyles();
for (ConditionalContainerStyleDescription conditionalStyle : conditionnalStyles) {
String predicateExpression = conditionalStyle.getPredicateExpression();
Result result = this.interpreter.evaluateExpression(variableManager.getVariables(), predicateExpression);
boolean shouldUseStyle = result.asBoolean().orElse(Boolean.FALSE).booleanValue();
if (shouldUseStyle) {
styleDescription = conditionalStyle.getStyle();
break;
}
}
return styleDescription;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ public EdgeDescription convert(EdgeMapping edgeMapping) {
LabelStyleDescriptionConverter labelStyleDescriptionConverter = new LabelStyleDescriptionConverter(this.interpreter, this.objectService);

// @formatter:off
Optional<LabelDescription> optionalBeginLabelDescription = Optional.ofNullable(edgeMapping.getStyle())
EdgeStyleDescription style = edgeMapping.getStyle();

Optional<LabelDescription> optionalBeginLabelDescription = Optional.ofNullable(style)
.map(EdgeStyleDescription::getBeginLabelStyleDescription)
.map(labelDescription -> this.createLabelDescription(labelStyleDescriptionConverter, labelDescription, "_beginlabel", edgeMapping)); //$NON-NLS-1$

Optional<LabelDescription> optionalCenterLabelDescription = Optional.ofNullable(edgeMapping.getStyle())
Optional<LabelDescription> optionalCenterLabelDescription = Optional.ofNullable(style)
.map(EdgeStyleDescription::getCenterLabelStyleDescription)
.map(labelDescription -> this.createLabelDescription(labelStyleDescriptionConverter, labelDescription, "_centerlabel", edgeMapping)); //$NON-NLS-1$

Optional<LabelDescription> optionalEndLabelDescription = Optional.ofNullable(edgeMapping.getStyle())
Optional<LabelDescription> optionalEndLabelDescription = Optional.ofNullable(style)
.map(EdgeStyleDescription::getEndLabelStyleDescription)
.map(labelDescription -> this.createLabelDescription(labelStyleDescriptionConverter, labelDescription, "_endlabel", edgeMapping)); //$NON-NLS-1$

Expand Down Expand Up @@ -139,7 +141,10 @@ public EdgeDescription convert(EdgeMapping edgeMapping) {
private LabelDescription createLabelDescription(LabelStyleDescriptionConverter labelStyleDescriptionConverter, BasicLabelStyleDescription siriusBasicLabelStyleDescription, String idSuffix,
EdgeMapping edgeMapping) {
String labelExpression = siriusBasicLabelStyleDescription.getLabelExpression();
LabelStyleDescription labelStyleDescription = labelStyleDescriptionConverter.convert(siriusBasicLabelStyleDescription);

Function<VariableManager, LabelStyleDescription> labelStyleDescriptionProvider = variableManager -> {
return labelStyleDescriptionConverter.convert(siriusBasicLabelStyleDescription);
};

Function<VariableManager, String> labelIdProvider = variableManager -> {
Object parentId = variableManager.getVariables().get(LabelDescription.OWNER_ID);
Expand All @@ -152,7 +157,7 @@ private LabelDescription createLabelDescription(LabelStyleDescriptionConverter l
return LabelDescription.newLabelDescription(id)
.idProvider(labelIdProvider)
.textProvider(textProvider)
.styleDescription(labelStyleDescription)
.styleDescriptionProvider(labelStyleDescriptionProvider)
.build();
// @formatter:on

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2020 Obeo.
* Copyright (c) 2019, 2021 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
Expand All @@ -15,14 +15,12 @@
import java.util.Objects;
import java.util.function.Function;

import org.eclipse.sirius.diagram.description.ConditionalEdgeStyleDescription;
import org.eclipse.sirius.diagram.description.EdgeMapping;
import org.eclipse.sirius.diagram.description.style.EdgeStyleDescription;
import org.eclipse.sirius.web.diagrams.ArrowStyle;
import org.eclipse.sirius.web.diagrams.EdgeStyle;
import org.eclipse.sirius.web.diagrams.LineStyle;
import org.eclipse.sirius.web.interpreter.AQLInterpreter;
import org.eclipse.sirius.web.interpreter.Result;
import org.eclipse.sirius.web.representations.VariableManager;

/**
Expand All @@ -43,23 +41,8 @@ public EdgeMappingStyleProvider(AQLInterpreter interpreter, EdgeMapping edgeMapp

@Override
public EdgeStyle apply(VariableManager variableManager) {
EdgeStyle edgeStyle = null;

var conditionnalStyles = this.edgeMapping.getConditionnalStyles();
for (ConditionalEdgeStyleDescription conditionalStyle : conditionnalStyles) {
String predicateExpression = conditionalStyle.getPredicateExpression();
Result result = this.interpreter.evaluateExpression(variableManager.getVariables(), predicateExpression);
boolean shouldUseStyle = result.asBoolean().orElse(Boolean.FALSE).booleanValue();
if (shouldUseStyle) {
edgeStyle = this.getEdgeStyle(variableManager, conditionalStyle.getStyle());
break;
}
}

if (edgeStyle == null) {
edgeStyle = this.getEdgeStyle(variableManager, this.edgeMapping.getStyle());
}
return edgeStyle;
EdgeStyleDescription edgeStyleDescription = new EdgeStyleDescriptionProvider(this.interpreter, this.edgeMapping).getEdgeStyleDescription(variableManager);
return this.getEdgeStyle(variableManager, edgeStyleDescription);
}

private EdgeStyle getEdgeStyle(VariableManager variableManager, EdgeStyleDescription style) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2021 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.web.compat.diagrams;

import java.util.List;
import java.util.Objects;

import org.eclipse.sirius.diagram.description.ConditionalEdgeStyleDescription;
import org.eclipse.sirius.diagram.description.EdgeMapping;
import org.eclipse.sirius.diagram.description.style.EdgeStyleDescription;
import org.eclipse.sirius.web.interpreter.AQLInterpreter;
import org.eclipse.sirius.web.interpreter.Result;
import org.eclipse.sirius.web.representations.VariableManager;

/**
* Compute the proper style description to use for a edge mapping.
*
* @author sbegaudeau
*/
public class EdgeStyleDescriptionProvider {
private final AQLInterpreter interpreter;

private final EdgeMapping edgeMapping;

public EdgeStyleDescriptionProvider(AQLInterpreter interpreter, EdgeMapping edgeMapping) {
this.interpreter = Objects.requireNonNull(interpreter);
this.edgeMapping = Objects.requireNonNull(edgeMapping);
}

public EdgeStyleDescription getEdgeStyleDescription(VariableManager variableManager) {
EdgeStyleDescription styleDescription = this.edgeMapping.getStyle();
List<ConditionalEdgeStyleDescription> conditionnalStyles = this.edgeMapping.getConditionnalStyles();
for (ConditionalEdgeStyleDescription conditionalStyle : conditionnalStyles) {
String predicateExpression = conditionalStyle.getPredicateExpression();
Result result = this.interpreter.evaluateExpression(variableManager.getVariables(), predicateExpression);
boolean shouldUseStyle = result.asBoolean().orElse(Boolean.FALSE).booleanValue();
if (shouldUseStyle) {
styleDescription = conditionalStyle.getStyle();
break;
}
}
return styleDescription;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public LabelStyleDescriptionConverter(AQLInterpreter interpreter, IObjectService
}

public LabelStyleDescription convert(org.eclipse.sirius.viewpoint.description.style.BasicLabelStyleDescription labelStyleDescription) {
Objects.requireNonNull(labelStyleDescription);
List<FontFormat> fontFormats = labelStyleDescription.getLabelFormat();

Function<VariableManager, String> iconURLProvider = (variableManager) -> {
Expand Down
Loading

0 comments on commit a574cc6

Please sign in to comment.