From 0d1f911f95ef51364852a4144e0ec5f5d2b6ba9f Mon Sep 17 00:00:00 2001 From: Jerome Gout Date: Fri, 13 Sep 2024 11:48:52 +0200 Subject: [PATCH] [3856] Add tree path fallback handler Bug: https://github.com/eclipse-sirius/sirius-web/issues/3856 Signed-off-by: Jerome Gout --- .../trees/handlers/TreePathEventHandler.java | 31 ++++++++++++++----- .../handlers/TreePathEventHandlerTests.java | 8 ++--- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/TreePathEventHandler.java b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/TreePathEventHandler.java index 9c6c350db99..36d8b11ed28 100644 --- a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/TreePathEventHandler.java +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/TreePathEventHandler.java @@ -12,9 +12,11 @@ *******************************************************************************/ package org.eclipse.sirius.components.collaborative.trees.handlers; +import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import org.eclipse.sirius.components.collaborative.api.ChangeDescription; @@ -25,6 +27,7 @@ import org.eclipse.sirius.components.collaborative.trees.dto.TreePath; import org.eclipse.sirius.components.collaborative.trees.dto.TreePathInput; import org.eclipse.sirius.components.collaborative.trees.dto.TreePathSuccessPayload; +import org.eclipse.sirius.components.collaborative.trees.services.api.ITreeNavigationService; import org.eclipse.sirius.components.core.api.ErrorPayload; import org.eclipse.sirius.components.core.api.IEditingContext; import org.eclipse.sirius.components.core.api.IPayload; @@ -49,9 +52,12 @@ public class TreePathEventHandler implements ITreeEventHandler { private final Logger logger = LoggerFactory.getLogger(TreePathEventHandler.class); + private final ITreeNavigationService treeNavigationService; + private final List treePathProviders; - public TreePathEventHandler(List treePathProviders) { + public TreePathEventHandler(List treePathProviders, ITreeNavigationService treeNavigationService) { + this.treeNavigationService = Objects.requireNonNull(treeNavigationService); this.treePathProviders = Objects.requireNonNull(treePathProviders); } @@ -68,12 +74,12 @@ public void handle(One payloadSink, Many changeDesc if (treeInput instanceof TreePathInput input) { Optional optionalPathProvider = this.treePathProviders.stream().filter(treePathProvider -> treePathProvider.canHandle(tree)).findFirst(); if (optionalPathProvider.isPresent()) { - IPayload resultPayload = optionalPathProvider.get().handle(editingContext, tree, input); - if (resultPayload instanceof TreePathSuccessPayload) { - payload = resultPayload; - } else if (resultPayload instanceof ErrorPayload errorPayload) { - this.logger.warn(errorPayload.messages().stream().map(Message::body).collect(Collectors.joining("; "))); - } + payload = optionalPathProvider.get().handle(editingContext, tree, input); + } else { + payload = this.handleDefaultTreePath(editingContext, tree, input); + } + if (payload instanceof ErrorPayload errorPayload) { + this.logger.warn(errorPayload.messages().stream().map(Message::body).collect(Collectors.joining("; "))); } } @@ -81,4 +87,15 @@ public void handle(One payloadSink, Many changeDesc payloadSink.tryEmitValue(payload); } + private IPayload handleDefaultTreePath(IEditingContext editingContext, Tree tree, TreePathInput input) { + int maxDepth = 0; + Set allAncestors = new LinkedHashSet<>(); + for (String selectionEntryId : input.selectionEntryIds()) { + List itemAncestors = this.treeNavigationService.getAncestors(editingContext, tree, selectionEntryId); + allAncestors.addAll(itemAncestors); + maxDepth = Math.max(maxDepth, itemAncestors.size()); + } + return new TreePathSuccessPayload(input.id(), new TreePath(allAncestors.stream().toList(), maxDepth)); + } + } diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/test/java/org/eclipse/sirius/components/collaborative/trees/architecture/handlers/TreePathEventHandlerTests.java b/packages/trees/backend/sirius-components-collaborative-trees/src/test/java/org/eclipse/sirius/components/collaborative/trees/architecture/handlers/TreePathEventHandlerTests.java index ad7c7989596..07954634996 100644 --- a/packages/trees/backend/sirius-components-collaborative-trees/src/test/java/org/eclipse/sirius/components/collaborative/trees/architecture/handlers/TreePathEventHandlerTests.java +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/test/java/org/eclipse/sirius/components/collaborative/trees/architecture/handlers/TreePathEventHandlerTests.java @@ -22,6 +22,7 @@ import org.eclipse.sirius.components.collaborative.trees.dto.TreePathInput; import org.eclipse.sirius.components.collaborative.trees.dto.TreePathSuccessPayload; import org.eclipse.sirius.components.collaborative.trees.handlers.TreePathEventHandler; +import org.eclipse.sirius.components.collaborative.trees.services.api.ITreeNavigationService; import org.eclipse.sirius.components.core.api.ErrorPayload; import org.eclipse.sirius.components.core.api.IEditingContext; import org.eclipse.sirius.components.core.api.IPayload; @@ -41,7 +42,7 @@ public class TreePathEventHandlerTests { @Test public void testTreePathWithEmptyProviders() { - var handler = new TreePathEventHandler(List.of()); + var handler = new TreePathEventHandler(List.of(), new ITreeNavigationService.NoOp()); TreePathInput input = new TreePathInput(UUID.randomUUID(), "editingContextId", "representationId", List.of()); assertThat(handler.canHandle(input)).isTrue(); @@ -68,7 +69,7 @@ public boolean canHandle(Tree tree) { } }; - var handler = new TreePathEventHandler(List.of(errorProvider)); + var handler = new TreePathEventHandler(List.of(errorProvider), new ITreeNavigationService.NoOp()); TreePathInput input = new TreePathInput(UUID.randomUUID(), "editingContextId", "representationId", List.of()); assertThat(handler.canHandle(input)).isTrue(); @@ -79,7 +80,6 @@ public boolean canHandle(Tree tree) { handler.handle(payloadSink, changeDescriptionSink, new IEditingContext.NoOp(), null, null, input); IPayload payload = payloadSink.asMono().block(); - assertThat(payload).isInstanceOf(TreePathSuccessPayload.class); - assertThat(((TreePathSuccessPayload) payload).treePath().toString()).isEqualTo("TreePath {treeItemIdsToExpand: [], maxDepth: 0}"); + assertThat(payload).isInstanceOf(ErrorPayload.class); } }