Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2ef4afb833f9269f9a7dd85de3703f3d30da4b46 (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
/*
 * 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'»
«IMPORT 'http://www.eclipse.org/emf/2002/Ecore'»
«IMPORT 'http://www.eclipse.org/emf/2002/GenModel'»

«EXTENSION xpt::GenModelUtils»
«EXTENSION xpt::diagram::Utils»

«REM»
	Check whether link may be created.
«ENDREM»
«DEFINE canCreate(link : gmfgen::GenLink) FOR gmfgen::LinkModelFacet-»

	«EXPAND xpt::Common::generatedMemberComment»
	public boolean canExecute() {
		if (source == null && target == null) {
			return false;
		}
		if (source != null && «EXPAND MetaModel::NotInstance('source') FOR getSourceType()») {
			return false;
		}
		if (target != null && «EXPAND MetaModel::NotInstance('target') FOR getTargetType()») {
			return false;
		}
		if (getSource() == null) {
			return true; // link creation is in progress; source is not defined yet
		}
		// target may be null here but it's possible to check constraint
		«EXPAND canCreateElement(link)-»
	}
«ENDDEFINE»

«DEFINE canCreateElement(link : gmfgen::GenLink) FOR gmfgen::LinkModelFacet-»
«ENDDEFINE»

«DEFINE canCreateElement(link : gmfgen::GenLink) FOR gmfgen::TypeLinkModelFacet-»
«IF hasContainerOtherThanSource(self)-»
	if (getContainer() == null) {
		return false;
	}
«ENDIF-»
	return «EXPAND validatorInvocation FOR link»(«IF hasContainerOtherThanSource(self)»getContainer(), «ENDIF»getSource(), getTarget());
«ENDDEFINE»

«DEFINE canCreateElement(link : gmfgen::GenLink) FOR gmfgen::FeatureLinkModelFacet-»
	return «EXPAND validatorInvocation FOR link»(getSource(), getTarget());
«ENDDEFINE»

«DEFINE validatorInvocation FOR gmfgen::GenLink-»
«diagram.getBaseItemSemanticEditPolicyQualifiedClassName()».«diagram.getLinkCreationConstraintsClassName()».canCreate«getUniqueIdentifier()-»
«ENDDEFINE»

«REM»
	Fields of command that creates link.
«ENDREM»
«DEFINE fields FOR gmfgen::LinkModelFacet»
	«EXPAND xpt::Common::generatedMemberComment»
	private final org.eclipse.emf.ecore.EObject source;

	«EXPAND xpt::Common::generatedMemberComment»
	private final org.eclipse.emf.ecore.EObject target;
«ENDDEFINE»

«DEFINE fields FOR gmfgen::TypeLinkModelFacet»
	«EXPAND xpt::Common::generatedMemberComment»
	private final org.eclipse.emf.ecore.EObject source;

	«EXPAND xpt::Common::generatedMemberComment»
	private final org.eclipse.emf.ecore.EObject target;
«IF hasContainerOtherThanSource(self)»

	«EXPAND xpt::Common::generatedMemberComment»
	private final «EXPAND MetaModel::QualifiedClassName FOR containmentMetaFeature.genClass» container;
«ENDIF-»
«ENDDEFINE»

«REM»
	Field accessors of command that creates link.
«ENDREM»
«DEFINE accessors FOR gmfgen::LinkModelFacet-»

	«EXPAND xpt::Common::generatedMemberComment»
	protected «EXPAND MetaModel::QualifiedClassName FOR getSourceType()» getSource() {
		return «EXPAND MetaModel::CastEObject('source') FOR getSourceType()»;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	protected «EXPAND MetaModel::QualifiedClassName FOR getTargetType()» getTarget() {
		return «EXPAND MetaModel::CastEObject('target') FOR getTargetType()»;
	}
	«EXPAND containerAccessor-»
«ENDDEFINE»

«DEFINE containerAccessor FOR gmfgen::LinkModelFacet»«ENDDEFINE»

«DEFINE containerAccessor FOR gmfgen::TypeLinkModelFacet-»
«IF hasContainerOtherThanSource(self)-»

	«EXPAND xpt::Common::generatedMemberComment»
	public «EXPAND MetaModel::QualifiedClassName FOR containmentMetaFeature.genClass» getContainer() {
		return container;
	}

	«EXPAND xpt::Common::generatedMemberComment(
		'Default approach is to traverse ancestors of the source to find instance of container.\n' + 
		'Modify with appropriate logic.'
	)»
	private static «EXPAND MetaModel::QualifiedClassName FOR containmentMetaFeature.genClass» deduceContainer(org.eclipse.emf.ecore.EObject source, org.eclipse.emf.ecore.EObject target) {
		// Find container element for the new link.
		// Climb up by containment hierarchy starting from the source
		// and return the first element that is instance of the container class.
		for (org.eclipse.emf.ecore.EObject element = source; element != null; element = element.eContainer()) {
			if («EXPAND MetaModel::IsInstance('element') FOR containmentMetaFeature.genClass») {
				return «EXPAND MetaModel::CastEObject('element') FOR containmentMetaFeature.genClass»;
			}
		}
		return null;
	}
«ENDIF-»

«ENDDEFINE»

«REM»
	Part of the constructor that performs initialization.
«ENDREM»
«DEFINE _init FOR gmfgen::LinkModelFacet-»
	this.source = source;
	this.target = target;
«ENDDEFINE»

«DEFINE _init FOR gmfgen::TypeLinkModelFacet-»
	this.source = source;
	this.target = target;
«IF hasContainerOtherThanSource(self)-»
	container = deduceContainer(source, target);
«ENDIF-»
«ENDDEFINE»

Back to the top