Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

How to extend the Eclipse 4 application model. This tutorial explains how to extend the Eclipse 4 application model and how to define a new renderer for new model elements. It also covers how to create custom editor implementations for these new model elements.

1. Application model extensibility

1.1. The application model structure

The application model of an Eclipse RCP application is based on an Eclipse Modeling Framework (EMF) model defined by the Eclipse platform. This structure defines, for example, that an MPart can be stored in an MPartSashContainer.

The Eclipse platform uses generated Java classes based on this EMF model. These classes are instantiated based on the Application.e4xmi file of the application including formation from model fragments, processors and other sources which modify the model, e.g., mode add-ons.

The Eclipse platform registers listeners on the attributes of these objects, so that it can react immediately if a model property changes.

1.2. Using extensions

The underlying structure of the application model can be extended by customers using standard EMF functionality. As the renderer framework is responsible for handling the model elements, you have to define a renderer which is responsible for the new model elements.

1.3. Adding an editor for a custom model elements

The org.eclipse.e4.tools.emf.ui plug-in defines the org.eclipse.e4.tools.emf.ui.editors extension point, which allows registering editors for new model elements.

2. Prerequisites

The following description assumes that you already have experience with Eclipse EMF, Eclipse 4 and the rendering framework of Eclipse 4.

3. Exercise: Extend the Eclipse application model

In this exercise you extend the structure of the Eclipse application model with an additional model element.

3.1. Creating a plug-in and add the dependencies

Create a new simple plug-in called com.vogella.e4.appmodel.extensions.

Wizard to create a simple plug-in

Add a dependency in your new plug-in to the following plug-ins via its MANIFEST.MF file.

  • org.eclipse.e4.ui.model.workbench

  • org.eclipse.e4.core.contexts

3.2. Create new model

Create a folder called model and create a new EMF model called _UiExtensions.ecore via File ▸ New ▸ Other…​ ▸ Eclipse Modeling Framework ▸ EMF Model.

Create the EMF model
To create the EMF model

Press the Finish button.

Check the properties for this plug-in via the Properties view.

3.3. Define relationships to the Eclipse application model

Right-click on the model and select Load Resource…​.

renderermodel10

Click on the button Browse Registered Packages and select the *UiModel/application model from the org.eclipse.e4.ui.model.workbench plug-in.

Define connections to an existing model
Define connections to an existing model

3.4. Define your custom model extension

Create a new EClass model element via right-mouse click. Via the Properties view, add Part as ESuper Types to it.

renderermodel50

Use BrowserPart as value for the Name attribute.

Right-click on the model element and add an EAttribute called uri with type of EString to it.

renderermodel54

The finished model should look like the following.

renderermodel60

3.5. Create the Genmodel

Right-click your .ecore file and select File ▸ New ▸ Other…​ ▸ EMF Generator model.

Follow the wizard similar to the following screenshots.

renderermodel62
renderermodel64
renderermodel66
renderermodel70

3.6. Generate the Java classes

Use your genmodel to create the Java code. Right-click on it and select Generate Model Code.

applicationmodel generatecode10

3.7. Make generated classes available

Export all package via the Runtime tab on the MANIFEST.MF file.

Please note that the first time you run the model generation, the plugin.xml file is created. Also the org.eclipse.emf.ecore.generated_package extension point is added to it. This extension point declares your custom model. It is only added if the plugin.xml file needs to get created. If you later adjust settings of your model, i.e., your package setting, you have to adjust the extension point manually.

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>

<!--
-->

<plugin>

   <extension point="org.eclipse.emf.ecore.generated_package">
      <package
            uri="http://www.vogella.com/extensions"
            class="extension.ExtensionPackage"
            genModel="model/UiExtensions.genmodel"/>
   </extension>

</plugin>
renderermodel80

Note down the uri from the extension point. You need to define this in your Application.e4xmi later.

4. Exercise: Create renderer for new model element

Define a new simple plug-in called com.vogella.e4.appmodel.renderer.

