|
| 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.views.explorer; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | +import java.util.Objects; |
| 17 | + |
| 18 | +import org.eclipse.sirius.components.collaborative.trees.api.IRenameTreeItemHandler; |
| 19 | +import org.eclipse.sirius.components.core.api.IEditingContext; |
| 20 | +import org.eclipse.sirius.components.core.api.ILabelService; |
| 21 | +import org.eclipse.sirius.components.core.api.IReadOnlyObjectPredicate; |
| 22 | +import org.eclipse.sirius.components.core.api.labels.StyledString; |
| 23 | +import org.eclipse.sirius.components.core.api.labels.StyledStringFragment; |
| 24 | +import org.eclipse.sirius.components.core.api.labels.StyledStringFragmentStyle; |
| 25 | +import org.eclipse.sirius.components.diagrams.description.DiagramDescription; |
| 26 | +import org.eclipse.sirius.components.representations.Failure; |
| 27 | +import org.eclipse.sirius.components.representations.IStatus; |
| 28 | +import org.eclipse.sirius.components.trees.Tree; |
| 29 | +import org.eclipse.sirius.components.trees.TreeItem; |
| 30 | +import org.eclipse.sirius.web.application.views.viewsexplorer.services.RepresentationDescriptionType; |
| 31 | +import org.eclipse.sirius.web.application.views.viewsexplorer.services.RepresentationKind; |
| 32 | +import org.eclipse.sirius.web.application.views.viewsexplorer.services.api.IViewsExplorerLabelServiceDelegate; |
| 33 | +import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.RepresentationMetadata; |
| 34 | +import org.eclipse.sirius.web.domain.services.api.IMessageService; |
| 35 | +import org.eclipse.syson.util.StandardDiagramsConstants; |
| 36 | +import org.springframework.stereotype.Service; |
| 37 | + |
| 38 | +/** |
| 39 | + * Provide the behavior of the views explorer fro SysON. |
| 40 | + * |
| 41 | + * @author frouene |
| 42 | + */ |
| 43 | +@Service |
| 44 | +public class SysONViewsExplorerLabelServiceDelegate implements IViewsExplorerLabelServiceDelegate { |
| 45 | + |
| 46 | + private final IReadOnlyObjectPredicate readOnlyObjectPredicate; |
| 47 | + |
| 48 | + private final List<IRenameTreeItemHandler> renameTreeItemHandlers; |
| 49 | + |
| 50 | + private final ILabelService labelService; |
| 51 | + |
| 52 | + private final IMessageService messageService; |
| 53 | + |
| 54 | + public SysONViewsExplorerLabelServiceDelegate(IReadOnlyObjectPredicate readOnlyObjectPredicate, List<IRenameTreeItemHandler> renameTreeItemHandlers, ILabelService labelService, IMessageService messageService) { |
| 55 | + this.readOnlyObjectPredicate = Objects.requireNonNull(readOnlyObjectPredicate); |
| 56 | + this.renameTreeItemHandlers = Objects.requireNonNull(renameTreeItemHandlers); |
| 57 | + this.labelService = Objects.requireNonNull(labelService); |
| 58 | + this.messageService = Objects.requireNonNull(messageService); |
| 59 | + } |
| 60 | + @Override |
| 61 | + public boolean canHandle(IEditingContext editingContext) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean isEditable(Object self) { |
| 67 | + return !this.readOnlyObjectPredicate.test(self) && self instanceof RepresentationMetadata; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public StyledString getLabel(Object self) { |
| 72 | + var result = StyledString.of(""); |
| 73 | + if (self instanceof RepresentationKind kind) { |
| 74 | + String name = kind.name(); |
| 75 | + String size = String.valueOf(kind.representationDescriptionTypes().stream().mapToLong(descType -> descType.representationsMetadata().size()).sum()); |
| 76 | + result = this.getColoredLabel(name, size); |
| 77 | + } else if (self instanceof RepresentationDescriptionType descriptionType) { |
| 78 | + String name = descriptionType.description().getLabel(); |
| 79 | + if (descriptionType.description() instanceof DiagramDescription) { |
| 80 | + name = StandardDiagramsConstants.getValueFromShortName(descriptionType.id()); |
| 81 | + } |
| 82 | + String size = String.valueOf(descriptionType.representationsMetadata().size()); |
| 83 | + result = this.getColoredLabel(name, size); |
| 84 | + } else { |
| 85 | + result = this.labelService.getStyledLabel(self); |
| 86 | + } |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public IStatus editLabel(IEditingContext editingContext, Tree tree, TreeItem treeItem, String newValue) { |
| 92 | + var optionalHandler = this.renameTreeItemHandlers.stream() |
| 93 | + .filter(handler -> handler.canHandle(editingContext, treeItem, newValue)) |
| 94 | + .findFirst(); |
| 95 | + |
| 96 | + if (optionalHandler.isPresent()) { |
| 97 | + IRenameTreeItemHandler renameTreeItemHandler = optionalHandler.get(); |
| 98 | + return renameTreeItemHandler.handle(editingContext, treeItem, newValue, tree); |
| 99 | + } |
| 100 | + |
| 101 | + return new Failure(this.messageService.failedToRename()); |
| 102 | + } |
| 103 | + |
| 104 | + private StyledString getColoredLabel(String label, String size) { |
| 105 | + return new StyledString(List.of( |
| 106 | + new StyledStringFragment("%s (%s)".formatted(label.toUpperCase(), size), StyledStringFragmentStyle.newDefaultStyledStringFragmentStyle() |
| 107 | + .foregroundColor("#261E588A") |
| 108 | + .build()) |
| 109 | + )); |
| 110 | + } |
| 111 | +} |
0 commit comments