|
| 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.general.view.services.nodeactions.managevisibility; |
| 14 | + |
| 15 | +import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramContext; |
| 16 | +import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramQueryService; |
| 17 | +import org.eclipse.sirius.components.collaborative.diagrams.api.nodeactions.IManageVisibilityMenuActionHandler; |
| 18 | +import org.eclipse.sirius.components.core.api.IEditingContext; |
| 19 | +import org.eclipse.sirius.components.diagrams.IDiagramElement; |
| 20 | +import org.eclipse.sirius.components.diagrams.Node; |
| 21 | +import org.eclipse.sirius.components.diagrams.events.HideDiagramElementEvent; |
| 22 | +import org.eclipse.sirius.components.representations.IStatus; |
| 23 | +import org.eclipse.sirius.components.representations.Success; |
| 24 | +import org.springframework.stereotype.Service; |
| 25 | + |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.Optional; |
| 29 | +import java.util.Set; |
| 30 | + |
| 31 | +/** |
| 32 | + * Handler for the menu action on the manage visibility modal that will reveal children that also have children. |
| 33 | + * |
| 34 | + * @author mcharfadi |
| 35 | + */ |
| 36 | +@Service |
| 37 | +public class GeneralViewManageVisibilityRevealValuedContentHandler implements IManageVisibilityMenuActionHandler { |
| 38 | + |
| 39 | + private final IDiagramQueryService diagramQueryService; |
| 40 | + |
| 41 | + public GeneralViewManageVisibilityRevealValuedContentHandler(IDiagramQueryService diagramQueryService) { |
| 42 | + this.diagramQueryService = Objects.requireNonNull(diagramQueryService); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public boolean canHandle(IEditingContext editingContext, IDiagramContext diagramContext, IDiagramElement diagramElement, String actionId) { |
| 47 | + return actionId.equals(GeneralViewManageVisibilityRevealValuedContentAction.ACTION_ID); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public IStatus handle(IEditingContext editingContext, IDiagramContext diagramContext, IDiagramElement diagramElement, String actionId) { |
| 52 | + Optional<Node> optionalNode = this.diagramQueryService.findNodeById(diagramContext.getDiagram(), diagramElement.getId()); |
| 53 | + Set<String> nodesToReveal = new HashSet<>(); |
| 54 | + Set<String> nodesToHide = new HashSet<>(); |
| 55 | + if (optionalNode.isPresent()) { |
| 56 | + optionalNode.get().getChildNodes().forEach(node -> { |
| 57 | + if (node.getChildNodes().isEmpty()) { |
| 58 | + nodesToHide.add(node.getId()); |
| 59 | + } else { |
| 60 | + nodesToReveal.add(node.getId()); |
| 61 | + } |
| 62 | + }); |
| 63 | + diagramContext.getDiagramEvents().add(new HideDiagramElementEvent(nodesToReveal, false)); |
| 64 | + diagramContext.getDiagramEvents().add(new HideDiagramElementEvent(nodesToHide, true)); |
| 65 | + } |
| 66 | + return new Success(); |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments