Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d0da44e611a057b796dab8b9dc2c5a8dd7412eec (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
package org.eclipse.papyrus.uml.diagram.sequence.util;

import java.util.Collection;

import org.eclipse.emf.common.util.EList;
import org.eclipse.papyrus.infra.tools.util.StringHelper;
import org.eclipse.papyrus.uml.tools.utils.ICustomAppearence;
import org.eclipse.papyrus.uml.tools.utils.MultiplicityElementUtil;
import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil;
import org.eclipse.papyrus.uml.tools.utils.PropertyUtil;
import org.eclipse.papyrus.uml.tools.utils.TypeUtil;
import org.eclipse.papyrus.uml.tools.utils.ValueSpecificationUtil;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Signal;
import org.eclipse.uml2.uml.ValueSpecification;

public class SignalUtil {

	private static String getCustomPropertyLabel(Message e, Property property, Collection<String> displayValue) {
		StringBuffer buffer = new StringBuffer();
		// visibility
		buffer.append(" ");
		if(displayValue.contains(ICustomAppearence.DISP_VISIBILITY)) {
			buffer.append(NamedElementUtil.getVisibilityAsSign(property));
		}
		// derived property
		if(displayValue.contains(ICustomAppearence.DISP_DERIVE)) {
			if(property.isDerived()) {
				buffer.append("/");
			}
		}
		boolean showEqualMark = false;
		// name
		if(displayValue.contains(ICustomAppearence.DISP_PARAMETER_NAME)) {
			buffer.append(" ");
			String name = StringHelper.trimToEmpty(property.getName());
			if(name.trim().length() > 0) {
				showEqualMark = true;
			}
			buffer.append(name);
		}
		if(displayValue.contains(ICustomAppearence.DISP_PARAMETER_TYPE)) {
			// type
			if(property.getType() != null) {
				buffer.append(": " + StringHelper.trimToEmpty(property.getType().getName()));
			} else {
				buffer.append(": " + TypeUtil.UNDEFINED_TYPE_NAME);
			}
			showEqualMark = true;
		}
		if(displayValue.contains(ICustomAppearence.DISP_MULTIPLICITY)) {
			// multiplicity -> do not display [1]
			String multiplicity = MultiplicityElementUtil.getMultiplicityAsString(property);
			buffer.append(multiplicity);
		}
		if(displayValue.contains(ICustomAppearence.DISP_DERIVE)) {
			String value = getValue(e, property);
			if(value != null) {
				if(showEqualMark) {
					buffer.append(" = ");
				}
				buffer.append(value);
			}
		} else if(displayValue.contains(ICustomAppearence.DISP_PARAMETER_DEFAULT)) {
			// default value
			if(property.getDefault() != null) {
				if(showEqualMark) {
					buffer.append(" = ");
				}
				buffer.append(property.getDefault());
			}
		}
		if(displayValue.contains(ICustomAppearence.DISP_MODIFIERS)) {
			boolean multiLine = displayValue.contains(ICustomAppearence.DISP_MULTI_LINE);
			// property modifiers
			String modifiers = PropertyUtil.getModifiersAsString(property, multiLine);
			if(!modifiers.equals("")) {
				if(multiLine) {
					buffer.append("\n");
				}
				if(!buffer.toString().endsWith(" ")) {
					buffer.append(" ");
				}
				buffer.append(modifiers);
			}
		}
		return buffer.toString();
	}

	private static String getValue(Message e, Property property) {
		try {
			Signal signal = (Signal)property.getOwner();
			int index = signal.getOwnedAttributes().indexOf(property);
			EList<ValueSpecification> arguments = e.getArguments();
			if(arguments.size() > index) {
				return ValueSpecificationUtil.getSpecificationValue(arguments.get(index));
			}
		} catch (Exception e1) {
		}
		return null;
	}

	/**
	 * return the custom label of the signal, given UML2 specification and a custom style.
	 *
	 * @param message
	 *
	 * @param style
	 *        the integer representing the style of the label
	 *
	 * @return the string corresponding to the label of the signal
	 */
	public static String getCustomLabel(Message message, Signal signal, Collection<String> displayValue) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" "); // adds " " first for correct display considerations
		// visibility
		if(displayValue.contains(ICustomAppearence.DISP_VISIBILITY)) {
			buffer.append(NamedElementUtil.getVisibilityAsSign(signal));
		}
		// name
		if(displayValue.contains(ICustomAppearence.DISP_NAME)) {
			buffer.append(" ");
			buffer.append(StringHelper.trimToEmpty(signal.getName()));
		}
		//
		// parameters : '(' parameter-list ')'
		buffer.append("(");
		buffer.append(getPropertiesAsString(message, signal, displayValue));
		buffer.append(")");
		return buffer.toString();
	}

	/**
	 * Returns signal properties as a string, the label is customized using a bit mask
	 *
	 * @return a string containing all properties separated by commas
	 */
	private static String getPropertiesAsString(Message e, Signal signal, Collection<String> displayValue) {
		StringBuffer propertiesString = new StringBuffer();
		boolean firstProperty = true;
		for(Property property : signal.getOwnedAttributes()) {
			// get the label for this property
			String propertyString = getCustomPropertyLabel(e, property, displayValue).trim();
			if(!propertyString.equals("")) {
				if(!firstProperty) {
					propertiesString.append(", ");
				}
				propertiesString.append(propertyString);
				firstProperty = false;
			}
		}
		return propertiesString.toString();
	}
}

Back to the top