Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a8b0fc909e881b0e53dc74d210651d07a0180028 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *
 * 
 * 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:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.textedit.property.xtext.ui.contentassist;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.papyrus.uml.textedit.property.xtext.scoping.UmlPropertyScopeProvider;
import org.eclipse.papyrus.uml.textedit.property.xtext.ui.contributions.UMLPropertyEditorPropertyUtil;
import org.eclipse.papyrus.uml.textedit.property.xtext.umlProperty.ModifierSpecification;
import org.eclipse.papyrus.uml.textedit.property.xtext.umlProperty.ModifiersRule;
import org.eclipse.papyrus.uml.textedit.property.xtext.umlProperty.MultiplicityRule;
import org.eclipse.papyrus.uml.textedit.property.xtext.umlProperty.PropertyRule;
import org.eclipse.papyrus.uml.textedit.property.xtext.umlProperty.QualifiedName;
import org.eclipse.papyrus.uml.textedit.property.xtext.umlProperty.TypeRule;
import org.eclipse.papyrus.uml.xtext.integration.CompletionProposalUtils;
import org.eclipse.papyrus.uml.xtext.integration.CustomCompletionProposal;
import org.eclipse.papyrus.uml.xtext.integration.core.ContextElementUtil;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Property;
import org.eclipse.xtext.Assignment;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext;
import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor;

/**
 * Customization of the default ProposalProvider of the textual property editor
 * 
 * see
 * http://www.eclipse.org/Xtext/documentation/latest/xtext.html#contentAssist on
 * how to customize content assistant
 */

public class UmlPropertyProposalProvider extends AbstractUmlPropertyProposalProvider {

	/**
	 * Provides custom completion for the specifying the type of a property
	 * 
	 * @see org.eclipse.papyrus.uml.textedit.property.xtext.ui.contentassist.AbstractUmlPropertyProposalProvider#completePropertyRule_Type(org.eclipse.emf.ecore.EObject,
	 *      org.eclipse.xtext.Assignment, org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext,
	 *      org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)
	 */
	@Override
	public void completePropertyRule_Type(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		List<Classifier> allClassifiers = new ArrayList<Classifier>();
		Namespace namespace = (Namespace)EcoreUtil.getRootContainer(ContextElementUtil.getContextElement(model.eResource()));
		allClassifiers.addAll(getRecursivelyOwnedClassifiers(namespace));
		allClassifiers.addAll(getRecursivelyImportedClassifiers(namespace));
		for(Classifier c : allClassifiers) {
			if(c.getQualifiedName().toLowerCase().contains(context.getPrefix().toLowerCase())) {
				String displayString = c.getQualifiedName();
				String completionString = CompletionProposalUtils.getQualifiedNameLabelWithSufficientDepth(c, namespace);
				ICompletionProposal completionProposal = CompletionProposalUtils.createCompletionProposalWithReplacementOfPrefix(c, completionString, displayString, context);
				acceptor.accept(completionProposal);
			}
		}
	}

	/**
	 * Provides custom completion for the root element in a qualified name
	 * 
	 * @see org.eclipse.papyrus.uml.textedit.property.xtext.ui.contentassist.AbstractUmlPropertyProposalProvider#completeTypeRule_Path(org.eclipse.emf.ecore.EObject,
	 *      org.eclipse.xtext.Assignment, org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext,
	 *      org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)
	 */
	@Override
	public void completeTypeRule_Path(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		Namespace root = (Namespace)EcoreUtil.getRootContainer(ContextElementUtil.getContextElement(model.eResource()));
		if(root == null) {
			return;
		}
		// first accept the root Model
		String completionString = root.getName() + "::";
		String displayString = root.getName() + "::";
		// String displayString = c.getName() ;
		CustomCompletionProposal completionProposal = CompletionProposalUtils.createCompletionProposalWithReplacementOfPrefix(root, completionString, displayString, context);
		acceptor.accept(completionProposal);

		// then accepts all packages imported by Model
		List<Package> importedPackages = root.getImportedPackages();
		for(Package p : importedPackages) {
			if(p.getName().toLowerCase().contains(context.getPrefix().toLowerCase())) {
				completionString = p.getName() + "::";
				displayString = p.getName() + "::";
				// String displayString = c.getName() ;
				completionProposal = CompletionProposalUtils.createCompletionProposalWithReplacementOfPrefix(root, completionString, displayString, context);
				acceptor.accept(completionProposal);
			}
		}

	}

