|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Obeo. |
| 3 | + * This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Obeo - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.syson.application.expressions.services; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Objects; |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +import org.eclipse.sirius.components.collaborative.diagrams.DiagramContext; |
| 21 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.ITool; |
| 22 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.SingleClickOnDiagramElementTool; |
| 23 | +import org.eclipse.sirius.components.collaborative.diagrams.dto.ToolSection; |
| 24 | +import org.eclipse.sirius.components.core.api.IEditingContext; |
| 25 | +import org.eclipse.sirius.components.core.api.IObjectSearchService; |
| 26 | +import org.eclipse.sirius.components.core.api.IReadOnlyObjectPredicate; |
| 27 | +import org.eclipse.sirius.components.diagrams.Edge; |
| 28 | +import org.eclipse.sirius.components.diagrams.Node; |
| 29 | +import org.eclipse.sirius.components.diagrams.description.IDiagramElementDescription; |
| 30 | +import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext; |
| 31 | +import org.eclipse.sirius.components.view.emf.diagram.api.IPaletteToolsProvider; |
| 32 | +import org.eclipse.syson.sysml.Element; |
| 33 | +import org.eclipse.syson.sysml.Expression; |
| 34 | +import org.eclipse.syson.sysml.metamodel.services.MetamodelQueryElementService; |
| 35 | +import org.springframework.stereotype.Service; |
| 36 | + |
| 37 | +/** |
| 38 | + * Contribute tools to manipulate {@link Expression} to specific elements's diagram palette: |
| 39 | + * <ul> |
| 40 | + * <li>"New expression" on diagram elements which represent SysML elements that can contain a new |
| 41 | + * {@link Expression}.</li> |
| 42 | + * <li>"Edit expression" on diagram elements which represent an {@link Expression} or an element which contains a single |
| 43 | + * (non-ambiguous) one.</li> |
| 44 | + * <li>"Delete expression" on diagram elements which contain a single (non-ambiguous) expression (on actual expression |
| 45 | + * element, the plain Delete is enough).</li> |
| 46 | + * </ul> |
| 47 | + * |
| 48 | + * @author pcdavid |
| 49 | + */ |
| 50 | +@Service |
| 51 | +public class ExpressionsPaletteToolsProvider implements IPaletteToolsProvider { |
| 52 | + |
| 53 | + private static final String NEW_EXPRESSION_TOOL_ID = "tool_new_expression"; |
| 54 | + |
| 55 | + private static final String NEW_EXPRESSION_TOOL_LABEL = "New expression"; |
| 56 | + |
| 57 | + private static final String EDIT_EXPRESSION_TOOL_ID = "tool_edit_expression"; |
| 58 | + |
| 59 | + private static final String EDIT_EXPRESSION_TOOL_LABEL = "Edit expression"; |
| 60 | + |
| 61 | + private static final String DELETE_EXPRESSION_TOOL_ID = "tool_delete_expression"; |
| 62 | + |
| 63 | + private static final String DELETE_EXPRESSION_TOOL_LABEL = "Delete expression"; |
| 64 | + |
| 65 | + private static final String EXPRESSION_TOOL_SECTION_ID = "toolSection_expression"; |
| 66 | + |
| 67 | + private static final String EXPRESSION_TOOL_SECTION_LABEL = "Expression"; |
| 68 | + |
| 69 | + private final IObjectSearchService objectSearchService; |
| 70 | + |
| 71 | + private final IReadOnlyObjectPredicate readOnlyObjectPredicate; |
| 72 | + |
| 73 | + private final MetamodelQueryElementService metamodelQueryElementService; |
| 74 | + |
| 75 | + public ExpressionsPaletteToolsProvider(IObjectSearchService objectSearchService, IReadOnlyObjectPredicate readOnlyObjectPredicate) { |
| 76 | + this.objectSearchService = Objects.requireNonNull(objectSearchService); |
| 77 | + this.readOnlyObjectPredicate = Objects.requireNonNull(readOnlyObjectPredicate); |
| 78 | + this.metamodelQueryElementService = new MetamodelQueryElementService(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public List<ToolSection> createExtraToolSections(IEditingContext editingContext, DiagramContext diagramContext, Object diagramElementDescription, Object diagramElement) { |
| 83 | + var tools = new ArrayList<ITool>(); |
| 84 | + |
| 85 | + var optionalTargetObjectId = this.getTargetObjectId(diagramElement); |
| 86 | + |
| 87 | + if (optionalTargetObjectId.isPresent() && diagramElementDescription instanceof IDiagramElementDescription nodeDescription) { |
| 88 | + var semanticObject = this.objectSearchService.getObject(editingContext, optionalTargetObjectId.get()); |
| 89 | + if (semanticObject.isPresent() && semanticObject.get() instanceof Element element) { |
| 90 | + if (editingContext instanceof IEMFEditingContext emfEditingContext && this.canHaveNewExpression(emfEditingContext, element)) { |
| 91 | + tools.add(new SingleClickOnDiagramElementTool(NEW_EXPRESSION_TOOL_ID, NEW_EXPRESSION_TOOL_LABEL, List.of(), List.of(nodeDescription), "", false, false, List.of())); |
| 92 | + } |
| 93 | + if (this.canEditExpression(element)) { |
| 94 | + tools.add(new SingleClickOnDiagramElementTool(EDIT_EXPRESSION_TOOL_ID, EDIT_EXPRESSION_TOOL_LABEL, List.of(), List.of(nodeDescription), "", false, false, List.of())); |
| 95 | + if (!this.metamodelQueryElementService.isTopLevelExpression(element)) { |
| 96 | + tools.add(new SingleClickOnDiagramElementTool(DELETE_EXPRESSION_TOOL_ID, DELETE_EXPRESSION_TOOL_LABEL, List.of(), List.of(nodeDescription), "", false, false, List.of())); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (!tools.isEmpty()) { |
| 103 | + return List.of(new ToolSection(EXPRESSION_TOOL_SECTION_ID, EXPRESSION_TOOL_SECTION_LABEL, List.of(), tools)); |
| 104 | + } else { |
| 105 | + return List.of(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private Optional<String> getTargetObjectId(Object diagramElement) { |
| 110 | + Optional<String> result = Optional.empty(); |
| 111 | + if (diagramElement instanceof Node node) { |
| 112 | + result = Optional.of(node.getTargetObjectId()); |
| 113 | + } else if (diagramElement instanceof Edge edge) { |
| 114 | + result = Optional.of(edge.getTargetObjectId()); |
| 115 | + } |
| 116 | + return result; |
| 117 | + } |
| 118 | + |
| 119 | + private boolean canHaveNewExpression(IEMFEditingContext editingContext, Element element) { |
| 120 | + return !this.readOnlyObjectPredicate.test(element) && this.metamodelQueryElementService.canContainExpressionDefinition(element) |
| 121 | + && !this.metamodelQueryElementService.hasSingleExpressionDefinition(element); |
| 122 | + } |
| 123 | + |
| 124 | + private boolean canEditExpression(Element element) { |
| 125 | + return this.metamodelQueryElementService.isTopLevelExpression(element) || (this.metamodelQueryElementService.hasSingleExpressionDefinition(element) |
| 126 | + && !this.metamodelQueryElementService.hasSingleExpressionDefinition(element.getOwner())); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public List<ITool> createQuickAccessTools(IEditingContext editingContext, DiagramContext diagramContext, Object diagramElementDescription, Object diagramElement) { |
| 131 | + return List.of(); |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments