Search in sources :

Example 1 with ReferenceBuilderService

use of org.osate.ge.services.ReferenceBuilderService in project osate2 by osate.

the class DeleteHandler method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (!(activeEditor instanceof InternalDiagramEditor)) {
        throw new RuntimeException("Unexpected editor: " + activeEditor);
    }
    final InternalDiagramEditor ageEditor = (InternalDiagramEditor) activeEditor;
    final ReferenceBuilderService refBuilder = Objects.requireNonNull((ReferenceBuilderService) ageEditor.getAdapter(ReferenceBuilderService.class), "Unable to retrieve reference builder service");
    final AadlModificationService aadlModificationService = Objects.requireNonNull((AadlModificationService) ageEditor.getAdapter(AadlModificationService.class), "Unable to retrieve modification service");
    // Get diagram and selected elements
    final List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
    if (!canExecute(selectedDiagramElements)) {
        throw new RuntimeException("canExecute() returned false");
    }
    if (!confirmDelete(selectedDiagramElements)) {
        return null;
    }
    final boolean boIsContext = anyBoIsDiagramContext(selectedDiagramElements, ageEditor.getDiagram(), refBuilder);
    ageEditor.getActionExecutor().execute("Delete", ExecutionMode.NORMAL, () -> {
        if (anyRequiresRawDelete(selectedDiagramElements)) {
            if (selectedDiagramElements.size() != 1) {
                throw new RuntimeException("Deleting multiple elements when using DeleteRaw is not supported");
            }
            final DiagramElement deToDelete = selectedDiagramElements.get(0);
            // This is safe because we have already check that at least one business object requires a raw deletion and there is exactly one
            // business object being deleted.
            final RawDeleter deleter = (RawDeleter) deToDelete.getBusinessObjectHandler();
            deleter.delete(new RawDeleteContext(deToDelete.getBusinessObject()));
        } else if (anyIsInAnnex(selectedDiagramElements)) {
            if (selectedDiagramElements.size() != 1) {
                throw new RuntimeException("Deleting multiple elements when deleting an element inside of an annex is not supported");
            }
            // Handle annex specially because we need to modify the annex itself instead of the root of the model.
            // Only a single annex element can be modified at a time because modifying annex elements as part of a
            // group is not supported.
            final BusinessObjectRemoval modInfo = createBusinessObjectRemovalOrRemoveDiagramElement(selectedDiagramElements.get(0));
            if (modInfo != null) {
                aadlModificationService.modify(modInfo.staleBoToModify, (boToModify) -> {
                    modInfo.remover.accept(boToModify);
                });
            }
        } else {
            // Group elements to be removed by resource. All the elements will be removed as part of the same modification.
            // This ensures that the appropriate element is retrieved regardless of the order in the model or the URI scheme.
            final ListMultimap<Resource, BusinessObjectRemoval> removals = ArrayListMultimap.create();
            for (final DiagramElement de : selectedDiagramElements) {
                final BusinessObjectRemoval removal = createBusinessObjectRemovalOrRemoveDiagramElement(de);
                if (removal != null) {
                    removals.put(removal.staleBoToModify.eResource(), removal);
                }
            }
            // Perform the modifications. One modification will be performed for each resource.
            final List<AadlModificationService.Modification<?, ?>> modifications = new ArrayList<>();
            for (final Entry<Resource, Collection<BusinessObjectRemoval>> entry : removals.asMap().entrySet()) {
                final Resource resource = entry.getKey();
                final EObject root = Objects.requireNonNull(resource.getContents().get(0), "unable to retrieve root element");
                final Modification<EObject, EObject> mod = AadlModificationService.Modification.create(root, (rootToModify) -> {
                    // could change in between removals and an incorrect element will be removed.
                    for (final BusinessObjectRemoval removal : entry.getValue()) {
                        final URI uri = EcoreUtil.getURI(removal.staleBoToModify);
                        Objects.requireNonNull(uri, "unable to retrieve uri for " + removal.staleBoToModify);
                        removal.boToModify = rootToModify.eResource().getResourceSet().getEObject(uri, true);
                    }
                    // Remove the business object using the stored business object to modify
                    for (final BusinessObjectRemoval removal : entry.getValue()) {
                        removal.remover.accept(removal.boToModify);
                    }
                });
                modifications.add(mod);
            }
            if (!modifications.isEmpty()) {
                aadlModificationService.modify(modifications);
            }
        }
        return null;
    });
    if (boIsContext) {
        // Close the editor if the context was deleted
        Display.getDefault().syncExec(() -> ageEditor.closeEditor());
    } else {
        ageEditor.clearSelection();
    }
    return null;
}
Also used : ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) BusinessObjectHandler(org.osate.ge.businessobjecthandling.BusinessObjectHandler) URI(org.eclipse.emf.common.util.URI) ListMultimap(com.google.common.collect.ListMultimap) CanDeleteContext(org.osate.ge.businessobjecthandling.CanDeleteContext) Modification(org.osate.ge.internal.services.AadlModificationService.Modification) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) DefaultAnnexSubclause(org.osate.aadl2.DefaultAnnexSubclause) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) HandlerUtil(org.eclipse.ui.handlers.HandlerUtil) InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) CustomDeleteContext(org.osate.ge.businessobjecthandling.CustomDeleteContext) Predicates(com.google.common.base.Predicates) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) IEditorPart(org.eclipse.ui.IEditorPart) EmfContainerProvider(org.osate.ge.EmfContainerProvider) PlatformUI(org.eclipse.ui.PlatformUI) ReferenceBuilderService(org.osate.ge.services.ReferenceBuilderService) Collection(java.util.Collection) CustomDeleter(org.osate.ge.businessobjecthandling.CustomDeleter) RawDeleter(org.osate.ge.businessobjecthandling.RawDeleter) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) EObject(org.eclipse.emf.ecore.EObject) ExecutionException(org.eclipse.core.commands.ExecutionException) Display(org.eclipse.swt.widgets.Display) CanonicalBusinessObjectReference(org.osate.ge.CanonicalBusinessObjectReference) DiagramElementUtil(org.osate.ge.internal.util.DiagramElementUtil) Objects(java.util.Objects) Consumer(java.util.function.Consumer) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) List(java.util.List) ExecutionMode(org.osate.ge.internal.services.ActionExecutor.ExecutionMode) AgeHandlerUtil(org.osate.ge.internal.ui.handlers.AgeHandlerUtil) RawDeleteContext(org.osate.ge.businessobjecthandling.RawDeleteContext) Entry(java.util.Map.Entry) Resource(org.eclipse.emf.ecore.resource.Resource) AnnexSubclause(org.osate.aadl2.AnnexSubclause) AadlModificationService(org.osate.ge.internal.services.AadlModificationService) AbstractHandler(org.eclipse.core.commands.AbstractHandler) NamedElement(org.osate.aadl2.NamedElement) AnnexLibrary(org.osate.aadl2.AnnexLibrary) Modification(org.osate.ge.internal.services.AadlModificationService.Modification) Resource(org.eclipse.emf.ecore.resource.Resource) RawDeleteContext(org.osate.ge.businessobjecthandling.RawDeleteContext) IEditorPart(org.eclipse.ui.IEditorPart) URI(org.eclipse.emf.common.util.URI) InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) Entry(java.util.Map.Entry) AadlModificationService(org.osate.ge.internal.services.AadlModificationService) ReferenceBuilderService(org.osate.ge.services.ReferenceBuilderService) EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) List(java.util.List) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ListMultimap(com.google.common.collect.ListMultimap) RawDeleter(org.osate.ge.businessobjecthandling.RawDeleter)

Example 2 with ReferenceBuilderService

use of org.osate.ge.services.ReferenceBuilderService in project osate2 by osate.

the class PropertySectionUtil method execute.

/**
 * Executes an operation in a context which is not associated with a diagram.
 * As such, elements are not added to the diagram based on the hints provided by the step results.
 * @param operation the operation to execute.
 */
public static void execute(final Operation operation) {
    if (operation != null) {
        final Bundle bundle = FrameworkUtil.getBundle(PropertySectionUtil.class);
        final IEclipseContext context = EclipseContextFactory.getServiceContext(bundle.getBundleContext());
        final AadlModificationService aadlModService = Objects.requireNonNull(context.getActive(AadlModificationService.class), "Unable to retrieve AADL modification service");
        final ReferenceBuilderService referenceBuilder = Objects.requireNonNull(context.getActive(ReferenceBuilderService.class), "Unable to retrieve reference builder service");
        final OperationExecutor operationExecutor = new OperationExecutor(aadlModService, referenceBuilder);
        operationExecutor.execute(operation);
    }
}
Also used : AadlModificationService(org.osate.ge.internal.services.AadlModificationService) ReferenceBuilderService(org.osate.ge.services.ReferenceBuilderService) Bundle(org.osgi.framework.Bundle) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) OperationExecutor(org.osate.ge.internal.operations.OperationExecutor)

