Package wt.util

Class WTMessage

java.lang.Object
wt.util.WTMessage
All Implemented Interfaces:
Externalizable, Serializable, Comparable, Evolvable, LocalizableMessage, Message

public class WTMessage extends Object implements LocalizableMessage, Evolvable, Serializable, Comparable
WTMessage is the wrapper class for getting a formatted, localized message, from a resource bundle.

The following are examples of how WTMessage would be used:

    Static usage:
       Object[] textInserts = { getName() };
       System.out.println(
          WTMessage.getLocalizedMessage( RESOURCE, "17", textInserts )
 );
 

Instance usage: Object[] textInserts = { getName() }; WTMessage wtMessage = new WTMessage( RESOURCE, "17", textInserts ); // ... other stuff wtMessage.getLocalizedMessage();

Localization of the WTMessage messages relies on the resource bundle and text formatting features of Java. In the example above, RESOURCE is a string constant that identifies the resource bundle containing the localizable message.

Objects that act as text insert parameters of the message will make use of the getLocalizedMessage(Locale) method, if those insert objects implement the wt.util.LocalizableMessage interface. This enables the message inserts to be localized, along with the message itself, at the time the localized message is requested.

Note that this class implements Evolvable and thus is intended to support serialization compatibility for long-term storage. This contract can only be met, however, if the insert objects are also Evolvable, EvolvableAdaptable, built-in Java wrapper types for primitive, String, or EnumeratedType objects. One must therefore keep this in mind when constructing WTMessage objects for possible long term storage, e.g. in database BLOBs. To ensure that Non-Evolvable insert objects aren't serialized into the DB, which cause deserialization compatibility issues, those objets are converted to Strings before serializing them.

If readExternal() hits unreadable textInserts, textInserts can be set to an empty array when configured with wt.util.WTMessage.IGNORE_INCOMPATIBLE_INSERTS=true. This allows for the message to be read without throwing an exception that cannot be resolved without removing the unreadable data from the database.

Supported API: true

Extendable: false

See Also:
  • Constructor Details

    • WTMessage

      public WTMessage(String rb, String key, Object[] params)
      Constructs a Windchill message with a localizable detailed message.

      Param objects of type wt.util.LocalizableMessage will be expanded by invoking getLocalizedMessage(Locale) on them.

      Supported API: true

      Parameters:
      rb - the name of the base resource bundle subclass containing the localizable message.
      key - the key associated with the localizable message
      params - an optional set of objects to be formatted into the localizable message text.
      See Also:
  • Method Details

    • getMessages

      public static WTMessage[] getMessages(String rb, String key, Object[] params)
      Loops over the arrayed elements of params to generate multiple WTMessages, one for each element.

      Param objects of type wt.util.LocalizableMessage will be expanded by invoking getLocalizedMessage(Locale) on them.

      Supported API: true

      Parameters:
      rb - the name of the base resource bundle subclass containing the localizable message.
      key - the key associated with the localizable message
      params - an optional set of objects to be formatted into the localizable message text. If an element of params is itself an array, loop over those elements to produce multiple WTMessages, one for each element.
      See Also:
    • getLocalizedMessage

      public String getLocalizedMessage()
      Gets the localized message, for the instance.

      Supported API: true
      Returns:
      The Localized message as string for the instance.
      See Also:
    • getLocalizedMessage

      public static String getLocalizedMessage(String resourceBundle, String messageKey)
      Gets the localized message. The localized message must not have any text inserts.

      Supported API: true
      Parameters:
      resourceBundle - the name of the base resource bundle subclass containing the localizable message.
      messageKey - the key associated with the localizable message
      Returns:
      The localized message as string for passed resourceBundle and messageKey.
      See Also:
    • getLocalizedMessage

      public static String getLocalizedMessage(String resourceBundle, String messageKey, Object[] textInserts)
      Gets the localized message, for passed arguments.

      Param objects of type wt.util.LocalizableMessage will be expanded by invoking getLocalizedMessage(Locale) on them.

      Supported API: true

      Parameters:
      resourceBundle - the name of the base resource bundle subclass containing the localizable message.
      messageKey - the key associated with the localizable message
      textInserts - an optional set of objects to be formatted into the localizable message text.
      Returns:
      The localized message as string for passed resourceBundle and messageKey.
      See Also:
    • formatLocalizedMessage

      public static String formatLocalizedMessage(String localizedString, Object[] inserts)
      This method will process an already localized string by escaping any appropriate characters, and then call java.text.MessageFormat on the String to properly insert any inserts. Wherever possible the method getLocalizedMessage( ) should be used, since this allows for caching of resources etc. However in some cases, to avoid rewriting major pieces of code, MessageFormat.format( ) can be directly replaced with WTMessage.formatLocalizedMessage( )

      Supported API: true
      Parameters:
      localizedString - The localized String to format
      inserts - The inserts to insert via the MessageFormat
      Returns:
      The localized String formatted properly.
    • getLocalizedMessage

      public static String getLocalizedMessage(String resourceBundle, String messageKey, Object[] textInserts, Locale locale)
      Gets the localized message, for passed arguments.

      Param objects of type wt.util.LocalizableMessage will be expanded by invoking getLocalizedMessage(Locale) on them.

      Implementation Notes: Any usage of "{" or "}" characters in which there are intended to be used as "{" or "}" (and not inset delimiters) should be enclosed in "'" (apostrophes). For example, if you would like to have a String

        "My name in curly brackets {developer}"

      Then the resource file should use a ' to escape the curly brakcets, such that the string becomes
        "My name in curly brackets '{'developer'}'"


      For further details please see the javadoc for java.text.MessageFormat.

      Supported API: true
      Parameters:
      resourceBundle - the name of the base resource bundle subclass containing the localizable message.
      messageKey - the key associated with the localizable message
      textInserts - an optional set of objects to be formatted into the localizable message text.
      locale - the locale for which to get the localized message.
      Returns:
      The localized message as string for passed resourceBundle, messageKey and locale.
      See Also:
    • getMessageIdentifier

      public String getMessageIdentifier()
      Gets the identity of the message.

      Supported API: true
      Returns:
      the identity, as the resource bundle name and the messageKey.
    • toString

      public String toString()
      Returns the localized message, for the instance.

      Supported API: true
      Overrides:
      toString in class Object