Search in sources :

Example 1 with DiagramElement

use of org.osate.ge.internal.diagram.runtime.DiagramElement in project osate2 by osate.

the class DeleteHandler method createBusinessObjectRemovalOrRemoveDiagramElement.

/**
 * Creates a BusinessObjectRemoval object which can be used to remove the business object for the diagram element.
 * If the diagram element's business object is an embedded business object, remove the element.
 * @param de
 * @return
 */
private static BusinessObjectRemoval createBusinessObjectRemovalOrRemoveDiagramElement(final DiagramElement de) {
    // Remove the EObject from the model
    final Object bo = de.getBusinessObject();
    final Object boHandler = de.getBusinessObjectHandler();
    if (bo instanceof EObject) {
        EObject boEObj = (EObject) bo;
        if (boHandler instanceof CustomDeleter) {
            final CustomDeleter deleter = (CustomDeleter) boHandler;
            final EObject ownerBo = boEObj.eContainer();
            return new BusinessObjectRemoval(ownerBo, (boToModify) -> {
                deleter.delete(new CustomDeleteContext(boToModify, bo));
            });
        }
        // When deleting AnnexSubclauses, the deletion must executed on the container DefaultAnnexSubclause
        if (boEObj instanceof AnnexSubclause && boEObj.eContainer() instanceof DefaultAnnexSubclause) {
            boEObj = boEObj.eContainer();
        }
        return new BusinessObjectRemoval(boEObj, (boToModify) -> EcoreUtil.remove(boToModify));
    } else if (bo instanceof EmfContainerProvider) {
        if (!(boHandler instanceof CustomDeleter)) {
            throw new RuntimeException("Business object handler '" + boHandler + "' for " + EmfContainerProvider.class.getName() + " based business object must implement " + CustomDeleter.class.getCanonicalName() + ".");
        }
        final CustomDeleter deleter = (CustomDeleter) boHandler;
        final EObject ownerBo = ((EmfContainerProvider) bo).getEmfContainer();
        return new BusinessObjectRemoval(ownerBo, (boToModify) -> {
            deleter.delete(new CustomDeleteContext(boToModify, bo));
        });
    } else if (bo instanceof EmbeddedBusinessObject) {
        // For embedded business objects, there isn't a model from which to remove the business object.
        // Instead, we remove the diagram element and return null.
        final AgeDiagram diagram = DiagramElementUtil.getDiagram(de);
        diagram.modify("Delete Element", m -> m.removeElement(de));
        return null;
    } else {
        // canDelete() should have returned false in this case
        throw new RuntimeException("Unhandled case: " + bo);
    }
}
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) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) EObject(org.eclipse.emf.ecore.EObject) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) EObject(org.eclipse.emf.ecore.EObject) EmfContainerProvider(org.osate.ge.EmfContainerProvider) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) CustomDeleter(org.osate.ge.businessobjecthandling.CustomDeleter) CustomDeleteContext(org.osate.ge.businessobjecthandling.CustomDeleteContext) DefaultAnnexSubclause(org.osate.aadl2.DefaultAnnexSubclause) DefaultAnnexSubclause(org.osate.aadl2.DefaultAnnexSubclause) AnnexSubclause(org.osate.aadl2.AnnexSubclause)

Example 2 with DiagramElement

use of org.osate.ge.internal.diagram.runtime.DiagramElement 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 3 with DiagramElement

use of org.osate.ge.internal.diagram.runtime.DiagramElement in project osate2 by osate.

the class DeleteHandler method calculateEnabled.

private boolean calculateEnabled(final Object evaluationContext) {
    final IEditorPart activeEditor = AgeHandlerUtil.getActiveEditorFromContext(evaluationContext);
    if (!(activeEditor instanceof InternalDiagramEditor)) {
        return false;
    }
    final List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
    return canExecute(selectedDiagramElements);
}
Also used : InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) IEditorPart(org.eclipse.ui.IEditorPart)

