Search in sources :

Example 1 with CanRenameContext

use of org.osate.ge.businessobjecthandling.CanRenameContext in project osate2 by osate.

the class EditorRenameUtil method canRename.

public static boolean canRename(final DiagramElement de) {
    final Object bo = de.getBusinessObject();
    // Only EObjects are supported at this time
    if (!(bo instanceof EObject)) {
        return false;
    }
    final BusinessObjectHandler handler = de.getBusinessObjectHandler();
    if (!handler.canRename(new CanRenameContext(bo))) {
        return false;
    }
    if (!RenameUtil.supportsNonLtkRename(handler) && RenameUtil.getRenameRefactoring(bo) == null) {
        return false;
    }
    return true;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject) BusinessObjectHandler(org.osate.ge.businessobjecthandling.BusinessObjectHandler) CanRenameContext(org.osate.ge.businessobjecthandling.CanRenameContext)

Example 2 with CanRenameContext

use of org.osate.ge.businessobjecthandling.CanRenameContext 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

CanRenameContext (org.osate.ge.businessobjecthandling.CanRenameContext)2 EObject (org.eclipse.emf.ecore.EObject)1 AadlPackage (org.osate.aadl2.AadlPackage)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 ClassifierCreationHelper (org.osate.ge.aadl2.internal.util.classifiers.ClassifierCreationHelper)1 BusinessObjectHandler (org.osate.ge.businessobjecthandling.BusinessObjectHandler)1 GetNameContext (org.osate.ge.businessobjecthandling.GetNameContext)1 Point (org.osate.ge.graphics.Point)1