Add the following plug-ins as dependencies to your new plug-in.

  • org.eclipse.e4.ui.workbench.renderers.swt

  • com.vogella.e4.appmodel.extensions

  • org.eclipse.jface

  • org.eclipse.e4.ui.model.workbench

  • org.eclipse.e4.ui.workbench.swt

  • org.eclipse.e4.core.contexts

Create the following new class called BrowserPartRenderer. This class is used as renderer for your new BrowserPart.

Create the following factory to assign a renderer to your new model element.

package com.vogella.e4.appmodel.renderer;

import org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.workbench.renderers.swt.WorkbenchRendererFactory;

import extensions.BrowserPart;

public class MyRendererFactory extends WorkbenchRendererFactory {

        private BrowserPartRenderer browserPartRenderer;

        @Override
        public AbstractPartRenderer getRenderer(MUIElement uiElement, Object parent) {

                if (uiElement instanceof BrowserPart) {
                        if (browserPartRenderer == null) {
                                browserPartRenderer = new BrowserPartRenderer();
                                super.initRenderer(browserPartRenderer);
                        }
                        return browserPartRenderer;
                }

                return super.getRenderer(uiElement, parent);
        }

}

5. Exercise: Use model extensions

5.1. Exercise: Create the application

Create a new Eclipse RCP application called com.vogella.e4.appmodel.app.

Add a dependency to the following plug-ins:

  • com.vogella.e4.appmodel.extension

  • com.vogella.e4.appmodel.renderer

You have to manually adjust the application model, as the default editors don’t support your new model elements. It is possible to develop custom components in the editor to handle your extensions but that is beyond this description.

Add your model name space to the Application.e4xmi file. Please note that xmlns:extension="http://www.vogella.com/ui/e4/extension defines the UI of your model extension. By this you can use extension:modelelement in the file referring to your custom model elements.

<?xml version="1.0" encoding="UTF-8"?>
<application:Application xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:advanced="http://www.eclipse.org/ui/2010/UIModel/application/ui/advanced"
xmlns:application="http://www.eclipse.org/ui/2010/UIModel/application"
xmlns:basic="http://www.eclipse.org/ui/2010/UIModel/application/ui/basic"
xmlns:menu="http://www.eclipse.org/ui/2010/UIModel/application/ui/menu"
xmlns:extensions="http://www.vogella.com/extensions"
xmi:id="_7VkpUKo8EeO_NLPjBpVjeQ" elementId="org.eclipse.e4.ide.application"
bindingContexts="_7VkpWao8EeO_NLPjBpVjeQ">
... more stuff...

Afterwards add your new part implementation to a part, the snippet to add it listed below.

<children xsi:type="extensions:BrowserPart"
elementId="com.vogella.e4.appmodel.app.part.0"
uri="http://www.eclipse.org"
label="Test"/>

=== Register your factory via an extension point

As a last step add the property rendererFactoryUri to your product in plugin.xml. Add a link to your new factory: platform:/plugin/com.vogella.e4.renderer.maps/com.vogella.e4.renderer.maps.MyRendererFactory

<?xml version="1.0" encoding="UTF-8"?>
<plugin>

   <extension
         id="product"
         point="org.eclipse.core.runtime.products">
      <product
            name="com.vogella.e4.appmodel.app"
            application="org.eclipse.e4.ui.workbench.swt.E4Application">
         <property
               name="applicationCSS"
               value="platform:/plugin/com.vogella.e4.appmodel.app/css/default.css">
         `
         <property
               name="appName"
               value="com.vogella.e4.appmodel.app">
         `
         <property
               name="rendererFactoryUri"
               value="bundleclass://com.vogella.e4.appmodel.renderer/[LINEBREAK]
               com.vogella.e4.appmodel.renderer.MyRendererFactory">
         `
      </product>
   </extension>

</plugin>

=== Run your application

Run your application. The result should display the webpage to which you pointed via the uri parameter.

renderermodel90

== About this website

== Links and Literature

=== 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 and License

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.