Search code examples
javaeclipsepluginsemfecore

How can i use generated elements from one emf model in another generated editor


For example:

Model 1:

abstract class Command{} 

Model 2:

class Command1 extends Command{ ...}
class Command2 extends Command{ ...}

Model 3:

class CommandHolder {
 val Command[+] commands;
}

Now i generate the Editor to Model3 and want Command1 and Command2 to become selectable as children of the CommandHolder Element.

This means, if i create a CommandHolder File with the commands element as root element and make a right click on it and select [New Child] i would like Command1 and Command2 to appear in this list.

Is this possible without changing the generated source code?

Greetings


Solution

  • So i could not realize it without changing the generated code but i have a working solution.

    In the specific item provider in the collectNewChildDescriptors Method from the CommandHolder i added

     List<EClass> cmdClasses = EcoreHelper.getAllClassesInheritFrom("Command");
        for(EClass cmdClass : cmdClasses){
            newChildDescriptors.add
            (createChildParameter
                (CommandPackage.Literals.COMMAND__COMMANDS,
                 EcoreHelper.createClassByName(cmdClass.getName())));
        }
    

    The EcoreHelper Methods:

    public static EObject createClassByName(String className) {
    
            EObject eObject = null;
            try {
                Registry registry = EPackage.Registry.INSTANCE;
                for (String key : new HashSet<String>(registry.keySet())) {
                EPackage ePackage = registry.getEPackage(key);
                if (ePackage.getClass().getName().startsWith("")) {
                    for (EClassifier eClassifier : ePackage.getEClassifiers()) {
                    if (eClassifier instanceof EClass) {
                        EClass eClass = (EClass) eClassifier;
                        if (eClass.getName().equals(className)) {
                        if (!eClass.isAbstract()) {
                            eObject = ePackage.getEFactoryInstance().create(eClass);
                        }
                        }
                    }
                    }
                }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return eObject;
            }
    
    
     public static List<EClass> getAllClassesInheritFrom(String className) {
            List<EClass> eClasses = new ArrayList<EClass>();
            try {
                Registry registry = EPackage.Registry.INSTANCE;
                for (String key : new HashSet<String>(registry.keySet())) {
                EPackage ePackage = registry.getEPackage(key);
                if (ePackage.getClass().getName().startsWith("")) {
                    for (EClassifier eClassifier : ePackage.getEClassifiers()) {
                    if (eClassifier instanceof EClass) {
                        EClass eClass = (EClass) eClassifier;
                        if (eClass.getName().equals(className)) {
                        if (!eClass.isAbstract()) {
                            eClasses.add(eClass);
    
                        }
                        } else {
                        for (EClass bla : eClass.getEAllSuperTypes()) {
                            if (bla.getName().equals(className)) {
                            if (!eClass.isAbstract()) {
                                eClasses.add(eClass);
                            }
                            }
                        }
                        }
                    }
                    }
                }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return eClasses;
            }