× Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

Constructing the Eclipse application model. This tutorial given an introduction how to replace the persistence of the application model.

1. Specifying the location of the application model file

The file name and location of the application model can be specified in the plugin.xml file. This is done via the applicationXMI property. The path to the file follows the "bundleSymbolicName"/"filename" pattern, e.g. test/ApplicationDifferent.e4xmi.

To do this open the plugin.xml file, select the Extensions tab and open the org.eclipse.core.runtime.products contribution.

Right-click on the product entry and select menu:New [Property]. Use applicationXMI as parameter name and com.example.e4.rcp.todo/ApplicationNew.e4xmi as value. This value consists of the BundleSymbolicName of the corresponding plug-in and the file name of your application model file.

tutorial product70

2. Custom application model persistence handler

Eclipse uses an instance of the IModelResourceHandler interface to load and save the application model at startup and shutdown. The default implementation of this interface is provided by the ResourceHandler class from the org.eclipse.e4.ui.internal.workbench package.

You can specify the modelResourceHandler as parameter on your product extension to point to another class via the bundleclass:// notation.

modelresourcehandler

This allows you to construct the initial application model from any source you desire. For example, you could use property files or construct the application model based on information from a database.

Implementing this interface requires knowledge about the Eclipse Modeling Framework (EMF). You can use the ResourceHandler as a template to see how the IModelResourceHandler interface could be implemented.

3. Saving and restoring parts of the application model

3.1. Store certain model elements

By default the Eclipse platform stores and restores the complete runtime application model, unless the startup parameter clearPersistedState is used. If this parameter is used, the whole user changes are deleted.

It is possible to store selected model elements via the E4XMIResourceFactory class. This allows a more flexible handling of the model persistence.

The following snippet shows a handler implementation, where the corresponding model elements of the active perspective are persisted in an xmi file.

package com.vogella.e4.model.persistence.handlers;

import java.io.FileOutputStream;
import java.io.IOException;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.internal.workbench.E4XMIResourceFactory;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;

public class SaveHandler {

        @Execute
        public void execute(EModelService modelService, MWindow window) {

                // store model of the active perspective
                MPerspective activePerspective = modelService.getActivePerspective(window);

                // create a resource, which is able to store e4 model elements
                E4XMIResourceFactory e4xmiResourceFactory = new E4XMIResourceFactory();
                Resource resource = e4xmiResourceFactory.createResource(null);

                // You must clone the perspective as snippet, otherwise the running
                // application would break, because the saving process of the resource
                // removes the element from the running application model
                MUIElement clonedPerspective = modelService.cloneElement(activePerspective, window);

                // add the cloned model element to the resource so that it may be stored
                resource.getContents().add((EObject) clonedPerspective);

                FileOutputStream outputStream = null;
                try {

                        // Use a stream to save the model element
                        outputStream = new FileOutputStream("/home/example/snippet.xmi");

                        resource.save(outputStream, null);
                } catch (IOException ex) {
                        ex.printStackTrace();
                } finally {
                        if (outputStream != null) {
                                try {
                                        outputStream.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
        }
}

3.2. Load stored model elements

The previously stored xmi file can be restored by using the load method of the Resource class.

The following snippet is an example, where a stored perspective is applied to an existing application model. In case an existing perspective with the same elementId as the one of the loaded perspective is found, it is removed. Afterwards the code adds and switches to the loaded perspective.

package com.vogella.e4.model.persistence.handlers;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.internal.workbench.E4XMIResourceFactory;
import org.eclipse.e4.ui.model.application.ui.MElementContainer;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.emf.ecore.resource.Resource;

public class LoadHandler {

        @Execute
        public void execute(EPartService partService,
                                EModelService modelService,
                                MWindow window) throws IOException {

                // create a resource, which is able to store e4 model elements
                E4XMIResourceFactory e4xmiResourceFactory = new E4XMIResourceFactory();
                Resource resource = e4xmiResourceFactory.createResource(null);

                FileInputStream inputStream = null;
                try {

                        inputStream = new FileInputStream("/home/example/snippet.xmi");

                        // load the stored model element
                        resource.load(inputStream, null);

                        if (!resource.getContents().isEmpty()) {

                                // after the model element is loaded it can be obtained from the
                                // contents of the resource
                                MPerspective loadedPerspective = (MPerspective) resource.getContents().get(0);

                                // get the parent perspective stack, so that the loaded
                                // perspective can be added to it.
                                MPerspective activePerspective = modelService.getActivePerspective(window);
                                MElementContainer<MUIElement> perspectiveParent = activePerspective.getParent();

                                // remove the current perspective, which should be replaced by
                                // the loaded one
                                List<MPerspective> alreadyPresentPerspective = modelService.findElements(window,
                                                loadedPerspective.getElementId(), MPerspective.class, null);
                                for (MPerspective perspective : alreadyPresentPerspective) {
                                        modelService.removePerspectiveModel(perspective, window);
                                }

                                // add the loaded perspective and switch to it
                                perspectiveParent.getChildren().add(loadedPerspective);

                                partService.switchPerspective(loadedPerspective);
                        }
                } finally {
                        if (inputStream != null) {
                                inputStream.close();
                        }
                }
        }
}

4. Learn more about Eclipse 4 RCP development

I hope you enjoyed this tutorial. You find this tutorial and much more information also in the Eclipse 4 RCP book.

5. About this website

6.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2016 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.