Skip to content

Commit

Permalink
[1456] Improve the diagram rendering cache to find nodes
Browse files Browse the repository at this point in the history
Bug: #1456
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
  • Loading branch information
sbegaudeau authored and AxelRICHARD committed Jan 19, 2023
1 parent c56bfe6 commit c8d26a9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Expand Up @@ -25,6 +25,7 @@
- https://github.com/eclipse-sirius/sirius-components/issues/1559[#1559] [view] It is now possible to specify the (computed) width and height separately for a _Node Style_ (instead of a single size before, which always resulted in square shapes).
- https://github.com/eclipse-sirius/sirius-components/issues/1560[#1560] Remove unused `EditingContextCompletionProposalsDataFetcher`
- https://github.com/eclipse-sirius/sirius-components/issues/1426[#1426] [view] Add missing data type on initialDirectEditLabelExpression
- https://github.com/eclipse-sirius/sirius-components/issues/1456[#1456] [diagram] Add new methods to the diagram rendering cache to compute the parent, ancestors, children and descendants of a given node identifier


== v2023.1.0
Expand Down
Expand Up @@ -100,6 +100,7 @@ public Element render() {

cache.put(nodeDescription.getId(), nodeElement);
cache.put(semanticElement, nodeElement);
cache.put(nodeElement, this.props.getParentElementId());
}

}
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2020 Obeo.
* Copyright (c) 2019, 2023 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 @@ -14,12 +14,13 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import org.eclipse.sirius.components.diagrams.elements.NodeElementProps;
import org.eclipse.sirius.components.representations.Element;

/**
Expand All @@ -29,12 +30,18 @@
*/
public class DiagramRenderingCache {

private final Map<UUID, List<Element>> nodeDescriptionIdToNodes = new HashMap<>();
private final Map<UUID, List<Element>> nodeDescriptionIdToNodes = new LinkedHashMap<>();

private final Map<String, Element> nodeIdToNode = new LinkedHashMap<>();

private final Map<Element, Object> nodeToObject = new LinkedHashMap<>();

private final Map<Object, List<Element>> objectToNodes = new LinkedHashMap<>();

private final Map<Element, String> nodeToParentElementId = new LinkedHashMap<>();

private final Map<String, List<Element>> nodeIdToChildren = new LinkedHashMap<>();

public void put(UUID nodeDescriptionId, Element nodeElement) {
this.nodeDescriptionIdToNodes.computeIfAbsent(nodeDescriptionId, id -> new ArrayList<>()).add(nodeElement);
}
Expand All @@ -44,6 +51,17 @@ public void put(Object object, Element nodeElement) {
this.objectToNodes.computeIfAbsent(object, obj -> new ArrayList<>()).add(nodeElement);
}

public void put(Element node, String parentNodeId) {
if (node.getProps() instanceof NodeElementProps nodeElementProps) {
this.nodeIdToNode.put(nodeElementProps.getId(), node);
this.nodeToParentElementId.put(node, parentNodeId);

var children = this.nodeIdToChildren.getOrDefault(parentNodeId, new ArrayList<>());
children.add(node);
this.nodeIdToChildren.put(parentNodeId, children);
}
}

public Map<UUID, List<Element>> getNodeDescriptionIdToNodes() {
return this.nodeDescriptionIdToNodes;
}
Expand All @@ -52,11 +70,52 @@ public Map<Element, Object> getNodeToObject() {
return this.nodeToObject;
}

public Map<Object, List<Element>> getObjectToNodes() {
return this.objectToNodes;
}

public List<Element> getElementsRepresenting(Object semanticObject) {
return this.objectToNodes.getOrDefault(semanticObject, Collections.emptyList());
}

public Map<Object, List<Element>> getObjectToNodes() {
return this.objectToNodes;
public Optional<Element> getParent(String nodeId) {
// @formatter:off
return Optional.ofNullable(this.nodeIdToNode.get(nodeId))
.map(this.nodeToParentElementId::get)
.map(this.nodeIdToNode::get);
// @formatter:on
}

public List<Element> getAncestors(String nodeId) {
List<Element> ancestors = new ArrayList<>();

var parent = this.getParent(nodeId).orElse(null);
while (parent != null) {
ancestors.add(parent);
if (parent.getProps() instanceof NodeElementProps nodeElementProps) {
parent = this.getParent(nodeElementProps.getId()).orElse(null);
}
}

return ancestors;
}

public List<Element> getChildren(String nodeId) {
return Optional.ofNullable(this.nodeIdToChildren.get(nodeId)).orElse(List.of());
}

public List<Element> getDescendants(String nodeId) {
List<Element> descendants = new ArrayList<>();

var children = this.getChildren(nodeId);
children.forEach(child -> {
descendants.add(child);
if (child.getProps() instanceof NodeElementProps nodeElementProps) {
descendants.addAll(this.getDescendants(nodeElementProps.getId()));
}
});

return descendants;
}

}

0 comments on commit c8d26a9

Please sign in to comment.