Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 90e25e1207187d639b5a25872451d1d70cd7e8c4 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<%@ jet package="org.eclipse.gmf.codegen.templates.policies" class="ItemSemanticEditPolicyGenerator"
	imports="org.eclipse.gmf.codegen.gmfgen.* org.eclipse.gmf.common.codegen.* org.eclipse.emf.codegen.ecore.genmodel.* java.util.*"
	skeleton="../common.skeleton" %>
<%
GenDiagram diagram = (GenDiagram) ((Object[]) argument)[0];
ImportUtil importManager = new ImportUtil(diagram.getEditPoliciesPackageName());
String pluginActivatorClass = importManager.getImportedName(diagram.getEditorGen().getPlugin().getActivatorQualifiedClassName());
%>
package <%=diagram.getEditPoliciesPackageName()%>;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.commands.EtoolsProxyCommand;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.SemanticEditPolicy;
import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeModelCommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyReferenceRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DuplicateElementsRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.GetEditContextRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientReferenceRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.gmf.runtime.notation.View;
<%importManager.markImportLocation(stringBuffer);%>

/**
 * @generated
 */
public class <%=diagram.getBaseItemSemanticEditPolicyClassName()%> extends SemanticEditPolicy {

	/**
	 * @generated
	 */
	protected Command getSemanticCommand(IEditCommandRequest request) {
		IEditCommandRequest completedRequest = completeRequest(request);
		Command semanticCommand = getSemanticCommandSwitch(completedRequest);
		if (semanticCommand == null) {
			return UnexecutableCommand.INSTANCE;
		}
		boolean shouldProceed = true;
		if (completedRequest instanceof DestroyRequest) {
			shouldProceed = shouldProceed((DestroyRequest) completedRequest);
		}
		if (shouldProceed) {
			if (completedRequest instanceof DestroyRequest) {
				ICommand deleteCommand = new DeleteCommand((View) getHost().getModel());
				semanticCommand = semanticCommand.chain(new EtoolsProxyCommand(deleteCommand));
			}
			return semanticCommand;
		}
		return UnexecutableCommand.INSTANCE;
	}

