0

I am trying to programmatically load an e4 application model in order to be able to iterate on the model elements. Currently I am facing the problem, that I don't know on how to correctly load it.

Given an Application.e4xmi I tried to simply load the file using simple load an existing model which however did not result in a populated Resource (null).

Then I found out about org.eclipse.e4.tools.emf.ui.common.XMIModelResource, I am however not able to instantiate the model using the following code

URI uriNew = URI.createURI("file:///Users/marco/github-clones/osara/at.osara.rcp/Application.e4xmi");
XMIModelResource xmimr = new XMIModelResource(uriNew);

as I get Package with uri 'http://www.eclipse.org/ui/2010/UIModel/application' not found. This ecore however is located in the already imported org.eclipse.e4.ui.model.workbench

Anybody got a hint on this? Thanks!

col.panic
  • 2,944
  • 4
  • 25
  • 31

2 Answers2

2

You could use the injected EModelService or MApplication classes. Here is more info.

Kyrill
  • 21
  • 5
  • I'm sorry, thats not the problem! In your info one loads the application model of an already available application! I am trying to load the application model out of a file without using it! – col.panic Feb 21 '13 at 09:16
  • Weird, i've get the following: URI uriNew = URI.createURI("file:///Users/kirill/Documents/Dev/DW/DomainWorkbench/org.domainworkbench/Application.e4xmi"); XMIModelResource xmimr = new XMIModelResource(uriNew); assertNotNull(xmimr); assertEquals(1, xmimr.getRoot().size()); Dependencies: Require-Bundle: org.eclipse.core.runtime, org.junit, org.eclipse.e4.tools.emf.ui;bundle-version="0.12.0", org.eclipse.platform;bundle-version="4.3.0", org.eclipse.core.databinding.observable;bundle-version="1.4.1" green... – Kyrill Feb 24 '13 at 09:08
  • And for org.eclipse.platform;bundle-version="4.2.1" it also works. (of course against the model without DynamicMenuContributions...). – Kyrill Feb 24 '13 at 09:19
2

The reason is quite simple; the respective EMF model is not yet registered in the workspace. In order to do so, the following code has to be executed before loading the model:

import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
ApplicationPackageImpl.init();

Here goes a full code sample for loading an Eclipse 4 Application model in a standalone main method:

import org.eclipse.e4.ui.internal.workbench.E4XMIResourceFactory;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

public class LoadAppModel {

    private static ResourceSet resourceSet = new ResourceSetImpl();

    public static void main(String[] args) {
        ApplicationPackageImpl.init();
        URI uri = URI
                .createURI("file:///Users/marco/git/pharmacy_at/at.medevit.ecrit.pharmacy_at.application/Application.e4xmi");

        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
                .put("e4xmi", new E4XMIResourceFactory());

        Resource res = resourceSet.getResource(uri, true);
        MApplication app = (MApplication) res.getContents().get(0);
        System.out.println(app.getElementId());
    }

}
col.panic
  • 2,944
  • 4
  • 25
  • 31