use of org.osate.ge.internal.services.AadlModificationService 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;
}
use of org.osate.ge.internal.services.AadlModificationService in project osate2 by osate.
the class SetBindingTool method activated.
@Override
public void activated(final ActivatedEvent ctx) {
final BusinessObjectContext[] selectedBocs = ctx.getSelectedBocs().toArray(new BusinessObjectContext[ctx.getSelectedBocs().size()]);
final AgeDiagram diagram = ctx.getDiagram();
final AadlModificationService aadlModService = ctx.getAadlModificatonService();
final UiService uiService = ctx.getUiService();
try {
final BusinessObjectContext componentImplementationBoc = ToolUtil.findComponentImplementationBoc(selectedBocs[0]);
// Open Dialog
if (currentWindow == null && componentImplementationBoc != null) {
currentWindow = new SetBindingWindow(Display.getCurrent().getActiveShell(), componentImplementationBoc, selectedBocs);
if (currentWindow.open() == Window.OK) {
// Ensure the diagram is configured to show the specified binding property
if (!diagram.getConfiguration().getEnabledAadlPropertyNames().contains(currentWindow.getSelectedProperty().getQualifiedName().toLowerCase())) {
diagram.modify("Configure Diagram", m -> {
m.setDiagramConfiguration(new DiagramConfigurationBuilder(diagram.getConfiguration()).addAadlProperty(currentWindow.getSelectedProperty().getQualifiedName()).build());
});
}
// Create the property association
createPropertyAssociations(aadlModService);
}
currentWindow = null;
}
} finally {
uiService.deactivateActiveTool();
}
}
use of org.osate.ge.internal.services.AadlModificationService in project osate2 by osate.
the class AgeBusinessObjectSelectionAdapterFactory method getAdapter.
@Override
public <T> T getAdapter(final Object adaptableObject, final Class<T> adapterType) {
final ISelection selection = (ISelection) adaptableObject;
if (BusinessObjectSelection.class.equals(adapterType)) {
final Bundle bundle = FrameworkUtil.getBundle(getClass());
final IEclipseContext context = EclipseContextFactory.getServiceContext(bundle.getBundleContext());
final AadlModificationService modificationService = context.get(AadlModificationService.class);
if (modificationService == null) {
return null;
}
return adapterType.cast(new UiBusinessObjectSelection(SelectionUtil.getSelectedBusinessObjectContexts(selection), modificationService));
} else if (IAadlPropertySource.class.equals(adapterType) && adaptableObject instanceof IStructuredSelection) {
final IStructuredSelection ss = (IStructuredSelection) adaptableObject;
if (ss.getFirstElement() instanceof DiagramElement) {
final DiagramElement de = (DiagramElement) ss.getFirstElement();
// If the business object is an AADL Named Element
if (de.getBusinessObject() instanceof NamedElement) {
final NamedElement namedElement = (NamedElement) de.getBusinessObject();
return adapterType.cast(new IAadlPropertySource() {
private final IXtextDocument document = AgeXtextUtil.getDocumentByRootElement(namedElement.getElementRoot());
private final NamedElement element = namedElement;
@Override
public IXtextDocument getDocument() {
return document;
}
@Override
public NamedElement getNamedElement() {
return element;
}
});
}
}
}
return null;
}
use of org.osate.ge.internal.services.AadlModificationService 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);
}
}
use of org.osate.ge.internal.services.AadlModificationService 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);
}
Aggregations