	/**
	 * Provides custom completion for specifying the type of a property, taking
	 * into account the path if the name is qualified
	 * 
	 * @see org.eclipse.papyrus.uml.textedit.property.xtext.ui.contentassist.AbstractUmlPropertyProposalProvider#completeTypeRule_Type(org.eclipse.emf.ecore.EObject,
	 *      org.eclipse.xtext.Assignment, org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext,
	 *      org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)
	 */
	@Override
	public void completeTypeRule_Type(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		Namespace namespace = ((Property)ContextElementUtil.getContextElement(model.eResource())).getNamespace();
		if(model instanceof TypeRule) {
			TypeRule typeRule = (TypeRule)model;
			QualifiedName path = typeRule.getPath();
			while(path.getRemaining() != null) {
				path = path.getRemaining();
			}
			namespace = path.getPath();
		} else if(!(model instanceof PropertyRule)) {
			return;
		}
		for(NamedElement n : namespace.getOwnedMembers()) {
			if(n instanceof Classifier) {
				if(n.getName().toLowerCase().contains(context.getPrefix().toLowerCase())) {
					String completionString = n.getName();
					String displayString = n.getName();
					CustomCompletionProposal completionProposal = CompletionProposalUtils.createCompletionProposalWithReplacementOfPrefix(n, completionString, displayString, context);
					acceptor.accept(completionProposal);
				}
			}
		}

	}

	/**
	 * Provides custom completion for a path in a qualified name
	 * 
	 * @see org.eclipse.papyrus.uml.textedit.property.xtext.ui.contentassist.AbstractUmlPropertyProposalProvider#completeQualifiedName_Path(org.eclipse.emf.ecore.EObject,
	 *      org.eclipse.xtext.Assignment, org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext,
	 *      org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)
	 */
	@Override
	public void completeQualifiedName_Path(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		// The customization consists in proposing nothing. Proposals are
		// already handled by other methods
	}

	/**
	 * Provides custom completion for a path, taking into account the path which
	 * has already been specified
	 * 
	 * @see org.eclipse.papyrus.uml.textedit.property.xtext.ui.contentassist.AbstractUmlPropertyProposalProvider#completeQualifiedName_Remaining(org.eclipse.emf.ecore.EObject,
	 *      org.eclipse.xtext.Assignment, org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext,
	 *      org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)
	 */
	@Override
	public void completeQualifiedName_Remaining(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		QualifiedName path = (QualifiedName)model;
		for(NamedElement n : path.getPath().getOwnedMembers()) {
			if(n instanceof Package) {
				if(n.getName().toLowerCase().contains(context.getPrefix().toLowerCase())) {
					String completionString = n.getName() + "::";
					String displayString = n.getName() + "::";
					CustomCompletionProposal completionProposal = CompletionProposalUtils.createCompletionProposalWithReplacementOfPrefix(n, completionString, displayString, context);
					acceptor.accept(completionProposal);
				}
			}
		}
		for(Package p : path.getPath().getImportedPackages()) {
			if(p.getName().toLowerCase().contains(context.getPrefix().toLowerCase())) {
				String completionString = p.getName() + "::";
				String displayString = p.getName() + "::";
				CustomCompletionProposal completionProposal = CompletionProposalUtils.createCompletionProposalWithReplacementOfPrefix(p, completionString, displayString, context);
				acceptor.accept(completionProposal);
			}
		}
	}

	@Override
	public void completeRedefinesRule_Property(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		for(Property inherited : UmlPropertyScopeProvider.retrieveInheritedProperties(model)) {
			if(inherited.getName().toLowerCase().contains(context.getPrefix().toLowerCase())) {
				String completionString = inherited.getName();
				String displayString = UMLPropertyEditorPropertyUtil.getLabel(inherited);
				CustomCompletionProposal completionProposal = CompletionProposalUtils.createCompletionProposalWithReplacementOfPrefix(inherited, completionString, displayString, context);
				acceptor.accept(completionProposal);
			}
		}
	}

	@Override
	public void completeSubsetsRule_Property(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		for(Property inherited : UmlPropertyScopeProvider.retrieveInheritedProperties(model)) {
			if(inherited.getName().toLowerCase().contains(context.getPrefix().toLowerCase())) {
				String completionString = inherited.getName();
				String displayString = UMLPropertyEditorPropertyUtil.getLabel(inherited);
				CustomCompletionProposal completionProposal = CompletionProposalUtils.createCompletionProposalWithReplacementOfPrefix(inherited, completionString, displayString, context);
				acceptor.accept(completionProposal);
			}
		}
	}

