Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 47f7a08668e4058c55ce86dddd8c77f72cdef15d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * 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»

Back to the top