Search in sources :

Example 1 with FlowImplementationSelectionDialog

use of org.osate.ge.aadl2.ui.internal.editor.FlowContributionItemUtil.FlowImplementationSelectionDialog in project osate2 by osate.

the class DeleteFlowContributionItem method createControl.

@Override
protected Control createControl(final Composite parent) {
    deleteFlowBtn = new Button(parent, SWT.PUSH);
    deleteFlowBtn.setImage(deleteImage);
    deleteFlowBtn.setToolTipText("Delete");
    updateButton();
    deleteFlowBtn.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(final SelectionEvent e) {
            if (editor != null && selectedFlow != null) {
                final AadlModificationService aadlModificationService = Objects.requireNonNull((AadlModificationService) editor.getAdapter(AadlModificationService.class), "Unable to retrieve modification service");
                getFlowToRemove().ifPresent(ne -> aadlModificationService.modify(ne, EcoreUtil::remove));
            }
        }

        /**
         * Get the flow to modify/remove
         * @return the flow to modify/remove
         */
        private Optional<NamedElement> getFlowToRemove() {
            final NamedElement selectedFlowSegment = selectedFlow.getFlowSegment();
            final BusinessObjectContext container = selectedFlow.getContainer();
            final ComponentImplementation componentImpl = FlowContributionItemUtil.getComponentImplementation(container);
            if (selectedFlowSegment instanceof EndToEndFlow) {
                final ComponentImplementation endToEndFlowContainer = AgeEmfUtil.resolveOrNull(selectedFlowSegment.getContainingComponentImpl(), ComponentImplementation.class, componentImpl.eResource().getResourceSet());
                return Optional.of(AgeEmfUtil.resolveOrNull(selectedFlowSegment, EndToEndFlow.class, endToEndFlowContainer.eResource().getResourceSet()));
            } else if (selectedFlowSegment instanceof FlowSpecification) {
                final FlowSpecification flowSpecification = AgeEmfUtil.resolveOrNull(selectedFlowSegment, FlowSpecification.class, componentImpl.eResource().getResourceSet());
                // If there are multiple flow implementations for the flow spec, choose desired one.
                final List<FlowImplementation> flowImpls = componentImpl.getOwnedFlowImplementations().stream().filter(fi -> fi.getSpecification() == flowSpecification).collect(Collectors.toList());
                return getFlowImplementation(flowImpls);
            } else {
                throw new RuntimeException("Unexpected flow type: " + selectedFlow.getFlowSegment());
            }
        }

        private Optional<NamedElement> getFlowImplementation(final List<FlowImplementation> flowImpls) {
            if (flowImpls.size() == 1) {
                return Optional.of(flowImpls.get(0));
            } else {
                final FlowImplementationSelectionDialog dlg = new FlowImplementationSelectionDialog(Display.getCurrent().getActiveShell(), flowImpls, "Select", "Choose the flow implementation to delete.");
                if (dlg.open() == Window.OK) {
                    return Optional.ofNullable(dlg.getSelectedFlowImplementation());
                }
            }
            return Optional.empty();
        }
    });
    return deleteFlowBtn;
}
Also used : ComponentImplementation(org.osate.aadl2.ComponentImplementation) Image(org.eclipse.swt.graphics.Image) InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) BusinessObjectContext(org.osate.ge.BusinessObjectContext) FlowImplementationSelectionDialog(org.osate.ge.aadl2.ui.internal.editor.FlowContributionItemUtil.FlowImplementationSelectionDialog) AgeEmfUtil(org.osate.ge.internal.util.AgeEmfUtil) HighlightableFlowInfo(org.osate.ge.aadl2.ui.internal.editor.FlowContributionItem.HighlightableFlowInfo) Composite(org.eclipse.swt.widgets.Composite) IEditorPart(org.eclipse.ui.IEditorPart) ControlContribution(org.eclipse.jface.action.ControlContribution) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) FlowSpecification(org.osate.aadl2.FlowSpecification) Button(org.eclipse.swt.widgets.Button) PlatformUI(org.eclipse.ui.PlatformUI) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) Display(org.eclipse.swt.widgets.Display) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ISharedImages(org.eclipse.ui.ISharedImages) List(java.util.List) Window(org.eclipse.jface.window.Window) FlowImplementation(org.osate.aadl2.FlowImplementation) SWT(org.eclipse.swt.SWT) AadlModificationService(org.osate.ge.internal.services.AadlModificationService) Optional(java.util.Optional) SelectionEvent(org.eclipse.swt.events.SelectionEvent) NamedElement(org.osate.aadl2.NamedElement) Control(org.eclipse.swt.widgets.Control) EndToEndFlow(org.osate.aadl2.EndToEndFlow) ComponentImplementation(org.osate.aadl2.ComponentImplementation) EndToEndFlow(org.osate.aadl2.EndToEndFlow) Optional(java.util.Optional) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) FlowImplementation(org.osate.aadl2.FlowImplementation) FlowImplementationSelectionDialog(org.osate.ge.aadl2.ui.internal.editor.FlowContributionItemUtil.FlowImplementationSelectionDialog) AadlModificationService(org.osate.ge.internal.services.AadlModificationService) FlowSpecification(org.osate.aadl2.FlowSpecification) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) NamedElement(org.osate.aadl2.NamedElement) BusinessObjectContext(org.osate.ge.BusinessObjectContext)

