EMF Forms Data Templates

The data template mechanism allows defining template instances of EObjects, which can be used as a basis when creating new instances, e.g. in table. Those template instances can be prefilled with some default values. As an example, in the table below, showing entities of type user, you could assign example values to some attributes for a “default german User”:

Please note, that if you just want to set some default values for new instances, you can and should still directly do that in the Ecore model. The data template mechanism adds two features compared to using Ecore default values:

  • It allows to define several templates for an EObject and select the template either:
    • by the user
    • by the location in the model (e.g. users contained in a specific reference)
    • both of these option in combination
  • Data Templates allow to you create more complex EObjects, including contained sub elements

In EMFForms new instances for a specific EReference are created by using the ReferenceService. The DefaultReferenceService, shipped with EMFForms, will collect all available data templates (see below how to register data templates). When there is more than one available data template for the reference type to create a wizard, this will be opened to select the desired template:

In case a EReference points to an EClass with several subclasses (e.g. to User, while there are subclasses UserA and UserB), the dialog will allow to you to select  the concrete class to be created first. In case there is only one template and only one class for a specific reference, no dialog will be shown (as before introducing data templates).

Providing Data Templates

There are essentially two ways of providing your custom data templates. EMF Forms support so-called data template provider, which serve a sources for data templates. You can either use the existing default data template provider, which facilitates xmi files as sources for data templates. In this case, you just need to provide a file containing a template EObject, which will be used as a template for new instances then. This simple default option is described in the following section.

As an alternative, you can implement and register your own data template provider, e.g. to programmatically instantiate data templates. To do this, implement the following interface and register your implementation as an OSGi service: org.eclipse.emfforms.core.services.datatemplate.TemplateProvider.

Finally, please note, that EMF Forms registers a “Blank Data Template Provider” by default. This provider will always deliver an “empty” EObject, which is an EObject instantiated with all default values specified in the Ecore model.

Provide XMI Data Templates

In this section, we describe how to provide a data template in EMF Forms using the XMI data template provider. Essentially, you will create and register a file, which contains an EObject instance to be used as a basis for new instances.

Creating a New Template Collection File

  1. Press CTRL + N to open the wizard to create a new file (Alternatively, select File -> New -> Other…)
  2. Filter for DataTemplate, select it in the EMF Forms folder, and click Next.

3. Select the folder in the workspace where the template collection file will be created. Enter the file’s name. Thereby, the “.datatemplate” extension is added automatically by the wizard. Create the file by clicking Finish.

4. This opens the template file which contains one template collection. A template collection can contain an arbitrary number of data templates. To create a template right-click on the highlighted Template Collection and click on Template.

5. The created template has a name and an instance. In our example, we enter “Professional Player” as the name and add an instance by clicking on the “Create New” Button of the instance (see red arrow).

6. In the opened dialog, select the EClass for which you want to create a new template and click Finish. In our example, this is “Player” of the bowling example package.

7. Now the instance can be edited in the editor. All configured fields are saved in the instance and the values will be applied when creating a new instance based on this template. In our example, we set the “Is Professional” field to true.

8. Save the template file and open the MANIFEST.MF of the bundle. We need to set it to be a singleton to be able to provide an extension. Afterwards, click on the “Extensions” tab of the Manifest editor to open the extensions configuration.

9. Click on Add… to add a new Extension.

10. In the opened dialog make sure to uncheck the option “Show only extension points from the required plug-ins”. Afterwards, search for the “datatemplate” and select “org.eclipse.emfforms.core.services.datatemplate.xmi” and add the extension by clicking Finish. When prompted whether you want to add the missing bundle as a dependency, confirm with Yes.

11. If not done automatically, select the template under the extension point and click the “Browse…” button to select the earlier created template collection file (templates.datatemplate).

12. As a final step we need to include the template collection file in the build. This is done in the “Build” tab of the Manifest editor.

13. In the Build tab make sure to check plugin.xml and the template collection file. Finally, save the Manifest file to finish the process.