|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 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.diagram.services; |
| 14 | + |
| 15 | +import java.text.MessageFormat; |
| 16 | +import java.util.Objects; |
| 17 | + |
| 18 | +import org.antlr.v4.runtime.CharStreams; |
| 19 | +import org.antlr.v4.runtime.CommonTokenStream; |
| 20 | +import org.antlr.v4.runtime.tree.ParseTree; |
| 21 | +import org.antlr.v4.runtime.tree.ParseTreeWalker; |
| 22 | +import org.eclipse.sirius.components.core.api.IFeedbackMessageService; |
| 23 | +import org.eclipse.sirius.components.representations.Message; |
| 24 | +import org.eclipse.sirius.components.representations.MessageLevel; |
| 25 | +import org.eclipse.syson.direct.edit.grammars.DirectEditLexer; |
| 26 | +import org.eclipse.syson.direct.edit.grammars.DirectEditParser; |
| 27 | +import org.eclipse.syson.services.DiagramDirectEditListener; |
| 28 | +import org.eclipse.syson.services.LabelService; |
| 29 | +import org.eclipse.syson.sysml.ConstraintUsage; |
| 30 | +import org.eclipse.syson.sysml.Element; |
| 31 | +import org.eclipse.syson.sysml.RequirementConstraintMembership; |
| 32 | +import org.springframework.stereotype.Service; |
| 33 | + |
| 34 | +/** |
| 35 | + * Label-related services doing mutations in diagrams. |
| 36 | + * |
| 37 | + * @author arichard |
| 38 | + */ |
| 39 | +@Service |
| 40 | +public class DiagramMutationLabelService { |
| 41 | + |
| 42 | + private final IFeedbackMessageService feedbackMessageService; |
| 43 | + |
| 44 | + public DiagramMutationLabelService(IFeedbackMessageService feedbackMessageService) { |
| 45 | + this.feedbackMessageService = Objects.requireNonNull(feedbackMessageService); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Apply the direct edit result (i.e. the newLabel) to the given {@link Element}. |
| 50 | + * |
| 51 | + * @param element |
| 52 | + * the given {@link Element}. |
| 53 | + * @param newLabel |
| 54 | + * the new value to apply. |
| 55 | + * @return the given {@link Element}. |
| 56 | + */ |
| 57 | + public Element directEdit(Element element, String newLabel) { |
| 58 | + return this.directEdit(element, newLabel, (String[]) null); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Apply the direct edit result (i.e. the newLabel) to the given {@link Element}, with the provided {@code options}. |
| 63 | + * <p> |
| 64 | + * This method is typically used to enable direct edit with some restrictions (e.g. de-activate the ability to edit |
| 65 | + * the name of an element via direct edit). See {@link #directEdit(Element, String)} to perform a direct edit with |
| 66 | + * default options. |
| 67 | + * </p> |
| 68 | + * |
| 69 | + * @param element |
| 70 | + * the given {@link Element} |
| 71 | + * @param newLabel |
| 72 | + * the new value to apply |
| 73 | + * @param options |
| 74 | + * the options of the direct edit |
| 75 | + * @return the given {@link Element} |
| 76 | + */ |
| 77 | + public Element directEdit(Element element, String newLabel, String... options) { |
| 78 | + return this.directEdit(element, newLabel, false, options); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Apply the direct edit result (i.e. the newLabel) to the given {@link Element}. |
| 83 | + * |
| 84 | + * @param element |
| 85 | + * the given {@link Element}. |
| 86 | + * @param newLabel |
| 87 | + * the new value to apply. |
| 88 | + * @return the given {@link Element}. |
| 89 | + */ |
| 90 | + public Element directEdit(Element element, String newLabel, boolean isCompartmentItem, String... options) { |
| 91 | + DirectEditLexer lexer = new DirectEditLexer(CharStreams.fromString(newLabel)); |
| 92 | + CommonTokenStream tokens = new CommonTokenStream(lexer); |
| 93 | + DirectEditParser parser = new DirectEditParser(tokens); |
| 94 | + ParseTree tree; |
| 95 | + if (element instanceof ConstraintUsage && element.getOwningMembership() instanceof RequirementConstraintMembership && isCompartmentItem) { |
| 96 | + // Use the constraint expression parser only if the element is a constraint owned by a requirement, other |
| 97 | + // constraints (including requirements) are parsed as regular elements. |
| 98 | + tree = parser.constraintExpression(); |
| 99 | + } else if (isCompartmentItem) { |
| 100 | + tree = parser.listItemExpression(); |
| 101 | + } else { |
| 102 | + tree = parser.nodeExpression(); |
| 103 | + } |
| 104 | + ParseTreeWalker walker = new ParseTreeWalker(); |
| 105 | + DiagramDirectEditListener listener = new DiagramDirectEditListener(element, this.feedbackMessageService, options); |
| 106 | + walker.walk(listener, tree); |
| 107 | + listener.resolveProxies().forEach(proxy -> { |
| 108 | + this.feedbackMessageService.addFeedbackMessage(new Message(MessageFormat.format("Unable to resolve \u2035{0}\u2035", proxy.nameToResolve()), MessageLevel.WARNING)); |
| 109 | + }); |
| 110 | + return element; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Apply the direct edit result (i.e. the newLabel) to the given graphical node {@link Element}. |
| 115 | + * |
| 116 | + * @param element |
| 117 | + * the given {@link Element}. |
| 118 | + * @param newLabel |
| 119 | + * the new value to apply. |
| 120 | + * @return the given {@link Element}. |
| 121 | + */ |
| 122 | + public Element directEditNode(Element element, String newLabel) { |
| 123 | + return this.directEdit(element, newLabel, false, (String[]) null); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Apply the direct edit result (i.e. the newLabel) to the given graphical list item {@link Element}. |
| 128 | + * |
| 129 | + * @param element |
| 130 | + * the given {@link Element}. |
| 131 | + * @param newLabel |
| 132 | + * the new value to apply. |
| 133 | + * @return the given {@link Element}. |
| 134 | + */ |
| 135 | + public Element directEditListItem(Element element, String newLabel) { |
| 136 | + return this.directEdit(element, newLabel, true, (String[]) null); |
| 137 | + } |
| 138 | + |
| 139 | + public Element editMultiplicityRangeCenterLabel(Element element, String newLabel) { |
| 140 | + return this.directEdit(element, newLabel, LabelService.NAME_OFF, LabelService.REDEFINITION_OFF, LabelService.SUBSETTING_OFF, LabelService.TYPING_OFF, LabelService.VALUE_OFF); |
| 141 | + } |
| 142 | + |
| 143 | + public Element editEdgeCenterLabel(Element element, String newLabel) { |
| 144 | + return this.directEdit(element, newLabel, LabelService.REDEFINITION_OFF, LabelService.SUBSETTING_OFF, LabelService.VALUE_OFF); |
| 145 | + } |
| 146 | +} |
0 commit comments