Search in sources :

Example 1 with ElementSelectionDialog

use of org.osate.ge.aadl2.ui.internal.dialogs.ElementSelectionDialog in project osate2 by osate.

the class DiagramContextChecker method promptToRelink.

/**
 * Returns whether or not the diagram context was adjusted.
 * @param diagram
 * @param missingContextRef
 * @return
 */
private boolean promptToRelink(final AgeDiagram diagram) {
    final CanonicalBusinessObjectReference missingContextRef = diagram.getConfiguration().getContextBoReference();
    final List<String> refSegs = missingContextRef.getSegments();
    if (refSegs.size() < 2) {
        return false;
    }
    final boolean isPackageRef = DeclarativeReferenceType.PACKAGE.getId().equals(refSegs.get(0));
    final boolean isClassifierRef = DeclarativeReferenceType.CLASSIFIER.getId().equals(refSegs.get(0));
    final boolean isSystemInstance = AadlReferenceUtil.isSystemInstanceReference(missingContextRef);
    if (!isPackageRef && !isClassifierRef && !isSystemInstance) {
        return false;
    }
    // Determine the options to present to the user
    final Collection<?> options;
    String searchPrefix = "";
    if (isPackageRef || isClassifierRef) {
        // Find all packages
        final Collection<IEObjectDescription> packageDescriptions = AadlModelAccessUtil.getAllEObjectsByType(project, Aadl2Package.eINSTANCE.getAadlPackage());
        if (isPackageRef) {
            options = packageDescriptions;
        } else {
            // isClassifierRef
            options = AadlModelAccessUtil.getAllEObjectsByType(project, Aadl2Package.eINSTANCE.getClassifier());
            // Check if the package portion of the qualified name is a valid package.
            // If so, use it as the initial filter
            final String referencedClassifierQualifiedName = refSegs.get(1);
            final String[] qualifiedNameParts = referencedClassifierQualifiedName.split("::");
            if (qualifiedNameParts.length == 2) {
                final String pkgName = qualifiedNameParts[0];
                for (final IEObjectDescription desc : packageDescriptions) {
                    if (desc.getName().toString("::").equalsIgnoreCase(pkgName)) {
                        searchPrefix = pkgName.toLowerCase() + "::";
                    }
                }
            }
        }
    } else if (isSystemInstance) {
        options = findInstanceModelFiles(project, new ArrayList<IPath>());
    } else {
        // Unexpected case: there is already a short circuit for the case where the reference isn't a package or classifier reference
        throw new RuntimeException("Unexpected case");
    }
    // Don't prompt if there aren't any options.
    if (options.size() == 0) {
        return false;
    }
    final ElementSelectionDialog dlg = new ElementSelectionDialog(null, "Missing Diagram Context", "Unable to find diagram context \"" + refService.getLabel(missingContextRef) + "\".\nIf the model element has been renamed, select the new name for the model element.", options);
    dlg.setFilter(searchPrefix);
    if (dlg.open() != Window.OK) {
        return false;
    }
    final CanonicalBusinessObjectReference newContextCanonicalRef;
    final RelativeBusinessObjectReference newContextRelativeRef;
    final Object newContext;
    if (isSystemInstance) {
        final IPath systemInstancePath = (IPath) dlg.getFirstSelectedElement();
        newContextCanonicalRef = AadlReferenceUtil.getCanonicalBusinessObjectReferenceForSystemInstance(systemInstanceLoader, systemInstancePath);
        newContextRelativeRef = AadlReferenceUtil.getRelativeBusinessObjectReferenceForSystemInstance(systemInstanceLoader, systemInstancePath);
        // Create a dummy system instance. It will be replaced as part of the diagram updating process.
        newContext = InstanceFactory.eINSTANCE.createSystemInstance();
    } else {
        final EObject newContextProxy = (EObject) dlg.getFirstSelectedElement();
        // Find the live object
        final ResourceSet liveResourceSet = AadlModelAccessUtil.getLiveResourceSet(project);
        newContext = EcoreUtil.resolve(newContextProxy, liveResourceSet);
        if (((EObject) newContext).eIsProxy()) {
            throw new RuntimeException("Unable to retrieve non-proxy object for selection");
        }
        // Find canonical and relative reference
        newContextCanonicalRef = refService.getCanonicalReference(newContext);
        if (newContextCanonicalRef == null) {
            throw new RuntimeException("Unable to retrieve reference for new diagram context: " + newContext);
        }
        newContextRelativeRef = refService.getRelativeReference(newContext);
        if (newContextRelativeRef == null) {
            throw new RuntimeException("Unable to retrieve relative reference for new diagram context: " + newContext);
        }
    }
    // Update the diagram
    diagram.modify("Update Diagram Context", m -> {
        // Update the diagram's context
        m.setDiagramConfiguration(new DiagramConfigurationBuilder(diagram.getConfiguration()).contextBoReference(newContextCanonicalRef).build());
        // Update the root element
        if (diagram.getChildren().size() == 1) {
            m.updateBusinessObject(diagram.getChildren().stream().findAny().get(), newContext, newContextRelativeRef);
        }
    });
    return true;
}
Also used : IPath(org.eclipse.core.runtime.IPath) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ElementSelectionDialog(org.osate.ge.aadl2.ui.internal.dialogs.ElementSelectionDialog) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) CanonicalBusinessObjectReference(org.osate.ge.CanonicalBusinessObjectReference) EObject(org.eclipse.emf.ecore.EObject) DiagramConfigurationBuilder(org.osate.ge.internal.diagram.runtime.DiagramConfigurationBuilder) EObject(org.eclipse.emf.ecore.EObject)

