The purpose of this first example is to make use of the classical EMF Library Model example and create a view for editing such models using an EMF Parsley enabled plug-in.
So let's start by creating the model plug-in with
You will end up with three plug-ins:
The editor plug-in project can be removed.
Please consider that here we are starting from this well known EMF model taken out-of-the-box from Eclipse, but you can start from your EMF model (in that case you may probably omit the ".edit" and ".editor" plugins, depending on your model).
Now you can create your first example with the appropriate wizard.
parts {
viewpart org.eclipse.emf.parsley.examples.firstexample.view.library.treeform {
viewname "My Library Tree Form"
viewclass
}
}
Here above we have just declared a view part with
So there is still an error, in fact we need to create the declared class. Suggestion: Just copy the above qualified class name ("org.eclipse.emf.parsley.examples.firstexample.views.LibraryTreeFormView"), select the project, right-click and select File -> New -> Class. Now paste the clipboard content into "Name:" field: the package and class name will be automatically splitted into right fields. Then click "Finish".
Now modify the view class code with this content:
import org.eclipse.emf.parsley.views.AbstractSaveableTreeFormView;
import org.eclipse.emf.common.util.URI;
public class LibraryTreeFormView extends AbstractSaveableTreeFormView {
protected URI createResourceURI() {
return URI.createFileURI( System.getProperty("user.home") + "/MyLibrary.library" );
}
}
Now get back to module.parlsey file, go just after "viewclass" keyword, type "LibraryTreeFormView" and Ctrl-Space: the content assist will set automatically the correct qualified name (it will insert the class name, and add the corresponding import at the beginning of the file):
viewclass org.eclipse.emf.parsley.examples.firstexample.views.LibraryTreeFormView
when you save the above file you will notice that a new file ("plugin.xml_emfparsley_gen") is generated into the root folder of your project. Just copy this file into "plugin.xml".
Let's focus briefly on the above createResourceURI() method: our goal is allowing to manage a library model instance which persists on a EMF Resource. In this example we choose to use the EMF default persistence (XMI), but you can provide any URI (e.g. using Teneo, CDO or any other EMF Resource Persistence implementation) In particular here we choose to persist the Resource in a XMI file named "MyLibrary.library" into the user home folder (you might want to change it with any other path).
We need a Dependency from the model plug-in: so open MANIFEST.MF file, go to Dependencies tab, press "Add..." button in "Required Plug-ins" section and insert "org.eclipse.emf.examples.library" among dependencies.
Before executing this code we need to do some little more work in order to initialize the Resource.
An easy way to overcome this issue can be the following:
public Class<? extends EmptyResourceInitializer> bindEmptyResourceInitializer() {
return LibraryEmptyResourceInitializer.class;
}
import org.eclipse.emf.parsley.resource.EmptyResourceInitializer;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.examples.extlibrary.EXTLibraryFactory;
import org.eclipse.emf.examples.extlibrary.Library;
public class LibraryEmptyResourceInitializer extends EmptyResourceInitializer {
public void initialize(Resource resource) {
super.initialize(resource);
Library library = EXTLibraryFactory.eINSTANCE.createLibrary();
resource.getContents().add(library);
}
}
now get back to the MANIFEST.MF and run the example
As an Eclipse RCP developer you know, of course, that this will start another Eclipse instance (unless you add an Application plug-in to the launch or define an Application in the current plug-in).
In this second Eclipse instance you can show the View in this way:
With this simple view you can start editing the model instance. For example you can set the "name" field; as soon as you start typing characters into this field you will notice that:
if you now perform a "Save" action the persistence mechanism will trigger and you will see that file <user.home>/MyLibrary.library is being created on the file system. From now on, this file will keep the state of the model object whenever you change and save it.
To create a Writer into the Library just right-click on the Library object and select New Child -> Writer
Please note that you might see a slightly different content in the above context-menu in case you deleted the .edit plugin when creating the model (e.g. "Writers Writer" instead of "Writer", "Stock Book" instead of "Book" and similar (this is because with EMF it is possible to customize labels also via .edit plugin).
Now set for instance the writer "name" field and save. Now just play around creating Books, associating them to Writers and so on. As you can see you can entirely manage the EMF model instance: creating, modifying and deleting elements.
Whenever the current selection on the upper side of the view changes, then the lower side shows the detail of this selection.
However, up to this point, you have no control over the field to be shown and its order; for example you may want just the "name" attribute for the Library and "name", "address" and "books" attributes for Writers and maybe "title", "authors" and "category" for Books.
Well, it's indeed very easy to obtain this: just edit the module.parsley file, adding the following import (without ending line with ";")
import org.eclipse.emf.examples.extlibrary.*
and then defining the features to show
module ... {
parts {
...
}
featuresProvider {
features {
Library -> name
Writer -> name, address, books
Book -> author, title, category
}
}
}
Remeber that code completion is available, just exploit it since it helps a lot.
If you restart now the application you will see that, when selecting an object, only the declared attributes will be shown. Furthermore, they are shown in the specified order.
Did you run the application in Debug mode? Well, then you can change fields and order, save and see the changes without even restarting the application.
Do you want to change text used for attribute labels in the detail? Just add the following:
propertyDescriptionProvider {
text {
Book : author -> 'Wrote by:'
Writer : name -> 'Name:'
}
}
labelProvider {
text {
Book b -> '"' + b.title + '"' +' (by '+ b.author.name + ')'
}
}