Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aa60a52deed1be88110420cf6f6e3e2f330b7ca7 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (c) 2007, 2009 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:
 *    Alexander Shatalin (Borland) - initial API and implementation
 *   modified by Patrick Tessier (CEA LIST)
 */

«IMPORT 'http://www.eclipse.org/gmf/2009/GenModel'»
«IMPORT 'http://www.eclipse.org/emf/2002/Ecore'»
«IMPORT 'http://www.eclipse.org/emf/2002/GenModel'»
«EXTENSION xpt::GenModelUtils»
«EXTENSION xpt::diagram::Utils»
«EXTENSION xpt::diagram::commands::file»
«EXTENSION xpt::diagram::commands::NodeConstraintUtils»
«EXTENSION xpt::OclMigrationProblems»

«DEFINE CreateNodeCommand FOR gmfgen::GenNode-»
«EXPAND xpt::Common::copyright FOR getDiagram().editorGen-»
package «getDiagram().editCommandsPackageName»;
«EXPAND xpt::Common::generatedClassComment»
public class «createCommandClassName» extends org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand {
	«REM» Mutating canvas auxiliary «ENDREM»
	«EXPAND xpt::Common::generatedMemberComment-»
	private org.eclipse.gmf.runtime.notation.Diagram diagram = null;
	«EXPAND xpt::Common::generatedMemberComment-»
	private org.eclipse.emf.ecore.EObject eObject = null;
	«REM» Mutating canvas auxiliary constructor «ENDREM»
	«EXPAND xpt::Common::generatedMemberComment-»
	public «createCommandClassName»(org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest req, org.eclipse.emf.ecore.EObject eObject, org.eclipse.gmf.runtime.notation.Diagram diagram) {
		super(req.getLabel(), null, req);
		this.eObject = eObject;
		this.diagram = diagram;
	}
	«REM» Mutating canvas auxiliary builder «ENDREM»	
	«EXPAND xpt::Common::generatedMemberComment-»
	public static «createCommandClassName» create(org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest req, org.eclipse.emf.ecore.EObject eObject, org.eclipse.gmf.runtime.notation.Diagram diagram) {
		return new «createCommandClassName»(req, eObject, diagram);
	}
	«EXPAND _constructor-»
	«EXPAND getElementToEdit-»
	«EXPAND canExecuteMethod-»
	«EXPAND doExecuteWithResultMethod»
	«EXPAND doConfigureMethod»	
}
«ENDDEFINE»
«DEFINE _constructor FOR gmfgen::GenNode-»
	«EXPAND xpt::Common::generatedMemberComment»
	public «createCommandClassName»(org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest req, org.eclipse.gmf.runtime.notation.Diagram diagram) {
		super(req.getLabel(), null, req);
		this.diagram = diagram;
	}
«ENDDEFINE»
/*
 * TODO: either use setElementToEdit, or generate downcasted version (which may be troublesome if containment and child features point to a different parent) 
 */
«DEFINE getElementToEdit FOR gmfgen::GenNode-»
	«EXPAND xpt::Common::generatedMemberComment('FIXME: replace with setElementToEdit()')»
protected org.eclipse.emf.ecore.EObject getElementToEdit() {
	
	«REM» Mutating canvas helper «ENDREM»
		org.eclipse.emf.ecore.EObject container =
				((org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest) getRequest()).getContainer();
		if (container instanceof org.eclipse.gmf.runtime.notation.View) {
			container = ((org.eclipse.gmf.runtime.notation.View) container).getElement();
		}
		if (container != null) {
			return container;
		}
		return eObject;
}
«ENDDEFINE»

