Search in sources :

Example 1 with ClassifierCreationHelper

use of org.osate.ge.aadl2.internal.util.classifiers.ClassifierCreationHelper in project osate2 by osate.

the class CreateClassifierPaletteCommand method buildCreateOperations.

/**
 * Build the operation that will be executed to create the classifier.
 * Returns null if the operation could not be created. For example: if the dialog was canceled.
 * @param pkg
 * @param targetBo
 * @param classifierType
 * @param project
 * @param namingService
 * @param rs
 * @return
 */
private ClassifierOperation buildCreateOperations(final AadlPackage pkg, final EObject targetBo, final IProject project, final ResourceSet rs) {
    final ClassifierCreationHelper classifierCreationHelper = new ClassifierCreationHelper(rs);
    // Handle case where target is a valid base classifier for quick creation.
    // Determine if the container is a valid base classifier
    final boolean targetIsValidBase = isValidBaseClassifier(targetBo);
    if (targetIsValidBase || !componentImplementation) {
        // 
        // Create the base operation part
        // 
        final EObject baseClassifier = targetIsValidBase ? targetBo : null;
        final ClassifierOperationPart basePart = baseClassifier == null ? ClassifierOperationPart.createNone() : ClassifierOperationPart.createExisting(baseClassifier);
        // 
        // Create the primary operation part
        // 
        final ClassifierOperationPartType primaryType;
        final ComponentCategory primaryComponentCategory;
        if (componentCategory == null) {
            primaryType = ClassifierOperationPartType.NEW_FEATURE_GROUP_TYPE;
            primaryComponentCategory = null;
        } else {
            primaryType = componentImplementation ? ClassifierOperationPartType.NEW_COMPONENT_IMPLEMENTATION : ClassifierOperationPartType.NEW_COMPONENT_TYPE;
            primaryComponentCategory = componentCategory;
        }
        final String defaultIdentifier = componentImplementation ? "impl" : "new_classifier";
        // Determine a unique name for the classifier
        final String potentialFullName = classifierCreationHelper.buildName(primaryType, pkg, defaultIdentifier, basePart);
        if (potentialFullName == null) {
            return null;
        }
        final String newName = AadlNamingUtil.buildUniqueIdentifier(pkg.getPublicSection(), potentialFullName);
        // Retrieve the identifier to be used for creation. For component implementations this is the part after the ".". For other types, it is the entire
        // name
        final String[] nameSegments = newName.split("\\.");
        final String primaryIdentifier = nameSegments[nameSegments.length - 1];
        final ClassifierOperationPart configuredPrimaryOperation = ClassifierOperationPart.createCreation(primaryType, pkg, primaryIdentifier, primaryComponentCategory);
        return new ClassifierOperation(configuredPrimaryOperation, basePart);
    } else {
        final ClassifierOperationDialog.Model model = new DefaultCreateSelectClassifierDialogModel(rs, "Configure component implementation.") {

            @Override
            public String getTitle() {
                return "Create Component Implementation";
            }

            @Override
            public Collection<?> getPackageOptions() {
                return AadlUiUtil.getEditablePackages(project);
            }

            @Override
            public Collection<?> getBaseSelectOptions(final ClassifierOperationPartType primaryOperation) {
                return AadlUiUtil.getValidBaseClassifierDescriptions(project, componentCategory, true);
            }

            @Override
            public Collection<?> getUnfilteredBaseSelectOptions(final ClassifierOperationPartType primaryOperation) {
                return null;
            }
        };
        // Show the dialog to determine the operation
        return ClassifierOperationDialog.show(Display.getCurrent().getActiveShell(), new ClassifierOperationDialog.ArgumentBuilder(model, EnumSet.of(ClassifierOperationPartType.NEW_COMPONENT_IMPLEMENTATION)).defaultPackage(pkg).showPrimaryPackageSelector(false).componentCategories(ImmutableList.of(componentCategory)).create());
    }
}
Also used : ClassifierOperationDialog(org.osate.ge.aadl2.ui.internal.dialogs.ClassifierOperationDialog) ClassifierOperationPart(org.osate.ge.aadl2.internal.util.classifiers.ClassifierOperationPart) ClassifierOperationPartType(org.osate.ge.aadl2.internal.util.classifiers.ClassifierOperationPartType) ClassifierCreationHelper(org.osate.ge.aadl2.internal.util.classifiers.ClassifierCreationHelper) DefaultCreateSelectClassifierDialogModel(org.osate.ge.aadl2.ui.internal.dialogs.DefaultCreateSelectClassifierDialogModel) ComponentCategory(org.osate.aadl2.ComponentCategory) EObject(org.eclipse.emf.ecore.EObject) ClassifierOperation(org.osate.ge.aadl2.internal.util.classifiers.ClassifierOperation)

Example 2 with ClassifierCreationHelper

use of org.osate.ge.aadl2.internal.util.classifiers.ClassifierCreationHelper in project osate2 by osate.

the class PasteAction method ensureBusinessObjectHasUniqueName.

