Skip to content

Commit

Permalink
[1316] Support a new parametric SVG node style
Browse files Browse the repository at this point in the history
bug: #1316
Signed-off-by: Laurent Fasani <laurent.fasani@obeo.fr>
  • Loading branch information
lfasani committed Sep 23, 2022
1 parent 7d544a5 commit a23898e
Show file tree
Hide file tree
Showing 41 changed files with 1,466 additions and 64 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
= Changelog

== v2022.11.0 (unreleased)

=== Architectural decision records

- [ADR-070] How to contribute a new diagram node style

=== Improvements

- https://github.com/eclipse-sirius/sirius-components/issues/1316[#1316] [diagram] Support parametric svg node style

== v2022.9.0

=== Architectural decision records
Expand Down
90 changes: 90 additions & 0 deletions doc/adrs/070_contribute_a_diagram_node_style.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
= ADR-070 - Contribute a diagram node style

== Context

Currently, the diagram styles are hardcoded and known by the sirius-component core in the backend and in the frontend.

The aim of this adr is to explain how to contribute a new diagram style from an additional project and what changes are needed in the sirius-component projects to support the contributed style.

=== Current state
On the backend, style are managed specifically for
* the rendering in `sirius-components-diagrams`
* the layout in `sirius-components-diagrams-layout`
** ELK layout
** Incremental layout
* the service called by the front end

On the frontend, styles are managed with a dedicated sprotty inherited view.

== Enhancement ==

For the rest of this adr, let's consider that the contributed style is a parametric svg style node.

=== Use cases

*Specifier*

When defining the diagram description with the web view modeler, the specifier should be able to select the contributed style as well as hardcoded existing style (such as node, node list and image styles)

*Developper*

On the backend, the developper will provide spring bean to allow
* defining the new nodestyle
* adding the node style as part of the view modeler
* the style rendering when rendering the diagram
* Optional (customizing the layout)

On the front end, the developper needs to provide a specific sprotty view to render the style on the frontend

=== Code changes

The code needs to evolve so that it is able to manage the contributed style without having any direct dependency of its definition.
Everywhere, the specific node styles are used, the code should adapt to get bean contribution that implement the right API:

* Diagram rendering: A new API `INodeStyleProvider` has been created to provide a node type and to instanciate the nodeStyle inheriting from `INodeStyle`
* Incremental and ELK layout: A new API `ICustomNodeLabelPositionProvider` has been defined to customize the position of the node Label. This API is used for both ELK and incremental layout


* Front end: the code must be able to render the new node style by adding a new sprotty view. In our example `node:parametric-svg` node type and `ParametricSVGView` have been added.
Currently, delegating the creation of the sprotty pojo is not yet supported. So the digram convertion from the data returned by the server to data managed by sprotty is hardcoded.
The link between the node type and the sprotty view capable of rendering the node is done `dependencyInjection`
```
configureModelElement(context, 'node:parametric-svg', Node, ParametricSVGView);
```

== Developper guide

=== backend

* View model: contribute an extension of the `view.ecore` and add a subtype of `NodeStyleDescription`
* Diagram rendering : Create a diagram node style
** Add a new POJO class representing the node style. It must implements `INodeStyle`
** Add a `INodeStyleProvider` to provide a node type and to instanciate the nodeStyle inheriting from `INodeStyle`

* ELK layout: If the default configuration from `LayoutConfiguratorRegistry` is not sufficient, it is possible to provide a bean implementing `IDiagramLayoutConfiguratorProvider` to customize the ELK configuration.
* Incremental and ELK layout:
** (Optional) Provide a bean implementing `ICustomNodeLabelPositionProvider` to customize the position of the node Label.

* Service: Add a graphqls file that contains `extend union INodeStyle = `<new Node Style>`` and the declaration of the new node type itself For example:
```
extend union INodeStyle = SVGNodeStyle

type SVGNodeStyle {
svgURL: String!
backgroundColor: String!
borderColor: String!
borderRadius: Int!
borderSize: Int!
borderStyle: LineStyle!
}
```

=== frontend
* Add a new diagram node style type that implements `INodeStyle`
* Add a new diagram node style type that extends `GQLINodeStyle`
* update the diagramEventSubscription
* Add a specific sprotty view that is able to render the style and configure the node style for sprotty

== Status

Work in progress
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*******************************************************************************
* Copyright (c) 2022 CEA.
* 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.core.api;

import java.util.EnumMap;

/**
* Service used to compute dynamically the svg image of node.
*
* @author lfasani
*/
public interface IParametricSVGImageFactory {

enum SVGAttribute {
WIDTH("width"), //$NON-NLS-1$
HEIGHT("height"), //$NON-NLS-1$
LABELWIDTH("labelWidth"), //$NON-NLS-1$
LABELHEIGHT("labelHeight"), //$NON-NLS-1$
COLOR("color"), //$NON-NLS-1$
BORDERCOLOR("borderColor"), //$NON-NLS-1$
BORDERSTYLE("borderStyle"), //$NON-NLS-1$
BORDERSIZE("borderSize"); //$NON-NLS-1$

private String label;

SVGAttribute(String label) {
this.label = label;
}

public String getLabel() {
return this.label;
}
}

byte[] createSvg(String svgType, EnumMap<SVGAttribute, String> attributesValues);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2020 Obeo.
* Copyright (c) 2019, 2022 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 @@ -26,6 +26,8 @@ public final class URLConstants {

public static final String IMAGE_BASE_PATH = API_BASE_PATH + "/images"; //$NON-NLS-1$

public static final String PARAMETRIC_IMAGE_BASE_PATH = API_BASE_PATH + "/parametricsvgs"; //$NON-NLS-1$

private URLConstants() {
// Prevent instantiation
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*******************************************************************************
* Copyright (c) 2022 CEA.
* 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.collaborative.diagrams.api;

import java.util.List;

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

/**
* Interface of the service used to get the path of the parametric svg images accessible from the jars used in the
* application.</br>
* The specifier must provide an implementation of this API as a {@link @Service} to provide available images.
*
* @author lfasani
*/
@PublicApi
public interface IParametricSVGImageRegistry {
/**
* Get the paths of the svg images than can be parameterized.</br>
* The images are those in the jar.
* <p>
* Warning: <b>The path must be unique</b>. Indeed, the images are contained in a jar but the system does not know
* about the jars. Then if an image is contained in the same folder structure in many jars, then the result to read
* the image will be unpredictable.
* </p>
*
* The folder must be of the form:
*
* <pre>
* (/ADDITIONAL_FOLDER1/.../ADDITIONAL_FOLDERn)/IMAGE
* </pre>
*
* <p>
* Path examples:</br>
* "/myparametricsvgfolder/myImage.svg", "/myParametricImage.svg"
* </p>
*
* @return a list a paths.
*/
List<ParametricSVGImage> getImages();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2022 CEA.
* 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.collaborative.diagrams.api;

import java.text.MessageFormat;
import java.util.Objects;
import java.util.UUID;

/**
* POJO representing a parametric svg image.
*
* @author lfasani
*/
public class ParametricSVGImage {
private UUID id;

private String label;

private String path;

public ParametricSVGImage(UUID id, String label, String path) {
this.id = Objects.requireNonNull(id);
this.label = Objects.requireNonNull(label);
this.path = Objects.requireNonNull(path);
}

public UUID getId() {
return this.id;
}

public String getLabel() {
return this.label;
}

public String getPath() {
return this.path;
}

@Override
public String toString() {
String pattern = "{0} '{' id: {1}, label: {2}, path: {3} '}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.label, this.path);
}
}
Loading

0 comments on commit a23898e

Please sign in to comment.