Example 3 with ReferenceBuilderService

use of org.osate.ge.services.ReferenceBuilderService in project osate2 by osate.

the class PasteAction method run.

@Override
public void run() {
    // Get services
    final Bundle bundle = FrameworkUtil.getBundle(getClass());
    final IEclipseContext context = EclipseContextFactory.getServiceContext(bundle.getBundleContext());
    final AadlModificationService aadlModificationService = Objects.requireNonNull(context.getActive(AadlModificationService.class), "Unable to retrieve AADL modification service");
    final ReferenceBuilderService refBuilder = Objects.requireNonNull(context.getActive(ReferenceBuilderService.class), "Unable to retrieve reference builder service");
    // Perform modification
    final DiagramNode dstDiagramNode = getDestinationDiagramNode();
    final EObject dstBo = getDestinationEObject(dstDiagramNode);
    final AgeDiagram diagram = DiagramElementUtil.getDiagram(dstDiagramNode);
    final List<DiagramElement> newDiagramElements = new ArrayList<>();
    diagram.modify("Paste", m -> {
        // The modifier will do the actual copying to the diagram elements. It will also copy the business objects
        // if the copied element includes the business object.
        final SimpleModifier<EObject> modifier = dstBoToModify -> {
            newDiagramElements.addAll(copyClipboardContents(dstBoToModify, dstDiagramNode, m, refBuilder));
        };
        // If any non-embedded business objects have been copied, then modify the AADL model. Otherwise, just modify the diagram.
        final boolean anyHaveNonEmbeddedBo = getCopiedDiagramElements().stream().anyMatch(de -> de.getCopiedBusinessObject() instanceof EObject);
        if (anyHaveNonEmbeddedBo) {
            aadlModificationService.modify(dstBo, modifier);
        } else {
            modifier.modify(null);
        }
        // Update the diagram. This will set business objects and update the diagram to be consistent.
        editor.getDiagramUpdater().updateDiagram(diagram);
    });
    // Update selection to match created diagram elements
    editor.selectDiagramNodes(newDiagramElements);
}
Also used : Element(org.osate.aadl2.Element) BusinessObjectHandler(org.osate.ge.businessobjecthandling.BusinessObjectHandler) RenameUtil(org.osate.ge.internal.ui.RenameUtil) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) DiagramElementLayoutUtil(org.osate.ge.internal.diagram.runtime.layout.DiagramElementLayoutUtil) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) ClipboardService(org.osate.ge.internal.services.ClipboardService) ClassifierCreationHelper(org.osate.ge.aadl2.internal.util.classifiers.ClassifierCreationHelper) Classifier(org.osate.aadl2.Classifier) ComponentType(org.osate.aadl2.ComponentType) DiagramNodePredicates(org.osate.ge.internal.diagram.runtime.DiagramNodePredicates) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) Bundle(org.osgi.framework.Bundle) ReferenceBuilderService(org.osate.ge.services.ReferenceBuilderService) Collection(java.util.Collection) PackageSection(org.osate.aadl2.PackageSection) EObject(org.eclipse.emf.ecore.EObject) AadlPackage(org.osate.aadl2.AadlPackage) AadlImportsUtil(org.osate.ge.aadl2.AadlImportsUtil) Point(org.osate.ge.graphics.Point) DiagramElementUtil(org.osate.ge.internal.util.DiagramElementUtil) Objects(java.util.Objects) List(java.util.List) AadlModificationService(org.osate.ge.internal.services.AadlModificationService) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) EclipseContextFactory(org.eclipse.e4.core.contexts.EclipseContextFactory) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) Feature(org.osate.aadl2.Feature) ComponentImplementation(org.osate.aadl2.ComponentImplementation) DiagramModification(org.osate.ge.internal.diagram.runtime.DiagramModification) ArrayList(java.util.ArrayList) InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) EClass(org.eclipse.emf.ecore.EClass) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Predicates(com.google.common.base.Predicates) GetNameContext(org.osate.ge.businessobjecthandling.GetNameContext) SimpleModifier(org.osate.ge.internal.services.AadlModificationService.SimpleModifier) Subcomponent(org.osate.aadl2.Subcomponent) XtextResource(org.eclipse.xtext.resource.XtextResource) AadlNameUtil(org.osate.ge.aadl2.internal.util.AadlNameUtil) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) Action(org.eclipse.jface.action.Action) CanRenameContext(org.osate.ge.businessobjecthandling.CanRenameContext) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) AgeHandlerUtil(org.osate.ge.internal.ui.handlers.AgeHandlerUtil) ComponentTypeRename(org.osate.aadl2.ComponentTypeRename) ActionFactory(org.eclipse.ui.actions.ActionFactory) NamedElement(org.osate.aadl2.NamedElement) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) AadlModificationService(org.osate.ge.internal.services.AadlModificationService) ReferenceBuilderService(org.osate.ge.services.ReferenceBuilderService) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) EObject(org.eclipse.emf.ecore.EObject) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext)