«REM» [AbstractElement] Modified for Abstract domain element «ENDREM»
«DEFINE doExecuteWithResultMethod FOR gmfgen::GenNode-»
	«EXPAND xpt::Common::generatedMemberComment»
	protected org.eclipse.gmf.runtime.common.core.command.CommandResult doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor monitor, org.eclipse.core.runtime.IAdaptable info) throws org.eclipse.core.commands.ExecutionException {
«REM» [AbstractElement] START «ENDREM»	
	
«IF modelFacet.metaClass.ecoreClass._abstract <> true-»
«REM» [AbstractElement] END   «ENDREM»
«IF modelFacet.isPhantomElement()-»
		«EXPAND phantomElementCreation(self, 'newElement') FOR modelFacet»
«ELSE-»
		«EXPAND normalElementCreation(self, 'newElement') FOR modelFacet»
«ENDIF»
		«EXPAND initialize(self, 'newElement') FOR modelFacet-»
«IF true/*FIXME boolean needsExternalConfiguration*/»
		doConfigure(newElement, monitor, info);
«ENDIF»
		((org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest) getRequest()).setNewElement(«EXPAND MetaModel::DowncastToEObject('newElement') FOR modelFacet.metaClass»);
		return org.eclipse.gmf.runtime.common.core.command.CommandResult.newOKCommandResult(newElement);
	}
«REM» [AbstractElement] START «ENDREM»	
«ELSE-»
		throw new UnsupportedOperationException("Unimplemented operation (abstract domain element).");
	}
«ENDIF»
«REM» [AbstractElement] END   «ENDREM»	
«ENDDEFINE»
/*
 * Unlike original CreateElementCommand, we don't keep track of IStatus from configureCommand.execute,
 * nor allow status setting from doDefaultCreation. The reason is ICommandProxy#execute implementation,
 * which ignores any status from wrapped ICommand. Besides, both CommandResult and IStatus seems too much to me.
 */
«DEFINE doConfigureMethod FOR gmfgen::GenNode-»
	«EXPAND xpt::Common::generatedMemberComment»
	protected void doConfigure(«EXPAND MetaModel::QualifiedClassName FOR modelFacet.metaClass» newElement, org.eclipse.core.runtime.IProgressMonitor monitor, org.eclipse.core.runtime.IAdaptable info) throws org.eclipse.core.commands.ExecutionException {
		org.eclipse.gmf.runtime.emf.type.core.IElementType elementType = ((org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest) getRequest()).getElementType();
		org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest configureRequest = new org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest(getEditingDomain(), «EXPAND MetaModel::DowncastToEObject('newElement') FOR modelFacet.metaClass», elementType);
		configureRequest.setClientContext(((org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest) getRequest()).getClientContext());
		configureRequest.addParameters(getRequest().getParameters());
		org.eclipse.gmf.runtime.common.core.command.ICommand configureCommand = elementType.getEditCommand(configureRequest);
		if (configureCommand != null && configureCommand.canExecute()) {
			configureCommand.execute(monitor, info);
		}
	}
«ENDDEFINE»
«DEFINE canExecuteMethod FOR gmfgen::GenNode-»
	«EXPAND xpt::Common::generatedMemberComment»
	public boolean canExecute() {
«IF modelFacet.isPhantomElement()-»
		return true;
«ELSE-»
	«EXPAND canExecute_Normal FOR modelFacet»
«ENDIF-»
	}
«ENDDEFINE»
«DEFINE canExecute_Normal FOR gmfgen::TypeModelFacet-»

