Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da78ce6a0f01d1ab70a2b92c9b94200aa9816fe7 (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
 * generated by Xtext
 */
package org.eclipse.papyrus.uml.textedit.common.xtext.ui.contentassist;

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

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.uml.textedit.common.xtext.ui.internal.UmlCommonActivator;
import org.eclipse.papyrus.uml.textedit.common.xtext.umlCommon.MultiplicityRule;
import org.eclipse.papyrus.uml.textedit.common.xtext.umlCommon.QualifiedName;
import org.eclipse.papyrus.uml.textedit.common.xtext.umlCommon.TypeRule;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
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.Type;
import org.eclipse.xtext.Assignment;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext;
import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor;

/**
 * see http://www.eclipse.org/Xtext/documentation/latest/xtext.html#contentAssist on how to customize content assistant
 */
public class UmlCommonProposalProvider extends AbstractUmlCommonProposalProvider {


	/** the label provider */
	protected ILabelProvider labelProvider;

	/** the edited model */
	private Namespace model = null;

	/** the edited element */
	private Element contextElement = null;

	/** the wanted type */
	private Class<?> wantedType = Type.class;

	/**
	 * 
	 * Constructor.
	 * 
	 * 
	 */
	public UmlCommonProposalProvider() {
		initModel();
	}

	/**
	 * Getter for {@link #model}
	 * 
	 * @return
	 *         {@link #model}
	 */
	protected Namespace getModel() {
		return this.model;
	}

	/**
	 * Getter for {@link #contextElement}
	 * 
	 * @return
	 *         {@link #contextElement}
	 */
	protected Element getContextElement() {
		return this.contextElement;
	}

	/**
	 * 
	 * This method initializes the fields {@link #model} {@link #contextElement} {@link #labelProvider} thanks to the current selection
	 * 
	 */
	protected void initModel() {
		IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		ISelection mySelection = activePage.getSelection();
		if(mySelection instanceof IStructuredSelection) {
			Object first = ((IStructuredSelection)mySelection).getFirstElement();
			if(first != null) {
				if(first instanceof IAdaptable) {
					Element el = (Element)((IAdaptable)first).getAdapter(Element.class);
					this.contextElement = el;
					if(el != null) {
						List<Namespace> namespaces = el.getNearestPackage().allNamespaces();
						if(namespaces.size() == 0) {
							this.model = el.getNearestPackage();
						} else {
							this.model = namespaces.get(namespaces.size() - 1);
						}
					}
				}
			}
		}

		try {
			labelProvider = ServiceUtilsForEObject.getInstance().getService(LabelProviderService.class, model).getLabelProvider();
		} catch (ServiceException ex) {
			Logger.getLogger(UmlCommonActivator.class).error(ex);
			labelProvider = new LabelProvider();
		}

		Assert.isNotNull(contextElement, "I can't find the edited element"); //$NON-NLS-1$
		Assert.isNotNull(this.model, "I can't find the model owning the edited element"); //$NON-NLS-1$
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.parameter.editor.xtext.ui.contentassist.AbstractUmlParameterProposalProvider#complete_MultiplicityRule(org.eclipse.emf.ecore.EObject,
	 *      org.eclipse.xtext.RuleCall, org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext,
	 *      org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)
	 * 
	 * @param model
	 * @param ruleCall
	 * @param context
	 * @param acceptor
	 */
	@Override
	public void complete_MultiplicityRule(EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {

		String one = "[1]"; //$NON-NLS-1$
		String one_star = "[1..*]"; //$NON-NLS-1$
		String star = "[*]"; //$NON-NLS-1$

		String completionString = ""; //$NON-NLS-1$
		String displayString = ""; //$NON-NLS-1$
		//String displayString = c.getName() ;
		ICompletionProposal completionProposal = null;

		completionString = "" + one.substring(context.getPrefix().length()); //$NON-NLS-1$
		displayString = "" + one; //$NON-NLS-1$
		completionProposal = createCompletionProposal(completionString, displayString, context);
		acceptor.accept(completionProposal);

		completionString = "" + one_star.substring(context.getPrefix().length()); //$NON-NLS-1$
		displayString = "" + one_star + "     "; //$NON-NLS-1$ //$NON-NLS-2$
		completionProposal = createCompletionProposal(completionString, displayString, context);
		acceptor.accept(completionProposal);

		completionString = "" + star.substring(context.getPrefix().length()); //$NON-NLS-1$
		displayString = "" + star; //$NON-NLS-1$
		completionProposal = createCompletionProposal(completionString, displayString, context);
		acceptor.accept(completionProposal);
	}


	/**
	 * 
	 * @see org.eclipse.papyrus.uml.textedit.common.xtext.ui.contentassist.AbstractUmlCommonProposalProvider#completeMultiplicityRule_Bounds(org.eclipse.emf.ecore.EObject,
	 *      org.eclipse.xtext.Assignment, org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext,
	 *      org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)
	 * 
	 * @param model
	 * @param assignment
	 * @param context
	 * @param acceptor
	 */
	@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("*")) { //$NON-NLS-1$ //$NON-NLS-2$
					String completionString = "*"; //$NON-NLS-1$
					String displayString = "*"; //$NON-NLS-1$
					ICompletionProposal completionProposal = createCompletionProposal(completionString, displayString, context);
					acceptor.accept(completionProposal);
				}
			}
		}
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.collaborationuse.editor.xtext.ui.contentassist.AbstractUmlCollaborationUseProposalProvider#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)
	 * 
	 * @param model
	 * @param assignment
	 * @param context
	 * @param acceptor
	 */
	@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.collaborationuse.editor.xtext.ui.contentassist.AbstractUmlCollaborationUseProposalProvider#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)
	 * 
	 * @param model
	 * @param assignment
	 * @param context
	 * @param acceptor
	 */
	@Override
	public void completeQualifiedName_Remaining(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {


		QualifiedName path = (QualifiedName)model;
		for(NamedElement n : path.getPath().getOwnedMembers()) {
			//we add the namespace only if he contains interesting element
			if(n instanceof Namespace) {
				if(n.getName().startsWith(context.getPrefix())) {
					String completionString = n.getName().substring(context.getPrefix().length()) + "::"; //$NON-NLS-1$
					String displayString = n.getName() + "::"; //$NON-NLS-1$
					ICompletionProposal completionProposal = createCompletionProposal(n, completionString, displayString, context);
					List<Type> accessibleType = getRecursivelyOwnedType((Namespace)n);
					accessibleType.addAll(getRecursivelyImportedType((Namespace)n));
					if(accessibleType.size() != 0) {
						acceptor.accept(completionProposal);
					}
				}

				//we add the namespace to the possible element, if the namespace has the correct type
				if(n.getName().startsWith(context.getPrefix())) {
					String completionString = n.getName().substring(context.getPrefix().length());
					String displayString = n.getName();
					ICompletionProposal completionProposal = createCompletionProposal(n, completionString, displayString, context);
					if(isWantedType(n)) {
						acceptor.accept(completionProposal);
					}
				}
			}
		}
		for(Package p : path.getPath().getImportedPackages()) {
			if(p.getName().startsWith(context.getPrefix())) {
				String completionString = p.getName().substring(context.getPrefix().length()) + "::"; //$NON-NLS-1$
				String displayString = p.getName() + "::"; //$NON-NLS-1$
				ICompletionProposal completionProposal = createCompletionProposal(p, completionString, displayString, context);
				List<Type> accessibleType = getRecursivelyOwnedType(p);
				accessibleType.addAll(getRecursivelyImportedType(p));
				if(accessibleType.size() != 0) {
					acceptor.accept(completionProposal);
				}
			}
		}
	}

	/**
	 * 
	 * Provides custom completion for the root element in a qualified name
	 * 
	 * @see org.eclipse.papyrus.uml.textedit.common.xtext.ui.contentassist.AbstractUmlCommonProposalProvider#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)
	 * 
	 * @param model
	 * @param assignment
	 * @param context
	 * @param acceptor
	 */
	@Override
	public void completeTypeRule_Path(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		Namespace root = getModel();
		if(root == null) {
			return;
		}

		// first accept the root Model
		String completionString = root.getName() + "::"; //$NON-NLS-1$
		String displayString = root.getName() + "::"; //$NON-NLS-1$
		//String displayString = c.getName() ;
		ICompletionProposal completionProposal = 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().startsWith(context.getPrefix())) {
				completionString = p.getName().substring(context.getPrefix().length()) + "::"; //$NON-NLS-1$
				displayString = p.getName() + "::"; //$NON-NLS-1$
				completionProposal = createCompletionProposal(root, completionString, displayString, context);
				List<Type> accessibleType = getRecursivelyOwnedType(p);
				accessibleType.addAll(getRecursivelyImportedType(p));
				if(accessibleType.size() != 0) {
					acceptor.accept(completionProposal);
				}
			}
		}
	}

	/**
	 * Provides custom completion for specifying the type of an {@link Element}, taking into account the path if the name is qualified
	 * 
	 * @see org.eclipse.papyrus.property.editor.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 = getContextElement().getNearestPackage();
		if(model instanceof TypeRule) {
			TypeRule typeRule = (TypeRule)model;
			QualifiedName path = typeRule.getPath();
			while(path.getRemaining() != null) {
				path = path.getRemaining();
			}
			namespace = path.getPath();
		}
		for(NamedElement n : namespace.getOwnedMembers()) {
			if(isWantedType(n)) {
				if(n.getName().startsWith(context.getPrefix())) {
					String completionString = n.getName().substring(context.getPrefix().length());
					String displayString = n.getName();
					ICompletionProposal completionProposal = createCompletionProposal(n, completionString, displayString, context);
					acceptor.accept(completionProposal);
				}
			}
			if(n instanceof Namespace) {
				for(Type t : getRecursivelyOwnedType((Namespace)n)) {
					if(t.getName().startsWith(context.getPrefix())) {
						String completionString = t.getName().substring(context.getPrefix().length());
						String displayString = t.getName();;
						ICompletionProposal completionProposal = createCompletionProposal(t, completionString, displayString, context);
						acceptor.accept(completionProposal);
					}
				}
			}
		}

	}

	/**
	 * Private Utility method for creating a completion proposal
	 * 
	 * @param namedElement
	 *        The named element for which completion proposal must be created
	 * @param completionString
	 *        The actual completion string
	 * @param displayString
	 *        The way the completion is displayed in the completion list
	 * @param context
	 *        Some information related to the context of the completion
	 * @return
	 */
	protected ICompletionProposal createCompletionProposal(NamedElement namedElement, String completionString, String displayString, ContentAssistContext context) {
		String additionalProposalInfo = "" + namedElement.getQualifiedName() + "\n" + '(' + namedElement.eClass().getName() + ')'; //$NON-NLS-1$ //$NON-NLS-2$

		ICompletionProposal completionProposal = new CompletionProposal(completionString, // String to be inserted
		context.getOffset(), // Offset
		context.getSelectedText().length(), // Replacement length
		completionString.length(), // cursorPosition
		labelProvider.getImage(namedElement), // image
		" " + displayString, // displayString //$NON-NLS-1$
		null, // contextInformation
		additionalProposalInfo // additionalProposalInfo
		);
		return completionProposal;
	}

	/**
	 * Private Utility method for creating a completion proposal
	 * 
	 * @param completionString
	 *        The actual completion string
	 * @param displayString
	 *        The way the completion is displayed in the completion list
	 * @param context
	 *        Some information related to the context of the completion
	 * @return
	 */
	protected ICompletionProposal createCompletionProposal(String completionString, String displayString, ContentAssistContext context) {

		ICompletionProposal completionProposal = new CompletionProposal(completionString, // String to be inserted
		context.getOffset(), // Offset
		context.getSelectedText().length(), // Replacement length
		completionString.length(), // cursorPosition
		null, // image
		" " + displayString, // displayString //$NON-NLS-1$
		null, // contextInformation
		null // additionalProposalInfo
		);
		return completionProposal;
	}

	/**
	 * 
	 * Private Utility method for creating a completion proposal with replacement of prefix
	 * 
	 * @param namedElement
	 *        The named element for which completion proposal must be created
	 * @param completionString
	 *        The actual completion string
	 * @param displayString
	 *        The way the completion is displayed in the completion list
	 * @param context
	 *        Some information related to the context of the completion
	 * @return
	 */
	protected ICompletionProposal createCompletionProposalWithReplacementOfPrefix(NamedElement namedElement, String completionString, String displayString, ContentAssistContext context) {
		String additionalProposalInfo = "" + namedElement.getQualifiedName() + "\n" + '(' + namedElement.eClass().getName() + ')'; //$NON-NLS-1$ //$NON-NLS-2$

		ICompletionProposal completionProposal = new CompletionProposal(completionString, // String to be inserted
		context.getOffset() - context.getPrefix().length(), // Offset
		context.getPrefix().length(), // Replacement length
		completionString.length(), // cursorPosition
		labelProvider.getImage(namedElement), // image
		" " + displayString, // displayString //$NON-NLS-1$
		null, // contextInformation
		additionalProposalInfo // additionalProposalInfo
		);
		return completionProposal;
	}

	/**
	 * 
	 * This method shall be overridden in order to look for a more specific {@link Type} Utility methods which returns the list of {@link Type} 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
	 */
	protected List<Type> getRecursivelyImportedType(Namespace context) {
		List<Type> recursivelyImportedTypes = new ArrayList<Type>();

		EList<org.eclipse.uml2.uml.Package> importedPackages = context.getImportedPackages();
		for(Package p : importedPackages) {
			recursivelyImportedTypes.addAll(getRecursivelyOwnedType(p));
		}

		return recursivelyImportedTypes;
	}

	/**
	 * This method shall be overridden in order to look for a more specific {@link Type}
	 * 
	 * Utility methods which returns the list of the type 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
	 */
	protected List<Type> getRecursivelyOwnedType(Namespace context) {
		List<Type> recursivelyOwnedTypes = new ArrayList<Type>();

		List<Element> allOwnedElements = context.getOwnedElements();
		for(Element e : allOwnedElements) {
			if(isWantedType(e)) {
				recursivelyOwnedTypes.add((Type)e);
			}
			if(e instanceof Namespace) {
				recursivelyOwnedTypes.addAll(this.getRecursivelyOwnedType((Namespace)e));
			}
		}

		return recursivelyOwnedTypes;
	}

	/**
	 * Inherited class should overridden this method in order to have a mode specific type
	 * 
	 * Tests if the element is an instance of the wanted type
	 * 
	 * @param e
	 *        the element to test
	 * @return
	 *         <code>true</code> is the element is an instance of the wanted type
	 */
	protected boolean isWantedType(Element e) {
		return this.wantedType.isInstance(e);
	}

	/**
	 * This method allows to precise the wanted type of the element (to be more restrictive that {@link Port}
	 * 
	 * Setter for {@link #wantedType}
	 * 
	 * @param type
	 *        the new type
	 */
	public void setWantedType(Class<?> type) {
		this.wantedType = type;
	}

}

Back to the top