carbidecpp22devenv/plugins/org.eclipse.gmf.templates.legacy_1.0.0.v20090614-0839/codegen.templates/xpt/expressions/AbstractExpression.xpt
changeset 422 033392511bf7
equal deleted inserted replaced
421:631a44165bcf 422:033392511bf7
       
     1 /*
       
     2  * Copyright (c) 2007, 2008 Borland Software Corporation
       
     3  * 
       
     4  * All rights reserved. This program and the accompanying materials
       
     5  * are made available under the terms of the Eclipse Public License v1.0
       
     6  * which accompanies this distribution, and is available at
       
     7  * http://www.eclipse.org/legal/epl-v10.html
       
     8  *
       
     9  * Contributors:
       
    10  *    Dmitry Stadnik (Borland) - initial API and implementation
       
    11  */
       
    12 
       
    13 «IMPORT "http://www.eclipse.org/gmf/2008/GenModel"»
       
    14 
       
    15 «DEFINE AbstractExpression FOR gmfgen::GenDiagram-»
       
    16 «EXPAND xpt::Common::copyright FOR editorGen-»
       
    17 package «editorGen.expressionProviders.expressionsPackageName»;
       
    18 
       
    19 «EXPAND xpt::Common::generatedClassComment»
       
    20 public abstract class «editorGen.expressionProviders.abstractExpressionClassName» «EXPAND supertypes»{
       
    21 
       
    22 «EXPAND status-»
       
    23 
       
    24 «EXPAND body-»
       
    25 
       
    26 «EXPAND context-»
       
    27 
       
    28 «EXPAND ctors-»
       
    29 
       
    30 «EXPAND evaluate-»
       
    31 
       
    32 «EXPAND performCast-»
       
    33 
       
    34 «EXPAND additions-»
       
    35 }
       
    36 «ENDDEFINE»
       
    37 
       
    38 «DEFINE supertypes FOR gmfgen::GenDiagram»«ENDDEFINE»
       
    39 «DEFINE additions FOR gmfgen::GenDiagram»«ENDDEFINE»
       
    40 
       
    41 «DEFINE status FOR gmfgen::GenDiagram-»
       
    42 	«EXPAND xpt::Common::generatedMemberComment»
       
    43 	private org.eclipse.core.runtime.IStatus status = org.eclipse.core.runtime.Status.OK_STATUS;	
       
    44 
       
    45 	«EXPAND xpt::Common::generatedMemberComment»
       
    46 	protected void setStatus(int severity, String message, Throwable throwable) {		
       
    47 		String pluginID = «editorGen.plugin.getActivatorQualifiedClassName()».ID;
       
    48 		this.status = new org.eclipse.core.runtime.Status(severity, pluginID, -1, (message != null) ? message : "", throwable); «EXPAND xpt::Common::nonNLS»
       
    49 		if(!this.status.isOK()) {
       
    50 			«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError("Expression problem:" + message + "body:"+ body(), throwable); «EXPAND xpt::Common::nonNLS» «EXPAND xpt::Common::nonNLS(2)»
       
    51 		}
       
    52 	}
       
    53 
       
    54 	«EXPAND xpt::Common::generatedMemberComment»
       
    55 	public org.eclipse.core.runtime.IStatus getStatus() {
       
    56 		return status;
       
    57 	}
       
    58 «ENDDEFINE»
       
    59 
       
    60 «DEFINE body FOR gmfgen::GenDiagram-»
       
    61 	«EXPAND xpt::Common::generatedMemberComment»
       
    62 	private final String myBody;
       
    63 
       
    64 	«EXPAND xpt::Common::generatedMemberComment»
       
    65 	public String body() {
       
    66 		return myBody;
       
    67 	}
       
    68 «ENDDEFINE»
       
    69 
       
    70 «DEFINE context FOR gmfgen::GenDiagram-»
       
    71 	«EXPAND xpt::Common::generatedMemberComment»
       
    72 	private final org.eclipse.emf.ecore.EClassifier myContext;
       
    73 
       
    74 	«EXPAND xpt::Common::generatedMemberComment»
       
    75 	public org.eclipse.emf.ecore.EClassifier context() {
       
    76 		return myContext;
       
    77 	}
       
    78 «ENDDEFINE»
       
    79 
       
    80 «DEFINE ctors FOR gmfgen::GenDiagram-»
       
    81 
       
    82 	«EXPAND xpt::Common::generatedMemberComment»
       
    83 	protected «editorGen.expressionProviders.abstractExpressionClassName»(String body, org.eclipse.emf.ecore.EClassifier context) {
       
    84 		myBody = body;
       
    85 		myContext = context;
       
    86 	}
       
    87 «ENDDEFINE»
       
    88 
       
    89 «DEFINE evaluate FOR gmfgen::GenDiagram-»
       
    90 	«EXPAND xpt::Common::generatedMemberComment»
       
    91 	protected abstract Object doEvaluate(Object context, java.util.Map env);
       
    92 
       
    93 	«EXPAND xpt::Common::generatedMemberComment»
       
    94 	public Object evaluate(Object context) {
       
    95 		return evaluate(context, java.util.Collections.EMPTY_MAP);
       
    96 	}
       
    97 
       
    98 «EXPAND xpt::Common::generatedMemberComment»
       
    99 	public Object evaluate(Object context, java.util.Map env) {
       
   100 		if(context().isInstance(context)) {
       
   101 			try {
       
   102 				return doEvaluate(context, env);
       
   103 			} catch(Exception e) {
       
   104 				«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError("Expression evaluation failure: " + body(), e);«EXPAND xpt::Common::nonNLS»
       
   105 			}
       
   106 		}
       
   107 		return null;
       
   108 	}
       
   109 «ENDDEFINE»
       
   110 
       
   111 «DEFINE performCast FOR gmfgen::GenDiagram-»
       
   112 	«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")»
       
   113 	public static Object performCast(Object value, org.eclipse.emf.ecore.EDataType targetType) {
       
   114 		if (targetType instanceof org.eclipse.emf.ecore.EEnum) {
       
   115 			if (value instanceof org.eclipse.emf.ecore.EEnumLiteral) {
       
   116 				org.eclipse.emf.ecore.EEnumLiteral literal = (org.eclipse.emf.ecore.EEnumLiteral) value;
       
   117 				return (literal.getInstance() != null) ? literal.getInstance() : literal;
       
   118 			}
       
   119 		}
       
   120 		if (false == value instanceof Number || targetType == null || targetType.getInstanceClass() == null) {
       
   121 			return value;
       
   122 		}
       
   123 		Class targetClass = targetType.getInstanceClass();
       
   124 		Number num = (Number) value;
       
   125 		Class valClass = value.getClass();
       
   126 		Class targetWrapperClass = targetClass;
       
   127 		if (targetClass.isPrimitive()) {
       
   128 			targetWrapperClass = org.eclipse.emf.ecore.util.EcoreUtil.wrapperClassFor(targetClass);
       
   129 		}
       
   130 		if (valClass.equals(targetWrapperClass)) {
       
   131 			return value;
       
   132 		}
       
   133 		if (Number.class.isAssignableFrom(targetWrapperClass)) {
       
   134 			if (targetWrapperClass.equals(Byte.class)) return new Byte(num.byteValue());
       
   135 			if (targetWrapperClass.equals(Integer.class)) return new Integer(num.intValue());
       
   136 			if (targetWrapperClass.equals(Short.class)) return new Short(num.shortValue());
       
   137 			if (targetWrapperClass.equals(Long.class)) return new Long(num.longValue());
       
   138 			if (targetWrapperClass.equals(java.math.BigInteger.class)) return java.math.BigInteger.valueOf(num.longValue());
       
   139 			if (targetWrapperClass.equals(Float.class)) return new Float(num.floatValue());
       
   140 			if (targetWrapperClass.equals(Double.class)) return new Double(num.doubleValue());
       
   141 			if (targetWrapperClass.equals(java.math.BigDecimal.class)) return new java.math.BigDecimal(num.doubleValue());
       
   142 		}
       
   143 		return value;
       
   144 	}
       
   145 «ENDDEFINE»