«IF not containmentMetaFeature.oclIsUndefined()-»
	«IF  not containmentMetaFeature.ecoreFeature.oclIsUndefined()-»
		«IF not isUnbounded(containmentMetaFeature.ecoreFeature) or (childMetaFeature <> containmentMetaFeature and not isUnbounded(childMetaFeature.ecoreFeature))-»
			«IF not isUnbounded(containmentMetaFeature.ecoreFeature)-»
				«EXPAND MetaModel::DeclareAndAssign('container', 'getElementToEdit()') FOR containmentMetaFeature.genClass-»
					«IF isSingleValued(containmentMetaFeature.ecoreFeature)»
				if («EXPAND MetaModel::getFeatureValue('container', containmentMetaFeature.genClass) FOR containmentMetaFeature» != null) {
					«ELSE-»
				if («EXPAND MetaModel::getFeatureValue('container', containmentMetaFeature.genClass) FOR containmentMetaFeature».size() >= «containmentMetaFeature.ecoreFeature.upperBound») {
					«ENDIF-»
					return false;
				}
				«ENDIF-»
				«IF childMetaFeature <> containmentMetaFeature and not isUnbounded(childMetaFeature.ecoreFeature)-»
					«IF isSingleValued(childMetaFeature.ecoreFeature)-»
				if («EXPAND MetaModel::getFeatureValue('container', containmentMetaFeature.genClass) FOR childMetaFeature» != null) {
					«ELSE-»
				if («EXPAND MetaModel::getFeatureValue('container', containmentMetaFeature.genClass) FOR childMetaFeature».size() >= «childMetaFeature.ecoreFeature.upperBound») {
					«ENDIF-»
					return false;
				}
			«ENDIF-»
		«ENDIF-»
	«ENDIF-»
«ENDIF-»

	org.eclipse.emf.ecore.EObject target = getElementToEdit();
	org.eclipse.papyrus.infra.viewpoints.policy.ModelAddData data = org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker.getCurrent().getChildAddData(diagram, target.eClass(), «EXPAND MetaModel::MetaClass FOR metaClass»);
	return data.isPermitted();
«ENDDEFINE»

«DEFINE phantomElementCreation(node : gmfgen::GenNode, varName : String) FOR gmfgen::TypeModelFacet-»
	// Uncomment to put "phantom" objects into the diagram file.		
	// org.eclipse.emf.ecore.resource.Resource resource = 
	// 		((org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest) getRequest()).getContainer().eResource();
	// if (resource == null) {
	// 	return null;
	// }
	org.eclipse.emf.ecore.resource.Resource resource = getElementToEdit().eResource();
	«EXPAND MetaModel::NewInstance(varName) FOR metaClass»
	resource.getContents().add(«EXPAND MetaModel::DowncastToEObject(varName) FOR metaClass»);
«ENDDEFINE»

«DEFINE normalElementCreation(node : gmfgen::GenNode, varName : String) FOR gmfgen::TypeModelFacet-»
	«EXPAND MetaModel::NewInstance(varName) FOR metaClass»
	org.eclipse.emf.ecore.EObject target = getElementToEdit();
	org.eclipse.papyrus.infra.viewpoints.policy.ModelAddData data = org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker.getCurrent().getChildAddData(diagram, target, «varName»);
	if (data.isPermitted()) {
		if (data.isPathDefined()) {
			if (!data.execute(target, «varName»))
				return org.eclipse.gmf.runtime.common.core.command.CommandResult.newErrorCommandResult("Failed to follow the policy-specified for the insertion of the new element");
		} else {
«IF not containmentMetaFeature.oclIsUndefined()-»
	«EXPAND MetaModel::DeclareAndAssign('qualifiedTarget', 'target') FOR containmentMetaFeature.genClass»
	«EXPAND MetaModel::modifyFeature('qualifiedTarget', containmentMetaFeature.genClass, varName) FOR containmentMetaFeature-»
«ELSE-»
	//
	// FIXME no containment feature found in the genmodel, toolsmith need to manually write code here to add «varName» to a parent
	//
«ENDIF»
		}
	} else {
		return org.eclipse.gmf.runtime.common.core.command.CommandResult.newErrorCommandResult("The active policy restricts the addition of this element");
	}
	
«IF hasExplicitChildFeature(self)-»
	«EXPAND MetaModel::DeclareAndAssign('childHolder', 'getElementToEdit()') FOR childMetaFeature.genClass»
	«EXPAND MetaModel::modifyFeature('childHolder', childMetaFeature.genClass, varName) FOR childMetaFeature»
«ENDIF-»
«ENDDEFINE»

«DEFINE initialize(node : gmfgen::GenNode, newElementVar : String) FOR gmfgen::TypeModelFacet»«EXPAND xpt::providers::ElementInitializers::initMethodCall(self, newElementVar) FOR node»«ENDDEFINE»

Back to the top