summaryrefslogtreecommitdiffstats
path: root/shared
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2016-10-12 22:44:00 +0300
committerPekka Hyvönen <pekka@vaadin.com>2016-12-09 09:39:00 +0200
commita48e26b9b897a721527d5731a44a587294cdb4ea (patch)
treecced106ec6ece884b878a1bd496071e1f22104d3 /shared
parent4ea2151053469f1092afaeab29587353c001e887 (diff)
downloadvaadin-framework-a48e26b9b897a721527d5731a44a587294cdb4ea.tar.gz
vaadin-framework-a48e26b9b897a721527d5731a44a587294cdb4ea.zip
Make UPPER_CASE_PROPERTY_IDS more human friendly by default (#20380)
Change-Id: I9bd845a08fa3c440f05d102ccf8920dfc1c95b6b
Diffstat (limited to 'shared')
-rw-r--r--shared/src/main/java/com/vaadin/shared/util/SharedUtil.java30
-rw-r--r--shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java28
2 files changed, 58 insertions, 0 deletions
diff --git a/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java b/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java
index 3ee30bd4b5..78217a10c9 100644
--- a/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java
+++ b/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java
@@ -126,6 +126,31 @@ public class SharedUtil implements Serializable {
return join(parts, " ");
}
+ /**
+ * Converts an UPPER_CASE_STRING to a human friendly format (Upper Case
+ * String).
+ * <p>
+ * Splits words on {@code _}. Examples:
+ * <p>
+ * {@literal MY_BEAN_CONTAINER} becomes {@literal My Bean Container}
+ * {@literal AWESOME_URL_FACTORY} becomes {@literal Awesome Url Factory}
+ * {@literal SOMETHING} becomes {@literal Something}
+ *
+ * @since
+ * @param upperCaseUnderscoreString
+ * The input string in UPPER_CASE_UNDERSCORE format
+ * @return A human friendly version of the input
+ */
+ public static String upperCaseUnderscoreToHumanFriendly(
+ String upperCaseUnderscoreString) {
+ String[] parts = upperCaseUnderscoreString.replaceFirst("^_*", "")
+ .split("_");
+ for (int i = 0; i < parts.length; i++) {
+ parts[i] = capitalize(parts[i].toLowerCase(Locale.ENGLISH));
+ }
+ return join(parts, " ");
+ }
+
private static boolean isAllUpperCase(String string) {
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
@@ -203,6 +228,11 @@ public class SharedUtil implements Serializable {
string = string.substring(dotLocation + 1);
}
+ if (string.matches("^[0-9A-Z_]+$")) {
+ // Deal with UPPER_CASE_PROPERTY_IDS
+ return upperCaseUnderscoreToHumanFriendly(string);
+ }
+
return camelCaseToHumanFriendly(string);
}
diff --git a/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java b/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java
index 6d8cab4fea..791637e1b1 100644
--- a/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java
+++ b/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java
@@ -150,4 +150,32 @@ public class SharedUtilTest {
SharedUtil.getDuplicates(new Object[] { 1.2, "a", 1.2 }));
}
+ @Test
+ public void propertyIdToHumanFriendly() {
+ Assert.assertEquals("", SharedUtil.propertyIdToHumanFriendly(""));
+ Assert.assertEquals("First Name",
+ SharedUtil.propertyIdToHumanFriendly("firstName"));
+ Assert.assertEquals("First Name",
+ SharedUtil.propertyIdToHumanFriendly("FirstName"));
+ Assert.assertEquals("First Name",
+ SharedUtil.propertyIdToHumanFriendly("FIRST_NAME"));
+ Assert.assertEquals("Firstname",
+ SharedUtil.propertyIdToHumanFriendly("FIRSTNAME"));
+
+ Assert.assertEquals("2015 Q3",
+ SharedUtil.propertyIdToHumanFriendly("2015_Q3"));
+ Assert.assertEquals("Column X",
+ SharedUtil.propertyIdToHumanFriendly("_COLUMN_X"));
+ Assert.assertEquals("Column X",
+ SharedUtil.propertyIdToHumanFriendly("__COLUMN_X"));
+ Assert.assertEquals("1column Foobar",
+ SharedUtil.propertyIdToHumanFriendly("1COLUMN_FOOBAR"));
+ Assert.assertEquals("Result 2015",
+ SharedUtil.propertyIdToHumanFriendly("RESULT_2015"));
+ Assert.assertEquals("2015result",
+ SharedUtil.propertyIdToHumanFriendly("2015RESULT"));
+ Assert.assertEquals("Result2015",
+ SharedUtil.propertyIdToHumanFriendly("RESULT2015"));
+
+ }
}