Example 4 with ReferenceBuilderService

use of org.osate.ge.services.ReferenceBuilderService in project osate2 by osate.

the class PasteAction method copyClipboardContents.

/**
 * Copies the clipboard contents to the destination business object and diagram element.
 * @return the diagram elements which were created. Does not include children of created diagram elements.
 */
private Collection<DiagramElement> copyClipboardContents(final EObject dstBoToModify, final DiagramNode dstDiagramNode, final DiagramModification m, final ReferenceBuilderService refBuilder) {
    // Determine the minimum coordinates from the elements whose positions will be copied
    // The minimum coordinates is null if none of the copied diagram elements have an absolute position. This is reasonable because the minimum coordinates
    // are only needed if a copied diagram element has an absolute position.
    final Point minCoordinates = getCopiedDiagramElements().stream().map(CopiedDiagramElement::getAbsolutePosition).filter(Predicates.notNull()).reduce((a, b) -> new Point(Math.min(a.x, b.x), Math.min(a.y, b.y))).orElse(null);
    // This list will contain the diagram elements that are created by the copying process. Does not contain their children.
    final List<DiagramElement> newDiagramElements = new ArrayList<>();
    // Copy each copied diagram element into the diagram and model.
    for (final CopiedDiagramElement copiedDiagramElement : getCopiedDiagramElements()) {
        final DiagramElement newDiagramElement;
        if (copiedDiagramElement.getCopiedBusinessObject() == null) {
            newDiagramElement = CopyAndPasteUtil.copyDiagramElement(copiedDiagramElement.getDiagramElement(), dstDiagramNode, copiedDiagramElement.getDiagramElement().getRelativeReference(), refBuilder);
        } else {
            final Object boFromCopiedDiagramElement = copiedDiagramElement.getCopiedBusinessObject();
            final RelativeBusinessObjectReference newRelativeRef;
            if (boFromCopiedDiagramElement instanceof EmbeddedBusinessObject) {
                // The relative reference will be assigned when copying the diagram element.
                newRelativeRef = null;
            } else if (boFromCopiedDiagramElement instanceof EObject) {
                // Get the list that to which the copied object will be added
                final EStructuralFeature compatibleFeature = getCompatibleStructuralFeature(copiedDiagramElement.getContainingFeature(), dstBoToModify.eClass());
                final Object containingFeatureValue = dstBoToModify.eGet(compatibleFeature);
                if (!(containingFeatureValue instanceof Collection)) {
                    throw new RuntimeException("Unexpected case. Value of containing feature was not a collection");
                }
                @SuppressWarnings("unchecked") final Collection<EObject> containingFeatureValueCollection = (Collection<EObject>) containingFeatureValue;
                final EObject copiedEObject = EcoreUtil.copy((EObject) boFromCopiedDiagramElement);
                containingFeatureValueCollection.add(copiedEObject);
                ensureBusinessObjectHasUniqueName(copiedEObject, copiedDiagramElement.getDiagramElement().getBusinessObjectHandler());
                ensurePackagesAreImported(copiedEObject);
                newRelativeRef = refBuilder.getRelativeReference(copiedEObject);
            } else {
                throw new RuntimeException("Unsupported case:  " + boFromCopiedDiagramElement);
            }
            newDiagramElement = CopyAndPasteUtil.copyDiagramElement(copiedDiagramElement.getDiagramElement(), dstDiagramNode, newRelativeRef, refBuilder);
        }
        // Set the position of the new diagram element. They are positioned relative to each other at a fixed offset within the new parent.
        final Point cp = copiedDiagramElement.getAbsolutePosition();
        final Point newPosition = cp == null ? null : new Point(cp.x - minCoordinates.x + 50, cp.y - minCoordinates.y + 50);
        DiagramElementLayoutUtil.moveElement(m, newDiagramElement, newPosition);
        // Remove existing element
        final DiagramElement existingDiagramElement = dstDiagramNode.getChildByRelativeReference(newDiagramElement.getRelativeReference());
        if (existingDiagramElement != null) {
            m.removeElement(existingDiagramElement);
        }
        // Add the new diagram element to the diagram.
        m.addElement(newDiagramElement);
        newDiagramElements.add(newDiagramElement);
    }
    return newDiagramElements;
}
Also used : Element(org.osate.aadl2.Element) BusinessObjectHandler(org.osate.ge.businessobjecthandling.BusinessObjectHandler) RenameUtil(org.osate.ge.internal.ui.RenameUtil) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) DiagramElementLayoutUtil(org.osate.ge.internal.diagram.runtime.layout.DiagramElementLayoutUtil) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) ClipboardService(org.osate.ge.internal.services.ClipboardService) ClassifierCreationHelper(org.osate.ge.aadl2.internal.util.classifiers.ClassifierCreationHelper) Classifier(org.osate.aadl2.Classifier) ComponentType(org.osate.aadl2.ComponentType) DiagramNodePredicates(org.osate.ge.internal.diagram.runtime.DiagramNodePredicates) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) Bundle(org.osgi.framework.Bundle) ReferenceBuilderService(org.osate.ge.services.ReferenceBuilderService) Collection(java.util.Collection) PackageSection(org.osate.aadl2.PackageSection) EObject(org.eclipse.emf.ecore.EObject) AadlPackage(org.osate.aadl2.AadlPackage) AadlImportsUtil(org.osate.ge.aadl2.AadlImportsUtil) Point(org.osate.ge.graphics.Point) DiagramElementUtil(org.osate.ge.internal.util.DiagramElementUtil) Objects(java.util.Objects) List(java.util.List) AadlModificationService(org.osate.ge.internal.services.AadlModificationService) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) EclipseContextFactory(org.eclipse.e4.core.contexts.EclipseContextFactory) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) Feature(org.osate.aadl2.Feature) ComponentImplementation(org.osate.aadl2.ComponentImplementation) DiagramModification(org.osate.ge.internal.diagram.runtime.DiagramModification) ArrayList(java.util.ArrayList) InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) EClass(org.eclipse.emf.ecore.EClass) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Predicates(com.google.common.base.Predicates) GetNameContext(org.osate.ge.businessobjecthandling.GetNameContext) SimpleModifier(org.osate.ge.internal.services.AadlModificationService.SimpleModifier) Subcomponent(org.osate.aadl2.Subcomponent) XtextResource(org.eclipse.xtext.resource.XtextResource) AadlNameUtil(org.osate.ge.aadl2.internal.util.AadlNameUtil) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) Action(org.eclipse.jface.action.Action) CanRenameContext(org.osate.ge.businessobjecthandling.CanRenameContext) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) AgeHandlerUtil(org.osate.ge.internal.ui.handlers.AgeHandlerUtil) ComponentTypeRename(org.osate.aadl2.ComponentTypeRename) ActionFactory(org.eclipse.ui.actions.ActionFactory) NamedElement(org.osate.aadl2.NamedElement) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) ArrayList(java.util.ArrayList) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) Point(org.osate.ge.graphics.Point) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) EObject(org.eclipse.emf.ecore.EObject) Collection(java.util.Collection) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) EObject(org.eclipse.emf.ecore.EObject)

