Skip to content

Commit

Permalink
[287] Refactor rendering for incremental layout
Browse files Browse the repository at this point in the history
Bug: eclipse-sirius#287
Signed-off-by: William Piers <william.piers@obeo.fr>
Signed-off-by: Florian Barbin <florian.barbin@obeo.fr>
  • Loading branch information
wpiers authored and sbegaudeau committed Mar 15, 2021
1 parent 18032a5 commit c053ad8
Show file tree
Hide file tree
Showing 63 changed files with 2,275 additions and 1,150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ private Element createNodeElement(UUID descriptionId) {
.style(style)
.position(Position.UNDEFINED)
.size(Size.UNDEFINED)
.absolutePosition(Position.UNDEFINED)
.children(List.of())
.build();
// @formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ private Element createNodeElement(UUID descriptionId) {
.style(style)
.position(Position.UNDEFINED)
.size(Size.UNDEFINED)
.absolutePosition(Position.UNDEFINED)
.children(List.of())
.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 and others.
* 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 @@ -13,6 +13,8 @@
package org.eclipse.sirius.web.diagrams.layout.api;

import org.eclipse.sirius.web.diagrams.Diagram;
import org.eclipse.sirius.web.diagrams.MoveEvent;
import org.eclipse.sirius.web.diagrams.Position;

/**
* Implementation of this interface will layout the given diagram.
Expand All @@ -21,4 +23,18 @@
*/
public interface ILayoutService {
Diagram layout(Diagram diagram);

/**
* A partial layout that layouts only impacted elements.
*
* @param diagram
* The new diagram to layout.
* @param moveEvent
* the {@link MoveEvent} that has trigger the new layout. Can be null if no event occurs.
* @param startingPosition
* the {@link Position} to use to create new graphical elements (following a tool applied on the diagram
* for instance.
* @return the new layouted diagram.
*/
Diagram incrementalLayout(Diagram diagram, MoveEvent moveEvent, Position startingPosition);
}
5 changes: 5 additions & 0 deletions backend/sirius-web-diagrams-layout/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@
<version>0.2.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-bridge</artifactId>
<version>1.11</version>
</dependency>
</dependencies>

<build>
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 and others.
* 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 @@ -24,12 +24,12 @@
*
* @author sbegaudeau
*/
public class ConvertedDiagram {
public class ELKConvertedDiagram {
private final ElkNode elkDiagram;

private final Map<String, ElkGraphElement> id2ElkGraphElements;

public ConvertedDiagram(ElkNode elkDiagram, Map<String, ElkGraphElement> id2ElkGraphElements) {
public ELKConvertedDiagram(ElkNode elkDiagram, Map<String, ElkGraphElement> id2ElkGraphElements) {
this.elkDiagram = Objects.requireNonNull(elkDiagram);
this.id2ElkGraphElements = Objects.requireNonNull(id2ElkGraphElements);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
import org.eclipse.sirius.web.diagrams.Diagram;
import org.eclipse.sirius.web.diagrams.Edge;
import org.eclipse.sirius.web.diagrams.ImageNodeStyle;
import org.eclipse.sirius.web.diagrams.ImageNodeStyleSizeProvider;
import org.eclipse.sirius.web.diagrams.ImageSizeProvider;
import org.eclipse.sirius.web.diagrams.Label;
import org.eclipse.sirius.web.diagrams.Node;
import org.eclipse.sirius.web.diagrams.Size;
import org.eclipse.sirius.web.diagrams.TextBounds;
import org.eclipse.sirius.web.diagrams.layout.incremental.provider.ImageNodeStyleSizeProvider;
import org.eclipse.sirius.web.diagrams.layout.incremental.provider.ImageSizeProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
Expand All @@ -53,7 +53,7 @@
* @author sbegaudeau
*/
@Service
public class DiagramConverter {
public class ELKDiagramConverter {

public static final IProperty<String> PROPERTY_TYPE = new Property<>("org.eclipse.sirius.web.layout.type"); //$NON-NLS-1$

Expand All @@ -67,23 +67,23 @@ public class DiagramConverter {

private final ImageNodeStyleSizeProvider imageNodeStyleSizeProvider;

private final Logger logger = LoggerFactory.getLogger(DiagramConverter.class);
private final Logger logger = LoggerFactory.getLogger(ELKDiagramConverter.class);

public DiagramConverter(TextBoundsService textBoundsService) {
public ELKDiagramConverter(TextBoundsService textBoundsService) {
this.textBoundsService = Objects.requireNonNull(textBoundsService);
this.imageSizeProvider = new ImageSizeProvider();
this.imageNodeStyleSizeProvider = new ImageNodeStyleSizeProvider(this.imageSizeProvider);
}

public ConvertedDiagram convert(Diagram diagram) {
public ELKConvertedDiagram convert(Diagram diagram) {
ElkNode elkDiagram = this.convertDiagram(diagram);

Map<String, ElkGraphElement> id2ElkGraphElements = new HashMap<>();
Map<String, ElkConnectableShape> connectableShapeIndex = new LinkedHashMap<>();
diagram.getNodes().stream().forEach(node -> this.convertNode(node, elkDiagram, connectableShapeIndex, id2ElkGraphElements));
diagram.getEdges().stream().forEach(edge -> this.convertEdge(edge, elkDiagram, connectableShapeIndex, id2ElkGraphElements));

return new ConvertedDiagram(elkDiagram, id2ElkGraphElements);
return new ELKConvertedDiagram(elkDiagram, id2ElkGraphElements);
}

private ElkNode convertDiagram(Diagram diagram) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @author hmarchadour
*/
@Service
public class LayoutedDiagramProvider {
public class ELKLayoutedDiagramProvider {

public Diagram getLayoutedDiagram(Diagram diagram, ElkNode elkDiagram, Map<String, ElkGraphElement> id2ElkGraphElements) {
Size size = Size.of(elkDiagram.getWidth(), elkDiagram.getHeight());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public LayoutConfiguratorRegistry(List<IDiagramLayoutConfiguratorProvider> custo
public LayoutConfigurator getDefaultLayoutConfigurator() {
// @formatter:off
SiriusWebLayoutConfigurator configurator = new SiriusWebLayoutConfigurator();
configurator.configureByType(DiagramConverter.DEFAULT_DIAGRAM_TYPE)
configurator.configureByType(ELKDiagramConverter.DEFAULT_DIAGRAM_TYPE)
.setProperty(CoreOptions.ALGORITHM, LayeredOptions.ALGORITHM_ID)
.setProperty(CoreOptions.HIERARCHY_HANDLING, HierarchyHandling.INCLUDE_CHILDREN)
.setProperty(LayeredOptions.LAYERING_STRATEGY, LayeringStrategy.NETWORK_SIMPLEX)
Expand All @@ -88,7 +88,7 @@ public LayoutConfigurator getDefaultLayoutConfigurator() {
.setProperty(CoreOptions.NODE_LABELS_PLACEMENT, NodeLabelPlacement.outsideTopCenter());

// This image type does not match any diagram item. We add it to define the image size as constraint for the node image parent.
configurator.configureByType(DiagramConverter.DEFAULT_IMAGE_TYPE)
configurator.configureByType(ELKDiagramConverter.DEFAULT_IMAGE_TYPE)
.setProperty(CoreOptions.NODE_SIZE_CONSTRAINTS, SizeConstraint.fixed());
// @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 and others.
* 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 @@ -14,6 +14,7 @@

import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import org.eclipse.elk.alg.layered.options.LayeredOptions;
import org.eclipse.elk.core.IGraphLayoutEngine;
Expand All @@ -25,8 +26,16 @@
import org.eclipse.elk.graph.ElkGraphElement;
import org.eclipse.elk.graph.ElkNode;
import org.eclipse.sirius.web.diagrams.Diagram;
import org.eclipse.sirius.web.diagrams.MoveEvent;
import org.eclipse.sirius.web.diagrams.Position;
import org.eclipse.sirius.web.diagrams.description.DiagramDescription;
import org.eclipse.sirius.web.diagrams.layout.api.ILayoutService;
import org.eclipse.sirius.web.diagrams.layout.incremental.IncrementalLayoutConvertedDiagram;
import org.eclipse.sirius.web.diagrams.layout.incremental.IncrementalLayoutDiagramConverter;
import org.eclipse.sirius.web.diagrams.layout.incremental.IncrementalLayoutEngine;
import org.eclipse.sirius.web.diagrams.layout.incremental.IncrementalLayoutedDiagramProvider;
import org.eclipse.sirius.web.diagrams.layout.incremental.data.DiagramLayoutData;
import org.eclipse.sirius.web.diagrams.layout.incremental.data.ILayoutData;
import org.eclipse.sirius.web.services.api.representations.IRepresentationDescriptionService;
import org.springframework.stereotype.Service;

Expand All @@ -38,25 +47,32 @@
@Service
public class LayoutService implements ILayoutService {

private final DiagramConverter diagramConverter;
private final ELKDiagramConverter elkDiagramConverter;

private final LayoutConfiguratorRegistry layoutConfiguratorRegistry;

private final LayoutedDiagramProvider layoutedDiagramProvider;
private final ELKLayoutedDiagramProvider elkLayoutedDiagramProvider;

private final IncrementalLayoutedDiagramProvider incrementalLayoutedDiagramProvider;

private final IRepresentationDescriptionService representationDescriptionService;

public LayoutService(DiagramConverter diagramConverter, LayoutConfiguratorRegistry layoutConfiguratorRegistry, LayoutedDiagramProvider layoutedDiagramProvider,
private final IncrementalLayoutDiagramConverter incrementalLayoutDiagramConverter;

public LayoutService(ELKDiagramConverter elkDiagramConverter, IncrementalLayoutDiagramConverter incrementalLayoutDiagramConverter, LayoutConfiguratorRegistry layoutConfiguratorRegistry,
ELKLayoutedDiagramProvider layoutedDiagramProvider, IncrementalLayoutedDiagramProvider incrementalLayoutedDiagramProvider,
IRepresentationDescriptionService representationDescriptionService) {
this.diagramConverter = Objects.requireNonNull(diagramConverter);
this.elkDiagramConverter = Objects.requireNonNull(elkDiagramConverter);
this.incrementalLayoutDiagramConverter = Objects.requireNonNull(incrementalLayoutDiagramConverter);
this.layoutConfiguratorRegistry = Objects.requireNonNull(layoutConfiguratorRegistry);
this.layoutedDiagramProvider = Objects.requireNonNull(layoutedDiagramProvider);
this.elkLayoutedDiagramProvider = Objects.requireNonNull(layoutedDiagramProvider);
this.incrementalLayoutedDiagramProvider = Objects.requireNonNull(incrementalLayoutedDiagramProvider);
this.representationDescriptionService = Objects.requireNonNull(representationDescriptionService);
}

@Override
public Diagram layout(Diagram diagram) {
ConvertedDiagram convertedDiagram = this.diagramConverter.convert(diagram);
ELKConvertedDiagram convertedDiagram = this.elkDiagramConverter.convert(diagram);

ElkNode elkDiagram = convertedDiagram.getElkDiagram();
var representationDescription = this.representationDescriptionService.findRepresentationDescriptionById(diagram.getDescriptionId());
Expand All @@ -74,9 +90,18 @@ public Diagram layout(Diagram diagram) {
engine.layout(elkDiagram, new BasicProgressMonitor());

Map<String, ElkGraphElement> id2ElkGraphElements = convertedDiagram.getId2ElkGraphElements();
Diagram layoutedDiagram = this.layoutedDiagramProvider.getLayoutedDiagram(diagram, elkDiagram, id2ElkGraphElements);
Diagram layoutedDiagram = this.elkLayoutedDiagramProvider.getLayoutedDiagram(diagram, elkDiagram, id2ElkGraphElements);

return layoutedDiagram;
}

@Override
public Diagram incrementalLayout(Diagram newDiagram, MoveEvent moveEvent, Position startingPosition) {
IncrementalLayoutConvertedDiagram convertedDiagram = this.incrementalLayoutDiagramConverter.convert(newDiagram);
DiagramLayoutData diagramLayoutData = convertedDiagram.getDiagramLayoutData();
new IncrementalLayoutEngine(moveEvent, startingPosition).layout(diagramLayoutData);
Map<UUID, ILayoutData> id2LayoutData = convertedDiagram.getId2LayoutData();
return this.incrementalLayoutedDiagramProvider.getLayoutedDiagram(newDiagram, diagramLayoutData, id2LayoutData);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public IPropertyHolder configureByType(String type) {
public void visit(final ElkGraphElement element) {
super.visit(element);

IPropertyHolder typeProperties = this.getPropertiesByType(element.getProperty(DiagramConverter.PROPERTY_TYPE));
IPropertyHolder typeProperties = this.getPropertiesByType(element.getProperty(ELKDiagramConverter.PROPERTY_TYPE));
this.applyProperties(element, typeProperties);

IPropertyHolder idProperties = this.getPropertiesById(element.getIdentifier());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*******************************************************************************
* Copyright (c) 2021 THALES GLOBAL SERVICES.
* 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.diagrams.layout.incremental;

import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import org.eclipse.sirius.web.diagrams.layout.incremental.data.DiagramLayoutData;
import org.eclipse.sirius.web.diagrams.layout.incremental.data.ILayoutData;

/**
* The result of the diagram transformation with the raw layout data and a cache of the values computed during the
* transformation.
*
* @author wpiers
*/
public class IncrementalLayoutConvertedDiagram {

private final DiagramLayoutData diagramLayoutData;

private final Map<UUID, ILayoutData> id2LayoutData;

public IncrementalLayoutConvertedDiagram(DiagramLayoutData diagramLayoutData, Map<UUID, ILayoutData> id2LayoutData) {
this.diagramLayoutData = Objects.requireNonNull(diagramLayoutData);
this.id2LayoutData = Objects.requireNonNull(id2LayoutData);
}

public DiagramLayoutData getDiagramLayoutData() {
return this.diagramLayoutData;
}

public Map<UUID, ILayoutData> getId2LayoutData() {
return this.id2LayoutData;
}
}
Loading

0 comments on commit c053ad8

Please sign in to comment.