001 package org.tynamo.util;
002
003 import org.apache.commons.lang.StringUtils;
004 import org.apache.tapestry5.internal.TapestryInternalUtils;
005 import org.apache.tapestry5.ioc.Messages;
006 import org.tynamo.descriptor.TynamoClassDescriptor;
007 import org.tynamo.descriptor.TynamoPropertyDescriptor;
008
009
010 public class DisplayNameUtils
011 {
012
013 private static final String PLURAL_SUFIX = "-plural";
014 private static final String SHORTDESC_SUFIX = "-shortDescription";
015
016
017 /**
018 * Looks for a label within the messages based on the class name.
019 * If found, it is used, otherwise the name is "linguistically pluralized" (only works in English)
020 * The message key is the full or the simple name of the entity suffixed with "-plural".
021 */
022 public static String getPluralDisplayName(TynamoClassDescriptor classDescriptor, Messages messages)
023 {
024 String fullName = classDescriptor.getType().getName() + PLURAL_SUFIX;
025 String shortName = classDescriptor.getType().getSimpleName() + PLURAL_SUFIX;
026 return selectDisplayName(fullName, shortName, Utils.pluralize(TapestryInternalUtils.toUserPresentable(classDescriptor.getType().getSimpleName())), messages);
027 }
028
029 /**
030 * Looks for a label within the messages based on the class name.
031 * If found, it is used, otherwise the name is converted to a user presentable form.
032 * The message key is either the full name of the entity or the simpleName
033 */
034 public static String getDisplayName(TynamoClassDescriptor classDescriptor, Messages messages)
035 {
036 return getDisplayName(classDescriptor.getType(), messages);
037 }
038
039 /**
040 * Looks for a label within the messages based on the class name.
041 * If found, it is used, otherwise the name is converted to a user presentable form.
042 * The message key is either the full name of the entity or the simpleName
043 */
044 public static String getDisplayName(Class type, Messages messages)
045 {
046 String fullName = type.getName();
047 String shortName = type.getSimpleName();
048 return selectDisplayName(fullName, shortName, TapestryInternalUtils.toUserPresentable(shortName), messages);
049 }
050
051 /**
052 * Looks for a label within the messages based on the property name.
053 * If found, it is used, otherwise the name is converted to a user presentable form.
054 * The message key is the property name suffixed with "-label".
055 */
056 public static String getDisplayName(TynamoPropertyDescriptor propertyDescriptor, Messages messages)
057 {
058 return TapestryInternalUtils.defaultLabel(propertyDescriptor.getName(), messages, propertyDescriptor.getName());
059 }
060
061 /**
062 * Looks for a label within the messages based on the class name.
063 * If found, it is used, otherwise an empty string is returned
064 * The message key is the property name suffixed with "-shortDescription".
065 */
066 public static String getShortDescription(TynamoClassDescriptor classDescriptor, Messages messages)
067 {
068 String fullName = classDescriptor.getType().getName() + SHORTDESC_SUFIX;
069 String shortName = classDescriptor.getType().getSimpleName() + SHORTDESC_SUFIX;
070 return selectDisplayName(fullName, shortName, StringUtils.EMPTY, messages);
071 }
072
073 /**
074 * Looks for a label within the messages based on the property name.
075 * If found, it is used, otherwise an empty string is returned
076 * The message key is the full or the simple name of the entity suffixed with "-shortDescription".
077 */
078 public static String getShortDescription(TynamoPropertyDescriptor propertyDescriptor, Messages messages)
079 {
080 String fullName = propertyDescriptor.getBeanType().getName() + "." + propertyDescriptor.getName() + SHORTDESC_SUFIX;
081 String shortName = propertyDescriptor.getName() + SHORTDESC_SUFIX;
082 return selectDisplayName(fullName, shortName, StringUtils.EMPTY, messages);
083 }
084
085
086 /**
087 * Selects a localized message, given two keys and a default value in case no key is found.
088 */
089 private static String selectDisplayName(String firstKey, String secondKey, String defValue, Messages messages)
090 {
091
092 if (messages.contains(firstKey)) return messages.get(firstKey);
093 if (messages.contains(secondKey)) return messages.get(secondKey);
094
095 return defValue;
096 }
097
098 }