|
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 * bblajer (Borland) - initial API and implementation |
|
11 */ |
|
12 |
|
13 «IMPORT "http://www.eclipse.org/gmf/2008/GenModel"» |
|
14 «IMPORT "http://www.eclipse.org/emf/2002/Ecore"» |
|
15 «IMPORT "http://www.eclipse.org/emf/2002/GenModel"» |
|
16 «EXTENSION xpt::GenModelUtils» |
|
17 |
|
18 «DEFINE ReconnectLinkSourceCommand FOR gmfgen::GenLink-» |
|
19 «EXPAND xpt::Common::copyright FOR diagram.editorGen» |
|
20 package «diagram.editCommandsPackageName»; |
|
21 |
|
22 «EXPAND xpt::Common::generatedClassComment» |
|
23 public class «EXPAND className» «EXPAND supertypes» { |
|
24 «EXPAND attributes-» |
|
25 «EXPAND ctors-» |
|
26 «EXPAND prepare-» |
|
27 «EXPAND canReconnect-» |
|
28 «EXPAND createCommand-» |
|
29 «EXPAND xpt::commands::Common::getRelationshipContainer FOR modelFacet-» |
|
30 «IF creationConstraints != null-» |
|
31 «EXPAND xpt::commands::domainModelRemoveCommand::domainModelRemoveCommand("edge")-» |
|
32 «ENDIF-» |
|
33 «EXPAND additions-» |
|
34 } |
|
35 «ENDDEFINE» |
|
36 |
|
37 «DEFINE className FOR gmfgen::GenLink»Reconnect«EXPAND xpt::commands::Common::commandNameInfix»SourceCommand«ENDDEFINE» |
|
38 «DEFINE qualifiedClassName FOR gmfgen::GenLink»«getDiagram().editCommandsPackageName».«EXPAND className»«ENDDEFINE» |
|
39 «DEFINE supertypes FOR gmfgen::GenLink»extends org.eclipse.emf.common.command.CommandWrapper«ENDDEFINE» |
|
40 |
|
41 «DEFINE attributes FOR gmfgen::GenLink-» |
|
42 «EXPAND xpt::Common::generatedMemberComment» |
|
43 private org.eclipse.gmf.runtime.notation.Edge edge; |
|
44 «EXPAND xpt::Common::generatedMemberComment» |
|
45 private org.eclipse.gmf.runtime.notation.View newSource; |
|
46 «EXPAND xpt::Common::generatedMemberComment» |
|
47 private org.eclipse.gmf.runtime.notation.View oldSource; |
|
48 «ENDDEFINE» |
|
49 |
|
50 «DEFINE ctors FOR gmfgen::GenLink-» |
|
51 «EXPAND xpt::Common::generatedMemberComment» |
|
52 public «EXPAND className»(org.eclipse.gef.requests.ReconnectRequest request) { |
|
53 this((org.eclipse.gmf.runtime.notation.Edge)request.getConnectionEditPart().getModel(), (org.eclipse.gmf.runtime.notation.View)request.getTarget().getModel()); |
|
54 } |
|
55 «EXPAND xpt::Common::generatedMemberComment» |
|
56 public «EXPAND className»(org.eclipse.gmf.runtime.notation.Edge edge, org.eclipse.gmf.runtime.notation.View newSource) { |
|
57 this.edge = edge; |
|
58 this.newSource = newSource; |
|
59 this.oldSource = edge.getSource(); |
|
60 } |
|
61 «ENDDEFINE» |
|
62 |
|
63 «DEFINE prepare FOR gmfgen::GenLink-» |
|
64 «EXPAND xpt::Common::generatedMemberComment» |
|
65 protected boolean prepare() { |
|
66 «IF creationConstraints == null-» |
|
67 if (!canReconnect()) { |
|
68 return false; |
|
69 } |
|
70 return super.prepare(); |
|
71 «ELSE-» |
|
72 final boolean[] resultHolder = new boolean[1]; |
|
73 //To validate the reconnection against constraints, the current link should be deleted. Of course, we must then undo its deletion. |
|
74 final org.eclipse.emf.transaction.TransactionalEditingDomain domainModelEditDomain = org.eclipse.emf.transaction.util.TransactionUtil.getEditingDomain(oldSource.getDiagram().getElement()); |
|
75 org.eclipse.emf.common.command.Command command = new org.eclipse.emf.common.command.AbstractCommand() { |
|
76 private org.eclipse.emf.common.command.Command deleteCommand = createDomainModelRemoveCommand(domainModelEditDomain); |
|
77 public boolean canExecute() { |
|
78 return deleteCommand.canExecute(); |
|
79 } |
|
80 public boolean canUndo() { |
|
81 return true; |
|
82 } |
|
83 public void redo() { |
|
84 } |
|
85 public void undo() { |
|
86 } |
|
87 public void execute() { |
|
88 deleteCommand.execute(); |
|
89 try { |
|
90 resultHolder[0] = canReconnect(); |
|
91 } finally { |
|
92 deleteCommand.undo(); |
|
93 } |
|
94 } |
|
95 }; |
|
96 if (!command.canExecute()) { |
|
97 return false; |
|
98 } |
|
99 new org.eclipse.gmf.runtime.lite.commands.WrappingCommand(domainModelEditDomain, command).execute(); |
|
100 if (!resultHolder[0]) { |
|
101 return false; |
|
102 } |
|
103 return super.prepare(); |
|
104 «ENDIF-» |
|
105 } |
|
106 «ENDDEFINE» |
|
107 |
|
108 «DEFINE canReconnect FOR gmfgen::GenLink-» |
|
109 «EXPAND xpt::Common::generatedMemberComment» |
|
110 private boolean canReconnect() { |
|
111 «EXPAND xpt::commands::Common::checkLinkConstraints("newSource.getElement()", "edge.getTarget().getElement()") FOR creationConstraints-» |
|
112 «EXPAND canReconnect FOR modelFacet-» |
|
113 } |
|
114 «ENDDEFINE» |
|
115 |
|
116 «DEFINE canReconnect FOR gmfgen::LinkModelFacet-» |
|
117 return false; |
|
118 «ENDDEFINE» |
|
119 |
|
120 «DEFINE canReconnect FOR gmfgen::TypeLinkModelFacet-» |
|
121 «IF containmentMetaFeature == null-» |
|
122 return false; |
|
123 «ELSE-» |
|
124 «ENDIF-» |
|
125 org.eclipse.emf.ecore.EObject containerEObj = getRelationshipContainer(newSource.getElement(), «EXPAND MetaModel::MetaClass FOR containmentMetaFeature.genClass»); |
|
126 «EXPAND MetaModel::DeclareAndAssign("container", "containerEObj") FOR containmentMetaFeature.genClass» |
|
127 if (container == null) { |
|
128 return false; |
|
129 } |
|
130 «EXPAND checkFeature("container", containmentMetaFeature.genClass) FOR containmentMetaFeature-» |
|
131 «LET childMetaFeature != null && childMetaFeature != containmentMetaFeature && !childMetaFeature.ecoreFeature.derived AS setChild-» |
|
132 «IF setChild-» |
|
133 «EXPAND checkFeature("container", containmentMetaFeature.genClass) FOR childMetaFeature-» |
|
134 «ENDIF-» |
|
135 «REM»No need to check the size of the source or target features: their size does not change«ENDREM-» |
|
136 «REM»Still need to check the eOpposite() of the source feature«ENDREM-» |
|
137 «LET sourceMetaFeature != null && !sourceMetaFeature.ecoreFeature.derived && ecore::EReference.isSuperTypeOf(sourceMetaFeature.ecoreFeature.eClass()) |
|
138 && ((ecore::EReference) sourceMetaFeature.ecoreFeature).eOpposite !=containmentMetaFeature.ecoreFeature && |
|
139 (setChild implies ((ecore::EReference) sourceMetaFeature.ecoreFeature).eOpposite != childMetaFeature.ecoreFeature) AS setSource-» |
|
140 «IF setSource-» |
|
141 «LET sourceMetaFeature.getReverse() AS sourceReverseFeature-» |
|
142 «IF sourceReverseFeature != null && !sourceReverseFeature.ecoreFeature.derived && sourceReverseFeature != containmentMetaFeature && sourceReverseFeature != childMetaFeature-» |
|
143 «EXPAND checkFeature("newSource.getElement()", null) FOR sourceReverseFeature-» |
|
144 «ENDIF-» |
|
145 «ENDLET-» |
|
146 «ENDIF-» |
|
147 «ENDLET-» |
|
148 «ENDLET-» |
|
149 return true; |
|
150 «ENDDEFINE» |
|
151 |
|
152 «DEFINE canReconnect FOR gmfgen::FeatureLinkModelFacet-» |
|
153 «EXPAND checkFeature("newSource.getElement()", null) FOR metaFeature-» |
|
154 return true; |
|
155 «ENDDEFINE» |
|
156 |
|
157 «DEFINE createCommand FOR gmfgen::GenLink-» |
|
158 «EXPAND xpt::Common::generatedMemberComment» |
|
159 protected org.eclipse.emf.common.command.Command createCommand() { |
|
160 org.eclipse.emf.transaction.TransactionalEditingDomain editingDomain = org.eclipse.emf.transaction.util.TransactionUtil.getEditingDomain(oldSource.getDiagram().getElement()); |
|
161 org.eclipse.emf.common.command.CompoundCommand result = new org.eclipse.emf.common.command.CompoundCommand(); |
|
162 result.append(new org.eclipse.gmf.runtime.lite.commands.ReconnectNotationalEdgeSourceCommand(edge, newSource)); |
|
163 «EXPAND appendDomainModelCommand FOR modelFacet-» |
|
164 return result; |
|
165 } |
|
166 «ENDDEFINE» |
|
167 |
|
168 «DEFINE appendDomainModelCommand FOR gmfgen::LinkModelFacet-» |
|
169 result.append(org.eclipse.emf.common.command.UnexecutableCommand.INSTANCE); |
|
170 «ENDDEFINE» |
|
171 |
|
172 «DEFINE appendDomainModelCommand FOR gmfgen::TypeLinkModelFacet-» |
|
173 «IF containmentMetaFeature == null-» |
|
174 result.append(org.eclipse.emf.common.command.UnexecutableCommand.INSTANCE); |
|
175 «ELSE-» |
|
176 «LET childMetaFeature != null && childMetaFeature != containmentMetaFeature && !childMetaFeature.ecoreFeature.derived AS setChild-» |
|
177 «LET sourceMetaFeature != null && !sourceMetaFeature.ecoreFeature.derived && ecore::EReference.isSuperTypeOf(sourceMetaFeature.ecoreFeature.eClass()) |
|
178 && ((ecore::EReference) sourceMetaFeature.ecoreFeature).eOpposite !=containmentMetaFeature.ecoreFeature && |
|
179 (setChild implies ((ecore::EReference) sourceMetaFeature.ecoreFeature).eOpposite != childMetaFeature.ecoreFeature) AS setSource-» |
|
180 org.eclipse.emf.ecore.EObject container = getRelationshipContainer(newSource.getElement(), «EXPAND MetaModel::MetaClass FOR containmentMetaFeature.genClass»); |
|
181 if (container == null) { |
|
182 return org.eclipse.emf.common.command.UnexecutableCommand.INSTANCE; |
|
183 } |
|
184 org.eclipse.emf.ecore.EObject oldContainer = edge.getElement().eContainer(); |
|
185 if (oldContainer == null) { |
|
186 return org.eclipse.emf.common.command.UnexecutableCommand.INSTANCE; |
|
187 } |
|
188 if (oldContainer != container) { |
|
189 result.append(«EXPAND xpt::commands::Common::unplugFeatureCommand("editingDomain", "oldContainer", "edge.getElement()") FOR containmentMetaFeature»); |
|
190 result.append(«EXPAND xpt::commands::Common::plugFeatureCommand("editingDomain", "container", "edge.getElement()") FOR containmentMetaFeature»); |
|
191 «IF setChild-» |
|
192 result.append(«EXPAND xpt::commands::Common::unplugFeatureCommand("editingDomain", "oldContainer", "edge.getElement()") FOR childMetaFeature»); |
|
193 result.append(«EXPAND xpt::commands::Common::plugFeatureCommand("editingDomain", "container", "edge.getElement()") FOR childMetaFeature»); |
|
194 «ENDIF-» |
|
195 } |
|
196 «IF setSource-» |
|
197 «IF sourceMetaFeature.ecoreFeature.many-» |
|
198 result.append(«EXPAND xpt::commands::Common::unplugFeatureCommand("editingDomain", "edge.getElement()", "oldSource.getElement()") FOR sourceMetaFeature»); |
|
199 «ENDIF-» |
|
200 result.append(«EXPAND xpt::commands::Common::plugFeatureCommand("editingDomain", "edge.getElement()", "newSource.getElement()") FOR sourceMetaFeature»); |
|
201 «ENDIF-» |
|
202 «ENDLET-» |
|
203 «ENDLET-» |
|
204 «ENDIF-» |
|
205 «ENDDEFINE» |
|
206 |
|
207 «DEFINE appendDomainModelCommand FOR gmfgen::FeatureLinkModelFacet-» |
|
208 result.append(«EXPAND xpt::commands::Common::unplugFeatureCommand("editingDomain", "oldSource.getElement()", "edge.getTarget().getElement()") FOR metaFeature»); |
|
209 result.append(«EXPAND xpt::commands::Common::plugFeatureCommand("editingDomain", "newSource.getElement()", "edge.getTarget().getElement()") FOR metaFeature»); |
|
210 «ENDDEFINE» |
|
211 |
|
212 «DEFINE checkFeature(String containerVar, genmodel::GenClass containerClass) FOR genmodel::GenFeature-» |
|
213 «IF ecoreFeature.upperBound > 0-» |
|
214 if («EXPAND xpt::commands::Common::featureMultiplicityComparator(containerVar, containerClass)») { |
|
215 return false; |
|
216 } |
|
217 «ENDIF-» |
|
218 «ENDDEFINE» |
|
219 |
|
220 «DEFINE additions FOR gmfgen::GenLink-» |
|
221 «ENDDEFINE» |