Exercise 1. Cross-Resource Containment
What This Exercise Is About
In the tutorial you've learned that containment references can be split across multiple files. The slides contained code snippets demonstrating how to use this feature in your application. This exercise presents how to use the generated editor to accomplish the same results.
The following UML diagram represents the model used in this exercise.

What You Should Be Able To Do
At the end of the lab, you should be able to:
- Customize .ecore and .genmodel files to allow cross-resource containment.
- Specify the resources that contain the instances in your model.
Required Materials
- Eclipse 3.2
- Eclipse Modeling Framework (EMF) 2.2
General Advice / Warnings
- Eclipse conventions suggest naming plug-in projects with a fully qualified plug-in ID, such as com.example.po. Do not use dashes in project names - this will create invalid entries in your MANIFEST.MF file.
- Make sure you're using the IBM JDK or else have implemented the Sun JDK Crimson DOM bug workaround (http://eclipse.org/emf/downloads-xerces.php).
Exercise Instructions
This exercise is carried out entirely using the Eclipse Software Development Kit (SDK) version 3.2 with the Eclipse Modeling Framework (EMF) 2.2 installed into it. The exercise instructions refer to this product as either Eclipse or as "the workbench".
These instructions are located in the exercises/Exercise1_CrossResource_Containment folder. If you browse into this location by expanding the folders, you should find one additional folder: org.eclipse.emf.tutorial.advanced.po. This is an Eclipse project that you will import into your workspace to complete this exercise.
The folder exercises/Solution1 contains examples of the files you will create during this exercise.
Directions
- Ensure Eclipse is running, and that EMF is properly installed.
- Bring up the Help -> About Eclipse SDK dialog.
- Click on Feature Details. Click the Feature Id column heading to order the features by that field. Then, check that at least the following EMF features are listed:
- org.eclipse.emf
- org.eclipse.emf.source
- org.eclipse.xsd
- org.eclipse.xsd.source
- Ensure that the JDK used by Eclipse is valid.
- Bring up the Help -> About Eclipse SDK dialog.
- Click on Configuration Details. Scroll down to where eclipse.vm is shown. This should be something like:
eclipse.vm=c:\ibm-java2-1.4\bin\java,
eclipse.vm=/opt/ibm-java2-1.4/bin/java, or similar
- If your Eclipse install has defaulted to using the Sun JDK 1.4, you must use a different one to avoid a bug in the Crimson DOM when importing from XSD. The following JDKs will work:
- IBM JDK 1.4.2sr2 or later
- IBM JDK 5.0
- Sun JDK 5.0
- Switch to the Java perspective, if not already there.
- Select Window -> Open Perspective -> Java.
- Select File -> Import....
- Expand the twisty next to General and select Existing Projects into Workspace. Hit Next >.
- Keep Select root directory selected, enter the full path of the org.eclipse.emf.tutorial.advanced.po folder located in this exercise's folder, and hit Enter. You can also use the Browse... button to navigate up to this folder.
- Make sure org.eclipse.emf.tutorial.advanced.po is selected in the Projects list and select Finish.
- Enable the Containment Proxies option in the PurchaseOrder generator model.
- Open the PurchaseOrder.genmodel file available in the model folder of the project imported in step B.
- Select the topmost node PurchaseOrder, right-click, and select Show Properties View (if this view is not already shown).
- Scroll the Properties view to find the Model twisty. Underneath is the Containment Proxies property. Set that property to true.

- Ensure that the PurchaseOrder's items reference is resolving proxies.
- Open the po.ecore file available in the model folder of the project imported in step B.
- Select the items reference of the PurchaseOrder class, right-click, and select Show Properties View (if this view is not already shown).
- Scroll the Properties view to find the Resolve Proxies property and verify that it is set to true.

- Generate all code from the genmodel file.
- Open the PurchaseOrder.genmodel file available in the model folder of the project imported in step B.
- Select the topmost node PurchaseOrder. Make sure you select the genmodel root, not a package or class below in the tree, as the selected node will determine what artifacts are generated. The benefit here is that if you later make changes that affect just a single class, you can regenerate only the relevant code, which is faster than regenerating everything.
- Right-click and select Generate All.

