blob: b42ee8d60404b8cf98a720bbe14282ac007ef2dc [file] [log] [blame]
/*
* Copyright (c) 2008 Borland Software Corporation
*
* 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:
* Artem Tikhomirov (Borland) - initial API and implementation
*/
«IMPORT "http://www.eclipse.org/gmf/2008/GenModel"»
«IMPORT "http://www.eclipse.org/emf/2002/Ecore"»
«IMPORT "http://www.eclipse.org/emf/2002/GenModel"»
«EXTENSION MetaModel»
/*
* Does instanceof check.
*/
«DEFINE IsInstance(String accessor) FOR GenClass
«accessor» instanceof «getQualifiedInterfaceName()»«ENDDEFINE»
// Shorthand, negates IsInstance - handy if you consider
// different approaches for generated and dynamic models: false == instanceof
// vs. !MetaModelFacility.isInstance
«DEFINE NotInstance(String accessor) FOR GenClass
false == «EXPAND IsInstance(accessor)»«ENDDEFINE»
/**
* Special kind of instanceof check, that compares eContainer() of the object.
* Since metaClass may be an external interface, eContainer() might need cast to EObject
*/
«DEFINE IsContainerInstance(String object, GenClass metaClass) FOR GenClass
«EXPAND _getEObjectFeature(object, "eContainer()") FOR metaClass» instanceof «getQualifiedInterfaceName()»«ENDDEFINE»
«DEFINE _getEObjectFeature(String object, String feature) FOR GenClass
«IF isExternalInterface()»((org.eclipse.emf.ecore.EObject) «object»).«feature»«ELSE»«object».«feature»«ENDIF
«ENDDEFINE»
// Public/API-sort templates are named with first letter capitalized.
// FIXME getFeatureValue* and setFeatureValue start with lowercase to indicate pending refactoring
«DEFINE getFeatureValue(String containerVar, genmodel::GenClass containerClass) FOR genmodel::GenFeature
«IF isExternalInterface(containerClass)-»
((«EXPAND featureTargetType») ((org.eclipse.emf.ecore.EObject) «containerVar»).eGetEXPAND MetaFeature»))«-»
«ELSE
«containerVar».«getGetAccessor()»()«-»
«ENDIF
«ENDDEFINE»
«DEFINE featureTargetType FOR GenFeature»«IF isListType()»java.util.List«ELSE»«EXPAND QualifiedClassName FOR findGenClassifier(genClass.genPackage.genModel, ecoreFeature.eType)»«ENDIF»«ENDDEFINE»
/*
* FIXME leave only one version of these two getFeatureValue
*
* A slightly more sophisticated version of getFeatureValue().
* @param containerName the name of the container
* @param feature the feature whose value is in interest
* @param containerMetaClass the <code>GenClass</code> of the container, or <code>null</code>, if the container is declared as an <code>org.eclipse.emf.ecore.EObject</code>.
* @param needsCastToResultType whether the cast to the result type is required (this parameter is only used if the <code>org.eclipse.emf.ecore.EClass</code> this feature belongs to is an external interface).
*/
«DEFINE getFeatureValue(String containerVar, genmodel::GenClass container, boolean needsCastToResultType) FOR genmodel::GenFeature
«IF isExternalInterface(genClass)-»
«IF needsCastToResultType»((«EXPAND featureTargetType») «ENDIF»«containerVar.parenthesizedCast(container, null)».eGetEXPAND MetaFeature»)«IF needsCastToResultType»)«ENDIF
«ELSE
«containerVar.parenthesizedCast(container, genClass)».«getGetAccessor()»()«-»
«ENDIF
«ENDDEFINE»
«DEFINE setFeatureValue(String containerVar, genmodel::GenClass containerGenClass, String valueVar) FOR genmodel::GenFeature
«EXPAND setFeatureValue(containerVar, containerGenClass, valueVar, false)»«ENDDEFINE»
// FIXME support list features as well, i.e. do .add() instead of eSet
«DEFINE setFeatureValue(String containerVar, genmodel::GenClass containerGenClass, String valueVar, boolean isPlainObjectValue) FOR genmodel::GenFeature
«IF isExternalInterface(containerGenClass)-»
((org.eclipse.emf.ecore.EObject) «containerVar»).eSetEXPAND MetaFeature», «valueVar»)«-»
«ELSE
«containerVar».set«getAccessorName(this)»(«IF !isPlainObjectValue»«valueVar»«ELSE»«IF isPrimitiveType()»«EXPAND _unwrapObjectToPrimitiveValue(valueVar)»«ELSE»(«EXPAND featureTargetType») «valueVar»«ENDIF»«ENDIF»)«-»
«ENDIF
«ENDDEFINE»
«DEFINE _unwrapObjectToPrimitiveValue(String valueVar) FOR GenFeature
((«EXPAND featureTargetType») «valueVar»).«ecoreFeature.eType.instanceClassName»Value()«ENDDEFINE»
«DEFINE MetaClass FOR genmodel::GenClassifier»«getQualifiedPackageInterfaceName(genPackage)».eINSTANCE.get«getClassifierAccessorName(this)»()«ENDDEFINE»
«DEFINE MetaFeature FOR genmodel::GenFeature»«getQualifiedPackageInterfaceName(genClass.genPackage)».eINSTANCE.get«getFeatureAccessorName(this)»()«ENDDEFINE»
// SomeFactory.eINSTANCE.createBlaBla();
//NB: for map entries, the resulting type is EObject, not the qualified interface name. If cast is needed, use (un)parenthesizedCast() extension.
//see GenClassImpl#hasFactoryInterfaceCreateMethod() for details why map entries should be treated differently
«DEFINE NewInstance FOR genmodel::GenClass
«IF isMapEntry()-»
«genPackage.getQualifiedFactoryInterfaceName()».«genPackage.getFactoryInstanceName()».createEXPAND MetaClass»)«ELSE
«genPackage.getQualifiedFactoryInterfaceName()».«genPackage.getFactoryInstanceName()».create«ecoreClass.name»()«ENDIF
«ENDDEFINE»
// XXX Need to take into account possible GenClass from generated and always available code
// e.g. Notation or Ecore
«DEFINE NewInstance(String varName) FOR genmodel::GenClass
«getQualifiedInterfaceName()» «varName» = «IF isMapEntry()»(«getQualifiedInterfaceName()») «ENDIF»«EXPAND NewInstance»;
«ENDDEFINE»
/*
* Ensures value is of type EObject, may be no-op if context GenClass is compatible with EObject.
* Note, injected value is not surrounded with parenthesis, may need to introduce another
* template to accomplish that if needed.
*/
«DEFINE DowncastToEObject(String value) FOR genmodel::GenClass»«IF isExternalInterface()»(org.eclipse.emf.ecore.EObject) «ENDIF»«value»«ENDDEFINE»
/*
* Declares new variable of appropriate type and assigns casted value to it.
*/
«DEFINE DeclareAndAssign(String assignee, String value) FOR GenClass
«getQualifiedInterfaceName()» «assignee» = getQualifiedInterfaceName()») «value»;«ENDDEFINE»
/*
* third boolean parameter is to indicate the value is not EObject, so may
* need extra cast in case dynamic model instances are in use.
*/
«DEFINE DeclareAndAssign(String assignee, String value, boolean isPlainObjectValue) FOR GenClass
«EXPAND DeclareAndAssign(assignee, value)»«ENDDEFINE»
«DEFINE DeclareAndAssign(String assignee, String value, boolean isPlainObjectValue) FOR GenClassifier
«getQualifiedClassName()» «assignee» = getQualifiedClassName()») «value»;«ENDDEFINE»
// @see IsContainerInstance
«DEFINE DeclareAndAssignContainer(String assignee, String object, GenClass metaClass) FOR GenClass
«getQualifiedInterfaceName()» «assignee» = getQualifiedInterfaceName()») «EXPAND _getEObjectFeature(object, "eContainer()") FOR metaClass»;«ENDDEFINE»
/*
* Declares new variable of context type and assignes a value obtained from 'src',
* which is of type 'srcMetaClass', via 'srcFeature'
*
* XXX in certain scenarions may need extra cast of the feature value
*/
«DEFINE DeclareAndAssign(String assignee, String src, GenClass srcMetaClass, GenFeature srcFeature) FOR GenClass
«getQualifiedInterfaceName()» «assignee» = «EXPAND getFeatureValue(src, srcMetaClass) FOR srcFeature»;«ENDDEFINE»
/*
* Same as DeclareAndAssign, with extra operation applied to source object
*/
«DEFINE DeclareAndAssign2(String assignee, String src, GenClass srcMetaClass, GenFeature srcFeature, String srcExt, boolean needCast) FOR GenClass
«getQualifiedInterfaceName()» «assignee» = «IF needCast»(«getQualifiedInterfaceName()») «ENDIF»«EXPAND getFeatureValue(src, srcMetaClass) FOR srcFeature».«srcExt»;«ENDDEFINE»
/*
* Cast value of type EObject to specific type. Would be no-op with dynamic model instances,
* therefore, the fact eObjectValue is actually EObject is essential
*/
«DEFINE CastEObject(String eObjectValue) FOR GenClass»(«getQualifiedInterfaceName()») «eObjectValue»«ENDDEFINE»
/**
* Qualified interface name of the generated EClass, or EObject for dynamic models.
* Use whenever class name is inevitable (e.g. method arguments)
* SHOULD NEVER APPEAR in instanceof or any other similar comparison operation
*/
«DEFINE QualifiedClassName FOR GenClass»«getQualifiedInterfaceName()»«ENDDEFINE»
«DEFINE QualifiedClassName FOR GenClassifier»«getQualifiedClassName()»«ENDDEFINE»
«DEFINE PopulateItemProviderFactories(String factoryListVar) FOR GenEditorGenerator
«FOREACH getAllDomainGenPackages(true) AS genPackage
«factoryListVar».add(new «genPackage.getQualifiedItemProviderAdapterFactoryClassName()»());
«ENDFOREACH
«ENDDEFINE»