use of org.osate.aadl2.DefaultAnnexSubclause in project osate2 by osate.
the class ConfigureInModesSection method addLocalMode.
private void addLocalMode(final Composite container, final Map.Entry<ModeFeature, ButtonState> entry, final Set<ModeFeature> derivedModesAvailable, final Map<ModeFeature, ModeFeature> localToDerivedModeMap, final Set<URI> urisOfElementsWhichRequireModes) {
final ModeFeature mf = entry.getKey();
final Button modeBtn = getWidgetFactory().createButton(container, mf.getName(), SWT.CHECK);
// Create derived mode drop down
final ComboViewer derivedModeFld;
final Label mappedLabel;
if (derivedModesAvailable == null) {
derivedModeFld = null;
mappedLabel = null;
} else {
mappedLabel = getWidgetFactory().createLabel(container, "->", SWT.CENTER);
mappedLabel.setText("->");
// Create mapped derived mode combo
derivedModeFld = new ComboViewer(container, SWT.DROP_DOWN | SWT.READ_ONLY);
derivedModeFld.setContentProvider(ArrayContentProvider.getInstance());
derivedModeFld.setLabelProvider(new LabelProvider() {
@Override
public String getText(final Object element) {
if (element instanceof ModeFeature) {
final ModeFeature modalFeature = (ModeFeature) element;
return modalFeature.getName();
}
return element.toString();
}
});
derivedModeFld.add(" ");
derivedModeFld.add(derivedModesAvailable.toArray());
final ModeFeature mappedDerivedMode = localToDerivedModeMap.get(mf);
// If child mode is contained in intersection of derived modes
if (derivedModesAvailable.contains(mappedDerivedMode)) {
derivedModeFld.setSelection(new StructuredSelection(mappedDerivedMode));
}
}
// Set button state
final ButtonState modeFeatureState = entry.getValue();
if (modeFeatureState == ButtonState.SELECTED) {
modeBtn.setSelection(true);
} else if (modeFeatureState == ButtonState.PARTIAL) {
modeBtn.setSelection(true);
modeBtn.setGrayed(true);
} else if (modeFeatureState == ButtonState.DISABLED_AND_PARTIAL || modeFeatureState == ButtonState.DISABLED) {
modeBtn.setEnabled(false);
boolean partialDisabled = modeFeatureState == ButtonState.DISABLED_AND_PARTIAL;
modeBtn.setGrayed(partialDisabled);
modeBtn.setSelection(partialDisabled);
if (derivedModeFld != null) {
derivedModeFld.getCombo().setEnabled(false);
mappedLabel.setEnabled(false);
}
}
final SelectionListener selectionListener = new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
// No changes if combo selection changes without enabled button
if (e.widget instanceof Combo && !modeBtn.getSelection()) {
return;
}
// Modify selected modal elements
final boolean modeBtnIsSelected = modeBtn.getSelection();
selectedBos.modify("Set In Modes", boc -> boc.getBusinessObject(NamedElement.class).isPresent(), boc -> {
final NamedElement ne = boc.getBusinessObject(NamedElement.class).get();
if (ne instanceof AnnexSubclause && ne.eContainer() instanceof DefaultAnnexSubclause) {
return (NamedElement) ne.eContainer();
}
return ne;
}, (ne, boc) -> {
final ModeFeature modeFeature = (ModeFeature) EcoreUtil.resolve(mf, ne.eResource());
if (ne instanceof Subcomponent && modeFeature instanceof Mode) {
final Subcomponent sc = (Subcomponent) ne;
// Remove mode binding always
for (final ModeBinding mb : sc.getOwnedModeBindings()) {
if (modeFeature.getName().equalsIgnoreCase(mb.getParentMode().getName())) {
sc.getOwnedModeBindings().remove(mb);
break;
}
}
// Add mode binding on button selection
if (modeBtnIsSelected) {
final ModeBinding newModeBinding = sc.createOwnedModeBinding();
newModeBinding.setParentMode((Mode) modeFeature);
final boolean isDerived = urisOfElementsWhichRequireModes.contains(EcoreUtil.getURI(ne));
// If modal element is derived, set derived mode
if (isDerived) {
final Object selection = ((StructuredSelection) derivedModeFld.getSelection()).getFirstElement();
final ModeFeature childMode = selection instanceof ModeFeature ? (ModeFeature) selection : null;
newModeBinding.setDerivedMode((Mode) childMode);
}
}
} else if (ne instanceof ModalPath) {
final ModalPath mp = (ModalPath) ne;
if (modeBtnIsSelected) {
mp.getInModeOrTransitions().add(modeFeature);
} else {
for (final ModeFeature mf : mp.getInModeOrTransitions()) {
if (modeFeature.getName().equalsIgnoreCase(mf.getName())) {
mp.getInModeOrTransitions().remove(mf);
break;
}
}
}
} else if (ne instanceof ModalElement && modeFeature instanceof Mode) {
final ModalElement modalElement = (ModalElement) ne;
if (modeBtnIsSelected) {
modalElement.getAllInModes().add((Mode) modeFeature);
} else {
for (final ModeFeature mf : modalElement.getInModes()) {
if (modeFeature.getName().equalsIgnoreCase(mf.getName())) {
modalElement.getAllInModes().remove(modeFeature);
break;
}
}
}
}
});
}
};
// Register selection listeners
modeBtn.addSelectionListener(selectionListener);
if (derivedModeFld != null) {
derivedModeFld.getCombo().addSelectionListener(selectionListener);
}
}
use of org.osate.aadl2.DefaultAnnexSubclause 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);
}
}
use of org.osate.aadl2.DefaultAnnexSubclause in project osate2 by osate.
the class AnnexHandler method getAnnexSubclauseIndex.
/**
* Returns a 0 based index for referencing an annex subclause in a list that contains only annex subclauses with the same type and owner
* @return
*/
public static int getAnnexSubclauseIndex(AnnexSubclause annexSubclause, final boolean useExtended) {
// Get the default annex library if a parsed annex subclause was specified. This is needed for the comparison later in the function.
if (!(annexSubclause instanceof DefaultAnnexSubclause)) {
if (annexSubclause.eContainer() instanceof DefaultAnnexSubclause) {
annexSubclause = (AnnexSubclause) annexSubclause.eContainer();
} else {
return -1;
}
}
final String annexName = annexSubclause.getName();
if (annexName == null) {
return -1;
}
final Classifier cl = annexSubclause.getContainingClassifier();
final List<Classifier> classifiers;
if (useExtended) {
classifiers = cl.getSelfPlusAllExtended();
// Get all related classifiers
if (cl instanceof ComponentImplementation) {
final ComponentType ct = ((ComponentImplementation) cl).getType();
if (ct != null) {
classifiers.addAll(ct.getSelfPlusAllExtended());
}
}
} else {
classifiers = Arrays.asList(cl);
}
int index = 0;
// Use reversed view of list so that base classifiers will be first. This is needed to ensure subclauses have unique indices
for (final Classifier tmpClassifier : Lists.reverse(classifiers)) {
for (final AnnexSubclause tmpSubclause : tmpClassifier.getOwnedAnnexSubclauses()) {
if (tmpSubclause == annexSubclause) {
return index;
} else if (annexName.equalsIgnoreCase(tmpSubclause.getName())) {
index++;
}
}
}
return -1;
}
use of org.osate.aadl2.DefaultAnnexSubclause in project osate2 by osate.
the class OpenBehaviorAnnexDiagramHandler method execute.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final ISelection selection = HandlerUtil.getCurrentSelection(event);
final DefaultAnnexSubclause diagramContext = BehaviorAnnexSelectionUtil.getDefaultBehaviorAnnexSubclause(selection, HandlerUtil.getActiveEditor(event)).orElseThrow(() -> new RuntimeException("diagramContext cannot be null"));
final DiagramService diagramService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(DiagramService.class);
diagramService.openOrCreateDiagramForBusinessObject(diagramContext);
return null;
}
use of org.osate.aadl2.DefaultAnnexSubclause in project osate2 by osate.
the class AnnexAwareCompletionProposalComputer method exec.
/*
* (non-Javadoc)
*
* @see org.eclipse.xtext.ui.editor.contentassist.CompletionProposalComputer#exec(org.eclipse.xtext.resource.XtextResource)
*/
@Override
public ICompletionProposal[] exec(XtextResource resource) throws Exception {
INode node = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset);
IParseResult originalResult = resource.getParseResult();
try {
if (node != null) {
EObject semanticObject = NodeModelUtils.findActualSemanticObjectFor(node);
if (semanticObject != null) {
EObject annexObject = null;
if (semanticObject instanceof DefaultAnnexSubclause) {
annexObject = ((DefaultAnnexSubclause) semanticObject).getParsedAnnexSubclause();
} else if (semanticObject instanceof DefaultAnnexLibrary) {
annexObject = ((DefaultAnnexLibrary) semanticObject).getParsedAnnexLibrary();
}
if (annexObject != null && offset >= NodeModelUtils.findActualNodeFor(semanticObject).getOffset()) {
IParseResult annexParseResult = ParseResultHolder.Factory.INSTANCE.adapt(annexObject).getParseResult();
if (annexParseResult != null) {
Injector injector = AnnexUtil.getInjector(annexParseResult);
if (injector != null) {
MembersInjector<AnnexState> memInject = injector.getMembersInjector(AnnexState.class);
memInject.injectMembers(state);
resource.setParseResult(annexParseResult);
ISelection selection = viewer.getSelectionProvider().getSelection();
String content = AnnexParseUtil.genWhitespace(node.getTotalOffset()) + AnnexUtil.getSourceText(annexObject).replaceFirst("\\{\\*\\*", " ");
IDocument document = new DummyXtextDocument(content);
viewer = new DummyTextViewer(selection, document);
}
}
}
execOriginal(resource);
}
}
} finally {
resource.setParseResult(originalResult);
resource.setEntryPoint(null);
}
return proposals.toArray(new ICompletionProposal[proposals.size()]);
}
Aggregations