carbidecpp22devenv/plugins/org.eclipse.gmf.templates.legacy_1.0.0.v20090614-0839/codegen.templates/xpt/expressions/AbstractExpression.xpt
author cawthron
Fri, 04 Dec 2009 12:29:56 -0600
changeset 779 4870ed7d9d38
parent 422 033392511bf7
permissions -rw-r--r--
remove .branch.txt

/*
 * Copyright (c) 2007, 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:
 *    Dmitry Stadnik (Borland) - initial API and implementation
 */

«IMPORT "http://www.eclipse.org/gmf/2008/GenModel"»

«DEFINE AbstractExpression FOR gmfgen::GenDiagram-»
«EXPAND xpt::Common::copyright FOR editorGen-»
package «editorGen.expressionProviders.expressionsPackageName»;

«EXPAND xpt::Common::generatedClassComment»
public abstract class «editorGen.expressionProviders.abstractExpressionClassName» «EXPAND supertypes»{

«EXPAND status-»

«EXPAND body-»

«EXPAND context-»

«EXPAND ctors-»

«EXPAND evaluate-»

«EXPAND performCast-»

«EXPAND additions-»
}
«ENDDEFINE»

«DEFINE supertypes FOR gmfgen::GenDiagram»«ENDDEFINE»
«DEFINE additions FOR gmfgen::GenDiagram»«ENDDEFINE»

«DEFINE status FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	private org.eclipse.core.runtime.IStatus status = org.eclipse.core.runtime.Status.OK_STATUS;	

	«EXPAND xpt::Common::generatedMemberComment»
	protected void setStatus(int severity, String message, Throwable throwable) {		
		String pluginID = «editorGen.plugin.getActivatorQualifiedClassName()».ID;
		this.status = new org.eclipse.core.runtime.Status(severity, pluginID, -1, (message != null) ? message : "", throwable); «EXPAND xpt::Common::nonNLS»
		if(!this.status.isOK()) {
			«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError("Expression problem:" + message + "body:"+ body(), throwable); «EXPAND xpt::Common::nonNLS» «EXPAND xpt::Common::nonNLS(2)»
		}
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public org.eclipse.core.runtime.IStatus getStatus() {
		return status;
	}
«ENDDEFINE»

«DEFINE body FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	private final String myBody;

	«EXPAND xpt::Common::generatedMemberComment»
	public String body() {
		return myBody;
	}
«ENDDEFINE»

«DEFINE context FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	private final org.eclipse.emf.ecore.EClassifier myContext;

	«EXPAND xpt::Common::generatedMemberComment»
	public org.eclipse.emf.ecore.EClassifier context() {
		return myContext;
	}
«ENDDEFINE»

«DEFINE ctors FOR gmfgen::GenDiagram-»

	«EXPAND xpt::Common::generatedMemberComment»
	protected «editorGen.expressionProviders.abstractExpressionClassName»(String body, org.eclipse.emf.ecore.EClassifier context) {
		myBody = body;
		myContext = context;
	}
«ENDDEFINE»

«DEFINE evaluate FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
	protected abstract Object doEvaluate(Object context, java.util.Map env);

	«EXPAND xpt::Common::generatedMemberComment»
	public Object evaluate(Object context) {
		return evaluate(context, java.util.Collections.EMPTY_MAP);
	}

«EXPAND xpt::Common::generatedMemberComment»
	public Object evaluate(Object context, java.util.Map env) {
		if(context().isInstance(context)) {
			try {
				return doEvaluate(context, env);
			} catch(Exception e) {
				«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError("Expression evaluation failure: " + body(), e);«EXPAND xpt::Common::nonNLS»
			}
		}
		return null;
	}
«ENDDEFINE»

«DEFINE performCast FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment("Expression may return number value which is not directly compatible with feature type (e.g. Double when Integer is expected), or EEnumLiteral meta-object when literal instance is expected")»
	public static Object performCast(Object value, org.eclipse.emf.ecore.EDataType targetType) {
		if (targetType instanceof org.eclipse.emf.ecore.EEnum) {
			if (value instanceof org.eclipse.emf.ecore.EEnumLiteral) {
				org.eclipse.emf.ecore.EEnumLiteral literal = (org.eclipse.emf.ecore.EEnumLiteral) value;
				return (literal.getInstance() != null) ? literal.getInstance() : literal;
			}
		}
		if (false == value instanceof Number || targetType == null || targetType.getInstanceClass() == null) {
			return value;
		}
		Class targetClass = targetType.getInstanceClass();
		Number num = (Number) value;
		Class valClass = value.getClass();
		Class targetWrapperClass = targetClass;
		if (targetClass.isPrimitive()) {
			targetWrapperClass = org.eclipse.emf.ecore.util.EcoreUtil.wrapperClassFor(targetClass);
		}
		if (valClass.equals(targetWrapperClass)) {
			return value;
		}
		if (Number.class.isAssignableFrom(targetWrapperClass)) {
			if (targetWrapperClass.equals(Byte.class)) return new Byte(num.byteValue());
			if (targetWrapperClass.equals(Integer.class)) return new Integer(num.intValue());
			if (targetWrapperClass.equals(Short.class)) return new Short(num.shortValue());
			if (targetWrapperClass.equals(Long.class)) return new Long(num.longValue());
			if (targetWrapperClass.equals(java.math.BigInteger.class)) return java.math.BigInteger.valueOf(num.longValue());
			if (targetWrapperClass.equals(Float.class)) return new Float(num.floatValue());
			if (targetWrapperClass.equals(Double.class)) return new Double(num.doubleValue());
			if (targetWrapperClass.equals(java.math.BigDecimal.class)) return new java.math.BigDecimal(num.doubleValue());
		}
		return value;
	}
«ENDDEFINE»