	/**
	 * Provides custom completion for keywords, in the context of "modifiers"
	 * specification
	 * 
	 * @see org.eclipse.xtext.ui.editor.contentassist.AbstractJavaBasedContentProposalProvider#completeKeyword(org.eclipse.xtext.Keyword,
	 *      org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext, org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)
	 */
	@Override
	public void completeKeyword(Keyword keyword, ContentAssistContext contentAssistContext, ICompletionProposalAcceptor acceptor) {
		EObject model = contentAssistContext.getCurrentModel();
		if(!(model instanceof ModifiersRule)) {
			super.completeKeyword(keyword, contentAssistContext, acceptor);
			return;
		}
		ModifiersRule modifiersRule = (ModifiersRule)model;
		boolean isOrdered = false;
		boolean isReadOnly = true;
		boolean isUnion = false;
		boolean isUnique = false;
		for(ModifierSpecification spec : modifiersRule.getValues()) {
			if(spec.getValue() != null) {
				switch(spec.getValue()) {
				case ORDERED:
					isOrdered = true;
					break;
				case READ_ONLY:
					isReadOnly = !isReadOnly;
					break;
				case UNION:
					isUnion = true;
					break;
				case UNIQUE:
					isUnique = true;
					break;
				default:
					break;
				}
			}
		}
		String value = keyword.getValue();
		if(value.equals("ordered")) {
			if(!isOrdered) {
				super.completeKeyword(keyword, contentAssistContext, acceptor);
			}
		} else if(value.equals("readOnly")) {
			if(!isReadOnly) {
				super.completeKeyword(keyword, contentAssistContext, acceptor);
			}
		} else if(value.equals("unique")) {
			if(!isUnique) {
				super.completeKeyword(keyword, contentAssistContext, acceptor);
			}
		} else if(value.equals("union")) {
			if(!isUnion) {
				super.completeKeyword(keyword, contentAssistContext, acceptor);
			}
		} else {
			super.completeKeyword(keyword, contentAssistContext, acceptor);
		}
	}

	@Override
	public void complete_MultiplicityRule(EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		String zero_one = "[0..1]";
		String one = "[1]";
		String one_star = "[1..*]";
		String star = "[*]";

		String completionString = "";
		String displayString = "";
		ICompletionProposal completionProposal = null;

		completionString = "" + zero_one.substring(context.getPrefix().length());
		displayString = "" + zero_one;
		completionProposal = CompletionProposalUtils.createCompletionProposal(completionString, displayString, context);
		acceptor.accept(completionProposal);

		completionString = "" + one.substring(context.getPrefix().length());
		displayString = "" + one;
		completionProposal = CompletionProposalUtils.createCompletionProposal(completionString, displayString, context);
		acceptor.accept(completionProposal);

		completionString = "" + one_star.substring(context.getPrefix().length());
		displayString = "" + one_star + "     ";
		completionProposal = CompletionProposalUtils.createCompletionProposal(completionString, displayString, context);
		acceptor.accept(completionProposal);

		completionString = "" + star.substring(context.getPrefix().length());
		displayString = "" + star;
		completionProposal = CompletionProposalUtils.createCompletionProposal(completionString, displayString, context);
		acceptor.accept(completionProposal);
	}

	@Override
	public void completeMultiplicityRule_Bounds(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {

		if(!(model instanceof MultiplicityRule)) {
			return;
		}

		MultiplicityRule multiplicityRule = (MultiplicityRule)model;

		if(multiplicityRule.getBounds().size() == 2) {
			String value = multiplicityRule.getBounds().get(1).getValue();
			try {
				Integer.valueOf(value);
			} catch (Exception e) {
				if(!multiplicityRule.getBounds().get(0).getValue().equals("*") && !multiplicityRule.getBounds().get(1).getValue().equals("*")) {
					String completionString = "*";
					String displayString = "*";
					ICompletionProposal completionProposal = CompletionProposalUtils.createCompletionProposal(completionString, displayString, context);
					acceptor.accept(completionProposal);
				}
			}
		}
	}

	/* ******************************************************************************
	 * Utility methods
	 * ***********************************************************
	 * *******************
	 */

	/**
	 * Utility methods wich returns the list of classifiers that are directly or
	 * indirectly owned by a context namespace
	 * 
	 * @param context
	 *        The context namespace
	 * @return the list of classifiers that are directly or indirectly owned by
	 *         the context namespace
	 */
	private List<Classifier> getRecursivelyOwnedClassifiers(Namespace context) {
		List<Classifier> recursivelyOwnedClassifiers = new ArrayList<Classifier>();

		List<Element> allOwnedElements = context.getOwnedElements();
		for(Element e : allOwnedElements) {
			if(e instanceof Classifier) {
				recursivelyOwnedClassifiers.add((Classifier)e);
			}
			if(e instanceof Namespace) {
				recursivelyOwnedClassifiers.addAll(getRecursivelyOwnedClassifiers((Namespace)e));
			}
		}

		return recursivelyOwnedClassifiers;
	}

	/**
	 * Utility methods which returns the list of classifiers that are directly
	 * or indirectly owned by the namespaces imported by a context namespace
	 * 
	 * @param context
	 *        The context namespace
	 * @return the list of classifiers that are directly or indirectly owned by
	 *         the namespaces imported by the context namespace
	 */
	private List<Classifier> getRecursivelyImportedClassifiers(Namespace context) {
		List<Classifier> recursivelyImportedClassifiers = new ArrayList<Classifier>();

		List<Package> importedPackages = context.getImportedPackages();
		for(Package p : importedPackages) {
			recursivelyImportedClassifiers.addAll(getRecursivelyOwnedClassifiers(p));
		}

		return recursivelyImportedClassifiers;
	}

}

Back to the top