Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c9286d311873abd225e06a3116fb25c880f50084 (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
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (c) 2007 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:
 *    bblajer (Borland) - initial API and implementation
 */

«IMPORT "http://www.eclipse.org/gmf/2005/GenModel/2.0"»
«IMPORT "http://www.eclipse.org/emf/2002/Ecore"»
«IMPORT "http://www.eclipse.org/emf/2002/GenModel"»
«EXTENSION xpt::GenModelUtils»

«DEFINE ReconnectLinkSourceCommand FOR gmfgen::GenLink-»
«EXPAND xpt::Common::copyright FOR diagram.editorGen»
package «diagram.editCommandsPackageName»;

«EXPAND xpt::Common::generatedClassComment»
public class «EXPAND className» «EXPAND supertypes» {
	«EXPAND attributes-»
	«EXPAND ctors-»
	«EXPAND prepare-»
	«EXPAND canReconnect-»
	«EXPAND createCommand-»
	«EXPAND xpt::commands::Common::getRelationshipContainer FOR modelFacet-»
«IF creationConstraints != null-»
	«EXPAND xpt::commands::domainModelRemoveCommand::domainModelRemoveCommand("edge")-»
«ENDIF-»
	«EXPAND additions-»
}
«ENDDEFINE»

«DEFINE className FOR gmfgen::GenLink»Reconnect«EXPAND xpt::commands::Common::commandNameInfix»SourceCommand«ENDDEFINE»

«DEFINE supertypes FOR gmfgen::GenLink»extends org.eclipse.emf.common.command.CommandWrapper«ENDDEFINE»

«DEFINE attributes FOR gmfgen::GenLink-»
	«EXPAND xpt::Common::generatedMemberComment»
	private org.eclipse.gmf.runtime.notation.Edge edge;
	«EXPAND xpt::Common::generatedMemberComment»
	private org.eclipse.gmf.runtime.notation.View newSource;
	«EXPAND xpt::Common::generatedMemberComment»
	private org.eclipse.gmf.runtime.notation.View oldSource;
«ENDDEFINE»

«DEFINE ctors FOR gmfgen::GenLink-»
	«EXPAND xpt::Common::generatedMemberComment»
	public «EXPAND className»(org.eclipse.gef.requests.ReconnectRequest request) {
		this((org.eclipse.gmf.runtime.notation.Edge)request.getConnectionEditPart().getModel(), (org.eclipse.gmf.runtime.notation.View)request.getTarget().getModel());
	}
	«EXPAND xpt::Common::generatedMemberComment»
	public «EXPAND className»(org.eclipse.gmf.runtime.notation.Edge edge, org.eclipse.gmf.runtime.notation.View newSource) {
		this.edge = edge;
		this.newSource = newSource;
		this.oldSource = edge.getSource();
	}
«ENDDEFINE»

«DEFINE prepare FOR gmfgen::GenLink-»
	«EXPAND xpt::Common::generatedMemberComment»
	protected boolean prepare() {
«IF creationConstraints == null-»
		if (!canReconnect()) {
			return false;
		}
		return super.prepare();
«ELSE-»
		final boolean[] resultHolder = new boolean[1];
		//To validate the reconnection against constraints, the current link should be deleted. Of course, we must then undo its deletion.
		final org.eclipse.emf.transaction.TransactionalEditingDomain domainModelEditDomain = org.eclipse.emf.transaction.util.TransactionUtil.getEditingDomain(oldSource.getDiagram().getElement());
		org.eclipse.emf.common.command.Command command = new org.eclipse.emf.common.command.AbstractCommand() {
			private org.eclipse.emf.common.command.Command deleteCommand = createDomainModelRemoveCommand(domainModelEditDomain);
			public boolean canExecute() {
				return deleteCommand.canExecute();
			}
			public boolean canUndo() {
				return true;
			}
			public void redo() {
			}
			public void undo() {
			}
			public void execute() {
				deleteCommand.execute();
				try {
					resultHolder[0] = canReconnect();
				} finally {
					deleteCommand.undo();
				}
			}
		};
		if (!command.canExecute()) {
			return false;
		}
		new org.eclipse.gmf.runtime.lite.commands.WrappingCommand(domainModelEditDomain, command).execute();
		if (!resultHolder[0]) {
			return false;
		}
		return super.prepare();
«ENDIF-»
	}
«ENDDEFINE»

«DEFINE canReconnect FOR gmfgen::GenLink-»
	«EXPAND xpt::Common::generatedMemberComment»
	private boolean canReconnect() {
		«EXPAND xpt::commands::Common::checkLinkConstraints("newSource.getElement()", "edge.getTarget().getElement()") FOR creationConstraints-»
		«EXPAND canReconnect FOR modelFacet-»
	}
«ENDDEFINE»

«DEFINE canReconnect FOR gmfgen::LinkModelFacet-»
return false;
«ENDDEFINE»

«DEFINE canReconnect FOR gmfgen::TypeLinkModelFacet-»
«IF containmentMetaFeature == null-»
return false;
«ELSE-»
«ENDIF-»
«containmentMetaFeature.genClass.getQualifiedInterfaceName()» container = («containmentMetaFeature.genClass.getQualifiedInterfaceName()») getRelationshipContainer(newSource.getElement(), «EXPAND xpt::Common::metaClassAccessor FOR containmentMetaFeature.genClass»);
if (container == null) {
	return false;
}
«EXPAND checkFeature("container", containmentMetaFeature.genClass) FOR containmentMetaFeature-»
«LET childMetaFeature != null && childMetaFeature != containmentMetaFeature && !childMetaFeature.ecoreFeature.derived AS setChild-»
«IF setChild-»
«EXPAND checkFeature("container", containmentMetaFeature.genClass) FOR childMetaFeature-»
«ENDIF-»
«REM»No need to check the size of the source or target features: their size does not change«ENDREM-»
«REM»Still need to check the eOpposite() of the source feature«ENDREM-»
«LET sourceMetaFeature != null && !sourceMetaFeature.ecoreFeature.derived && ecore::EReference.isSuperTypeOf(sourceMetaFeature.ecoreFeature.eClass()) 
	&& ((ecore::EReference) sourceMetaFeature.ecoreFeature).eOpposite !=containmentMetaFeature.ecoreFeature && 
		(setChild implies ((ecore::EReference) sourceMetaFeature.ecoreFeature).eOpposite != childMetaFeature.ecoreFeature) AS setSource-»
	«IF setSource-»
		«LET sourceMetaFeature.getReverse() AS sourceReverseFeature-»
			«IF sourceReverseFeature != null && !sourceReverseFeature.ecoreFeature.derived && sourceReverseFeature != containmentMetaFeature && sourceReverseFeature != childMetaFeature-»
«EXPAND checkFeature("newSource.getElement()", null) FOR sourceReverseFeature-»
			«ENDIF-»
		«ENDLET-»
	«ENDIF-»
«ENDLET-»
«ENDLET-»
return true;
«ENDDEFINE»

«DEFINE canReconnect FOR gmfgen::FeatureLinkModelFacet-»
«EXPAND checkFeature("newSource.getElement()", null) FOR metaFeature-»
return true;
«ENDDEFINE»

«DEFINE createCommand FOR gmfgen::GenLink-»
	«EXPAND xpt::Common::generatedMemberComment»
	protected org.eclipse.emf.common.command.Command createCommand() {
		org.eclipse.emf.transaction.TransactionalEditingDomain editingDomain = org.eclipse.emf.transaction.util.TransactionUtil.getEditingDomain(oldSource.getDiagram().getElement());
		org.eclipse.emf.common.command.CompoundCommand result = new org.eclipse.emf.common.command.CompoundCommand();
		result.append(new org.eclipse.gmf.runtime.lite.commands.ReconnectNotationalEdgeSourceCommand(edge, newSource));
		«EXPAND appendDomainModelCommand FOR modelFacet-»
		return result;
	}
«ENDDEFINE»

«DEFINE appendDomainModelCommand FOR gmfgen::LinkModelFacet-»
result.append(org.eclipse.emf.common.command.UnexecutableCommand.INSTANCE);
«ENDDEFINE»

«DEFINE appendDomainModelCommand FOR gmfgen::TypeLinkModelFacet-»
«IF containmentMetaFeature == null-»
result.append(org.eclipse.emf.common.command.UnexecutableCommand.INSTANCE);
«ELSE-»
«LET childMetaFeature != null && childMetaFeature != containmentMetaFeature && !childMetaFeature.ecoreFeature.derived AS setChild-»
«LET sourceMetaFeature != null && !sourceMetaFeature.ecoreFeature.derived && ecore::EReference.isSuperTypeOf(sourceMetaFeature.ecoreFeature.eClass()) 
	&& ((ecore::EReference) sourceMetaFeature.ecoreFeature).eOpposite !=containmentMetaFeature.ecoreFeature && 
		(setChild implies ((ecore::EReference) sourceMetaFeature.ecoreFeature).eOpposite != childMetaFeature.ecoreFeature) AS setSource-»
		org.eclipse.emf.ecore.EObject container = getRelationshipContainer(newSource.getElement(), «EXPAND xpt::Common::metaClassAccessor FOR containmentMetaFeature.genClass»);
		if (container == null) {
			return org.eclipse.emf.common.command.UnexecutableCommand.INSTANCE;
		}
		org.eclipse.emf.ecore.EObject oldContainer = edge.getElement().eContainer();
		if (oldContainer == null) {
			return org.eclipse.emf.common.command.UnexecutableCommand.INSTANCE;
		}
		if (oldContainer != container) {
			result.append(«EXPAND xpt::commands::Common::unplugFeatureCommand("editingDomain", "oldContainer", "edge.getElement()") FOR containmentMetaFeature»);
			result.append(«EXPAND xpt::commands::Common::plugFeatureCommand("editingDomain", "container", "edge.getElement()") FOR containmentMetaFeature»);
	«IF setChild-»
			result.append(«EXPAND xpt::commands::Common::unplugFeatureCommand("editingDomain", "oldContainer", "edge.getElement()") FOR childMetaFeature»);
			result.append(«EXPAND xpt::commands::Common::plugFeatureCommand("editingDomain", "container", "edge.getElement()") FOR childMetaFeature»);
	«ENDIF-»
		}
	«IF setSource-»
		«IF sourceMetaFeature.ecoreFeature.many-»
		result.append(«EXPAND xpt::commands::Common::unplugFeatureCommand("editingDomain", "edge.getElement()", "oldSource.getElement()") FOR sourceMetaFeature»);
		«ENDIF-»
		result.append(«EXPAND xpt::commands::Common::plugFeatureCommand("editingDomain", "edge.getElement()", "newSource.getElement()") FOR sourceMetaFeature»);
	«ENDIF-»
«ENDLET-»
«ENDLET-»
«ENDIF-»
«ENDDEFINE»

«DEFINE appendDomainModelCommand FOR gmfgen::FeatureLinkModelFacet-»
result.append(«EXPAND xpt::commands::Common::unplugFeatureCommand("editingDomain", "oldSource.getElement()", "edge.getTarget().getElement()") FOR metaFeature»);
result.append(«EXPAND xpt::commands::Common::plugFeatureCommand("editingDomain", "newSource.getElement()", "edge.getTarget().getElement()") FOR metaFeature»);
«ENDDEFINE»

«DEFINE checkFeature(String containerVar, genmodel::GenClass containerClass) FOR genmodel::GenFeature-»
«IF ecoreFeature.upperBound > 0-»
if («EXPAND xpt::commands::Common::featureMultiplicityComparator(containerVar, containerClass)») {
	return false;
}
«ENDIF-»
«ENDDEFINE»

«DEFINE additions FOR gmfgen::GenLink-»
«ENDDEFINE»

Back to the top