Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d1bdce53b7500837f79547b6da1886f9f49cc118 (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
/*****************************************************************************
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.properties.ui.util;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.papyrus.infra.properties.contexts.Context;
import org.eclipse.papyrus.infra.properties.contexts.DataContextElement;
import org.eclipse.papyrus.infra.properties.contexts.DataContextPackage;
import org.eclipse.papyrus.infra.properties.contexts.Property;
import org.eclipse.papyrus.infra.properties.environment.Namespace;
import org.eclipse.papyrus.infra.properties.internal.ui.Activator;

/**
 * A Helper class for miscellaneous elements of the Property view framework
 *
 * @author Camille Letavernier
 */
public class PropertiesUtil {

	/**
	 * @param source
	 * @return
	 * 		the given String with the first letter capitalized
	 */
	public static String firstToUpper(String source) {
		if (source.length() == 0) {
			return source;
		}
		return source.substring(0, 1).toUpperCase() + source.substring(1);
	}

	/**
	 * @param source
	 * @return
	 * 		the given String with the first letter lowered
	 */
	public static String firstToLower(String source) {
		if (source.length() == 0) {
			return source;
		}
		return source.substring(0, 1).toLowerCase() + source.substring(1);
	}

	/**
	 * Returns the formatted label of the property
	 *
	 * @param property
	 * @return
	 */
	public static String getLabel(Property property) {
		if (property.getLabel() == null || property.getLabel().trim().equals("")) {
			return getLabel(property.getName());
		}

		return property.getLabel();
	}

	/**
	 * @param variableName
	 * @return
	 * 		A formatted version of the given variable name
	 */
	public static String getLabel(String variableName) {
		// "CamelCase" to "Natural case"
		String formattedValue = variableName;

		// replace fooBar by foo Bar
		formattedValue = formattedValue.replaceAll("([a-z])([A-Z])", "$1 $2"); //$NON-NLS-1$ //$NON-NLS-2$

		// replace FOOAndBar by FOO And Bar
		formattedValue = formattedValue.replaceAll("([A-Z]+)([A-Z])([a-z])", "$1 $2$3"); //$NON-NLS-1$ //$NON-NLS-2$

		// Capitalize the first word and lower the other ones : foo Bar -> Foo bar
		// Keep the upper case for acronyms FOO Bar -> FOO bar
		String[] words = formattedValue.split("\\s+"); //$NON-NLS-1$
		formattedValue = firstToUpper(words[0]);
		for (int i = 1; i < words.length; i++) {
			formattedValue += " "; //$NON-NLS-1$
			if (words[i].matches("^[A-Z]{2,}")) { //$NON-NLS-1$
				formattedValue += words[i];
			} else {
				formattedValue += firstToLower(words[i]);
			}
		}

		Activator.log.debug("\"" + formattedValue + "\""); //$NON-NLS-1$ //$NON-NLS-2$
		return formattedValue;
	}

	/**
	 * Tests if the given value is equal to the namespace's value
	 *
	 * @param namespace
	 * @param value
	 * @return
	 * 		True if they are equal
	 */
	public static boolean namespaceEquals(Namespace namespace, String value) {
		if (namespace == null) {
			return value == null || value.trim().equals(""); //$NON-NLS-1$
		} else {
			return namespace.getValue().equals(value);
		}
	}

	/**
	 * Tests if the given name is equal to the namespace's name
	 *
	 * @param namespace
	 * @param name
	 * @return
	 * 		True if they are equal
	 */
	public static boolean namespaceEqualsByName(Namespace namespace, String name) {
		if (namespace == null) {
			return name == null || name.trim().equals(""); //$NON-NLS-1$
		} else {
			return namespace.getName().equals(name);
		}
	}

	/**
	 * Return the full value of the namespace declaration
	 * e.g. clr-namespace:org.eclipse.papyrus.infra.properties.ui
	 *
	 * @param namespace
	 *            The namespace we want to prefix
	 * @return
	 * 		The prefixed namespace
	 */
	public static String getPrefixedValue(Namespace namespace) {
		String prefixedValue = ""; //$NON-NLS-1$
		if (namespace.getPrefix() != null && !namespace.getPrefix().trim().equals("")) { //$NON-NLS-1$
			prefixedValue = namespace.getPrefix() + ":"; //$NON-NLS-1$
		}
		prefixedValue += namespace.getValue();

		return prefixedValue;
	}

	/**
	 * Return the full name of the namespace declaration
	 * e.g. xmlns:ppe
	 *
	 * @param namespace
	 *            The namespace for which we want to get the qualified name
	 * @return
	 * 		The namespace's qualified name
	 */
	public static String getQualifiedName(Namespace namespace) {
		if (namespace.getName() == null || namespace.getName().trim().equals("")) { //$NON-NLS-1$
			return "xmlns"; //$NON-NLS-1$
		} else {
			return "xmlns:" + namespace.getName(); //$NON-NLS-1$
		}
	}

	/**
	 * Retrieve the DataContextElement matching the given qualifiedName.
	 *
	 * @param qualifiedName
	 *            The fully qualified name of the DataContextElement. The separator is ":"
	 *            e.g. : UML:NamedElement
	 * @param fromContextElements
	 *            The collection of DataContextElements in which the method should look
	 * @return
	 * 		The matching DataContextElement, or null if none was found
	 */
	public static DataContextElement getContextElementByQualifiedName(String qualifiedName, Collection<? extends DataContextElement> fromContextElements) {
		int index = qualifiedName.indexOf(":"); //$NON-NLS-1$
		if (index >= 0) {
			String name = qualifiedName.substring(0, index);
			qualifiedName = qualifiedName.substring(qualifiedName.indexOf(":") + 1, qualifiedName.length()); //$NON-NLS-1$
			for (DataContextElement contextElement : fromContextElements) {
				if (contextElement instanceof DataContextPackage && contextElement.getName().equals(name)) {
					DataContextElement result = getContextElementByQualifiedName(qualifiedName, ((DataContextPackage) contextElement).getElements());
					if (result != null) {
						return result;
					}
				}
			}
		} else {
			for (DataContextElement element : fromContextElements) {
				if (element.getName().equals(qualifiedName)) {
					return element;
				}
			}
		}
		return null;
	}

	/**
	 * Returns the list of Context on which the given context depends, directly
	 * or indirectly
	 *
	 * @param context
	 *            The context for which we want to retrieve the list of dependencies
	 * @return
	 * 		The list of Contexts on which the given context depends
	 */
	public static List<Context> getDependencies(Context context) {
		List<Context> result = new LinkedList<Context>();
		if (context == null) {
			return result;
		}

		result.add(context);
		findDependencies(context, result);
		return result;
	}

	private static void findDependencies(Context context, List<Context> result) {
		for (Context dependency : context.getDependencies()) {
			if (!result.contains(dependency)) {
				result.add(dependency);
				findDependencies(dependency, result);
			}
		}
	}

	/**
	 * Returns the set of DataContextElement containing the whole inheritance hierarchy
	 * for the given source DataContextElements
	 *
	 * @param source
	 *            The collection of DataContextElements for which we want to retrieve all inherited elements
	 * @return
	 * 		All DataContextElements inherited (Directly or indirectly) by at least one of the source
	 *         context elements
	 */
	public static Set<DataContextElement> getAllContextElements(Collection<DataContextElement> source) {
		Set<DataContextElement> result = new HashSet<DataContextElement>();
		getAllContextElements(source, result);
		return result;
	}

	private static void getAllContextElements(Collection<DataContextElement> source, Set<DataContextElement> result) {
		for (DataContextElement element : source) {
			if (!result.contains(element)) {
				result.add(element);
				getAllContextElements(element.getSupertypes(), result);
			}
		}
	}

	/**
	 * A util method to make big strings fit in a restricted amount of space,
	 * such as a tooltip. The method will add new lines in the string at
	 * a regular interval.
	 *
	 * @param string
	 *            The string to split
	 * @param maxCharPerLine
	 *            The maximum number of characters per line in the resulting string
	 * @return
	 * 		The split string
	 */
	public static String resizeString(String string, int maxCharPerLine) {
		if (string.trim().length() <= maxCharPerLine) {
			return string.trim();
		}

		String[] stringChunks = string.split("\n|\r|\r\n|\n\r"); //$NON-NLS-1$

		List<String> chunks = new LinkedList<String>();

		for (String chunk : stringChunks) {
			chunk = chunk.trim();
			if (chunk.length() > maxCharPerLine) {
				Matcher matcher = Pattern.compile("(.{0," + maxCharPerLine + "}\\b\\p{Punct}?)").matcher(chunk); //$NON-NLS-1$ //$NON-NLS-2$
				while (matcher.find()) {
					String group = matcher.group(1);
					chunks.add(group);
				}
			} else {
				chunks.add(chunk);
			}
		}

		String result = ""; //$NON-NLS-1$
		for (String chunk : chunks) {
			result += chunk.trim() + "\n"; //$NON-NLS-1$
		}

		return result.trim();
	}
}

Back to the top