wiki:SplitDomainFile

Version 2 (modified by jdamerow, 16 years ago) (diff)

--

Split domain file

<back

Scenario: Root element A contains element B and element C. Element C is to be saved in a separate domain file.

Solution:

  • Create an element CReference.
  • CReference contains an element C with containment=false.
  • Override method doDefaultElementCreation() in class CReferenceCreateCommand. This method contains the creation of the corresponding element C, similar to the following:
    // create CReference-Object
    EObject cReference = super.doDefaultElementCreation();
    
    // get resource of root element A
    Resource aResource = getElementToEdit().eResource();
    
    // create C object
    C c = ExampleFactory.eINSTANCE.createC();
    
    // add c to cReference
    ((CReference)cReference).setC(c);
    
    // create model file for C
    File cFile = new File(pathToCFile);
    if (!cFile.exists())
       try {
    	cFile.createNewFile();
       } catch (IOException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
       }
    
    // create resource for c
    Resource cResource = aResource.getResourceSet().createResource(
    	URI.createFileURI(cFile.getAbsolutePath()));
    
    // add c to new cResource
    cResource.getContents().add(c);
    
    // save resource; this will also be done by the save command of the generated editor unless you reduce the save command
    try {
       cResource.save(ExampleDiagramEditorUtil.getSaveOptions());
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
    
  • The last step is to update the modification stamp of the ResourceSetInfo. This is an inner class of your XXXDocumentProvider. If you don't update the modification stamp, you get an error similar to "The file has been changed on the file system" when you save your diagram. Modify the method notifyChanged of class ResourceSetModificationListener (inner class of XXXDocumentProvider):
    if (!notification.isTouch()
       && myModifiedFilter.matches(notification)) {
       // Update modification stamp if new Resource was added to ResourceSet
       if (notification.getNotifier() instanceof ResourceSet) {
    	if (notification.getEventType() == Notification.ADD) {
    	   myInfo.setModificationStamp(computeModificationStamp(myInfo));
    	}
       }
       // ...
    }