| 1 | = Split domain file = |
| 2 | |
| 3 | [wiki:gmf:gmf <back] |
| 4 | |
| 5 | Scenario: Root element {{{A}}} contains element {{{B}}} and element {{{C}}}. Element {{{C}}} is to be saved in a separate domain file. |
| 6 | |
| 7 | Solution: |
| 8 | - Create an element {{{CReference}}}. |
| 9 | - {{{CReference}}} contains an element {{{C}}} with {{{containment=false}}}. |
| 10 | - Override method {{{doDefaultElementCreation()}}} in class {{{CReferenceCreateCommand}}}. This method contains the creation of the corresponding element {{{C}}}, similar to the following: |
| 11 | {{{ |
| 12 | // create CReference-Object |
| 13 | EObject cReference = super.doDefaultElementCreation(); |
| 14 | |
| 15 | // get resource of root element A |
| 16 | Resource aResource = getElementToEdit().eResource(); |
| 17 | |
| 18 | // create C object |
| 19 | C c = ExampleFactory.eINSTANCE.createC(); |
| 20 | |
| 21 | // add c to cReference |
| 22 | ((CReference)cReference).setC(c); |
| 23 | |
| 24 | // create model file for C |
| 25 | File cFile = new File(pathToCFile); |
| 26 | if (!cFile.exists()) |
| 27 | try { |
| 28 | cFile.createNewFile(); |
| 29 | } catch (IOException e) { |
| 30 | // TODO Auto-generated catch block |
| 31 | e.printStackTrace(); |
| 32 | } |
| 33 | |
| 34 | // create resource for c |
| 35 | Resource cResource = aResource.getResourceSet().createResource( |
| 36 | URI.createFileURI(cFile.getAbsolutePath())); |
| 37 | |
| 38 | // add c to new cResource |
| 39 | cResource.getContents().add(c); |
| 40 | |
| 41 | // save resource; this will also be done by the save command of the generated editor unless you reduce the save command |
| 42 | try { |
| 43 | cResource.save(ExampleDiagramEditorUtil.getSaveOptions()); |
| 44 | } catch (IOException e) { |
| 45 | // TODO Auto-generated catch block |
| 46 | e.printStackTrace(); |
| 47 | } |
| 48 | }}} |