Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6ec5265eb8da19ea333edf20a5c96330516df212 (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
/*****************************************************************************
 * Copyright (c) 2009 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:
 *  Yann TANGUY (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.tools.utils;

import java.util.Collection;

import org.eclipse.uml2.uml.Parameter;

/**
 * Utility class for <code>org.eclipse.uml2.uml.Parameter</code><BR>
 */
public class ParameterUtil {

	/**
	 * Returns the modifier of the property, separated by a comma, as as single line if <code>multiline</code> is <code>false</code> or as a multiline
	 * string if <code>multiline</code> is <code>false</code>.
	 *
	 * @param multiLine
	 *        boolean that indicates if the string should have several lines when set to <code>true</code> or only one line when set to
	 *        <code>false</code>.
	 *
	 * @return a string giving all modifiers for the property
	 */
	public static String getModifiersAsString(Parameter parameter, boolean multiLine) {
		StringBuffer buffer = new StringBuffer();
		boolean needsComma = false;
		String NL = (multiLine) ? "\n" : " ";

		// Return parameter modifiers
		if(parameter.isOrdered()) {
			needsComma = updateModifiersString(buffer, needsComma, NL, "ordered");;
		}
		if(parameter.isUnique()) {
			needsComma = updateModifiersString(buffer, needsComma, NL, "unique");
		}
		if(parameter.isException()) {
			needsComma = updateModifiersString(buffer, needsComma, NL, "exception");
		}
		if(parameter.isStream()) {
			needsComma = updateModifiersString(buffer, needsComma, NL, "stream");
		}

		if(!buffer.toString().equals("")) {
			buffer.insert(0, "{");
			buffer.append("}");
		}

		return buffer.toString();
	}

	/**
	 * Update the modifiers string
	 *
	 * @param buffer
	 *        the existing bufferString to append
	 * @param needsComma
	 *        if it needs coma
	 * @param NL
	 *        if it is multiline
	 * @param message
	 *        the message top
	 * @return true because the modifier string is no more empty
	 */
	private static boolean updateModifiersString(StringBuffer buffer, boolean needsComma, String NL, String message) {
		if(needsComma) {
			buffer.append(",");
			buffer.append(NL);
		}
		buffer.append(message);
		return true;
	}

	/**
	 * return the full label of the Parameter.
	 *
	 * @return the string corresponding to the label of the parameter
	 */
	public static String getLabel(Parameter parameter) {
		StringBuffer buffer = new StringBuffer();
		// visibility
		buffer.append(" ");
		buffer.append(NamedElementUtil.getVisibilityAsSign(parameter));

		// direction
		buffer.append(" ");
		buffer.append(parameter.getDirection().getLiteral());

		// name
		buffer.append(" ");
		if(parameter.getName() != null) {
			buffer.append(parameter.getName());
		}

		// type
		if(parameter.getType() != null) {
			buffer.append(": " + parameter.getType().getName());
		} else {
			buffer.append(": " + TypeUtil.UNDEFINED_TYPE_NAME);
		}

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

		// default value
		if(parameter.getDefault() != null) {
			buffer.append(" = ");
			buffer.append(parameter.getDefault());
		}

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

		return buffer.toString();
	}

	/**
	 * return the custom label of the property, given UML2 specification and a custom style.
	 *
	 * @param style
	 *        the integer representing the style of the label
	 *
	 * @return the string corresponding to the label of the property
	 */
	public static String getCustomLabel(Parameter parameter, Collection<String> maskValues) {
		StringBuffer buffer = new StringBuffer();
		// visibility
		buffer.append(" ");
		if(maskValues.contains(ICustomAppearence.DISP_VISIBILITY)) {
			buffer.append(NamedElementUtil.getVisibilityAsSign(parameter));
		}

		// direction property
		if(maskValues.contains(ICustomAppearence.DISP_PARAMETER_DIRECTION) || maskValues.contains(ICustomAppearence.DISP_DIRECTION)) {
			buffer.append(" ");
			buffer.append(parameter.getDirection().getLiteral());
		}

		// name
		if(maskValues.contains(ICustomAppearence.DISP_PARAMETER_NAME) || maskValues.contains(ICustomAppearence.DISP_NAME)) {
			buffer.append(" ");
			buffer.append(parameter.getName());
		}

		if(maskValues.contains(ICustomAppearence.DISP_PARAMETER_TYPE) || maskValues.contains(ICustomAppearence.DISP_TYPE)) {
			// type
			if(parameter.getType() != null) {
				buffer.append(": " + parameter.getType().getName());
			} else {
				buffer.append(": " + TypeUtil.UNDEFINED_TYPE_NAME);
			}
		}

		if(maskValues.contains(ICustomAppearence.DISP_PARAMETER_MULTIPLICITY) || maskValues.contains(ICustomAppearence.DISP_MULTIPLICITY)) {
			// multiplicity -> do not display [1]
			String multiplicity = MultiplicityElementUtil.getMultiplicityAsString(parameter);
			buffer.append(multiplicity);
		}

		if(maskValues.contains(ICustomAppearence.DISP_PARAMETER_DEFAULT) || maskValues.contains(ICustomAppearence.DISP_DEFAULT_VALUE)) {
			// default value
			if(parameter.getDefault() != null) {
				buffer.append(" = ");
				buffer.append(parameter.getDefault());
			}
		}

		if(maskValues.contains(ICustomAppearence.DISP_MODIFIERS)) {
			boolean multiLine = (maskValues.contains(ICustomAppearence.DISP_MULTI_LINE));
			// property modifiers
			String modifiers = ParameterUtil.getModifiersAsString(parameter, multiLine);
			if(!modifiers.equals("")) {
				if(multiLine) {
					buffer.append("\n");
				}
				buffer.append(modifiers);
			}
		}
		return buffer.toString();
	}

	/**
	 * Returns the default value as a String
	 *
	 * @param equalSign
	 *        boolean set to <code>true</code> if the label must have the <code>=</code> sign
	 *        before the default value
	 * @return the default value as a String
	 */
	private static String getDefaultAsString(Parameter parameter, boolean equalSign) {
		String defaultString = "";
		// default value
		if((parameter.getDefault() != null) && !parameter.getDefault().equals("")) {
			if(equalSign) {
				defaultString += "= ";
			}
			defaultString += parameter.getDefault();
		}
		return "";
	}
}

Back to the top