/**
 * If the element is renameable and the name can be validated, then generate a name that passes validation.
 * Otherwise, do not change the element's name. Contains special handling for component implementations.
 * @param bo
 * @param boHandler
 */
private static void ensureBusinessObjectHasUniqueName(final EObject bo, final BusinessObjectHandler boHandler) {
    if (supportsRenaming(bo, boHandler) && boHandler.canRename(new CanRenameContext(bo))) {
        // Determine the current name of the business object.
        final String originalName = boHandler.getNameForRenaming(new GetNameContext(bo));
        // contain the component type / component type alias appropriate for the destination package.
        if (bo instanceof ComponentImplementation) {
            final ComponentImplementation ci = (ComponentImplementation) bo;
            final ComponentType ct = ci.getType();
            final ClassifierCreationHelper classifierCreationHelper = new ClassifierCreationHelper(ci.eResource().getResourceSet());
            final String ciTypeName;
            if (ct == null) {
                ciTypeName = ci.getTypeName();
            } else {
                if (!AadlNameUtil.namesAreEqual(ci.getNamespace(), ct.getNamespace())) {
                    if (!(ci.getNamespace() instanceof PackageSection)) {
                        throw new RuntimeException("New component implementation is not contained in a package section");
                    }
                    final PackageSection section = (PackageSection) ci.getNamespace();
                    // Import the package if necessary
                    final AadlPackage typePkg = (AadlPackage) ct.getNamespace().getOwner();
                    AadlImportsUtil.addImportIfNeeded(section, typePkg);
                    // Create an alias for the component type
                    final ClassifierCreationHelper.RenamedTypeDetails aliasDetails = classifierCreationHelper.getRenamedType(section, typePkg, ct.getName());
                    if (!aliasDetails.exists) {
                        final ComponentTypeRename ctr = section.createOwnedComponentTypeRename();
                        ctr.setName(aliasDetails.aliasName);
                        ctr.setCategory(ct.getCategory());
                        ctr.setRenamedComponentType(ct);
                    }
                    ciTypeName = aliasDetails.aliasName;
                } else {
                    ciTypeName = ct.getName();
                }
            }
            setName(bo, boHandler, ciTypeName + ".osate_ge_temporary_name_00001");
        } else {
            // Set name to dummy name so that validate name will work as expected. Many implementations
            // of validate name check if the name has changed.
            setName(bo, boHandler, "");
        }
        // Determine a new name for the business object
        final String baseName = originalName;
        String newName = originalName;
        int count = 1;
        while (true) {
            final String result = RenameUtil.checkNewNameValidity(bo, boHandler, newName);
            if (result == null) {
                break;
            }
            newName = baseName + "_copy" + (count == 1 ? "" : Integer.toString(count));
            count++;
        }
        // specified value does not include the type name.
        if (bo instanceof ComponentImplementation) {
            newName = ((ComponentImplementation) bo).getTypeName() + "." + newName;
        }
        // Update the business object's name
        setName(bo, boHandler, newName);
    }
}
Also used : ComponentImplementation(org.osate.aadl2.ComponentImplementation) GetNameContext(org.osate.ge.businessobjecthandling.GetNameContext) ComponentType(org.osate.aadl2.ComponentType) AadlPackage(org.osate.aadl2.AadlPackage) PackageSection(org.osate.aadl2.PackageSection) ClassifierCreationHelper(org.osate.ge.aadl2.internal.util.classifiers.ClassifierCreationHelper) ComponentTypeRename(org.osate.aadl2.ComponentTypeRename) CanRenameContext(org.osate.ge.businessobjecthandling.CanRenameContext) Point(org.osate.ge.graphics.Point)

Aggregations

ClassifierCreationHelper (org.osate.ge.aadl2.internal.util.classifiers.ClassifierCreationHelper)2 EObject (org.eclipse.emf.ecore.EObject)1 AadlPackage (org.osate.aadl2.AadlPackage)1 ComponentCategory (org.osate.aadl2.ComponentCategory)1 ComponentImplementation (org.osate.aadl2.ComponentImplementation)1 ComponentType (org.osate.aadl2.ComponentType)1 ComponentTypeRename (org.osate.aadl2.ComponentTypeRename)1 PackageSection (org.osate.aadl2.PackageSection)1 ClassifierOperation (org.osate.ge.aadl2.internal.util.classifiers.ClassifierOperation)1 ClassifierOperationPart (org.osate.ge.aadl2.internal.util.classifiers.ClassifierOperationPart)1 ClassifierOperationPartType (org.osate.ge.aadl2.internal.util.classifiers.ClassifierOperationPartType)1 ClassifierOperationDialog (org.osate.ge.aadl2.ui.internal.dialogs.ClassifierOperationDialog)1 DefaultCreateSelectClassifierDialogModel (org.osate.ge.aadl2.ui.internal.dialogs.DefaultCreateSelectClassifierDialogModel)1 CanRenameContext (org.osate.ge.businessobjecthandling.CanRenameContext)1 GetNameContext (org.osate.ge.businessobjecthandling.GetNameContext)1 Point (org.osate.ge.graphics.Point)1