Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0e46b3393f8f2d7f65fc58265c82ff9652201699 (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
/*****************************************************************************
 * 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.infra.gmfdiag.xtext.glue.contentassist;

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

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.papyrus.infra.core.utils.DisplayUtils;
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.xtext.ui.editor.contentassist.ContentAssistContext;

/**
 * @author CEA LIST - Initial contribution and API
 */
public class CompletionProposalUtils {

	protected final static ILabelProvider labelProvider = DisplayUtils.getLabelProvider() ;
	
	/**
	 * Public 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 completion proposal
	 */
	public static CustomCompletionProposal createCompletionProposal(NamedElement namedElement, 
														String completionString, 
														String displayString, 
														ContentAssistContext context) {
		String additionalProposalInfo = "" + namedElement.getQualifiedName() + "\n" + '(' + namedElement.eClass().getName() + ')' ;
		
		CustomCompletionProposal completionProposal = new CustomCompletionProposal(completionString, 	// String to be inserted 
				context.getOffset(),  							// Offset
				context.getSelectedText().length(),				// Replacement length
				completionString.length(),						// cursorPosition
				labelProvider.getImage(namedElement)	,	// image
				" " + displayString,									// displayString
				null							,				// contextInformation
				additionalProposalInfo,							// additionalProposalInfo
				context);
		return completionProposal ;
	}
	
	/**
	 * Public 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 completion proposal
	 */
	public static CustomCompletionProposal createCompletionProposalWithReplacementOfPrefix(NamedElement namedElement, 
														String completionString, 
														String displayString, 
														ContentAssistContext context) {
		String additionalProposalInfo = "" + namedElement.getQualifiedName() + "\n" + '(' + namedElement.eClass().getName() + ')' ;
		
		CustomCompletionProposal completionProposal = new CustomCompletionProposal(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
				null							,				// contextInformation
				additionalProposalInfo,							// additionalProposalInfo
				context);
		return completionProposal ;
	}
	
	/**
	 * Public 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 completion proposal
	 */
	public static CustomCompletionProposal createCompletionProposal(String completionString, 
														String displayString, 
														ContentAssistContext context) {
		
		CustomCompletionProposal completionProposal = new CustomCompletionProposal(completionString, 	// String to be inserted 
				context.getOffset(),  							// Offset
				context.getSelectedText().length(),				// Replacement length
				completionString.length(),						// cursorPosition
				null	,										// image
				" " + displayString,							// displayString
				null							,				// contextInformation
				null,											// additionalProposalInfo
				context);
		return completionProposal ;
	}
	
	/**
	 * Public utility method that computes a qualified name, taking into account packages imported by the namespace model
	 * 
	 * @param namedElement 
	 * @param model 
	 * @return the qualified name label
	 */
	public static String getQualifiedNameLabelWithSufficientDepth(NamedElement namedElement, Namespace model) {
		String label = "" ;
		
		List<Package> importedPackages = new ArrayList<Package>(model.getImportedPackages()) ;
		
		List<Namespace> visitedNamespaces = new ArrayList<Namespace>() ;
		Namespace currentNamespace = namedElement.getNamespace() ;
		
		boolean rootFound = false ;
		boolean modelIsTheRoot = false ;
		
		while (currentNamespace != null && !rootFound) {
			visitedNamespaces.add(currentNamespace) ;
			if (importedPackages.contains(currentNamespace) || currentNamespace == model) {
				rootFound = true ;
				if (currentNamespace == model)
					modelIsTheRoot = true ;
			}
			Element owner = currentNamespace.getOwner() ;
			while (owner != null && !(owner instanceof Namespace))
				owner = owner.getOwner() ;
			
			currentNamespace = owner != null ? (Namespace)owner : null ;
		}
		
		for (int i = visitedNamespaces.size() - 1 - (modelIsTheRoot ? 1 : 0) ; i >= 0 ; i--) {
			label += visitedNamespaces.get(i).getName() + "::" ; 
		}
		
		return label + namedElement.getName() ;
	}
	
}

Back to the top