From 1d4158da3bceb98b34d409a091e18570fefeca09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20B=C3=A9gaudeau?= Date: Thu, 12 Sep 2024 14:50:25 +0200 Subject: [PATCH] [test] Add selection dialog support to Papaya MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Bégaudeau --- .../ClassDiagramDescriptionProvider.java | 4 + .../ImportExistingTypesToolProvider.java | 115 ++++++++++++++++++ .../PapayaRepresentationServices.java | 23 ++++ 3 files changed, 142 insertions(+) create mode 100644 packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/classdiagram/tools/ImportExistingTypesToolProvider.java diff --git a/packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/classdiagram/ClassDiagramDescriptionProvider.java b/packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/classdiagram/ClassDiagramDescriptionProvider.java index aa3ba1343f..d229b911cb 100644 --- a/packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/classdiagram/ClassDiagramDescriptionProvider.java +++ b/packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/classdiagram/ClassDiagramDescriptionProvider.java @@ -34,6 +34,7 @@ import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.InterfaceNodeDescriptionProvider; import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.RecordNodeDescriptionProvider; import org.eclipse.sirius.web.papaya.representations.classdiagram.tools.ClassDiagramDropToolProvider; +import org.eclipse.sirius.web.papaya.representations.classdiagram.tools.ImportExistingTypesToolProvider; /** * Used to provide the view model used to create class diagrams. @@ -79,8 +80,11 @@ public RepresentationDescription create(IColorProvider colorProvider) { } private DiagramPalette diagramPalette(IViewDiagramElementFinder cache) { + var importExistingTypesTool = new ImportExistingTypesToolProvider().getNodeTool(cache); var dropTool = new ClassDiagramDropToolProvider().getDropTool(cache); + return new DiagramBuilders().newDiagramPalette() + .nodeTools(importExistingTypesTool) .dropTool(dropTool) .build(); } diff --git a/packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/classdiagram/tools/ImportExistingTypesToolProvider.java b/packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/classdiagram/tools/ImportExistingTypesToolProvider.java new file mode 100644 index 0000000000..9be86dc50c --- /dev/null +++ b/packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/classdiagram/tools/ImportExistingTypesToolProvider.java @@ -0,0 +1,115 @@ +/******************************************************************************* + * 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.web.papaya.representations.classdiagram.tools; + +import org.eclipse.sirius.components.view.builder.IViewDiagramElementFinder; +import org.eclipse.sirius.components.view.builder.generated.diagram.DiagramBuilders; +import org.eclipse.sirius.components.view.builder.generated.view.ViewBuilders; +import org.eclipse.sirius.components.view.diagram.NodeContainmentKind; +import org.eclipse.sirius.components.view.diagram.NodeTool; +import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.ClassNodeDescriptionProvider; +import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.EnumNodeDescriptionProvider; +import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.InterfaceNodeDescriptionProvider; +import org.eclipse.sirius.web.papaya.representations.classdiagram.nodedescriptions.RecordNodeDescriptionProvider; + +/** + * Used to provide the tool used to import existing types. + * + * @author sbegaudeau + */ +@SuppressWarnings("checkstyle:MultipleStringLiterals") +public class ImportExistingTypesToolProvider { + + public NodeTool getNodeTool(IViewDiagramElementFinder cache) { + var classNodeDescription = cache.getNodeDescription(ClassNodeDescriptionProvider.NAME).orElse(null); + var interfaceNodeDescription = cache.getNodeDescription(InterfaceNodeDescriptionProvider.NAME).orElse(null); + var enumNodeDescription = cache.getNodeDescription(EnumNodeDescriptionProvider.NAME).orElse(null); + var recordNodeDescription = cache.getNodeDescription(RecordNodeDescriptionProvider.NAME).orElse(null); + + var treeDescription = new DiagramBuilders().newSelectionDialogTreeDescription() + .elementsExpression("aql:self.eResource().getResourceSet().getResources()") + .childrenExpression("aql:self.getChildren()") + .isSelectableExpression("aql:self.oclIsKindOf(papaya::Type)") + .build(); + + var dialogDescription = new DiagramBuilders().newSelectionDialogDescription() + .selectionMessage("Select the types to import") + .selectionDialogTreeDescription(treeDescription) + .build(); + + var ifClass = new ViewBuilders().newIf() + .conditionExpression("aql:selectedObject.eClass() = papaya::Class") + .children( + new DiagramBuilders().newCreateView() + .elementDescription(classNodeDescription) + .semanticElementExpression("aql:selectedObject") + .parentViewExpression("aql:selectedNode") + .containmentKind(NodeContainmentKind.CHILD_NODE) + .build() + ) + .build(); + + var ifInterface = new ViewBuilders().newIf() + .conditionExpression("aql:selectedObject.eClass() = papaya::Interface") + .children( + new DiagramBuilders().newCreateView() + .elementDescription(interfaceNodeDescription) + .semanticElementExpression("aql:selectedObject") + .parentViewExpression("aql:selectedNode") + .containmentKind(NodeContainmentKind.CHILD_NODE) + .build() + ) + .build(); + + var ifRecord = new ViewBuilders().newIf() + .conditionExpression("aql:selectedObject.eClass() = papaya::Record") + .children( + new DiagramBuilders().newCreateView() + .elementDescription(recordNodeDescription) + .semanticElementExpression("aql:selectedObject") + .parentViewExpression("aql:selectedNode") + .containmentKind(NodeContainmentKind.CHILD_NODE) + .build() + ) + .build(); + + var ifEnum = new ViewBuilders().newIf() + .conditionExpression("aql:selectedObject.eClass() = papaya::Enum") + .children( + new DiagramBuilders().newCreateView() + .elementDescription(enumNodeDescription) + .semanticElementExpression("aql:selectedObject") + .parentViewExpression("aql:selectedNode") + .containmentKind(NodeContainmentKind.CHILD_NODE) + .build() + ) + .build(); + + return new DiagramBuilders().newNodeTool() + .name("Import existing types") + .iconURLsExpression("/icons/full/obj16/Class.svg") + .dialogDescription(dialogDescription) + .body( + new ViewBuilders().newChangeContext() + .expression("aql:self") + .children( + ifClass, + ifInterface, + ifRecord, + ifEnum + ) + .build() + ) + .build(); + } +} diff --git a/packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/services/PapayaRepresentationServices.java b/packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/services/PapayaRepresentationServices.java index 18878c4bc2..cd7f7077fe 100644 --- a/packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/services/PapayaRepresentationServices.java +++ b/packages/sirius-web/backend/sirius-web-papaya/src/main/java/org/eclipse/sirius/web/papaya/representations/services/PapayaRepresentationServices.java @@ -12,11 +12,14 @@ *******************************************************************************/ package org.eclipse.sirius.web.papaya.representations.services; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.edit.provider.IItemLabelProvider; import org.eclipse.sirius.components.core.api.IEditingContext; import org.eclipse.sirius.components.core.api.IObjectSearchService; @@ -51,4 +54,24 @@ public List getSynchronizedObjects(IEditingContext editingContext, List .map(EObject.class::cast) .toList(); } + + public ResourceSet getResourceSet(Resource resource) { + return resource.getResourceSet(); + } + + public List getResources(ResourceSet resourceSet) { + return resourceSet.getResources(); + } + + public List getChildren(Object object) { + List children = new ArrayList<>(); + + if (object instanceof Resource resource) { + children.addAll(resource.getContents()); + } else if (object instanceof EObject eObject) { + children.addAll(eObject.eContents()); + } + + return children; + } }