- Three new projects will be created: org.eclipse.emf.tutorial.advanced.po.edit (edit support code not dependent on Eclipse), org.eclipse.emf.tutorial.advanced.po.editor (editor code dependent on Eclipse), and org.eclipse.emf.tutorial.advanced.po.tests (JUnit test skeletons for exercising any volatile features and operations defined in the model). Additionally, new classes and packages will be created within the org.eclipse.emf.tutorial.advanced.po project.
- The code should be compiled automatically as it is generated, and should recompile whenever it is changed. If you have disabled automatic building in the workbench preferences, you can initiate compilation manually by selecting Build All from the Project menu.
- Observe the Problems view. There should be warnings about code never being used locally. These stubs will be used by tests that you could write to test your model, but that is out of the scope of this exercise. You can safely ignore these warnings.

All steps below will be done in a second, testing workbench, or Eclipse Application workbench.
- Launch a new Eclipse Application to try out your editor.
- Select your plug-in project org.eclipse.emf.tutorial.advanced.po in the Package Explorer.
- Select Run As -> Eclipse Application from the Run toolbar drop-down or menu. Note that the Run menu is context-sensitive, so options available under Run As will change depending on where your cursor is (which view/editor and what file type).
- Once it opens, close or minimize the Welcome view.
- Select About Eclipse SDK from the Help menu, click on Plug-in Details, and look for the contributed PurchaseOrder plug-ins, org.eclipse.emf.tutorial.advanced.po, org.eclipse.emf.tutorial.advanced.po.edit, org.eclipse.emf.tutorial.advanced.po.editor, and org.eclipse.emf.tutorial.advanced.po.tests.
- The PurchaseOrder Model wizard can now be used to create a new instance of the model.
- Bring up the File -> New -> Project... dialog.
- Expand General and select Project. Click the Next > button.
- Give the project a name, such as po, and click the Finish button.
- Right-click the project and select New -> Other... from the pop-up menu.
- Expand Example EMF Model Creation Wizards and select PO Model. Click the Next > button.
- Enter a file name for the PurchaseOrder model, such as PO.po. Make sure it ends with a .po extension. Then, click the Next > button.
- Select Purchase Order as the model object and click the Finish button.
- The newly created PurchaseOrder model is opened in an editor.
- The root object in this editor corresponds to the PO.po resource. Under it lies an instance of the PurchaseOrder class.
- The steps below will show you how to use the model editor to create instance data. You can also skip this step by copying the Solution1/data/PO.po file to your runtime workspace, then editing the resulting file if desired.
- Expand the platform:/resource/po/PO.po resource to see the Purchase Order object.
- Right-click the order and select New Child -> Item from the pop-up menu. A new Item is added to the purchase order.
- If the Properties view isn't already showing, right-click the new Item object and select Show Properties View from the pop-up menu.
- In the Properties view, click on the Value column of the Product Name property, and provide a name for the item ("p1" for example). The label in the editor will be updated when you hit <Enter>.
- Repeat the last steps to add one or two more items, using different names for each item.
- Select File -> Save to serialize the purchase order.

All the objects created until now are located in the PO.po resource. In this step, you will use the Control... menu action to place the items in different resources. In this context, "to control an object" means to separate it from the resource of its container and, similarly, "to uncontrol the object" means to put it back in the containment tree.
- Right-click the first item created in the previous step and select Control... from the pop-up menu.
- Enter the URI ("platform:/resource/po/items.po" for example) of the new resource that will store the selected item or use the Browse Workspace... button.
- Press the OK button when done.

- Notice the changes in the editor:
- The "controlled" item has now a visual indication that it is located in a separate resource.
- The new resource is loaded in the editor's resource set, hence presented in the tree viewer

- Select File -> Save to save the changes made to the purchase order.
- Notice that the items resource is now available in the workspace. Open this resource and analyze its content.

- Spend a couple of minutes experimenting with the other items. Try to control them to either new or existing resources.
- "Uncontrol" all controlled items.
- Right-click one of the controlled items in the editor for the PO.po resource and select Uncontrol.
- Notice the changes in the editor. The items are now located in the PO.po resource (but the resources containing them were not deleted).

If all items are located in the PO.po resource, the other resources (items.po for example) won't be loaded into the editor's resource set the next time the resource is opened.
You customized an existing model to enable cross-resource containment and generated the model, edit, and editor code. You created an instance of the model and controlled one or more contained objects.