One of the great advantages of modeling data with a language like Ecore, is that the model facilitates making generic tools for manipulating data, including editing it in a user interface. E.g. the type of attributes allows you to automatically select appropriate editing widget in a property sheet and the containment references allows you to add child creation menus containing only the relevant types and support drag & drop which the proper checks for validity.
Creating the instance
In Eclipse, you usually use wizards to create files. However, while modelling there is no wizard or installed editor, so to create model instances we must use a different mechanism. Both the Sample Ecore Model Editor (default editor for Ecore files) and the Ecore Editor allows you to right-click on an EClass and select Create Dynamic Instance... This will prompt you for the file name, and then it create will create a file with an instance of that EClass in the XMI serialisation format. Finally, the default editor is opened, and you can start building a model instance, i.e. an EObject graph. The term dynamic instance relates to the fact that the mechanism doesn't rely on generated Java classes, but instead uses a special EObject subclass that supports all aspects of Ecore including EAttributes and EReferences. Hence, they have the same behaviour as instances of generated Java classes. Note that to be able to open the file without having the model installed into Eclipse, the file includes a reference to the Ecore model location. This allows the loading mechanism to also load the model and be able to make sense of the contents. Note that if the (newly create) file is re-opened with the model installed, it will use the Java classes and can support validation that uses Java code. |
The generic Ecore instance editor (Sample Ecore Model Editor)
EMF provides a completely generic editor, named Sample Reflective Ecore Model Editor, for editing any Ecore model instance, i.e. EObject graphs, that provides tree-based editing of the main hierarchical structure of EObject elements, with commands for creating, deleting, copying and pasting elements, and a property sheet for editing details. The latter is shown in the standard Properties view outside the editor itself. An example of the editor and Properties view is show right. The root node represents the file, which contains an OrgModel containing an OrgUnit and a Role, and the OrgUnit contains a Person. Since the Person is selected, its attributes (EAttributes and EReferences) are shown in the Properties view. To create new elements, you can right-click on the parent (to-be) and select New Child and the type of element to add. Only legal types of elements are shown, e.g. you can add an OrgUnit or a Role to an OrgModel and a Person to an OrgUnit, but not a Role to a Person. The new element is inserted at the bottom of the list of children underneath the parent, so you may have to move it using drag & drop. If you want to place the new element in the middle of a list of children, you can alternatively right-click on the child just above where you want the new element and select New Sibling and the element type. The Properties view allows you to edit the EStructuralFeatures (EAttributes and EReferences) of the selected element. Click in the Value column to enter editing mode. Single valued features are edited in-line, while multi-valued features are edited in a pop-up dialog. In Eclipse's Edit menu you'll find the standard delete, cut, copy and paste commands that are accessible using the standard keyboard shortcuts. The latter two are particularly useful to save time, e.g. to make an EObject similar to an existing one, it is often quicker to copy and paste it into its containing EObject and then edit the differing attributes, rather than using New Child or New Sibling. |
The EMF Forms-based editor
The EMF Forms project has contributed another generic editor. This editor is based on the default one and hence is tree-based, but has some improvements. When right-clicking you immediately get a menu for selecting the type of child element to add, and it prompts you for relevant data. The editor has its own pane for properties, with appropriate editors for each attribute, so you won't be using the Properties view. An example of the editor and property pane is show right. Unfortunately, the editor cannot be used immediately, as it needs to be registered in plugin.xml as an editor for your model's file extension first. For more about this editor, see http://eclipsesource.com/blogs/tutorials/emf-forms-editors/#genericeditor. |
|