Skip to content

Commit

Permalink
[3647] Add support for diagram style with background customization
Browse files Browse the repository at this point in the history
Bug: #3647
Signed-off-by: Florian ROUËNÉ <[email protected]>
  • Loading branch information
frouene committed Jun 20, 2024
1 parent 6b7efa3 commit ca22269
Show file tree
Hide file tree
Showing 54 changed files with 2,044 additions and 168 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ This dialog presents diagram elements in a tree and allows to select them and up
+
image:doc/screenshots/diagramFilterView.png[Diagram Filter View, 70%]
- https://github.com/eclipse-sirius/sirius-web/issues/3523[#3523] [gantt] Support rounding dates when changing dates from gantt
- https://github.com/eclipse-sirius/sirius-web/issues/3647[#3647] [diagram] Add support for diagram style with background customization

=== Improvements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void initialize() {
.nodeDescriptions(List.of(this.getNodeDescription(UUID.randomUUID().toString())))
.edgeDescriptions(List.of())
.dropHandler(variableManager -> new Failure(""))
.styleProvider(variableManager -> null)
.build();

Diagram diagram = Diagram.newDiagram(UUID.randomUUID().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void initialize() {
.nodeDescriptions(List.of(this.getNodeDescription(UUID.randomUUID().toString())))
.edgeDescriptions(List.of())
.dropHandler(variableManager -> new Failure(""))
.styleProvider(variableManager -> null)
.build();

Node node = Node.newNode(UUID.randomUUID().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public DiagramDescription convert(org.eclipse.sirius.diagram.description.Diagram
.canCreatePredicate(canCreatePredicate)
.labelProvider(labelProvider)
.autoLayout(this.isAutoLayoutMode(siriusDiagramDescription))
.arrangeLayoutDirection(ArrangeLayoutDirection.UNDEFINED);
.arrangeLayoutDirection(ArrangeLayoutDirection.UNDEFINED)
.styleProvider(variableManager -> null);

for (IDiagramDescriptionPopulator diagramDescriptionPopulator : this.diagramDescriptionPopulators) {
diagramDescriptionPopulator.populate(builder, siriusDiagramDescription, interpreter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ type Diagram implements Representation {
position: Position!
nodes: [Node!]!
edges: [Edge!]!
style: DiagramStyle
layoutData: DiagramLayoutData!
}

type DiagramStyle {
background: String
}

type DiagramLayoutData {
nodeLayoutData: [NodeLayoutData!]!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public Optional<IRepresentationDescription> findById(IEditingContext editingCont
.nodeDescriptions(new ArrayList<>())
.targetObjectIdProvider(variableManager -> "targetObjectId")
.dropHandler(variableManager -> new Failure(""))
.styleProvider(variableManager -> null)
.build();
// @formatter:on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public void testGetConnectorTools() {
.edgeDescriptions(new ArrayList<>())
.palettes(List.of(palette))
.dropHandler(variableManager -> new Failure(""))
.styleProvider(variableManager -> null)
.build();

Node sourceNode = this.getNode(SOURCE_NODE_ID, SOURCE_NODE_TARGET_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public DiagramDescription getDiagramDescription(String diagramDescriptionId, Lis
.edgeDescriptions(edgeDescriptions)
.palettes(palettes)
.dropHandler(variableManager -> new Failure(""))
.styleProvider(variableManager -> null)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
@Immutable
public final class Diagram implements IRepresentation {

public static final String KIND = IRepresentation.KIND_PREFIX + "?type=Diagram";

private String id;
Expand All @@ -50,10 +51,20 @@ public final class Diagram implements IRepresentation {

private DiagramLayoutData layoutData;

private DiagramStyle style;

private Diagram() {
// Prevent instantiation
}

public static Builder newDiagram(String id) {
return new Builder(id);
}

public static Builder newDiagram(Diagram diagram) {
return new Builder(diagram);
}

@Override
public String getId() {
return this.id;
Expand Down Expand Up @@ -99,12 +110,8 @@ public DiagramLayoutData getLayoutData() {
return this.layoutData;
}

public static Builder newDiagram(String id) {
return new Builder(id);
}

public static Builder newDiagram(Diagram diagram) {
return new Builder(diagram);
public DiagramStyle getStyle() {
return this.style;
}

@Override
Expand All @@ -120,9 +127,10 @@ public String toString() {
*/
@SuppressWarnings("checkstyle:HiddenField")
public static final class Builder {
private String id;

private String kind = KIND;
private final String id;

private final String kind = KIND;

private String targetObjectId;

Expand All @@ -138,6 +146,8 @@ public static final class Builder {

private List<Edge> edges;

private DiagramStyle style;

private DiagramLayoutData layoutData = new DiagramLayoutData(Map.of(), Map.of(), Map.of());

private Builder(String id) {
Expand All @@ -153,6 +163,7 @@ private Builder(Diagram diagram) {
this.size = diagram.getSize();
this.nodes = diagram.getNodes();
this.edges = diagram.getEdges();
this.style = diagram.getStyle();
this.layoutData = diagram.getLayoutData();
}

Expand Down Expand Up @@ -191,6 +202,11 @@ public Builder edges(List<Edge> edges) {
return this;
}

public Builder style(DiagramStyle style) {
this.style = Objects.requireNonNull(style);
return this;
}

public Builder layoutData(DiagramLayoutData layoutData) {
this.layoutData = Objects.requireNonNull(layoutData);
return this;
Expand All @@ -207,6 +223,7 @@ public Diagram build() {
diagram.size = Objects.requireNonNull(this.size);
diagram.nodes = Objects.requireNonNull(this.nodes);
diagram.edges = Objects.requireNonNull(this.edges);
diagram.style = this.style; // Optional on purpose
diagram.layoutData = Objects.requireNonNull(this.layoutData);
return diagram;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*******************************************************************************
* Copyright (c) 2024 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.diagrams;

import java.util.Objects;

import org.eclipse.sirius.components.annotations.Immutable;

/**
* A diagram style.
*
* @author frouene
*/
@Immutable
public final class DiagramStyle {

private String background;

private DiagramStyle() {
// prevent initialisation
}

public static Builder newDiagramStyle() {
return new Builder();
}

public String getBackground() {
return this.background;
}

/**
* The builder used to create the diagram style.
*
* @author frouene
*/
@SuppressWarnings("checkstyle:HiddenField")
public static final class Builder {

private String background;

public Builder background(String background) {
this.background = Objects.requireNonNull(background);
return this;
}

public DiagramStyle build() {
DiagramStyle diagramStyle = new DiagramStyle();
diagramStyle.background = Objects.requireNonNull(this.background);
return diagramStyle;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public Element render() {

String diagramId = optionalPreviousDiagram.map(Diagram::getId).orElseGet(() -> UUID.randomUUID().toString());
String targetObjectId = diagramDescription.getTargetObjectIdProvider().apply(variableManager);
var style = diagramDescription.getStyleProvider().apply(variableManager);

DiagramRenderingCache cache = new DiagramRenderingCache();

IDiagramElementRequestor diagramElementRequestor = new DiagramElementRequestor();
INodeDescriptionRequestor nodeDescriptionRequestor = new NodeDescriptionRequestor(allDiagramDescriptions);
// @formatter:off
var nodes = diagramDescription.getNodeDescriptions().stream()
.map(nodeDescription -> {
var previousNodes = optionalPreviousDiagram.map(previousDiagram -> diagramElementRequestor.getRootNodes(previousDiagram, nodeDescription))
Expand Down Expand Up @@ -94,30 +94,31 @@ public Element render() {
return new Element(EdgeComponent.class, edgeComponentProps);
})
.toList();
// @formatter:on

List<Element> children = new ArrayList<>();
children.addAll(nodes);
children.addAll(edges);

// @formatter:off
Position position = optionalPreviousDiagram.map(Diagram::getPosition)
.orElse(Position.UNDEFINED);


Size size = optionalPreviousDiagram.map(Diagram::getSize)
.orElse(Size.UNDEFINED);

DiagramElementProps diagramElementProps = DiagramElementProps.newDiagramElementProps(diagramId)
DiagramElementProps.Builder diagramElementPropsBuilder = DiagramElementProps.newDiagramElementProps(diagramId)
.targetObjectId(targetObjectId)
.descriptionId(diagramDescription.getId())
.label(label)
.position(position)
.size(size)
.children(children)
.build();
// @formatter:on
return new Element(DiagramElementProps.TYPE, diagramElementProps);
.children(children);

if (style != null) {
diagramElementPropsBuilder.style(style);
}

return new Element(DiagramElementProps.TYPE, diagramElementPropsBuilder.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.eclipse.sirius.components.annotations.Immutable;
import org.eclipse.sirius.components.diagrams.ArrangeLayoutDirection;
import org.eclipse.sirius.components.diagrams.DiagramStyle;
import org.eclipse.sirius.components.diagrams.tools.Palette;
import org.eclipse.sirius.components.representations.IRepresentationDescription;
import org.eclipse.sirius.components.representations.IStatus;
Expand Down Expand Up @@ -66,6 +67,8 @@ public final class DiagramDescription implements IRepresentationDescription {

private Function<VariableManager, IStatus> dropNodeHandler;

private Function<VariableManager, DiagramStyle> styleProvider;

private DiagramDescription() {
// Prevent instantiation
}
Expand Down Expand Up @@ -129,6 +132,10 @@ public Function<VariableManager, IStatus> getDropNodeHandler() {
return this.dropNodeHandler;
}

public Function<VariableManager, DiagramStyle> getStyleProvider() {
return this.styleProvider;
}

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, label: {2}, nodeDescriptionCount: {3}, edgeDescriptionCount: {4}'}'";
Expand Down Expand Up @@ -167,6 +174,8 @@ public static final class Builder {

private Function<VariableManager, IStatus> dropNodeHandler;

private Function<VariableManager, DiagramStyle> styleProvider;

private Builder(String id) {
this.id = Objects.requireNonNull(id);
}
Expand All @@ -184,6 +193,7 @@ private Builder(DiagramDescription diagramDescription) {
this.edgeDescriptions = diagramDescription.getEdgeDescriptions();
this.dropHandler = diagramDescription.getDropHandler();
this.dropNodeHandler = diagramDescription.getDropNodeHandler();
this.styleProvider = diagramDescription.getStyleProvider();
}

public Builder label(String label) {
Expand Down Expand Up @@ -241,6 +251,11 @@ public Builder dropNodeHandler(Function<VariableManager, IStatus> dropNodeHandle
return this;
}

public Builder styleProvider(Function<VariableManager, DiagramStyle> styleProvider) {
this.styleProvider = Objects.requireNonNull(styleProvider);
return this;
}

public DiagramDescription build() {
DiagramDescription diagramDescription = new DiagramDescription();
diagramDescription.id = Objects.requireNonNull(this.id);
Expand All @@ -255,6 +270,7 @@ public DiagramDescription build() {
diagramDescription.edgeDescriptions = Objects.requireNonNull(this.edgeDescriptions);
diagramDescription.dropHandler = Objects.requireNonNull(this.dropHandler);
diagramDescription.dropNodeHandler = this.dropNodeHandler; // Optional on purpose.
diagramDescription.styleProvider = Objects.requireNonNull(this.styleProvider);
return diagramDescription;
}
}
Expand Down
Loading

0 comments on commit ca22269

Please sign in to comment.