diff options
| author | cbrun | 2014-11-07 09:34:00 +0000 |
|---|---|---|
| committer | cbrun | 2014-11-07 09:34:00 +0000 |
| commit | 59640e5973fb7b2d3b101b69bbffa5395b32a3bc (patch) | |
| tree | 3e9124d847cce83c0b727db269f5dc64caf4e93b | |
| parent | faf06e4e81cae1a343367579e15042c6f077434c (diff) | |
| download | org.eclipse.ecoretools-59640e5973fb7b2d3b101b69bbffa5395b32a3bc.tar.gz org.eclipse.ecoretools-59640e5973fb7b2d3b101b69bbffa5395b32a3bc.tar.xz org.eclipse.ecoretools-59640e5973fb7b2d3b101b69bbffa5395b32a3bc.zip | |
[443967] Setup a missing package handler if EMF >= 2.9
2 files changed, 118 insertions, 1 deletions
diff --git a/org.eclipse.emf.ecoretools.design/src/org/eclipse/emf/ecoretools/design/internal/GenModelMissingPackageHandler.java b/org.eclipse.emf.ecoretools.design/src/org/eclipse/emf/ecoretools/design/internal/GenModelMissingPackageHandler.java new file mode 100644 index 0000000..91a6cea --- /dev/null +++ b/org.eclipse.emf.ecoretools.design/src/org/eclipse/emf/ecoretools/design/internal/GenModelMissingPackageHandler.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * Copyright (c) 2014 Obeo + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.emf.ecoretools.design.internal; + +import java.util.List; +import java.util.Map; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.plugin.EcorePlugin; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.ecore.xmi.XMLResource; + +public class GenModelMissingPackageHandler { + + public static void setupPackageHandler(final ResourceSet set) { + // If we're in the reflective editor, set up an option to handle missing + // packages. + // + final EPackage genModelEPackage = set.getPackageRegistry().getEPackage("http://www.eclipse.org/emf/2002/GenModel"); + if (genModelEPackage != null) { + set.getLoadOptions().put(XMLResource.OPTION_MISSING_PACKAGE_HANDLER, new XMLResource.MissingPackageHandler() { + protected EClass genModelEClass; + + protected EStructuralFeature genPackagesFeature; + + protected EClass genPackageEClass; + + protected EStructuralFeature ecorePackageFeature; + + protected Map<String, URI> ePackageNsURIToGenModelLocationMap; + + public EPackage getPackage(String nsURI) { + // Initialize the metadata for accessing the GenModel + // reflective the first time. + // + if (genModelEClass == null) { + genModelEClass = (EClass) genModelEPackage.getEClassifier("GenModel"); + genPackagesFeature = genModelEClass.getEStructuralFeature("genPackages"); + genPackageEClass = (EClass) genModelEPackage.getEClassifier("GenPackage"); + ecorePackageFeature = genPackageEClass.getEStructuralFeature("ecorePackage"); + } + + // Initialize the map from registered package namespaces to + // their GenModel locations the first time. + // + if (ePackageNsURIToGenModelLocationMap == null) { + ePackageNsURIToGenModelLocationMap = EcorePlugin.getEPackageNsURIToGenModelLocationMap(true); + } + + // Look up the namespace URI in the map. + // + EPackage ePackage = null; + URI uri = ePackageNsURIToGenModelLocationMap.get(nsURI); + if (uri != null) { + // If we find it, demand load the model. + // + Resource resource = set.getResource(uri, true); + + // Locate the GenModel and fetech it's genPackages. + // + EObject genModel = (EObject) EcoreUtil.getObjectByType(resource.getContents(), genModelEClass); + @SuppressWarnings("unchecked") + List<EObject> genPackages = (List<EObject>) genModel.eGet(genPackagesFeature); + for (EObject genPackage : genPackages) { + // Check if that package's Ecore Package has them + // matching namespace URI. + // + EPackage dynamicEPackage = (EPackage) genPackage.eGet(ecorePackageFeature); + if (nsURI.equals(dynamicEPackage.getNsURI())) { + // If so, that's the package we want to return, + // and we add it to the registry so it's easy to + // find from now on. + // + ePackage = dynamicEPackage; + set.getPackageRegistry().put(nsURI, ePackage); + break; + } + } + } + return ePackage; + } + }); + } + + } + +} diff --git a/org.eclipse.emf.ecoretools.design/src/org/eclipse/emf/ecoretools/design/service/EcoreToolsDesignPlugin.java b/org.eclipse.emf.ecoretools.design/src/org/eclipse/emf/ecoretools/design/service/EcoreToolsDesignPlugin.java index 663760f..9e7244b 100644 --- a/org.eclipse.emf.ecoretools.design/src/org/eclipse/emf/ecoretools/design/service/EcoreToolsDesignPlugin.java +++ b/org.eclipse.emf.ecoretools.design/src/org/eclipse/emf/ecoretools/design/service/EcoreToolsDesignPlugin.java @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.emf.ecoretools.design.service; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashSet; @@ -22,6 +23,8 @@ import org.eclipse.core.runtime.Status; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.plugin.EcorePlugin; import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.xmi.XMLResource; +import org.eclipse.emf.ecoretools.design.internal.GenModelMissingPackageHandler; import org.eclipse.sirius.business.api.componentization.ViewpointRegistry; import org.eclipse.sirius.business.api.session.Session; import org.eclipse.sirius.business.api.session.SessionManager; @@ -78,7 +81,7 @@ public class EcoreToolsDesignPlugin extends Plugin { @SuppressWarnings("unchecked") @Override public void notifyAddSession(Session newSession) { - ResourceSet set = newSession.getTransactionalEditingDomain().getResourceSet(); + final ResourceSet set = newSession.getTransactionalEditingDomain().getResourceSet(); Map<URI, URI> result = null; // Invoke computePlatformURIMap by reflection because this API // change in EMF @@ -107,6 +110,19 @@ public class EcoreToolsDesignPlugin extends Plugin { EcoreToolsDesignPlugin.getDefault().getLog().log(status); } + + + try { + Field f = XMLResource.class.getField("OPTION_MISSING_PACKAGE_HANDLER"); + /* + * we are in EMF 2.9 or superior, we can setup the missing package handler. + */ + GenModelMissingPackageHandler.setupPackageHandler(set); + } catch (NoSuchFieldException e) { + } catch (SecurityException e) { + } + + newSession.getEventBroker().addLocalTrigger(GenModelAutoReload.SHOULD_RELOAD, new GenModelAutoReload(newSession.getTransactionalEditingDomain())); newSession.getEventBroker().addLocalTrigger(GenModelUpdateGenFeatureContainment.SHOULD_UPDATE, new GenModelUpdateGenFeatureContainment(newSession)); |