Example 2 with ElementSelectionDialog

use of org.osate.ge.aadl2.ui.internal.dialogs.ElementSelectionDialog in project osate2 by osate.

the class AadlUiUtil method getBusinessObjectToModify.

/**
 * Returns the business object from the specified list which should be modified. If there are multiple objects in the specified list,
 * the user will be prompted to select one. The first element will be the default. Must be called from the UI thread.
 * @param potentialBusinessObjects must have at least one element. If empty, an exception will be thrown.
 * @return the object to modify. Will return null if the user cancels the selection prompt.
 */
@SuppressWarnings("unchecked")
public static <BusinessObjectType> BusinessObjectType getBusinessObjectToModify(final List<BusinessObjectType> potentialBusinessObjects, final boolean forcePrompt) {
    if (potentialBusinessObjects.isEmpty()) {
        throw new RuntimeException("potentialBusinessObjects is empty");
    }
    // Determine which business object should be modified
    final BusinessObjectType selectedBo;
    if (forcePrompt || potentialBusinessObjects.size() > 1) {
        // Prompt the user for the classifier
        final ElementSelectionDialog dlg = new ElementSelectionDialog(Display.getCurrent().getActiveShell(), "Select an Element to Modify", "Select an element to modify.", potentialBusinessObjects);
        dlg.setInitialSelections(new Object[] { potentialBusinessObjects.get(0) });
        if (dlg.open() == Window.CANCEL) {
            return null;
        }
        selectedBo = (BusinessObjectType) dlg.getFirstSelectedElement();
    } else {
        selectedBo = potentialBusinessObjects.get(0);
    }
    return selectedBo;
}
Also used : ElementSelectionDialog(org.osate.ge.aadl2.ui.internal.dialogs.ElementSelectionDialog)

Aggregations

ElementSelectionDialog (org.osate.ge.aadl2.ui.internal.dialogs.ElementSelectionDialog)2 IPath (org.eclipse.core.runtime.IPath)1 EObject (org.eclipse.emf.ecore.EObject)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)1 CanonicalBusinessObjectReference (org.osate.ge.CanonicalBusinessObjectReference)1 RelativeBusinessObjectReference (org.osate.ge.RelativeBusinessObjectReference)1 DiagramConfigurationBuilder (org.osate.ge.internal.diagram.runtime.DiagramConfigurationBuilder)1