Example 4 with DiagramElement

use of org.osate.ge.internal.diagram.runtime.DiagramElement in project osate2 by osate.

the class InstantiateHandler 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 List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
    if (selectedDiagramElements.size() == 0) {
        throw new RuntimeException("No element selected");
    }
    final DiagramElement selectedElement = selectedDiagramElements.get(0);
    final Object bo = selectedElement.getBusinessObject();
    if (!(bo instanceof ComponentImplementation)) {
        throw new RuntimeException("BO is not a component implementation");
    }
    try {
        InstantiateModel.buildInstanceModelFile((ComponentImplementation) bo);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return null;
}
Also used : InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) ComponentImplementation(org.osate.aadl2.ComponentImplementation) IEditorPart(org.eclipse.ui.IEditorPart) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 5 with DiagramElement

use of org.osate.ge.internal.diagram.runtime.DiagramElement in project osate2 by osate.

the class PropertyValueGroupHandler method getGraphicalConfiguration.

@Override
public Optional<GraphicalConfiguration> getGraphicalConfiguration(final GetGraphicalConfigurationContext ctx) {
    final BusinessObjectContext boc = ctx.getBusinessObjectContext();
    final PropertyValueGroup pvg = boc.getBusinessObject(PropertyValueGroup.class).orElseThrow();
    if (pvg.getReferenceId() == null) {
        return Optional.of(createTextGraphicalConfiguration());
    } else {
        // Try to get the referenced element
        final DiagramElement referencedElement = ctx.getDiagram().findElementById(pvg.getReferenceId());
        if (referencedElement == null) {
            return Optional.empty();
        }
        // If the reference is from the child to an ancestor or from an ancestor to a child, show it as text if it is is based on a completely processed
        // property association. Otherwise, don't show it at all.
        final BusinessObjectContext parent = boc.getParent();
        if (BusinessObjectContextUtil.isAncestor(parent, referencedElement) || BusinessObjectContextUtil.isAncestor(referencedElement, parent)) {
            if (pvg.getFirstValueBasedOnCompletelyProcessedAssociation() == null) {
                return Optional.empty();
            } else {
                return Optional.of(createTextGraphicalConfiguration());
            }
        }
        return Optional.of(GraphicalConfigurationBuilder.create().graphic(graphic).style(pvg.isAbstract() ? abstractStyle : referenceStyle).source(boc.getParent()).destination(referencedElement).build());
    }
}
Also used : PropertyValueGroup(org.osate.ge.aadl2.internal.model.PropertyValueGroup) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) BusinessObjectContext(org.osate.ge.BusinessObjectContext)

Aggregations

DiagramElement (org.osate.ge.internal.diagram.runtime.DiagramElement)131 AgeDiagram (org.osate.ge.internal.diagram.runtime.AgeDiagram)45 List (java.util.List)31 Point (org.osate.ge.graphics.Point)29 DiagramNode (org.osate.ge.internal.diagram.runtime.DiagramNode)29 InternalDiagramEditor (org.osate.ge.internal.ui.editor.InternalDiagramEditor)26 Objects (java.util.Objects)25 IEditorPart (org.eclipse.ui.IEditorPart)23 ArrayList (java.util.ArrayList)22 Collectors (java.util.stream.Collectors)22 BusinessObjectContext (org.osate.ge.BusinessObjectContext)21 Collection (java.util.Collection)19 Collections (java.util.Collections)18 Dimension (org.osate.ge.graphics.Dimension)18 DockArea (org.osate.ge.internal.diagram.runtime.DockArea)18 Optional (java.util.Optional)16 AgeConnection (org.osate.ge.graphics.internal.AgeConnection)16 DiagramElementUtil (org.osate.ge.internal.util.DiagramElementUtil)16 ElkPort (org.eclipse.elk.graph.ElkPort)14 Node (javafx.scene.Node)13