Skip to content

Commit

Permalink
[3856] Add tree path fallback handler
Browse files Browse the repository at this point in the history
Bug: eclipse-sirius#3856
Signed-off-by: Jerome Gout <[email protected]>
  • Loading branch information
jerome-obeo committed Sep 13, 2024
1 parent b659a5c commit 0d1f911
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -49,9 +52,12 @@ public class TreePathEventHandler implements ITreeEventHandler {

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

private final ITreeNavigationService treeNavigationService;

private final List<ITreePathProvider> treePathProviders;

public TreePathEventHandler(List<ITreePathProvider> treePathProviders) {
public TreePathEventHandler(List<ITreePathProvider> treePathProviders, ITreeNavigationService treeNavigationService) {
this.treeNavigationService = Objects.requireNonNull(treeNavigationService);
this.treePathProviders = Objects.requireNonNull(treePathProviders);
}

Expand All @@ -68,17 +74,28 @@ public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDesc
if (treeInput instanceof TreePathInput input) {
Optional<ITreePathProvider> 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("; ")));
}
}

changeDescriptionSink.tryEmitNext(changeDescription);
payloadSink.tryEmitValue(payload);
}

private IPayload handleDefaultTreePath(IEditingContext editingContext, Tree tree, TreePathInput input) {
int maxDepth = 0;
Set<String> allAncestors = new LinkedHashSet<>();
for (String selectionEntryId : input.selectionEntryIds()) {
List<String> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
}

0 comments on commit 0d1f911

Please sign in to comment.