Skip to content

Commit

Permalink
[896] Allow to make specific changes before and after the layout
Browse files Browse the repository at this point in the history
Bug: eclipse-sirius#896
Change-Id: Ia06fcedcef118aff35741f57e2847d9e1aa53862
Signed-off-by: Axel RICHARD <axel.richard@obeo.fr>
  • Loading branch information
AxelRICHARD committed Dec 20, 2021
1 parent beef83c commit e718a93
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Currently it is not possible to compute different values for width and height.
- https://github.com/eclipse-sirius/sirius-components/issues/134[#134] [workbench] The internal API of the workbench is now ready to accept features leveraging a multi-selection
- [form] Add a tooltip to always make the full label available
- [core] Customize the Spring `ObjectMapper` instead of creating a brand new one from scratch in our `ObjectMapperConfiguration`
- https://github.com/eclipse-sirius/sirius-components/issues/896[#896] [diagram] Allow to make specific changes before and after the layout

=== Bug fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

import org.eclipse.elk.core.util.IGraphElementVisitor;
import org.eclipse.elk.graph.ElkGraphElement;
import org.eclipse.elk.graph.ElkNode;
import org.eclipse.elk.graph.properties.IPropertyHolder;
import org.eclipse.sirius.web.core.api.IEditingContext;
import org.eclipse.sirius.web.diagrams.Diagram;

/**
* Interface that can configure layout options based on the {@code id} and {@code type} attributes of diagram elements.
Expand All @@ -30,4 +33,8 @@ public interface ISiriusWebLayoutConfigurator extends IGraphElementVisitor {

IPropertyHolder configureByElementClass(Class<? extends ElkGraphElement> elementClass);

ElkNode applyBeforeLayout(ElkNode elkDiagram, IEditingContext editingContext, Diagram diagram);

ElkNode applyAfterLayout(ElkNode elkDiagram, IEditingContext editingContext, Diagram diagram);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.eclipse.elk.core.options.SizeConstraint;
import org.eclipse.elk.core.options.SizeOptions;
import org.eclipse.elk.graph.ElkEdge;
import org.eclipse.elk.graph.ElkNode;
import org.eclipse.sirius.web.core.api.IEditingContext;
import org.eclipse.sirius.web.diagrams.Diagram;
import org.eclipse.sirius.web.diagrams.NodeType;
import org.eclipse.sirius.web.diagrams.description.DiagramDescription;
Expand Down Expand Up @@ -122,4 +124,25 @@ public ISiriusWebLayoutConfigurator getLayoutConfigurator(Diagram diagram, Diagr
}
return this.getDefaultLayoutConfigurator();
}

public ElkNode applyBeforeLayout(ElkNode elkDiagram, IEditingContext editingContext, Diagram diagram, DiagramDescription diagramDescription) {
for (var customLayoutProvider : this.customLayoutProviders) {
var customLayout = customLayoutProvider.getLayoutConfigurator(diagram, diagramDescription);
if (customLayout.isPresent()) {
return customLayout.get().applyBeforeLayout(elkDiagram, editingContext, diagram);
}
}
return elkDiagram;
}

public ElkNode applyAfterLayout(ElkNode elkDiagram, IEditingContext editingContext, Diagram diagram, DiagramDescription diagramDescription) {
for (var customLayoutProvider : this.customLayoutProviders) {
var customLayout = customLayoutProvider.getLayoutConfigurator(diagram, diagramDescription);
if (customLayout.isPresent()) {
return customLayout.get().applyAfterLayout(elkDiagram, editingContext, diagram);
}
}
return elkDiagram;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,16 @@ public Diagram layout(IEditingContext editingContext, Diagram diagram) {
ELKConvertedDiagram convertedDiagram = this.elkDiagramConverter.convert(diagram);

ElkNode elkDiagram = convertedDiagram.getElkDiagram();
var representationDescription = this.representationDescriptionSearchService.findById(editingContext, diagram.getDescriptionId());
var optionalRepresentationDescription = this.representationDescriptionSearchService.findById(editingContext, diagram.getDescriptionId());
ISiriusWebLayoutConfigurator layoutConfigurator;
if (representationDescription.isPresent() && representationDescription.get() instanceof DiagramDescription) {
layoutConfigurator = this.layoutConfiguratorRegistry.getLayoutConfigurator(diagram, (DiagramDescription) representationDescription.get());
if (optionalRepresentationDescription.isPresent()) {
var representationDescription = optionalRepresentationDescription.get();
if (representationDescription instanceof DiagramDescription) {
elkDiagram = this.layoutConfiguratorRegistry.applyBeforeLayout(elkDiagram, editingContext, diagram, (DiagramDescription) representationDescription);
layoutConfigurator = this.layoutConfiguratorRegistry.getLayoutConfigurator(diagram, (DiagramDescription) representationDescription);
} else {
layoutConfigurator = this.layoutConfiguratorRegistry.getDefaultLayoutConfigurator();
}
} else {
layoutConfigurator = this.layoutConfiguratorRegistry.getDefaultLayoutConfigurator();
}
Expand All @@ -95,6 +101,13 @@ public Diagram layout(IEditingContext editingContext, Diagram diagram) {
IGraphLayoutEngine engine = new RecursiveGraphLayoutEngine();
engine.layout(elkDiagram, new BasicProgressMonitor());

if (optionalRepresentationDescription.isPresent()) {
var representationDescription = optionalRepresentationDescription.get();
if (representationDescription instanceof DiagramDescription) {
elkDiagram = this.layoutConfiguratorRegistry.applyAfterLayout(elkDiagram, editingContext, diagram, (DiagramDescription) representationDescription);
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

import org.eclipse.elk.core.LayoutConfigurator;
import org.eclipse.elk.graph.ElkGraphElement;
import org.eclipse.elk.graph.ElkNode;
import org.eclipse.elk.graph.properties.IPropertyHolder;
import org.eclipse.elk.graph.properties.MapPropertyHolder;
import org.eclipse.sirius.web.core.api.IEditingContext;
import org.eclipse.sirius.web.diagrams.Diagram;

/**
* Specialized {@link LayoutConfigurator} that can configure layout options based on the {@code id} and {@code type}
Expand Down Expand Up @@ -92,4 +95,14 @@ private SiriusWebLayoutConfigurator overrideWith(SiriusWebLayoutConfigurator lay
return this;
}

@Override
public ElkNode applyBeforeLayout(ElkNode elkDiagram, IEditingContext editingContext, Diagram diagram) {
return elkDiagram;
}

@Override
public ElkNode applyAfterLayout(ElkNode elkDiagram, IEditingContext editingContext, Diagram diagram) {
return elkDiagram;
}

}

0 comments on commit e718a93

Please sign in to comment.