	/**
	 * @generated
	 */
	protected Command getSemanticCommandSwitch(IEditCommandRequest req) {
		if (req instanceof CreateRelationshipRequest) {
			return getCreateRelationshipCommand((CreateRelationshipRequest) req);
		} else if (req instanceof CreateElementRequest) {
			return getCreateCommand((CreateElementRequest) req);
		} else if (req instanceof ConfigureRequest) {
			return getConfigureCommand((ConfigureRequest) req);
		} else if (req instanceof DestroyElementRequest) {
			return getDestroyElementCommand((DestroyElementRequest) req);
		} else if (req instanceof DestroyReferenceRequest) {
			return getDestroyReferenceCommand((DestroyReferenceRequest) req);
		} else if (req instanceof DuplicateElementsRequest) {
			return getDuplicateCommand((DuplicateElementsRequest) req);
		} else if (req instanceof GetEditContextRequest) {
			return getEditContextCommand((GetEditContextRequest) req);
		} else if (req instanceof MoveRequest) {
			return getMoveCommand((MoveRequest) req);
		} else if (req instanceof ReorientReferenceRelationshipRequest) {
			return getReorientReferenceRelationshipCommand((ReorientReferenceRelationshipRequest) req);
		} else if (req instanceof ReorientRelationshipRequest) {
			return getReorientRelationshipCommand((ReorientRelationshipRequest) req);
		} else if (req instanceof SetRequest) {
			return getSetCommand((SetRequest) req);
		}
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getConfigureCommand(ConfigureRequest req) {
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getCreateRelationshipCommand(CreateRelationshipRequest req) {
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getCreateCommand(CreateElementRequest req) {
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getSetCommand(SetRequest req) {
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getEditContextCommand(GetEditContextRequest req) {
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getDestroyElementCommand(DestroyElementRequest req) {
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getDestroyReferenceCommand(DestroyReferenceRequest req) {
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getDuplicateCommand(DuplicateElementsRequest req) {
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getMoveCommand(MoveRequest req) {
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getReorientReferenceRelationshipCommand(ReorientReferenceRelationshipRequest req) {
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getReorientRelationshipCommand(ReorientRelationshipRequest req) {
		return null;
	}

	/**
	 * @generated
	 */
	protected Command getMSLWrapper(ICommand cmd) {
		CompositeModelCommand modelCmd = new CompositeModelCommand(cmd.getLabel());
		modelCmd.compose(cmd);
		return new EtoolsProxyCommand(modelCmd);
	}

	/**
	 * @generated
	 */
	protected EObject getSemanticElement() {
		return ViewUtil.resolveSemanticElement((View) getHost().getModel());
	}

	/**
	 * Finds container element for the new relationship of the specified type.
	 * Default implementation goes up by containment hierarchy starting from
	 * the specified element and returns the first element that is instance of
	 * the specified container class.
	 * 
	 * @generated
	 */
	protected EObject getRelationshipContainer(EObject element, EClass containerClass, IElementType relationshipType) {
		for (; element != null; element = element.eContainer()) {
			if (containerClass.isSuperTypeOf(element.eClass())) {
				return element;
			}
		}
		return null;
	}

<%
if(diagram.hasLinkCreationConstraints()) {
%>
	/**
	 * @generated 
	 */
	protected static class <%=diagram.getLinkCreationConstraintsClassName()%> {
<%
	for (Iterator it = diagram.getLinks().iterator(); it.hasNext();) {
		GenLink nextLink = (GenLink) it.next();
		GenLinkConstraints linkConstraints = nextLink.getCreationConstraints();
		if(linkConstraints != null) {						
			String srcConstraint = linkConstraints.getSourceEnd() != null ? linkConstraints.getSourceEnd().getBody() : null;
			String targetConstraint = linkConstraints.getTargetEnd() != null ? linkConstraints.getTargetEnd().getBody(): null;
			GenClass srcContext = linkConstraints.getSourceEndContextClass();
			GenClass targetContext = linkConstraints.getTargetEndContextClass();
%>
		/**
		 * @generated 
		 */
		public static final <%=diagram.getLinkCreationConstraintsClassName()%> <%=linkConstraints.getConstraintsInstanceFieldName()%> = new <%=diagram.getLinkCreationConstraintsClassName()%>(
			new LinkEndConstraint(
				<%=srcConstraint != null ? "\"" + srcConstraint + "\"" : null%>, //$NON-NLS-1$
				<%=(srcContext!=null) ? getMetaClassAccessor(srcContext, importManager) : null%>),
			new LinkEndConstraint(
				<%=targetConstraint != null ? "\"" + targetConstraint + "\"" : null%>, //$NON-NLS-1$
				<%=(targetContext!=null) ? getMetaClassAccessor(targetContext, importManager) : null%>));
<%
		} // end of LinkConstraints if
	} // end of link iteration
%>
		/**
		 * @generated 
		 */	
		private static final String OPPOSITE_END_VAR = "oppositeEnd"; //$NON-NLS-1$
		/**
		 * @generated 
		 */	
		private <%=importManager.getImportedName("org.eclipse.emf.ocl.expressions.VariableDeclaration")%> varOppositeEndToSource;
		/**
		 * @generated 
		 */	
		private <%=importManager.getImportedName("org.eclipse.emf.ocl.expressions.VariableDeclaration")%> varOppositeEndToTarget;
		/**
		 * @generated 
		 */	
		private <%=importManager.getImportedName("org.eclipse.emf.ocl.query.Query")%> srcEndInv;
		/**
		 * @generated 
		 */	
		private <%=importManager.getImportedName("org.eclipse.emf.ocl.query.Query")%> targetEndInv;
		/**
		 * @generated 
		 */		
		public <%=diagram.getLinkCreationConstraintsClassName()%>(LinkEndConstraint sourceEnd, LinkEndConstraint targetEnd) {			
			if(sourceEnd != null && sourceEnd.context != null && sourceEnd.body != null) {				
				if(targetEnd != null && targetEnd.context != null) {
					this.varOppositeEndToTarget = createVar(OPPOSITE_END_VAR, targetEnd.context);			
				}
				this.srcEndInv = createQuery(sourceEnd, varOppositeEndToTarget);
			}
			
			if(targetEnd != null && targetEnd.context != null && targetEnd.body != null) {
				if(sourceEnd != null && sourceEnd.context != null) {
					this.varOppositeEndToSource = createVar(OPPOSITE_END_VAR, sourceEnd.context);				
				}
				this.targetEndInv = createQuery(targetEnd, varOppositeEndToSource);			
			}
		}
		
		/**
		 * @generated 
		 */	
		public boolean canCreateLink(<%=importManager.getImportedName("org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest")%> req, boolean isBackDirected) {
			Object source = req.getSource();
			Object target = req.getTarget();

			Query sourceConstraint = isBackDirected ? targetEndInv : srcEndInv;
			Query targetConstraint = null;
			if(req.getTarget() != null) {
				targetConstraint = isBackDirected ? srcEndInv : targetEndInv;
			}
			boolean isSourceAccepted = sourceConstraint != null ? evaluate(sourceConstraint, source, target, false) : true;
			if(isSourceAccepted && targetConstraint != null) {
				return evaluate(targetConstraint, target, source, true);
			}
			return isSourceAccepted;
		}
		
		/**
		 * @generated 
		 */	
		private Query createQuery(LinkEndConstraint constraint, <%=importManager.getImportedName("org.eclipse.emf.ocl.expressions.VariableDeclaration")%> oppositeEndDecl) {
			final <%=importManager.getImportedName("org.eclipse.emf.ocl.expressions.VariableDeclaration")%> oppositeEndDeclLocal = oppositeEndDecl;
			try {			
				<%=importManager.getImportedName("org.eclipse.emf.ocl.helper.IOclHelper")%> oclHelper = 
					<%=importManager.getImportedName("org.eclipse.emf.ocl.helper.HelperUtil")%>.createOclHelper(new <%=importManager.getImportedName("org.eclipse.emf.ocl.parser.EcoreEnvironmentFactory")%>() {
					public <%=importManager.getImportedName("org.eclipse.emf.ocl.parser.Environment")%> createClassifierContext(Object context) {
						<%=importManager.getImportedName("org.eclipse.emf.ocl.parser.Environment")%> env = super.createClassifierContext(context);
						if(oppositeEndDeclLocal != null) {
							env.addElement(oppositeEndDeclLocal.getName(), oppositeEndDeclLocal, true);
						}
						return env;
					}
				});
				oclHelper.setContext(constraint.context);
				return <%=importManager.getImportedName("org.eclipse.emf.ocl.query.QueryFactory")%>.eINSTANCE.createQuery(oclHelper.createInvariant(constraint.body));
			} catch (Exception e) {
				<%=pluginActivatorClass%>.getInstance().logError(null, e);
				return null;
			}
		}
	
		/**
		 * @generated 
		 */		
		private static boolean evaluate(<%=importManager.getImportedName("org.eclipse.emf.ocl.query.Query")%> query, Object sourceEnd, Object oppositeEnd, boolean clearEnv) {
			<%=importManager.getImportedName("org.eclipse.emf.ocl.parser.EvaluationEnvironment")%> evalEnv = query.getEvaluationEnvironment();
			evalEnv.replace(OPPOSITE_END_VAR, oppositeEnd);
			try {
				Object val = query.evaluate(sourceEnd);			
				return (val instanceof Boolean) ? ((Boolean)val).booleanValue() : false;
			} catch(Exception e) {	
				<%=pluginActivatorClass%>.getInstance().logError(null, e);	
				if(evalEnv != null) evalEnv.clear();			
				return false;
			} finally {
				if(clearEnv) evalEnv.clear();
			}
		}
	
		/**
		 * @generated 
		 */		
		private static <%=importManager.getImportedName("org.eclipse.emf.ocl.expressions.VariableDeclaration")%> createVar(String name, <%=importManager.getImportedName("org.eclipse.emf.ecore.EClassifier")%> type) {
			<%=importManager.getImportedName("org.eclipse.emf.ocl.expressions.VariableDeclaration")%> var = <%=importManager.getImportedName("org.eclipse.emf.ocl.expressions.ExpressionsFactory")%>.eINSTANCE.createVariableDeclaration();
			var.setName(name);
			var.setType(<%=importManager.getImportedName("org.eclipse.emf.ocl.parser.EcoreEnvironment")%>.getOclType(type));		
			return var;
		}	
	}
	/**
	 * @generated 
	 */	
	static class LinkEndConstraint {
		final <%=importManager.getImportedName("org.eclipse.emf.ecore.EClassifier")%> context;
		final String body;
		LinkEndConstraint(String body, <%=importManager.getImportedName("org.eclipse.emf.ecore.EClassifier")%> context) {
			this.context = context;
			this.body = body;
		}
	}
<%} //end of LinkConstraints %>	
}
<%importManager.emitSortedImports();%>

Back to the top