diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 916362ec3b..7cb567544f 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -116,6 +116,7 @@ This will allow specifier to create images that fir perfectly in the project tem - https://github.com/eclipse-sirius/sirius-web/issues/3826[#3826] [form] Make EMF default form support attributes with a date - https://github.com/eclipse-sirius/sirius-web/issues/2163[#2163] [form] Make EMF default form support non changeable features - https://github.com/eclipse-sirius/sirius-web/issues/4086[#4086] [form] Wrap widget returned by property section in a div with a specific classname +- https://github.com/eclipse-sirius/sirius-web/issues/4088[#4088] Change tree item context menu entries internal management == v2024.9.0 diff --git a/packages/forms/backend/sirius-components-collaborative-widget-reference/src/main/java/org/eclipse/sirius/components/collaborative/widget/reference/browser/ModelBrowsersDescriptionProvider.java b/packages/forms/backend/sirius-components-collaborative-widget-reference/src/main/java/org/eclipse/sirius/components/collaborative/widget/reference/browser/ModelBrowsersDescriptionProvider.java index 5c36cb20bb..7b43b674e4 100644 --- a/packages/forms/backend/sirius-components-collaborative-widget-reference/src/main/java/org/eclipse/sirius/components/collaborative/widget/reference/browser/ModelBrowsersDescriptionProvider.java +++ b/packages/forms/backend/sirius-components-collaborative-widget-reference/src/main/java/org/eclipse/sirius/components/collaborative/widget/reference/browser/ModelBrowsersDescriptionProvider.java @@ -140,7 +140,6 @@ private TreeDescription getModelBrowserDescription(String descriptionId, Functio .treeItemObjectProvider(this::getTreeItemObject) .parentObjectProvider(this::getParentObject) .treeItemLabelProvider(this::getLabel) - .contextMenuEntries(List.of()) .build(); } @@ -280,8 +279,7 @@ private StyledString getLabel(VariableManager variableManager) { StyledString styledString = this.objectService.getStyledLabel(self); if (!styledString.toString().isBlank()) { return styledString; - } - else { + } else { var kind = this.objectService.getKind(self); label = this.urlParser.getParameterValues(kind).get(SemanticKindConstants.ENTITY_ARGUMENT).get(0); } diff --git a/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/explorer/services/ExplorerDescriptionProvider.java b/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/explorer/services/ExplorerDescriptionProvider.java index f24dad3fa5..abbfc6bb88 100644 --- a/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/explorer/services/ExplorerDescriptionProvider.java +++ b/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/explorer/services/ExplorerDescriptionProvider.java @@ -122,7 +122,6 @@ public List getRepresentationDescriptions(IEditingCo .renameHandler(this::getRenameHandler) .treeItemObjectProvider(this::getTreeItemObject) .treeItemLabelProvider(this::getLabel) - .contextMenuEntries(List.of()) .build(); return List.of(explorerTreeDescription); } diff --git a/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/tree/DomainExplorerRepresentationDescriptionProvider.java b/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/tree/DomainExplorerRepresentationDescriptionProvider.java index 988ee6e0f2..038ebb2a5e 100644 --- a/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/tree/DomainExplorerRepresentationDescriptionProvider.java +++ b/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/tree/DomainExplorerRepresentationDescriptionProvider.java @@ -56,7 +56,6 @@ public List getRepresentationDescriptions(IEditingCo .canCreatePredicate(variableManager -> false) .targetObjectIdProvider(variableManager -> variableManager.get(IEditingContext.EDITING_CONTEXT, IEditingContext.class).map(IEditingContext::getId).orElse(null)) .elementsProvider(this::getElements) - .contextMenuEntries(List.of()) .build(); return List.of(explorerDescription); diff --git a/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/tree/DomainTreeRepresentationDescriptionProvider.java b/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/tree/DomainTreeRepresentationDescriptionProvider.java index b71ebb46bf..8602076442 100644 --- a/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/tree/DomainTreeRepresentationDescriptionProvider.java +++ b/packages/sirius-web/backend/sirius-web-application/src/main/java/org/eclipse/sirius/web/application/views/tree/DomainTreeRepresentationDescriptionProvider.java @@ -102,7 +102,6 @@ public List getRepresentationDescriptions(IEditingCo .deleteHandler(variableManager -> new Success()) .renameHandler((variableManager, newName) -> new Success()) .treeItemObjectProvider(this::getTreeItemObject) - .contextMenuEntries(List.of()) .build(); return List.of(treeDescription); } diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/api/IFetchTreeItemContextMenuEntryDataProvider.java b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/api/IFetchTreeItemContextMenuEntryDataProvider.java new file mode 100644 index 0000000000..810052fcaf --- /dev/null +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/api/IFetchTreeItemContextMenuEntryDataProvider.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2024 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 + * 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.trees.api; + +import org.eclipse.sirius.components.collaborative.trees.dto.FetchTreeItemContextMenuEntryData; +import org.eclipse.sirius.components.core.api.IEditingContext; +import org.eclipse.sirius.components.trees.Tree; +import org.eclipse.sirius.components.trees.TreeItem; +import org.eclipse.sirius.components.trees.description.TreeDescription; + +/** + * Interface allowing to provide the data of fetch context menu entries necessary to perform the fetch action itself. + * + * @author Jerome Gout + */ +public interface IFetchTreeItemContextMenuEntryDataProvider { + + boolean canHandle(TreeDescription treeDescription); + + FetchTreeItemContextMenuEntryData handle(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem, String treeItemMenuContextEntryId); +} diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/api/ISingleClickTreeItemContextMenuEntryExecutor.java b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/api/ISingleClickTreeItemContextMenuEntryExecutor.java new file mode 100644 index 0000000000..d5d6b46de3 --- /dev/null +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/api/ISingleClickTreeItemContextMenuEntryExecutor.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2024 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 + * 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.trees.api; + +import org.eclipse.sirius.components.core.api.IEditingContext; +import org.eclipse.sirius.components.representations.IStatus; +import org.eclipse.sirius.components.trees.Tree; +import org.eclipse.sirius.components.trees.TreeItem; +import org.eclipse.sirius.components.trees.description.TreeDescription; + +/** + * Interface allowing to perform single click context menu entries. + * + * @author Jerome Gout + */ +public interface ISingleClickTreeItemContextMenuEntryExecutor { + + boolean canExecute(TreeDescription treeDescription); + + IStatus execute(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem, String treeItemMenuContextEntryId); +} diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/api/ITreeItemContextMenuEntryProvider.java b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/api/ITreeItemContextMenuEntryProvider.java new file mode 100644 index 0000000000..167e2dedc1 --- /dev/null +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/api/ITreeItemContextMenuEntryProvider.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2024 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 + * 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.trees.api; + +import java.util.List; + +import org.eclipse.sirius.components.collaborative.trees.dto.ITreeItemContextMenuEntry; +import org.eclipse.sirius.components.core.api.IEditingContext; +import org.eclipse.sirius.components.trees.Tree; +import org.eclipse.sirius.components.trees.TreeItem; +import org.eclipse.sirius.components.trees.description.TreeDescription; + +/** + * Interface allowing to provide context menu entries for a tree item. + * + * @author Jerome Gout + */ +public interface ITreeItemContextMenuEntryProvider { + + boolean canHandle(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem); + + List getTreeItemContextMenuEntries(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem); +} diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/FetchTreeItemContextMenuEntryDataEventHandler.java b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/FetchTreeItemContextMenuEntryDataEventHandler.java index 9b047088d3..77ef45bc2c 100644 --- a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/FetchTreeItemContextMenuEntryDataEventHandler.java +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/FetchTreeItemContextMenuEntryDataEventHandler.java @@ -12,12 +12,14 @@ *******************************************************************************/ package org.eclipse.sirius.components.collaborative.trees.handlers; +import java.util.List; import java.util.Objects; import java.util.Optional; import org.eclipse.sirius.components.collaborative.api.ChangeDescription; import org.eclipse.sirius.components.collaborative.api.ChangeKind; import org.eclipse.sirius.components.collaborative.api.Monitoring; +import org.eclipse.sirius.components.collaborative.trees.api.IFetchTreeItemContextMenuEntryDataProvider; import org.eclipse.sirius.components.collaborative.trees.api.ITreeEventHandler; import org.eclipse.sirius.components.collaborative.trees.api.ITreeInput; import org.eclipse.sirius.components.collaborative.trees.dto.FetchTreeItemContextMenuEntryData; @@ -28,10 +30,6 @@ import org.eclipse.sirius.components.core.api.ErrorPayload; import org.eclipse.sirius.components.core.api.IEditingContext; import org.eclipse.sirius.components.core.api.IPayload; -import org.eclipse.sirius.components.representations.VariableManager; -import org.eclipse.sirius.components.trees.FetchTreeItemContextMenuEntry; -import org.eclipse.sirius.components.trees.FetchTreeItemContextMenuEntryKind; -import org.eclipse.sirius.components.trees.ITreeItemContextMenuEntry; import org.eclipse.sirius.components.trees.Tree; import org.eclipse.sirius.components.trees.TreeItem; import org.eclipse.sirius.components.trees.description.TreeDescription; @@ -56,10 +54,13 @@ public class FetchTreeItemContextMenuEntryDataEventHandler implements ITreeEvent private final Counter counter; + private final List fetchTreeItemContextMenuEntryDataProviders; + public FetchTreeItemContextMenuEntryDataEventHandler(ICollaborativeTreeMessageService messageService, ITreeQueryService treeQueryService, - MeterRegistry meterRegistry) { + MeterRegistry meterRegistry, List fetchTreeItemContextMenuEntryDataProviders) { this.messageService = Objects.requireNonNull(messageService); this.treeQueryService = Objects.requireNonNull(treeQueryService); + this.fetchTreeItemContextMenuEntryDataProviders = Objects.requireNonNull(fetchTreeItemContextMenuEntryDataProviders); this.counter = Counter.builder(Monitoring.EVENT_HANDLER) .tag(Monitoring.NAME, this.getClass().getSimpleName()) @@ -86,21 +87,11 @@ public void handle(One payloadSink, Many changeDesc if (optionalTreeItem.isPresent()) { TreeItem treeItem = optionalTreeItem.get(); - var variableManager = new VariableManager(); - variableManager.put(IEditingContext.EDITING_CONTEXT, editingContext); - variableManager.put(TreeDescription.TREE, tree); - variableManager.put(TreeItem.SELECTED_TREE_ITEM, treeItem); - variableManager.put(TreeDescription.ID, input.treeItemId()); - var semanticTreeItemObject = treeDescription.getTreeItemObjectProvider().apply(variableManager); - variableManager.put(VariableManager.SELF, semanticTreeItemObject); - - Optional action = treeDescription.getContextMenuEntries().stream() - .filter(a -> Objects.equals(a.getId(), input.menuEntryId())) + Optional metadata = this.fetchTreeItemContextMenuEntryDataProviders.stream() + .filter(provider -> provider.canHandle(treeDescription)) + .map(provider -> provider.handle(editingContext, treeDescription, tree, treeItem, input.menuEntryId())) + .filter(Objects::nonNull) .findFirst(); - Optional metadata = action - .filter(FetchTreeItemContextMenuEntry.class::isInstance) - .map(FetchTreeItemContextMenuEntry.class::cast) - .map(fetchTreeItemContextMenuEntry -> this.getFetchMetadata(fetchTreeItemContextMenuEntry, variableManager)); if (metadata.isPresent()) { payload = new FetchTreeItemContextMenuEntryDataSuccessPayload(treeInput.id(), metadata.get()); } @@ -110,11 +101,4 @@ public void handle(One payloadSink, Many changeDesc changeDescriptionSink.tryEmitNext(changeDescription); payloadSink.tryEmitValue(payload); } - - private FetchTreeItemContextMenuEntryData getFetchMetadata(FetchTreeItemContextMenuEntry fetchAction, VariableManager variableManager) { - String urlToFetch = fetchAction.getUrlToFetch().apply(variableManager); - FetchTreeItemContextMenuEntryKind fetchKind = fetchAction.getFetchKind().apply(variableManager); - return new FetchTreeItemContextMenuEntryData(urlToFetch, fetchKind); - } - } diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/InvokeSingleClickTreeItemContextMenuEntryEventHandler.java b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/InvokeSingleClickTreeItemContextMenuEntryEventHandler.java index fab89a760b..9ee1142430 100644 --- a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/InvokeSingleClickTreeItemContextMenuEntryEventHandler.java +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/InvokeSingleClickTreeItemContextMenuEntryEventHandler.java @@ -12,11 +12,13 @@ *******************************************************************************/ package org.eclipse.sirius.components.collaborative.trees.handlers; +import java.util.List; import java.util.Objects; import org.eclipse.sirius.components.collaborative.api.ChangeDescription; import org.eclipse.sirius.components.collaborative.api.ChangeKind; import org.eclipse.sirius.components.collaborative.api.Monitoring; +import org.eclipse.sirius.components.collaborative.trees.api.ISingleClickTreeItemContextMenuEntryExecutor; import org.eclipse.sirius.components.collaborative.trees.api.ITreeEventHandler; import org.eclipse.sirius.components.collaborative.trees.api.ITreeInput; import org.eclipse.sirius.components.collaborative.trees.dto.InvokeSingleClickTreeItemContextMenuEntryInput; @@ -28,10 +30,8 @@ import org.eclipse.sirius.components.core.api.SuccessPayload; import org.eclipse.sirius.components.representations.Failure; import org.eclipse.sirius.components.representations.Success; -import org.eclipse.sirius.components.representations.VariableManager; import org.eclipse.sirius.components.trees.Tree; import org.eclipse.sirius.components.trees.TreeItem; -import org.eclipse.sirius.components.trees.SingleClickTreeItemContextMenuEntry; import org.eclipse.sirius.components.trees.description.TreeDescription; import org.springframework.stereotype.Service; @@ -54,10 +54,13 @@ public class InvokeSingleClickTreeItemContextMenuEntryEventHandler implements IT private final Counter counter; + private final List singleClickTreeItemContextMenuEntryExecutors; + public InvokeSingleClickTreeItemContextMenuEntryEventHandler(ICollaborativeTreeMessageService messageService, ITreeQueryService treeQueryService, - MeterRegistry meterRegistry) { + MeterRegistry meterRegistry, List singleClickTreeItemContextMenuEntryExecutors) { this.messageService = Objects.requireNonNull(messageService); this.treeQueryService = Objects.requireNonNull(treeQueryService); + this.singleClickTreeItemContextMenuEntryExecutors = Objects.requireNonNull(singleClickTreeItemContextMenuEntryExecutors); this.counter = Counter.builder(Monitoring.EVENT_HANDLER) .tag(Monitoring.NAME, this.getClass().getSimpleName()) @@ -78,33 +81,22 @@ public void handle(One payloadSink, Many changeDesc ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, treeInput.representationId(), treeInput); if (treeInput instanceof InvokeSingleClickTreeItemContextMenuEntryInput input) { - var optionalTreeItem = this.treeQueryService.findTreeItem(tree, input.treeItemId()); if (optionalTreeItem.isPresent()) { TreeItem treeItem = optionalTreeItem.get(); - VariableManager variableManager = new VariableManager(); - variableManager.put(IEditingContext.EDITING_CONTEXT, editingContext); - variableManager.put(TreeItem.SELECTED_TREE_ITEM, treeItem); - variableManager.put(TreeDescription.ID, input.treeItemId()); - variableManager.put(TreeDescription.TREE, tree); - var semanticTreeItemObject = treeDescription.getTreeItemObjectProvider().apply(variableManager); - variableManager.put(VariableManager.SELF, semanticTreeItemObject); - - var optionalAction = treeDescription.getContextMenuEntries().stream() - .filter(action -> Objects.equals(action.getId(), input.menuEntryId())) - .filter(SingleClickTreeItemContextMenuEntry.class::isInstance) - .map(SingleClickTreeItemContextMenuEntry.class::cast) - .findFirst(); - if (optionalAction.isPresent()) { - var status = optionalAction.get().getHandler().apply(variableManager); - if (status instanceof Success success) { - changeDescription = new ChangeDescription(success.getChangeKind(), treeInput.representationId(), treeInput, success.getParameters()); - payload = new SuccessPayload(treeInput.id()); - } else if (status instanceof Failure failure) { - payload = new ErrorPayload(treeInput.id(), failure.getMessages()); - } + var status = this.singleClickTreeItemContextMenuEntryExecutors.stream() + .filter(executor -> executor.canExecute(treeDescription)) + .findFirst() + .map(executor -> executor.execute(editingContext, treeDescription, tree, treeItem, input.menuEntryId())) + .orElseGet(() -> new Failure(this.messageService.noSingleClickTreeItemExecutor())); + + if (status instanceof Success success) { + changeDescription = new ChangeDescription(success.getChangeKind(), treeInput.representationId(), treeInput, success.getParameters()); + payload = new SuccessPayload(treeInput.id()); + } else if (status instanceof Failure failure) { + payload = new ErrorPayload(treeInput.id(), failure.getMessages()); } } } diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/TreeItemContextMenuEventHandler.java b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/TreeItemContextMenuEventHandler.java index 188a434a53..3c6703797d 100644 --- a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/TreeItemContextMenuEventHandler.java +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/handlers/TreeItemContextMenuEventHandler.java @@ -21,6 +21,8 @@ import org.eclipse.sirius.components.collaborative.api.Monitoring; import org.eclipse.sirius.components.collaborative.trees.api.ITreeEventHandler; import org.eclipse.sirius.components.collaborative.trees.api.ITreeInput; +import org.eclipse.sirius.components.collaborative.trees.api.ITreeItemContextMenuEntryProvider; +import org.eclipse.sirius.components.collaborative.trees.dto.ITreeItemContextMenuEntry; import org.eclipse.sirius.components.collaborative.trees.dto.TreeItemContextMenuInput; import org.eclipse.sirius.components.collaborative.trees.dto.TreeItemContextMenuSuccessPayload; import org.eclipse.sirius.components.collaborative.trees.services.api.ICollaborativeTreeMessageService; @@ -28,10 +30,6 @@ import org.eclipse.sirius.components.core.api.ErrorPayload; import org.eclipse.sirius.components.core.api.IEditingContext; import org.eclipse.sirius.components.core.api.IPayload; -import org.eclipse.sirius.components.representations.VariableManager; -import org.eclipse.sirius.components.trees.FetchTreeItemContextMenuEntry; -import org.eclipse.sirius.components.trees.ITreeItemContextMenuEntry; -import org.eclipse.sirius.components.trees.SingleClickTreeItemContextMenuEntry; import org.eclipse.sirius.components.trees.Tree; import org.eclipse.sirius.components.trees.TreeItem; import org.eclipse.sirius.components.trees.description.TreeDescription; @@ -56,10 +54,13 @@ public class TreeItemContextMenuEventHandler implements ITreeEventHandler { private final Counter counter; + private final List contextMenuEntryProviders; + public TreeItemContextMenuEventHandler(ICollaborativeTreeMessageService messageService, ITreeQueryService treeQueryService, - MeterRegistry meterRegistry) { + MeterRegistry meterRegistry, List contextMenuEntryProviders) { this.messageService = Objects.requireNonNull(messageService); this.treeQueryService = Objects.requireNonNull(treeQueryService); + this.contextMenuEntryProviders = Objects.requireNonNull(contextMenuEntryProviders); this.counter = Counter.builder(Monitoring.EVENT_HANDLER) .tag(Monitoring.NAME, this.getClass().getSimpleName()) @@ -80,45 +81,22 @@ public void handle(One payloadSink, Many changeDesc ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, treeInput.representationId(), treeInput); if (treeInput instanceof TreeItemContextMenuInput input) { - var optionalTreeItem = this.treeQueryService.findTreeItem(tree, input.treeItemId()); - if (optionalTreeItem.isPresent()) { TreeItem treeItem = optionalTreeItem.get(); - var variableManager = new VariableManager(); - variableManager.put(IEditingContext.EDITING_CONTEXT, editingContext); - variableManager.put(TreeDescription.TREE, tree); - variableManager.put(TreeItem.SELECTED_TREE_ITEM, treeItem); - variableManager.put(TreeDescription.ID, input.treeItemId()); - var semanticTreeItemObject = treeDescription.getTreeItemObjectProvider().apply(variableManager); - variableManager.put(VariableManager.SELF, semanticTreeItemObject); - - var candidates = treeDescription.getContextMenuEntries().stream() - .filter(action -> action.getPrecondition().apply(variableManager)) - .map(treeItemContextMenuEntry -> this.convertTreeItemContextAction(treeItemContextMenuEntry, variableManager)) + var entries = this.contextMenuEntryProviders.stream() + .filter(provider -> provider.canHandle(editingContext, treeDescription, tree, treeItem)) + .flatMap(provider -> provider.getTreeItemContextMenuEntries(editingContext, treeDescription, tree, treeItem).stream()) .filter(Objects::nonNull) - .sorted(Comparator.comparing(org.eclipse.sirius.components.collaborative.trees.dto.ITreeItemContextMenuEntry::label)) + .sorted(Comparator.comparing(ITreeItemContextMenuEntry::label)) .toList(); - payload = new TreeItemContextMenuSuccessPayload(treeInput.id(), candidates); + payload = new TreeItemContextMenuSuccessPayload(treeInput.id(), entries); } } changeDescriptionSink.tryEmitNext(changeDescription); payloadSink.tryEmitValue(payload); } - - private org.eclipse.sirius.components.collaborative.trees.dto.ITreeItemContextMenuEntry convertTreeItemContextAction(ITreeItemContextMenuEntry treeItemContextMenuEntry, VariableManager variableManager) { - org.eclipse.sirius.components.collaborative.trees.dto.ITreeItemContextMenuEntry result = null; - String label = treeItemContextMenuEntry.getLabel().apply(variableManager); - List iconURL = treeItemContextMenuEntry.getIconURL().apply(variableManager); - if (treeItemContextMenuEntry instanceof SingleClickTreeItemContextMenuEntry simpleAction) { - result = new org.eclipse.sirius.components.collaborative.trees.dto.SingleClickTreeItemContextMenuEntry(simpleAction.getId(), label, iconURL); - } else if (treeItemContextMenuEntry instanceof FetchTreeItemContextMenuEntry fetchAction) { - result = new org.eclipse.sirius.components.collaborative.trees.dto.FetchTreeItemContextMenuEntry(fetchAction.getId(), label, iconURL); - } - return result; - } - } diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/services/CollaborativeTreeMessageService.java b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/services/CollaborativeTreeMessageService.java index 74b2023221..6d008215b7 100644 --- a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/services/CollaborativeTreeMessageService.java +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/services/CollaborativeTreeMessageService.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2021, 2022 Obeo. + * Copyright (c) 2021, 2024 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 @@ -26,6 +26,7 @@ */ @Service public class CollaborativeTreeMessageService implements ICollaborativeTreeMessageService { + private final MessageSourceAccessor messageSourceAccessor; public CollaborativeTreeMessageService(@Qualifier("collaborativeTreeMessageSourceAccessor") MessageSourceAccessor messageSourceAccessor) { @@ -36,4 +37,9 @@ public CollaborativeTreeMessageService(@Qualifier("collaborativeTreeMessageSourc public String invalidInput(String expectedInputTypeName, String receivedInputTypeName) { return this.messageSourceAccessor.getMessage("INVALID_INPUT", new Object[] { expectedInputTypeName, receivedInputTypeName }); } + + @Override + public String noSingleClickTreeItemExecutor() { + return this.messageSourceAccessor.getMessage("NO_SINGLE_CLICK_TREE_ITEM_EXECUTOR"); + } } diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/services/api/ICollaborativeTreeMessageService.java b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/services/api/ICollaborativeTreeMessageService.java index 7cb0c4094f..3f98563d41 100644 --- a/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/services/api/ICollaborativeTreeMessageService.java +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/main/java/org/eclipse/sirius/components/collaborative/trees/services/api/ICollaborativeTreeMessageService.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2021, 2022 Obeo. + * Copyright (c) 2021, 2024 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 @@ -18,8 +18,11 @@ * @author sbegaudeau */ public interface ICollaborativeTreeMessageService { + String invalidInput(String expectedInputTypeName, String receivedInputTypeName); + String noSingleClickTreeItemExecutor(); + /** * Implementation which does nothing, used for mocks in unit tests. * @@ -32,5 +35,10 @@ public String invalidInput(String expectedInputTypeName, String receivedInputTyp return ""; } + @Override + public String noSingleClickTreeItemExecutor() { + return ""; + } + } } diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/main/resources/messages/sirius-web-spring-collaborative-trees.properties b/packages/trees/backend/sirius-components-collaborative-trees/src/main/resources/messages/sirius-web-spring-collaborative-trees.properties index 91a3a29bb2..37e76430f2 100644 --- a/packages/trees/backend/sirius-components-collaborative-trees/src/main/resources/messages/sirius-web-spring-collaborative-trees.properties +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/main/resources/messages/sirius-web-spring-collaborative-trees.properties @@ -1,5 +1,5 @@ -################################################################################################ -# Copyright (c) 2021 Obeo. +################################################################################ +# Copyright (c) 2021, 2024 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 @@ -9,5 +9,6 @@ # # Contributors: # Obeo - initial API and implementation -################################################################################################ +################################################################################# INVALID_INPUT=Invalid input type, "{0}" has been received while "{1}" was expected +NO_SINGLE_CLICK_TREE_ITEM_EXECUTOR=No existing action for this entry menu diff --git a/packages/trees/backend/sirius-components-collaborative-trees/src/test/java/org/eclipse/sirius/components/collaborative/trees/architecture/handlers/DefaultExpandAllTreePathHandlerTests.java b/packages/trees/backend/sirius-components-collaborative-trees/src/test/java/org/eclipse/sirius/components/collaborative/trees/architecture/handlers/DefaultExpandAllTreePathHandlerTests.java index 4eea9eed0e..9b38f6afd6 100644 --- a/packages/trees/backend/sirius-components-collaborative-trees/src/test/java/org/eclipse/sirius/components/collaborative/trees/architecture/handlers/DefaultExpandAllTreePathHandlerTests.java +++ b/packages/trees/backend/sirius-components-collaborative-trees/src/test/java/org/eclipse/sirius/components/collaborative/trees/architecture/handlers/DefaultExpandAllTreePathHandlerTests.java @@ -71,7 +71,6 @@ public Optional findById(IEditingContext editingCont .treeItemIdProvider(v -> "itemId") .treeItemObjectProvider(v -> new Object()) .treeItemLabelProvider(v -> null) - .contextMenuEntries(List.of()) .build(); return Optional.of(diagramDescription); diff --git a/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/FetchTreeItemContextMenuEntry.java b/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/FetchTreeItemContextMenuEntry.java deleted file mode 100644 index 05dd8f543d..0000000000 --- a/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/FetchTreeItemContextMenuEntry.java +++ /dev/null @@ -1,138 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2024 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 - * 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.trees; - -import java.util.List; -import java.util.Objects; -import java.util.function.Function; - -import org.eclipse.sirius.components.annotations.Immutable; -import org.eclipse.sirius.components.representations.VariableManager; - -/** - * Fetch context menu entry performed only in the front end side. - * - * @author Jerome Gout - */ -@Immutable -public final class FetchTreeItemContextMenuEntry implements ITreeItemContextMenuEntry { - - private String id; - - private Function label; - - private Function> iconURL; - - private Function precondition; - - private Function urlToFetch; - - private Function fetchKind; - - private FetchTreeItemContextMenuEntry() { - // Prevent instantiation - } - - @Override - public String getId() { - return this.id; - } - - @Override - public Function getLabel() { - return this.label; - } - - @Override - public Function> getIconURL() { - return this.iconURL; - } - - @Override - public Function getPrecondition() { - return this.precondition; - } - - public Function getUrlToFetch() { - return this.urlToFetch; - } - - public Function getFetchKind() { - return this.fetchKind; - } - - public static Builder newFetchTreeItemContextMenuEntry(String id) { - return new Builder(id); - } - - /** - * The builder used to create the fetch tree item context action. - * - * @author Jerome Gout - */ - @SuppressWarnings("checkstyle:HiddenField") - public static final class Builder { - - private String id; - - private Function label; - - private Function> iconURL; - - private Function precondition; - - private Function urlToFetch; - - private Function fetchKind; - - public Builder(String id) { - this.id = Objects.requireNonNull(id); - } - - public Builder label(Function label) { - this.label = label; - return this; - } - - public Builder iconURL(Function> iconURL) { - this.iconURL = iconURL; - return this; - } - - public Builder precondition(Function precondition) { - this.precondition = precondition; - return this; - } - - public Builder urlToFetch(Function urlToFetch) { - this.urlToFetch = urlToFetch; - return this; - } - - public Builder fetchKind(Function fetchKind) { - this.fetchKind = fetchKind; - return this; - } - - public FetchTreeItemContextMenuEntry build() { - var action = new FetchTreeItemContextMenuEntry(); - action.id = Objects.requireNonNull(this.id); - action.label = Objects.requireNonNull(this.label); - action.iconURL = Objects.requireNonNull(this.iconURL); - action.precondition = Objects.requireNonNull(this.precondition); - action.urlToFetch = Objects.requireNonNull(this.urlToFetch); - action.fetchKind = Objects.requireNonNull(this.fetchKind); - return action; - } - } -} diff --git a/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/ITreeItemContextMenuEntry.java b/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/ITreeItemContextMenuEntry.java deleted file mode 100644 index 57758a1f2f..0000000000 --- a/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/ITreeItemContextMenuEntry.java +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2024 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 - * 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.trees; - -import java.util.List; -import java.util.function.Function; - -import org.eclipse.sirius.components.representations.VariableManager; - -/** - * Interface implemented by all the tree item context menu entries. - * - * @author Jerome Gout - */ -public interface ITreeItemContextMenuEntry { - String getId(); - - Function getLabel(); - - Function> getIconURL(); - - Function getPrecondition(); - -} diff --git a/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/SingleClickTreeItemContextMenuEntry.java b/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/SingleClickTreeItemContextMenuEntry.java deleted file mode 100644 index c50ee61ab5..0000000000 --- a/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/SingleClickTreeItemContextMenuEntry.java +++ /dev/null @@ -1,125 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2024 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 - * 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.trees; - -import java.util.List; -import java.util.Objects; -import java.util.function.Function; - -import org.eclipse.sirius.components.annotations.Immutable; -import org.eclipse.sirius.components.representations.IStatus; -import org.eclipse.sirius.components.representations.VariableManager; - -/** - * Single click tree item context menu entry. - * - * @author Jerome Gout - */ -@Immutable -public final class SingleClickTreeItemContextMenuEntry implements ITreeItemContextMenuEntry { - - private String id; - - private Function label; - - private Function> iconURL; - - private Function precondition; - - private Function handler; - - private SingleClickTreeItemContextMenuEntry() { - // Prevent instantiation - } - - @Override - public String getId() { - return this.id; - } - - @Override - public Function getLabel() { - return this.label; - } - - @Override - public Function> getIconURL() { - return this.iconURL; - } - - @Override - public Function getPrecondition() { - return this.precondition; - } - - public Function getHandler() { - return this.handler; - } - - public static Builder newSingleClickTreeItemContextMenuEntry(String id) { - return new Builder(id); - } - - /** - * The builder used to create the simple tree item context action. - * - * @author Jerome Gout - */ - @SuppressWarnings("checkstyle:HiddenField") - public static final class Builder { - - private String id; - - private Function label; - - private Function> iconURL; - - private Function precondition; - - private Function handler; - - public Builder(String id) { - this.id = Objects.requireNonNull(id); - } - - public Builder label(Function label) { - this.label = label; - return this; - } - - public Builder iconURL(Function> iconURL) { - this.iconURL = iconURL; - return this; - } - - public Builder precondition(Function precondition) { - this.precondition = precondition; - return this; - } - - public Builder handler(Function handler) { - this.handler = handler; - return this; - } - - public SingleClickTreeItemContextMenuEntry build() { - SingleClickTreeItemContextMenuEntry action = new SingleClickTreeItemContextMenuEntry(); - action.id = Objects.requireNonNull(this.id); - action.label = Objects.requireNonNull(this.label); - action.iconURL = Objects.requireNonNull(this.iconURL); - action.precondition = Objects.requireNonNull(this.precondition); - action.handler = Objects.requireNonNull(this.handler); - return action; - } - } -} diff --git a/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/description/TreeDescription.java b/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/description/TreeDescription.java index dc4d2e019c..129476acad 100644 --- a/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/description/TreeDescription.java +++ b/packages/trees/backend/sirius-components-trees/src/main/java/org/eclipse/sirius/components/trees/description/TreeDescription.java @@ -24,7 +24,6 @@ import org.eclipse.sirius.components.representations.IRepresentationDescription; import org.eclipse.sirius.components.representations.IStatus; import org.eclipse.sirius.components.representations.VariableManager; -import org.eclipse.sirius.components.trees.ITreeItemContextMenuEntry; /** * The root concept of the description of a tree representation. @@ -50,8 +49,6 @@ public final class TreeDescription implements IRepresentationDescription { private String label; - private List contextMenuEntries; - private Function idProvider; private Function treeItemIdProvider; @@ -110,10 +107,6 @@ public String getLabel() { return this.label; } - public List getContextMenuEntries() { - return this.contextMenuEntries; - } - public Function getIdProvider() { return this.idProvider; } @@ -205,8 +198,6 @@ public static final class Builder { private String label; - private List contextMenuEntries; - private Function idProvider; private Function treeItemIdProvider; @@ -250,7 +241,6 @@ private Builder(String id) { private Builder(TreeDescription treeDescription) { this.id = treeDescription.getId(); this.label = treeDescription.getLabel(); - this.contextMenuEntries = treeDescription.getContextMenuEntries(); this.idProvider = treeDescription.getIdProvider(); this.treeItemIdProvider = treeDescription.getTreeItemIdProvider(); this.kindProvider = treeDescription.getKindProvider(); @@ -281,11 +271,6 @@ public Builder label(String label) { return this; } - public Builder contextMenuEntries(List contextMenuEntries) { - this.contextMenuEntries = contextMenuEntries; - return this; - } - public Builder idProvider(Function idProvider) { this.idProvider = Objects.requireNonNull(idProvider); return this; @@ -380,7 +365,6 @@ public TreeDescription build() { TreeDescription treeDescription = new TreeDescription(); treeDescription.id = Objects.requireNonNull(this.id); treeDescription.label = Objects.requireNonNull(this.label); - treeDescription.contextMenuEntries = Objects.requireNonNull(this.contextMenuEntries); treeDescription.idProvider = Objects.requireNonNull(this.idProvider); treeDescription.treeItemIdProvider = Objects.requireNonNull(this.treeItemIdProvider); treeDescription.kindProvider = Objects.requireNonNull(this.kindProvider); diff --git a/packages/trees/backend/sirius-components-trees/src/test/java/org/eclipse/sirius/components/trees/renderer/TreeRendererTests.java b/packages/trees/backend/sirius-components-trees/src/test/java/org/eclipse/sirius/components/trees/renderer/TreeRendererTests.java index d2c93fd7e2..c36b831f0b 100644 --- a/packages/trees/backend/sirius-components-trees/src/test/java/org/eclipse/sirius/components/trees/renderer/TreeRendererTests.java +++ b/packages/trees/backend/sirius-components-trees/src/test/java/org/eclipse/sirius/components/trees/renderer/TreeRendererTests.java @@ -163,7 +163,6 @@ private TreeDescription createDescription(TreeNode root) { .treeItemIdProvider(v -> this.getSelfNode(v).getId()) .treeItemObjectProvider(v -> root.search(v.get("treeItemId", String.class).get(), new HashSet<>())) .treeItemLabelProvider(v -> StyledString.of(v.get(VariableManager.SELF, TreeNode.class).map(TreeNode::getId).orElse(FAKE_ID))) - .contextMenuEntries(List.of()) .build(); } diff --git a/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/diagram/SelectionDialogDescriptionConverter.java b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/diagram/SelectionDialogDescriptionConverter.java index 8347ec18db..28e0cc0183 100644 --- a/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/diagram/SelectionDialogDescriptionConverter.java +++ b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/diagram/SelectionDialogDescriptionConverter.java @@ -217,7 +217,6 @@ private TreeDescription createTreeDescription(SelectionDialogDescription selecti .treeItemObjectProvider(this::getTreeItemObject) .parentObjectProvider(this::getParentObject) .treeItemLabelProvider(labelProvider) - .contextMenuEntries(List.of()) .build(); } diff --git a/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewFetchTreeItemContextMenuEntryDataProvider.java b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewFetchTreeItemContextMenuEntryDataProvider.java new file mode 100644 index 0000000000..0ca4a4a7c4 --- /dev/null +++ b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewFetchTreeItemContextMenuEntryDataProvider.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * Copyright (c) 2024 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 + * 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.view.emf.tree; + +import java.util.Objects; +import java.util.UUID; +import java.util.function.Function; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.sirius.components.collaborative.trees.api.IFetchTreeItemContextMenuEntryDataProvider; +import org.eclipse.sirius.components.collaborative.trees.dto.FetchTreeItemContextMenuEntryData; +import org.eclipse.sirius.components.core.api.IEditingContext; +import org.eclipse.sirius.components.interpreter.AQLInterpreter; +import org.eclipse.sirius.components.representations.VariableManager; +import org.eclipse.sirius.components.trees.FetchTreeItemContextMenuEntryKind; +import org.eclipse.sirius.components.trees.Tree; +import org.eclipse.sirius.components.trees.TreeItem; +import org.eclipse.sirius.components.trees.description.TreeDescription; +import org.eclipse.sirius.components.view.View; +import org.eclipse.sirius.components.view.emf.IViewRepresentationDescriptionSearchService; +import org.eclipse.sirius.components.view.emf.ViewRepresentationDescriptionPredicate; +import org.eclipse.sirius.components.view.emf.api.IViewAQLInterpreterFactory; +import org.eclipse.sirius.components.view.tree.FetchTreeItemContextMenuEntry; +import org.springframework.stereotype.Service; + +/** + * Fetch tree item context menu entry data provider for view tree model. + * + * @author Jerome Gout + */ +@Service +public class ViewFetchTreeItemContextMenuEntryDataProvider implements IFetchTreeItemContextMenuEntryDataProvider { + + private final ViewRepresentationDescriptionPredicate viewRepresentationDescriptionPredicate; + + private final IViewRepresentationDescriptionSearchService viewRepresentationDescriptionSearchService; + + private final IViewAQLInterpreterFactory aqlInterpreterFactory; + + private final Function idProvider = (eObject) -> UUID.nameUUIDFromBytes(EcoreUtil.getURI(eObject).toString().getBytes()); + + public ViewFetchTreeItemContextMenuEntryDataProvider(ViewRepresentationDescriptionPredicate viewRepresentationDescriptionPredicate, IViewRepresentationDescriptionSearchService viewRepresentationDescriptionSearchService, IViewAQLInterpreterFactory aqlInterpreterFactory) { + this.viewRepresentationDescriptionPredicate = Objects.requireNonNull(viewRepresentationDescriptionPredicate); + this.viewRepresentationDescriptionSearchService = Objects.requireNonNull(viewRepresentationDescriptionSearchService); + this.aqlInterpreterFactory = Objects.requireNonNull(aqlInterpreterFactory); + } + + @Override + public boolean canHandle(TreeDescription treeDescription) { + return this.viewRepresentationDescriptionPredicate.test(treeDescription); + } + + @Override + public FetchTreeItemContextMenuEntryData handle(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem, String treeItemMenuContextEntryId) { + var optionalTreeDescription = this.viewRepresentationDescriptionSearchService + .findById(editingContext, treeDescription.getId()) + .filter(org.eclipse.sirius.components.view.tree.TreeDescription.class::isInstance) + .map(org.eclipse.sirius.components.view.tree.TreeDescription.class::cast); + if (optionalTreeDescription.isPresent()) { + var viewTreeDescription = optionalTreeDescription.get(); + + AQLInterpreter interpreter = this.aqlInterpreterFactory.createInterpreter(editingContext, (View) viewTreeDescription.eContainer()); + + VariableManager variableManager = new VariableManager(); + variableManager.put(IEditingContext.EDITING_CONTEXT, editingContext); + variableManager.put(org.eclipse.sirius.components.trees.description.TreeDescription.TREE, tree); + variableManager.put(TreeItem.SELECTED_TREE_ITEM, treeItem); + variableManager.put(org.eclipse.sirius.components.trees.description.TreeDescription.ID, treeItem.getId()); + var semanticTreeItemObject = treeDescription.getTreeItemObjectProvider().apply(variableManager); + variableManager.put(VariableManager.SELF, semanticTreeItemObject); + + var fetchEntry = viewTreeDescription.getContextMenuEntries().stream() + .filter(FetchTreeItemContextMenuEntry.class::isInstance) + .map(FetchTreeItemContextMenuEntry.class::cast) + .filter(entry -> Objects.equals(treeItemMenuContextEntryId, this.idProvider.apply(entry).toString())) + .findFirst(); + if (fetchEntry.isPresent()) { + var urlToFetch = this.evaluateString(variableManager, interpreter, fetchEntry.get().getUrlExression()); + return new FetchTreeItemContextMenuEntryData(urlToFetch, this.convertFetchKind(fetchEntry.get().getKind())); + } + } + return null; + } + + private FetchTreeItemContextMenuEntryKind convertFetchKind(org.eclipse.sirius.components.view.tree.FetchTreeItemContextMenuEntryKind fetchActionKind) { + return switch (fetchActionKind) { + case DOWNLOAD -> FetchTreeItemContextMenuEntryKind.DOWNLOAD; + case OPEN -> FetchTreeItemContextMenuEntryKind.OPEN; + }; + } + + private String evaluateString(VariableManager variableManager, AQLInterpreter interpreter, String expression) { + return interpreter.evaluateExpression(variableManager.getVariables(), expression) + .asString() + .orElse(""); + } + +} diff --git a/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewSingleClickTreeItemContextMenuEntryExecutor.java b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewSingleClickTreeItemContextMenuEntryExecutor.java new file mode 100644 index 0000000000..e3b6b71541 --- /dev/null +++ b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewSingleClickTreeItemContextMenuEntryExecutor.java @@ -0,0 +1,134 @@ +/******************************************************************************* + * Copyright (c) 2024 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 + * 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.view.emf.tree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; +import java.util.function.Function; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.sirius.components.collaborative.api.ChangeKind; +import org.eclipse.sirius.components.collaborative.trees.api.ISingleClickTreeItemContextMenuEntryExecutor; +import org.eclipse.sirius.components.core.api.IEditService; +import org.eclipse.sirius.components.core.api.IEditingContext; +import org.eclipse.sirius.components.core.api.IFeedbackMessageService; +import org.eclipse.sirius.components.interpreter.AQLInterpreter; +import org.eclipse.sirius.components.representations.Failure; +import org.eclipse.sirius.components.representations.IStatus; +import org.eclipse.sirius.components.representations.Message; +import org.eclipse.sirius.components.representations.MessageLevel; +import org.eclipse.sirius.components.representations.Success; +import org.eclipse.sirius.components.representations.VariableManager; +import org.eclipse.sirius.components.trees.Tree; +import org.eclipse.sirius.components.trees.TreeItem; +import org.eclipse.sirius.components.trees.description.TreeDescription; +import org.eclipse.sirius.components.view.Operation; +import org.eclipse.sirius.components.view.View; +import org.eclipse.sirius.components.view.emf.IViewRepresentationDescriptionSearchService; +import org.eclipse.sirius.components.view.emf.OperationInterpreter; +import org.eclipse.sirius.components.view.emf.ViewRepresentationDescriptionPredicate; +import org.eclipse.sirius.components.view.emf.api.IViewAQLInterpreterFactory; +import org.eclipse.sirius.components.view.tree.SingleClickTreeItemContextMenuEntry; +import org.springframework.stereotype.Service; + +/** + * Fetch tree item context menu entry data provider for view tree model. + * + * @author Jerome Gout + */ +@Service +public class ViewSingleClickTreeItemContextMenuEntryExecutor implements ISingleClickTreeItemContextMenuEntryExecutor { + + private final IFeedbackMessageService feedbackMessageService; + + private final IEditService editService; + + private final ViewRepresentationDescriptionPredicate viewRepresentationDescriptionPredicate; + + private final IViewRepresentationDescriptionSearchService viewRepresentationDescriptionSearchService; + + private final IViewAQLInterpreterFactory aqlInterpreterFactory; + + private final Function idProvider = (eObject) -> UUID.nameUUIDFromBytes(EcoreUtil.getURI(eObject).toString().getBytes()); + + public ViewSingleClickTreeItemContextMenuEntryExecutor(IFeedbackMessageService feedbackMessageService, IEditService editService, ViewRepresentationDescriptionPredicate viewRepresentationDescriptionPredicate, IViewRepresentationDescriptionSearchService viewRepresentationDescriptionSearchService, IViewAQLInterpreterFactory aqlInterpreterFactory) { + this.feedbackMessageService = Objects.requireNonNull(feedbackMessageService); + this.editService = Objects.requireNonNull(editService); + this.viewRepresentationDescriptionPredicate = Objects.requireNonNull(viewRepresentationDescriptionPredicate); + this.viewRepresentationDescriptionSearchService = Objects.requireNonNull(viewRepresentationDescriptionSearchService); + this.aqlInterpreterFactory = Objects.requireNonNull(aqlInterpreterFactory); + } + + @Override + public boolean canExecute(TreeDescription treeDescription) { + return this.viewRepresentationDescriptionPredicate.test(treeDescription); + } + + @Override + public IStatus execute(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem, String treeItemMenuContextEntryId) { + var optionalTreeDescription = this.viewRepresentationDescriptionSearchService + .findById(editingContext, treeDescription.getId()) + .filter(org.eclipse.sirius.components.view.tree.TreeDescription.class::isInstance) + .map(org.eclipse.sirius.components.view.tree.TreeDescription.class::cast); + if (optionalTreeDescription.isPresent()) { + var viewTreeDescription = optionalTreeDescription.get(); + + AQLInterpreter interpreter = this.aqlInterpreterFactory.createInterpreter(editingContext, (View) viewTreeDescription.eContainer()); + + VariableManager variableManager = new VariableManager(); + variableManager.put(IEditingContext.EDITING_CONTEXT, editingContext); + variableManager.put(TreeDescription.TREE, tree); + variableManager.put(TreeItem.SELECTED_TREE_ITEM, treeItem); + variableManager.put(TreeDescription.ID, treeItem.getId()); + var semanticTreeItemObject = treeDescription.getTreeItemObjectProvider().apply(variableManager); + variableManager.put(VariableManager.SELF, semanticTreeItemObject); + + var singleClickEntry = viewTreeDescription.getContextMenuEntries().stream() + .filter(SingleClickTreeItemContextMenuEntry.class::isInstance) + .map(SingleClickTreeItemContextMenuEntry.class::cast) + .filter(entry -> Objects.equals(treeItemMenuContextEntryId, this.idProvider.apply(entry).toString())) + .findFirst(); + if (singleClickEntry.isPresent()) { + return this.executeOperations(variableManager, interpreter, singleClickEntry.get().getBody()); + } + } + return this.buildFailureWithFeedbackMessages("Something went wrong while handling the context menu action"); + } + + private IStatus executeOperations(VariableManager variableManager, AQLInterpreter interpreter, List operations) { + OperationInterpreter operationInterpreter = new OperationInterpreter(interpreter, this.editService); + Optional optionalVariableManager = operationInterpreter.executeOperations(operations, variableManager); + if (optionalVariableManager.isEmpty()) { + return this.buildFailureWithFeedbackMessages("Something went wrong while handling the context menu action"); + } else { + return this.buildSuccessWithSemanticChangeAndFeedbackMessages(); + } + } + + private Failure buildFailureWithFeedbackMessages(String technicalMessage) { + List errorMessages = new ArrayList<>(); + errorMessages.add(new Message(technicalMessage, MessageLevel.ERROR)); + errorMessages.addAll(this.feedbackMessageService.getFeedbackMessages()); + return new Failure(errorMessages); + } + + private Success buildSuccessWithSemanticChangeAndFeedbackMessages() { + return new Success(ChangeKind.SEMANTIC_CHANGE, Map.of(), this.feedbackMessageService.getFeedbackMessages()); + } + +} diff --git a/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewTreeDescriptionConverter.java b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewTreeDescriptionConverter.java index bae893b202..f522308e88 100644 --- a/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewTreeDescriptionConverter.java +++ b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewTreeDescriptionConverter.java @@ -16,42 +16,27 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.UUID; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EObject; -import org.eclipse.sirius.components.collaborative.api.ChangeKind; import org.eclipse.sirius.components.collaborative.trees.api.IDeleteTreeItemHandler; import org.eclipse.sirius.components.collaborative.trees.api.IRenameTreeItemHandler; -import org.eclipse.sirius.components.core.api.IEditService; import org.eclipse.sirius.components.core.api.IEditingContext; -import org.eclipse.sirius.components.core.api.IFeedbackMessageService; import org.eclipse.sirius.components.core.api.labels.StyledString; import org.eclipse.sirius.components.emf.DomainClassPredicate; import org.eclipse.sirius.components.interpreter.AQLInterpreter; import org.eclipse.sirius.components.representations.Failure; import org.eclipse.sirius.components.representations.IRepresentationDescription; import org.eclipse.sirius.components.representations.IStatus; -import org.eclipse.sirius.components.representations.Message; -import org.eclipse.sirius.components.representations.MessageLevel; -import org.eclipse.sirius.components.representations.Success; import org.eclipse.sirius.components.representations.VariableManager; -import org.eclipse.sirius.components.trees.FetchTreeItemContextMenuEntryKind; -import org.eclipse.sirius.components.trees.ITreeItemContextMenuEntry; import org.eclipse.sirius.components.trees.Tree; import org.eclipse.sirius.components.trees.TreeItem; import org.eclipse.sirius.components.trees.description.TreeDescription; import org.eclipse.sirius.components.trees.renderer.TreeRenderer; -import org.eclipse.sirius.components.view.Operation; import org.eclipse.sirius.components.view.RepresentationDescription; import org.eclipse.sirius.components.view.emf.IRepresentationDescriptionConverter; -import org.eclipse.sirius.components.view.emf.OperationInterpreter; -import org.eclipse.sirius.components.view.tree.FetchTreeItemContextMenuEntry; -import org.eclipse.sirius.components.view.tree.SingleClickTreeItemContextMenuEntry; -import org.eclipse.sirius.components.view.tree.TreeItemContextMenuEntry; import org.springframework.stereotype.Service; /** @@ -70,16 +55,10 @@ public class ViewTreeDescriptionConverter implements IRepresentationDescriptionC private final List deleteTreeItemHandlers; - private final IEditService editService; - - private final IFeedbackMessageService feedbackMessageService; - - public ViewTreeDescriptionConverter(ITreeIdProvider treeIdProvider, List deleteTreeItemHandlers, List renameTreeItemHandlers, IEditService editService, IFeedbackMessageService feedbackMessageService) { + public ViewTreeDescriptionConverter(ITreeIdProvider treeIdProvider, List deleteTreeItemHandlers, List renameTreeItemHandlers) { this.treeIdProvider = Objects.requireNonNull(treeIdProvider); this.renameTreeItemHandlers = Objects.requireNonNull(renameTreeItemHandlers); this.deleteTreeItemHandlers = Objects.requireNonNull(deleteTreeItemHandlers); - this.editService = Objects.requireNonNull(editService); - this.feedbackMessageService = Objects.requireNonNull(feedbackMessageService); } @Override @@ -113,7 +92,6 @@ public IRepresentationDescription convert(RepresentationDescription representati .parentObjectProvider(variableManager -> this.evaluateObject(interpreter, variableManager, viewTreeDescription.getParentExpression())) .deleteHandler(this::getDeleteHandler) .renameHandler(this::getRenameHandler) - .contextMenuEntries(this.getContextMenuActions(interpreter, viewTreeDescription)) ; return builder.build(); @@ -191,16 +169,6 @@ private List evaluateObjectList(AQLInterpreter interpreter, VariableMana .orElse(List.of()); } - private IStatus executeOperations(AQLInterpreter interpreter, VariableManager variableManager, List operations) { - OperationInterpreter operationInterpreter = new OperationInterpreter(interpreter, this.editService); - Optional optionalVariableManager = operationInterpreter.executeOperations(operations, variableManager); - if (optionalVariableManager.isEmpty()) { - return this.buildFailureWithFeedbackMessages("Something went wrong while handling the context menu action"); - } else { - return this.buildSuccessWithSemanticChangeAndFeedbackMessages(); - } - } - private IStatus getDeleteHandler(VariableManager variableManager) { var optionalEditingContext = variableManager.get(IEditingContext.EDITING_CONTEXT, IEditingContext.class); var optionalTreeItem = variableManager.get(TreeItem.SELECTED_TREE_ITEM, TreeItem.class); @@ -254,49 +222,4 @@ private StyledString getTreeItemLabel(AQLInterpreter interpreter, VariableManage } return result; } - - private java.util.List getContextMenuActions(AQLInterpreter interpreter, org.eclipse.sirius.components.view.tree.TreeDescription viewTreeDescription) { - return viewTreeDescription.getContextMenuEntries().stream() - .map(action -> this.convertContextMenuEntry(interpreter, action)) - .toList(); - } - - private ITreeItemContextMenuEntry convertContextMenuEntry(AQLInterpreter interpreter, TreeItemContextMenuEntry viewTreeItemContextAction) { - ITreeItemContextMenuEntry result = null; - if (viewTreeItemContextAction instanceof SingleClickTreeItemContextMenuEntry viewSimpleAction) { - result = org.eclipse.sirius.components.trees.SingleClickTreeItemContextMenuEntry.newSingleClickTreeItemContextMenuEntry(UUID.randomUUID().toString()) - .label(variableMananger -> this.evaluateString(interpreter, variableMananger, viewSimpleAction.getLabelExpression())) - .iconURL(variableMananger -> this.evaluateStringList(interpreter, variableMananger, viewSimpleAction.getIconURLExpression())) - .precondition(variableMananger -> this.evaluateBoolean(interpreter, variableMananger, viewSimpleAction.getPreconditionExpression())) - .handler(variableMananger -> this.executeOperations(interpreter, variableMananger, viewSimpleAction.getBody())) - .build(); - } else if (viewTreeItemContextAction instanceof FetchTreeItemContextMenuEntry viewFetchAction) { - result = org.eclipse.sirius.components.trees.FetchTreeItemContextMenuEntry.newFetchTreeItemContextMenuEntry(UUID.randomUUID().toString()) - .label(variableMananger -> this.evaluateString(interpreter, variableMananger, viewFetchAction.getLabelExpression())) - .iconURL(variableMananger -> this.evaluateStringList(interpreter, variableMananger, viewFetchAction.getIconURLExpression())) - .precondition(variableMananger -> this.evaluateBoolean(interpreter, variableMananger, viewFetchAction.getPreconditionExpression())) - .urlToFetch(variableMananger -> this.evaluateString(interpreter, variableMananger, viewFetchAction.getUrlExression())) - .fetchKind(variableMananger -> this.convertFetchKind(viewFetchAction.getKind())) - .build(); - } - return result; - } - - private Failure buildFailureWithFeedbackMessages(String technicalMessage) { - List errorMessages = new ArrayList<>(); - errorMessages.add(new Message(technicalMessage, MessageLevel.ERROR)); - errorMessages.addAll(this.feedbackMessageService.getFeedbackMessages()); - return new Failure(errorMessages); - } - - private Success buildSuccessWithSemanticChangeAndFeedbackMessages() { - return new Success(ChangeKind.SEMANTIC_CHANGE, Map.of(), this.feedbackMessageService.getFeedbackMessages()); - } - - private FetchTreeItemContextMenuEntryKind convertFetchKind(org.eclipse.sirius.components.view.tree.FetchTreeItemContextMenuEntryKind fetchActionKind) { - return switch (fetchActionKind) { - case DOWNLOAD -> FetchTreeItemContextMenuEntryKind.DOWNLOAD; - case OPEN -> FetchTreeItemContextMenuEntryKind.OPEN; - }; - } } diff --git a/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewTreeItemContextMenuEntryProvider.java b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewTreeItemContextMenuEntryProvider.java new file mode 100644 index 0000000000..6a0728a1ff --- /dev/null +++ b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/tree/ViewTreeItemContextMenuEntryProvider.java @@ -0,0 +1,139 @@ +/******************************************************************************* + * Copyright (c) 2024 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 + * 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.view.emf.tree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; +import java.util.function.Function; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.sirius.components.collaborative.trees.api.ITreeItemContextMenuEntryProvider; +import org.eclipse.sirius.components.collaborative.trees.dto.ITreeItemContextMenuEntry; +import org.eclipse.sirius.components.core.api.IEditingContext; +import org.eclipse.sirius.components.interpreter.AQLInterpreter; +import org.eclipse.sirius.components.representations.VariableManager; +import org.eclipse.sirius.components.trees.Tree; +import org.eclipse.sirius.components.trees.TreeItem; +import org.eclipse.sirius.components.trees.description.TreeDescription; +import org.eclipse.sirius.components.view.View; +import org.eclipse.sirius.components.view.emf.IViewRepresentationDescriptionSearchService; +import org.eclipse.sirius.components.view.emf.ViewRepresentationDescriptionPredicate; +import org.eclipse.sirius.components.view.emf.api.IViewAQLInterpreterFactory; +import org.eclipse.sirius.components.view.tree.FetchTreeItemContextMenuEntry; +import org.eclipse.sirius.components.view.tree.SingleClickTreeItemContextMenuEntry; +import org.eclipse.sirius.components.view.tree.TreeItemContextMenuEntry; +import org.springframework.stereotype.Service; + +/** + * Tree item context menu entry provider for view tree model. + * + * @author Jerome Gout + */ +@Service +public class ViewTreeItemContextMenuEntryProvider implements ITreeItemContextMenuEntryProvider { + + private final ViewRepresentationDescriptionPredicate viewRepresentationDescriptionPredicate; + + private final IViewRepresentationDescriptionSearchService viewRepresentationDescriptionSearchService; + + private final IViewAQLInterpreterFactory aqlInterpreterFactory; + + private final Function idProvider = (eObject) -> UUID.nameUUIDFromBytes(EcoreUtil.getURI(eObject).toString().getBytes()); + + public ViewTreeItemContextMenuEntryProvider(ViewRepresentationDescriptionPredicate viewRepresentationDescriptionPredicate, IViewRepresentationDescriptionSearchService viewRepresentationDescriptionSearchService, IViewAQLInterpreterFactory aqlInterpreterFactory) { + this.viewRepresentationDescriptionPredicate = Objects.requireNonNull(viewRepresentationDescriptionPredicate); + this.viewRepresentationDescriptionSearchService = Objects.requireNonNull(viewRepresentationDescriptionSearchService); + this.aqlInterpreterFactory = Objects.requireNonNull(aqlInterpreterFactory); + } + + @Override + public boolean canHandle(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem) { + return this.viewRepresentationDescriptionPredicate.test(treeDescription); + } + + @Override + public List getTreeItemContextMenuEntries(IEditingContext editingContext, TreeDescription treeDescription, Tree tree, TreeItem treeItem) { + var optionalTreeDescription = this.viewRepresentationDescriptionSearchService + .findById(editingContext, treeDescription.getId()) + .filter(org.eclipse.sirius.components.view.tree.TreeDescription.class::isInstance) + .map(org.eclipse.sirius.components.view.tree.TreeDescription.class::cast); + if (optionalTreeDescription.isPresent()) { + var viewTreeDescription = optionalTreeDescription.get(); + + AQLInterpreter interpreter = this.aqlInterpreterFactory.createInterpreter(editingContext, (View) viewTreeDescription.eContainer()); + + VariableManager variableManager = new VariableManager(); + variableManager.put(IEditingContext.EDITING_CONTEXT, editingContext); + variableManager.put(TreeDescription.TREE, tree); + variableManager.put(TreeItem.SELECTED_TREE_ITEM, treeItem); + variableManager.put(TreeDescription.ID, treeItem.getId()); + var semanticTreeItemObject = treeDescription.getTreeItemObjectProvider().apply(variableManager); + variableManager.put(VariableManager.SELF, semanticTreeItemObject); + + return viewTreeDescription.getContextMenuEntries().stream() + .filter(viewAction -> this.isValidActionPrecondition(viewAction, variableManager, interpreter)) + .map(treeItemContextMenuEntry -> this.convertContextAction(treeItemContextMenuEntry, variableManager, interpreter)) + .toList(); + } + return List.of(); + } + + private ITreeItemContextMenuEntry convertContextAction(TreeItemContextMenuEntry viewTreeItemContextAction, VariableManager variableManager, AQLInterpreter interpreter) { + ITreeItemContextMenuEntry result = null; + var id = this.idProvider.apply(viewTreeItemContextAction).toString(); + var label = this.evaluateString(variableManager, interpreter, viewTreeItemContextAction.getLabelExpression()); + var iconURL = this.evaluateStringList(variableManager, interpreter, viewTreeItemContextAction.getIconURLExpression()); + if (viewTreeItemContextAction instanceof SingleClickTreeItemContextMenuEntry) { + result = new org.eclipse.sirius.components.collaborative.trees.dto.SingleClickTreeItemContextMenuEntry(id, label, iconURL); + } else if (viewTreeItemContextAction instanceof FetchTreeItemContextMenuEntry) { + result = new org.eclipse.sirius.components.collaborative.trees.dto.FetchTreeItemContextMenuEntry(id, label, iconURL); + } + return result; + } + + private boolean isValidActionPrecondition(TreeItemContextMenuEntry viewContextAction, VariableManager variableManager, AQLInterpreter interpreter) { + var precondition = viewContextAction.getPreconditionExpression(); + if (precondition != null && !precondition.isBlank()) { + return this.evaluateBoolean(variableManager, interpreter, precondition); + } + return false; + } + + private String evaluateString(VariableManager variableManager, AQLInterpreter interpreter, String expression) { + return interpreter.evaluateExpression(variableManager.getVariables(), expression) + .asString() + .orElse(""); + } + + private List evaluateStringList(VariableManager variableManager, AQLInterpreter interpreter, String expression) { + List values = new ArrayList<>(); + if (expression != null && !expression.isBlank()) { + Optional> optionalResult = interpreter.evaluateExpression(variableManager.getVariables(), expression).asObjects(); + if (optionalResult.isPresent()) { + values = optionalResult.get().stream().filter(String.class::isInstance).map(String.class::cast).toList(); + } + } + return values; + } + + private Boolean evaluateBoolean(VariableManager variableManager, AQLInterpreter interpreter, String expression) { + return interpreter.evaluateExpression(variableManager.getVariables(), expression) + .asBoolean() + .orElse(true); + } + +}