# Advanced Features

In this chapter we describe some advanced features.

## Building with Maven

We provide Maven artifacts in Maven Central that allows you to compile the Parsley DSL sources with Maven/Tycho, using the xtext-maven-plugin.

This is a typical configuration (a complete example can be found here: https://github.com/eclipse-emf-parsley/emf-parsley/blob/master/tests/org.eclipse.emf.parsley.tests.views/pom.xml):

<plugin>
<groupId>org.eclipse.xtext</groupId>
<artifactId>xtext-maven-plugin</artifactId>
<version>${xtext-version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<languages>
<language>
<setup>org.eclipse.emf.parsley.dsl.EmfParsleyDslStandaloneSetup</setup>
<outputConfigurations>
<outputConfiguration>
<outputDirectory>${basedir}/emfparsley-gen</outputDirectory>
</outputConfiguration>
</outputConfigurations>
</language>
</languages>
</configuration>
<dependencies>
<dependency>
<groupId>org.eclipse.emf.parsley</groupId>
<artifactId>org.eclipse.emf.parsley.dsl</artifactId>
<version>${parsley-version}</version>
</dependency>
</dependencies>
</plugin>

## Testing Framework

We provide some utility classes for testing EMF Parsley components in the feature "EMF Parsley Junit4 Support". By deriving from one of the abstract classes in our testing bundle, you will be able to write tests that are meant to be run as Junit test, that is to say, NOT as Plug-in Junit tests. Thus, you will not need a running Eclipse product to execute such tests: they will be much faster. Indeed, many parts of Parsley can be tested even without a running Eclipse.

- AbstractEmfParsleyTest (src): this provides a few utility methods, e.g., for creating an Injector.
- AbstractEmfParsleyShellBasedTest (src): this allows to run Junit tests that require a Display and a Shell.
- AbstractEmfParsleyControlBasedTest (src): an extension of the previous class for tests that also require databinding capabilities, e.g., tests for Control elements; this provides many assert methods for several kinds of controls, such as assertCheckbox, assertCombo, etc.

We use these classes for testing most of our classes; you might want to have a look at the project org.eclipse.emf.parsley.tests for some usage examples.

## Eclipse 4.x

Instead of using the Extension Point mechanism, EMF Parsley leverages from DSL and Google Guice Injection.

Because of this, it is very easy to use it with Eclipse 4.x (e4).

First Example Setup

If you followed the steps described in section First Example you will have already what we need to begin. Otherwise the following wizard will bring you to that point.

1. File -> New... -> Example...
2. from Category "EMF Parsley Examples", select "EMF Parsley First Example"
3. press Next and Finish

You will end up with three plug-ins:

- org.eclipse.emf.parsley.examples.firstexample (the EMF Parsley example plug-in)
- org.eclipse.emf.examples.library (the model plug-in)
- org.eclipse.emf.examples.library.edit (the model.edit plug-in)

As a reminder, in section First Example we reached the point where we launched a second Eclipse instance (but, of course, just defining a product you could have a standalone 3.x application) with a view (called "My Library Tree Form") that allowed to manage the model.

Preparing for a pure e4 Application

What we will do now is starting from the previous step and create an e4 Application (on top of the previous plug-ins) that gets to the same result, but now with a pure e4 Part.

In order to do this we need to export the "org.eclipse.emf.parsley.examples.firstexample" package from the first plug-in.

Create an e4 Application

Now let's create a new, empty, e4 application, e.g. "org.eclipse.emf.parsley.examples.firstexample.application" (you can find details on how to create e4 applications in our tutorials).

Create a Part and ensure that the application starts.

Using a TreeComposite into an e4 Part

In the just created plug-in we need dependencies from the previous plug-ins: so open the org.eclipse.emf.parsley.examples.firstexample.application/MANIFEST.MF file, go to Dependencies tab and add the three previous plug-ins. Add also "org.eclipse.emf.parsley" plug-in. Don't forget to add the previous, and the required plug-ins, also to the Product.

Open the Part java class and make the following changes:

// Use these imports during Organizing Imports operation
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.swt.widgets.Composite;
// The part implements IMenuListener for context menu handling
public class MyEclipse4Part {
//the EMF Parley composite for showing a tree and a detail form
private TreeFormComposite treeFormComposite;
//the EMF Resource
private Resource resource;
//URI for EMF Resource
private URI uri = URI.createFileURI(System.getProperty("user.home")
+ "/MyLibrary.library");

Modify the @PostConstruct method with this code:

@PostConstruct
public void postConstruct(Composite parent) {
// Guice injector
private Injector injector = FirstexampleInjectorProvider.getInjector();
// The EditingDomain is needed for context menu and drag and drop
EditingDomain editingDomain = injector.getInstance(EditingDomain.class);
ResourceLoader resourceLoader = injector.getInstance(ResourceLoader.class);
//load the resource
resource = resourceLoader.getResource(editingDomain, uri).getResource();
TreeFormFactory treeFormFactory = injector.getInstance(TreeFormFactory.class);
//create the tree-form composite
treeFormComposite = treeFormFactory.createTreeFormComposite(parent, SWT.BORDER);
// Guice injected viewer context menu helper
ViewerContextMenuHelper contextMenuHelper = injector.getInstance(ViewerContextMenuHelper.class);
// Guice injected viewer drag and drop helper
ViewerDragAndDropHelper dragAndDropHelper = injector.getInstance(ViewerDragAndDropHelper.class);
// set context menu and drag and drop
contextMenuHelper.addViewerContextMenu(treeFormComposite.getViewer(), editingDomain);
dragAndDropHelper.addDragAndDrop(treeFormComposite.getViewer(), editingDomain);
//update the composite
treeFormComposite.update(resource);
}

The Google Guice Injector (not to be confused with the Eclipse e4 Injector) is retrieved using the injector provider class generated by the DSL compiler (see also Obtaining the Injector).

If you now run the application you will be able to manage the model:

but you will notice that it is not possible to persist the changes to the model.

Adding the dirty state and Save command

In order to allow persisting the model changes we have to add the dirty state handling to the part and the Save command to the application. Let's start with adding the following attribute to the part

@Inject
MDirtyable dirty;

add to @PostConstruct method the following code in order to update the dirty state

editingDomain.getCommandStack().addCommandStackListener(
new CommandStackListener() {
public void commandStackChanged(EventObject event) {
if (dirty != null)
dirty.setDirty(true);
}
});

and add the @Persist method, which will be called when the part is saved

@Persist
public void save(MDirtyable dirty) throws IOException {
resource.save(null);
if (dirty != null) {
dirty.setDirty(false);
}
}

and, in the end, add the Save handler along with the correspondent Command and Menu (you can find how to create handlers, commands and menus in an e4 applications in our tutorials)

import jakarta.inject.Named;
public class SaveHandler {
@Execute
void execute(EPartService partService, @Named(IServiceConstants.ACTIVE_PART) MPart part) {
partService.savePart(part, false);
}
}

## RAP

As you may know RAP (Remote Application Platform) is a technology that allows you to run an Eclipse RCP application over the web.

In order to obtain this goal you have to setup a specific RAP Target Platform, for instance the one that RAP itself provides once you install it.

However when you want to use an Eclipse RCP framework over the RAP Platform, you generally have to deal with dependencies, since not all Eclipse frameworks are ready-to-use with RAP, especially those related with the SWT layer.

EMF Parsley provides a proper RAP Target Platform that allows you to start leveraging Parsley potentials to the web the same way you have learned to do with desktop (RCP) development.

Installing the RAP Tools

To begin with, you need to install the RAP Tools into the IDE. This can be accomplished with the following steps:

1. Help -> Install New Software ...
2. select the main Eclipse Update site
3. expand category "Web, XML, Java EE and OSGi Enterprise Development"
4. select "RAP Tools" and complete the installation, restarting the IDE at the end
5. after IDE restarts just close the Welcome page

Setup the EMF Parsley RAP Target Platform

After having installed EMF Parsley as described here and created a new workspace, you can setup the EMF Parsley RAP Target Platform in the following way:

1. File -> New... -> Example...
2. from Category "EMF Parsley Examples", select "EMF Parsley RAP Target Platform Example"
3. press Next and Finish
4. open the Target Definition file emf-parsely-rap.target
5. wait until the "Resolving Target Definition" job is done (check the status bar)
6. when finished, click on hyperlink "Set as Target Platform"

You will end up with a RAP-enabled workspace, enhanced by EMF and Parsley!

Running the Parsley RAP UI Example

Here is the fastest way to get a working web application with all the stuff put togheter:

1. File -> New... -> Example...
2. from Category "EMF Parsley Examples", select "EMF Parsley RAP Example"
3. press Next and Finish
4. expand plug-in "org.eclipse.emf.parsley.examples.rap.ui"
5. right-click "Emf_Parsley_RAP_UI_Example.launch" and click "Run as" "Emf_Parsley_RAP_UI_Example"

What you will get is a web application that allows you to interact with the model instance as you would do in a desktop (RCP) environment.

In this web application you can see two views:

- the one on the left is a read-only view; it just reflects the model content, but it does not react to changes (the classic Eclipse dirty indicator is not triggered by changes) and you are not able to save. Its model is created in class org.eclipse.emf.parsley.examples.rap.ui.GuiceModule.CustomResourceManager and is not persisted
- the view on the right is instead a Saveable view and therefore it not only triggers the dirty state after a change, but also allows you to save the modifications with the automatic dirty state reset. Its model is persisted in file System.getProperty("java.io.tmpdir")+"/My.model")

Of course, since this is a web application, you can also open a browser on another pc or device on the same network and type the address, replacing 127.0.0.1 with the IP of the machine where the application was launched.

Running the Parsley RAP CDO Example

The EMF default XMI persistence is certainly very handy to start with, but as soon as you want a more production-ready EMF persistence architecture, well, CDO is for sure the way to go. In fact with CDO you basically have an EMF model instance shared between clients, that also allows the clients to be synchronized with the model changes.

In this example, in order to keep things simple, we will use CDO with an in-memory store (MEMStore) whose contents will be lost once the server is stopped. However CDO can be configured for usage with RDBMS, Object-oriented or NO-SQL databases (see here for details)

To start with we need a CDO Server running and we can obtain it with an example plugin that can be used both in an RCP and in a RAP workspace.

1. File -> New... -> Example...
2. from Category "EMF Parsley Examples", select "EMF Parsley CDO Server Example"
3. press Next and Finish
4. expand plug-in "org.eclipse.emf.parsley.examples.cdo.server"
5. right-click "CDOServerExample.launch" and click "Run as" "CDOServerExample"
6. a message on the Console "Repository[demo] started!" informs that the CDO Server instance is started!

Now we can create the web application that will use the CDO server just started.

1. File -> New... -> Example...
2. from Category "EMF Parsley Examples", select "EMF Parsley RAP CDO Example"
3. press Next and Finish

The plug-in projects created are:

- the Model (org.eclipse.emf.parsley.examples.cdo.model)
- a Parsley plug-in with a TreeForm (org.eclipse.emf.parsley.examples.cdo.treeform)
- the webapp (org.eclipse.emf.parsley.examples.cdo.rap)

Then let's start the application

1. expand plug-in "org.eclipse.emf.parsley.examples.cdo.rap"
2. right-click "EMF-Parsley_Library_RAP.launch" and click "Run as" "EMF-Parsley_Library_RAP"

If you happen to see this

just press the refresh button and should see the following

Now feel free to open the same address from more browsers window (yes, on different machines or devices, possibly) and see the power of this technology stack at work!