Example 2 with FlowImplementationSelectionDialog

use of org.osate.ge.aadl2.ui.internal.editor.FlowContributionItemUtil.FlowImplementationSelectionDialog in project osate2 by osate.

the class EditFlowContributionItem method createControl.

@Override
protected Control createControl(final Composite parent) {
    editFlowBtn = new Button(parent, SWT.PUSH);
    editFlowBtn.setImage(editIcon.createImage());
    editFlowBtn.setToolTipText("Edit...");
    updateButton();
    editFlowBtn.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(final SelectionEvent e) {
            segmentNodes.clear();
            final DiagramElement container = selectedHighlightableFlow.getDiagramElementContainer();
            final NamedElement flowSegment = AgeAadlUtil.getRootRefinedElement(selectedHighlightableFlow.getFlowSegment());
            final ComponentImplementation ci = FlowContributionItemUtil.getComponentImplementation(container.getBusinessObject());
            // Set focus to editor for activating create flow tool
            editor.setFocus();
            editor.selectDiagramNodes(Collections.singletonList(editor.getDiagram()));
            final UiService uiService = Adapters.adapt(editor, UiService.class);
            // Create dialog and activate appropriate flow tool
            if (flowSegment instanceof EndToEndFlow) {
                final EndToEndFlow endToEndFlow = AgeEmfUtil.resolveOrNull(flowSegment, EndToEndFlow.class, ci.eResource().getResourceSet());
                // Activate tool
                uiService.activateTool(new CreateEndToEndFlowSpecificationTool(editor, container, endToEndFlow));
            } else if (flowSegment instanceof FlowSpecification) {
                final FlowSpecification fs = AgeEmfUtil.resolveOrNull(flowSegment, FlowSpecification.class, ci.eResource().getResourceSet());
                getFlowImplementation(ci, fs).ifPresent(fi -> {
                    uiService.activateTool(new CreateFlowImplementationTool(editor, container, fi));
                });
            } else {
                throw new RuntimeException("Unsupported flow type.");
            }
        }

        private Optional<FlowImplementation> getFlowImplementation(final ComponentImplementation ci, final FlowSpecification flowSpec) {
            final List<FlowImplementation> flowImpls = ci.getAllFlowImplementations().stream().filter(fi -> fi.getSpecification() == flowSpec).collect(Collectors.toList());
            if (flowImpls.size() == 1) {
                return Optional.of(flowImpls.get(0));
            } else {
                final FlowImplementationSelectionDialog dlg = new FlowImplementationSelectionDialog(Display.getCurrent().getActiveShell(), flowImpls, "Select", "Choose the flow implementation to edit.");
                if (dlg.open() == Window.OK) {
                    return Optional.ofNullable(dlg.getSelectedFlowImplementation());
                }
            }
            return Optional.empty();
        }
    });
    return editFlowBtn;
}
Also used : ComponentImplementation(org.osate.aadl2.ComponentImplementation) EndToEndFlow(org.osate.aadl2.EndToEndFlow) CreateFlowImplementationTool(org.osate.ge.aadl2.ui.internal.tools.CreateFlowImplementationTool) Optional(java.util.Optional) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) FlowImplementationSelectionDialog(org.osate.ge.aadl2.ui.internal.editor.FlowContributionItemUtil.FlowImplementationSelectionDialog) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) UiService(org.osate.ge.internal.services.UiService) CreateEndToEndFlowSpecificationTool(org.osate.ge.aadl2.ui.internal.tools.CreateEndToEndFlowSpecificationTool) FlowSpecification(org.osate.aadl2.FlowSpecification) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) List(java.util.List) NamedElement(org.osate.aadl2.NamedElement)

Aggregations

List (java.util.List)2 Optional (java.util.Optional)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 Button (org.eclipse.swt.widgets.Button)2 ComponentImplementation (org.osate.aadl2.ComponentImplementation)2 EndToEndFlow (org.osate.aadl2.EndToEndFlow)2 FlowSpecification (org.osate.aadl2.FlowSpecification)2 NamedElement (org.osate.aadl2.NamedElement)2 FlowImplementationSelectionDialog (org.osate.ge.aadl2.ui.internal.editor.FlowContributionItemUtil.FlowImplementationSelectionDialog)2 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 EcoreUtil (org.eclipse.emf.ecore.util.EcoreUtil)1 ControlContribution (org.eclipse.jface.action.ControlContribution)1 Window (org.eclipse.jface.window.Window)1 SWT (org.eclipse.swt.SWT)1 Image (org.eclipse.swt.graphics.Image)1 Composite (org.eclipse.swt.widgets.Composite)1 Control (org.eclipse.swt.widgets.Control)1 Display (org.eclipse.swt.widgets.Display)1