Example 5 with ReferenceBuilderService

use of org.osate.ge.services.ReferenceBuilderService in project osate2 by osate.

the class ShowElementHandler method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (!(activeEditor instanceof InternalDiagramEditor)) {
        throw new RuntimeException("Unexpected editor: " + activeEditor);
    }
    // Get diagram and selected elements
    final InternalDiagramEditor diagramEditor = (InternalDiagramEditor) activeEditor;
    final List<BusinessObjectContext> bocsToAdd = AgeHandlerUtil.getSelectedBusinessObjectContexts().stream().filter(boc -> !(boc instanceof DiagramNode)).collect(Collectors.toList());
    final AgeDiagram diagram = diagramEditor.getDiagram();
    if (diagram == null) {
        throw new RuntimeException("Unable to get diagram");
    }
    // Get services
    final DiagramUpdater diagramUpdater = diagramEditor.getDiagramUpdater();
    final LayoutInfoProvider layoutInfoProvider = Objects.requireNonNull(Adapters.adapt(diagramEditor, LayoutInfoProvider.class), "Unable to retrieve layout info provider");
    final ReferenceBuilderService refBuilder = Objects.requireNonNull(Adapters.adapt(diagramEditor, ReferenceBuilderService.class), "Unable to retrieve reference builder");
    final BusinessObjectNode boTree = DiagramToBusinessObjectTreeConverter.createBusinessObjectNode(diagram);
    // Add the selected elements to the business object tree
    for (final BusinessObjectContext bocToAdd : bocsToAdd) {
        // Get list of nodes that represents the path from the root to the business object context we need to add
        final LinkedList<BusinessObjectContext> bocPath = new LinkedList<>();
        for (BusinessObjectContext tmp = bocToAdd; !(tmp instanceof AgeDiagram); tmp = tmp.getParent()) {
            bocPath.addFirst(tmp);
        }
        // For each element in that path. Create BusinessObjectNode if it doesn't exist
        BusinessObjectNode parentNode = boTree;
        for (final BusinessObjectContext tmpBoc : bocPath) {
            // Get relative reference of the business object
            final RelativeBusinessObjectReference ref;
            if (tmpBoc instanceof DiagramElement) {
                ref = ((DiagramElement) tmpBoc).getRelativeReference();
            } else {
                ref = refBuilder.getRelativeReference(tmpBoc.getBusinessObject());
            }
            // Look for a matching node
            BusinessObjectNode childNode = parentNode.getChild(ref);
            // If it doesn't exist, create it
            if (childNode == null) {
                childNode = new BusinessObjectNode(parentNode, UUID.randomUUID(), ref, tmpBoc.getBusinessObject(), Completeness.UNKNOWN, false);
            }
            // Continue to the next element in the path using the child node as the new parent
            parentNode = childNode;
        }
    }
    // Update the diagram
    diagramEditor.getActionExecutor().execute("Show", ExecutionMode.NORMAL, () -> {
        // Update the diagram
        diagramUpdater.updateDiagram(diagram, boTree);
        // Update layout
        diagram.modify("Layout Incrementally", m -> DiagramElementLayoutUtil.layoutIncrementally(diagram, m, layoutInfoProvider));
        return null;
    });
    return null;
}
Also used : ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) DiagramToBusinessObjectTreeConverter(org.osate.ge.internal.diagram.runtime.updating.DiagramToBusinessObjectTreeConverter) DiagramElementLayoutUtil(org.osate.ge.internal.diagram.runtime.layout.DiagramElementLayoutUtil) BusinessObjectNode(org.osate.ge.internal.diagram.runtime.updating.BusinessObjectNode) HandlerUtil(org.eclipse.ui.handlers.HandlerUtil) InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) BusinessObjectContext(org.osate.ge.BusinessObjectContext) LinkedList(java.util.LinkedList) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) Completeness(org.osate.ge.internal.diagram.runtime.updating.Completeness) IEditorPart(org.eclipse.ui.IEditorPart) DiagramUpdater(org.osate.ge.internal.diagram.runtime.updating.DiagramUpdater) ReferenceBuilderService(org.osate.ge.services.ReferenceBuilderService) UUID(java.util.UUID) ExecutionException(org.eclipse.core.commands.ExecutionException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Adapters(org.eclipse.core.runtime.Adapters) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) List(java.util.List) ExecutionMode(org.osate.ge.internal.services.ActionExecutor.ExecutionMode) AbstractHandler(org.eclipse.core.commands.AbstractHandler) LayoutInfoProvider(org.osate.ge.internal.diagram.runtime.layout.LayoutInfoProvider) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) DiagramUpdater(org.osate.ge.internal.diagram.runtime.updating.DiagramUpdater) IEditorPart(org.eclipse.ui.IEditorPart) LinkedList(java.util.LinkedList) InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) BusinessObjectNode(org.osate.ge.internal.diagram.runtime.updating.BusinessObjectNode) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) ReferenceBuilderService(org.osate.ge.services.ReferenceBuilderService) BusinessObjectContext(org.osate.ge.BusinessObjectContext) LayoutInfoProvider(org.osate.ge.internal.diagram.runtime.layout.LayoutInfoProvider)

Aggregations

ReferenceBuilderService (org.osate.ge.services.ReferenceBuilderService)8 DiagramElement (org.osate.ge.internal.diagram.runtime.DiagramElement)6 AgeDiagram (org.osate.ge.internal.diagram.runtime.AgeDiagram)5 AadlModificationService (org.osate.ge.internal.services.AadlModificationService)5 InternalDiagramEditor (org.osate.ge.internal.ui.editor.InternalDiagramEditor)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Objects (java.util.Objects)4 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)4 EObject (org.eclipse.emf.ecore.EObject)4 BusinessObjectHandler (org.osate.ge.businessobjecthandling.BusinessObjectHandler)4 Bundle (org.osgi.framework.Bundle)4 Predicates (com.google.common.base.Predicates)3 Collection (java.util.Collection)3 EcoreUtil (org.eclipse.emf.ecore.util.EcoreUtil)3 IEditorPart (org.eclipse.ui.IEditorPart)3 RelativeBusinessObjectReference (org.osate.ge.RelativeBusinessObjectReference)3 Collections (java.util.Collections)2 AbstractHandler (org.eclipse.core.commands.AbstractHandler)2 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)2