How To Enable UUID In EMF Generated Model To Get Copy&Paste Working

If you get very strange results using Copy&Paste in GMF diagram editor then in all likelihood your EMF model doesn’t use Universally Unique Identifiers. EMF’s XMIResourceImpl supports generation of UUID transparently but it’s disabled by default. To enable it you have to overwrite XMIResourceImpl‘s useUUIDs() method and make model plug-in use that new Resource implementation.

  1. Overwrite useUUIDs()
  2. Create new class which extends XMIResourceImpl and overwrites useUUIDs(). Put it for example in yourmodel.utils package.

    package mymodel.util;
    
    import org.eclipse.emf.common.util.URI;
    import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
    
    public class MymodelResource extends XMIResourceImpl {
    
            public MymodelResource() {
                    super();
            }
    
            public MymodelResource(URI uri) {
                    super(uri);
            }
    
            /* (non-Javadoc)
             * @see org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl#useUUIDs()
             */
            protected boolean useUUIDs() {
                    return true;
            }
    }
    
  3. Prepare special ResourceFactory
  4. This ResourceFactory factory should always create instances of MymodelResource class.

    package mymodel.util;
    
    import org.eclipse.emf.common.util.URI;
    import org.eclipse.emf.ecore.resource.Resource;
    import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
    
    public class MymodelResourceFactory extends XMIResourceFactoryImpl {
    
            public MymodelResourceFactory() {
                    super();
            }
    
            /* (non-Javadoc)
             * @see org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl#createResource(org.eclipse.emf.common.util.URI)
             */
            public Resource createResource(URI uri) {
                    return new MymodelResource(uri);
            }
    }
    
  5. Wire new ResourceFactory with model plug-in
  6. To plug.xml of your model plug-in add extension_parser extension:

       <extension point="org.eclipse.emf.ecore.extension_parser">
          <parser
             type="gnmsmodel"
             class="com.globalus.globnms.model.gnmsmodel.util.GnmsmodelResourceFactory">
          </parser>
       </extension>
    
    

All done. Now when you modify your model and save it you will get UUID’s stored in XMI file. This will of course bread relations from GMF’a notation model. Notation model should be deleted and initialized again.

[ratings]

5 Responses

  1. KTB says:

    Hello, thx for your post. It helped me a lot :)

  2. Sairam Meghi says:

    I am able to copy and paste the elements in a compartment, however when i am copying the element which has compartment i am facing serious problems.

  3. Sairam Meghi says:

    How to disable copy and paste from the editor?? Please let me know

  4. Of course, what a fantastic blog and instructive posts, I will bookmark your blog.Have an awsome day!