Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f3c949f1b5be64ff9b958d37e39af80cfa81c981 (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
/*****************************************************************************
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.textedit.parameter.xtext.ui.contributions;

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

import org.eclipse.emf.common.util.EList;
import org.eclipse.papyrus.uml.tools.utils.MultiplicityElementUtil;
import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil;
import org.eclipse.papyrus.uml.tools.utils.ParameterUtil;
import org.eclipse.papyrus.uml.tools.utils.TypeUtil;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Parameter;
import org.eclipse.uml2.uml.ParameterEffectKind;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.util.UMLSwitch;

/**
 *
 * This class provides methods to get the label to display for a Parameter
 *
 *
 */
public class UMLParameterEditorUtil extends ParameterUtil {

	/**
	 * Returns the string used to represent this parameter
	 *
	 * @param parameter
	 *            a parameter
	 * @return
	 *         the string used to represent this parameter
	 */
	public static String getLabel(Parameter parameter) {
		StringBuffer buffer = new StringBuffer();
		// visibility
		buffer.append(" "); //$NON-NLS-1$
		buffer.append(NamedElementUtil.getVisibilityAsSign(parameter));

		// direction
		buffer.append(" "); //$NON-NLS-1$
		buffer.append(parameter.getDirection().getLiteral());

		// name
		buffer.append(" "); //$NON-NLS-1$
		if (parameter.getName() != null) {
			buffer.append(parameter.getName());
		}

		// type
		if (parameter.getType() != null) {
			EList<Namespace> namespaces = parameter.allNamespaces();
			buffer.append(" : " + getTypeLabel(parameter.getType(), namespaces.get(namespaces.size() - 1))); //$NON-NLS-1$
		} else {
			buffer.append(" : " + TypeUtil.UNDEFINED_TYPE_NAME); //$NON-NLS-1$
		}

		// multiplicity -> do not display [1]
		String multiplicity = MultiplicityElementUtil.getMultiplicityAsString(parameter);
		if (!multiplicity.trim().equals("[1]")) { //$NON-NLS-1$
			buffer.append(multiplicity);
		}

		// property modifiers
		buffer.append(" ");
		buffer.append(ParameterUtil.getModifiersAsString(parameter, false));
		buffer.append(getEffectAsString(parameter));

		// default value
		if (parameter.getDefaultValue() != null) {
			buffer.append(" = "); //$NON-NLS-1$
			String defaultValue = new UMLSwitch<String>() {
				@Override
				public String caseLiteralBoolean(org.eclipse.uml2.uml.LiteralBoolean object) {
					return Boolean.toString(object.booleanValue());
				}

				@Override
				public String caseLiteralInteger(org.eclipse.uml2.uml.LiteralInteger object) {
					return Integer.toString(object.integerValue());
				}

				@Override
				public String caseLiteralNull(org.eclipse.uml2.uml.LiteralNull object) {
					return "null"; //$NON-NLS-1$
				}

				@Override
				public String caseLiteralString(org.eclipse.uml2.uml.LiteralString object) {
					return "\"" + object.stringValue() + "\"";
				}

				@Override
				public String caseLiteralReal(org.eclipse.uml2.uml.LiteralReal object) {
					return Double.toString(object.getValue());
				}

				@Override
				public String caseLiteralUnlimitedNatural(org.eclipse.uml2.uml.LiteralUnlimitedNatural object) {
					return object.getValue() < 0 ? "*" : Integer.toString(object.getValue());
				}

			}.doSwitch(parameter.getDefaultValue());

			buffer.append(defaultValue);
		}
		return buffer.toString();

	}

	/**
	 * Returns the effect of the parameter as a String
	 *
	 * @param parameter
	 *            the parameter
	 * @return
	 *         The effect of the parameter as a String
	 */
	public static String getEffectAsString(Parameter parameter) {
		StringBuffer buffer = new StringBuffer();
		ParameterEffectKind effect = parameter.getEffect();
		buffer.append(effect.toString());

		if (!buffer.toString().equals("")) { //$NON-NLS-1$
			buffer.insert(0, "{effect: "); //$NON-NLS-1$
			buffer.append("}"); //$NON-NLS-1$
		}
		return buffer.toString();
	}

	/**
	 * Returns the name of the type with its qualified name
	 *
	 * @param type
	 *            a type
	 * @return
	 *         the name of the type with its qualified name
	 */
	public static String getTypeLabel(Type type, Namespace model) {
		String label = ""; //$NON-NLS-1$

		List<Package> importedPackages = new ArrayList<Package>(model.getImportedPackages());

		List<Package> visitedPackages = new ArrayList<Package>();
		Package currentPackage = type.getNearestPackage();

		boolean rootFound = false;

		while (currentPackage != null && !rootFound) {
			visitedPackages.add(currentPackage);
			if (importedPackages.contains(currentPackage) || currentPackage == model) {
				rootFound = true;
			}
			Element owner = currentPackage.getOwner();
			while (owner != null && !(owner instanceof Package)) {
				owner = owner.getOwner();
			}

			currentPackage = owner != null ? (Package) owner : null;
		}

		for (int i = visitedPackages.size() - 1; i >= 0; i--) {
			label += visitedPackages.get(i).getName() + "::"; //$NON-NLS-1$
		}

		return label + type.getName();
	}
}

Back to the top