Class DefaultViewElementBuilderContext

All Implemented Interfaces:
ReadableAttributes, WritableAttributes, LocalizedTextResolver, ViewElementBuilderContext, org.springframework.core.AttributeAccessor
Direct Known Subclasses:
IteratorViewElementBuilderContext

public class DefaultViewElementBuilderContext extends AttributeOverridingSupport implements ViewElementBuilderContext

Standard implementation of a ViewElementBuilderContext that optionally allows a parent set of attributes to be provided. Attributes from the parent will be visible in the context, but attributes set on the builder context directly will hide the same attributes from the parent. Hiding with a non-null value is possible, but removing attributes entirely from the parent is not.

For example when creating a new context based on a Model, all attributes from the model will be available on the context. Additional attributes can be added to the context only and will not be available in the original Model. Changes to the model should propagate to the model (depending on the model implementation).

Custom implementations can override AttributeOverridingSupport.setParent(ReadableAttributes) to allow the parent to be set after construction.

This implementation also registers some default attributes, like the WebResourceRegistry, see registerMissingDefaultAttributes(ViewElementBuilderContext)

. See also DefaultViewElementBuilderContext(boolean) for sub class implementations that do not want the default attributes set.

When there is no web context, this implementation will use default fallback implementations for the public signature methods of ViewElementBuilderContext. Ensuring that the calls themselves will not result in NPEs being thrown.

Author:
Arne Vandamme
See Also:
  • Field Details

    • FALLBACK_TEXT_RESOLVER

      public static final LocalizedTextResolver FALLBACK_TEXT_RESOLVER
    • FALLBACK_MESSAGE_SOURCE

      public static final org.springframework.context.MessageSource FALLBACK_MESSAGE_SOURCE
  • Constructor Details

    • DefaultViewElementBuilderContext

      public DefaultViewElementBuilderContext()
    • DefaultViewElementBuilderContext

      protected DefaultViewElementBuilderContext(boolean registerDefaultAttributes)
    • DefaultViewElementBuilderContext

      public DefaultViewElementBuilderContext(org.springframework.ui.Model parent)
    • DefaultViewElementBuilderContext

      public DefaultViewElementBuilderContext(Map<String,Object> parent)
    • DefaultViewElementBuilderContext

      public DefaultViewElementBuilderContext(ReadableAttributes parent)
  • Method Details

    • setWebResourceRegistry

      public void setWebResourceRegistry(WebResourceRegistry webResourceRegistry)

      Set the WebResourceRegistry for this builder context. Builders can register additional resources if required for the view elements they generate. For example a datepicker control could register additional javascript.

      This method is an alias to calling AttributeSupport.setAttribute(Class, Object) with WebResourceRegistry.class as attribute name, and the instance as value. The explicit method indicates that this attribute is not required but is expected.

      Parameters:
      webResourceRegistry - instance
    • getWebResourceRegistry

      public WebResourceRegistry getWebResourceRegistry()
    • setWebAppLinkBuilder

      public void setWebAppLinkBuilder(WebAppLinkBuilder linkBuilder)
      Set the WebAppLinkBuilder that should be used for calls to buildLink(String). By default the link builder attached to the current request would have been registered.
      Parameters:
      linkBuilder - instance
    • getWebAppLinkBuilder

      public WebAppLinkBuilder getWebAppLinkBuilder()
    • setMessageSource

      public void setMessageSource(org.springframework.context.MessageSource messageSource)
      Set the MessageSource that should be used in this builder context.
      Parameters:
      messageSource - to use
    • getMessageSource

      public org.springframework.context.MessageSource getMessageSource()
    • setLocalizedTextResolver

      public void setLocalizedTextResolver(LocalizedTextResolver textResolver)
      Set the LocalizedTextResolver to use in this builder context.
      Parameters:
      textResolver - to use
    • getLocalizedTextResolver

      public LocalizedTextResolver getLocalizedTextResolver()
    • buildLink

      public String buildLink(String baseLink)
      Description copied from interface: ViewElementBuilderContext
      Will build a link using the WebAppLinkBuilder attribute that is present on this context. If none is present, the baseLink will remain unmodified.
      Specified by:
      buildLink in interface ViewElementBuilderContext
      Parameters:
      baseLink - to process
      Returns:
      processed link
    • resolveText

      public String resolveText(String text)
      Description copied from interface: LocalizedTextResolver
      Localize the text using the Locale attached to the current thread.
      Specified by:
      resolveText in interface LocalizedTextResolver
      Parameters:
      text - to localize
      Returns:
      localized text
    • resolveText

      public String resolveText(String text, Locale locale)
      Description copied from interface: LocalizedTextResolver
      Localize the text using the Locale specified.
      Specified by:
      resolveText in interface LocalizedTextResolver
      Parameters:
      text - to localize
      locale - to use
      Returns:
      localized text
    • resolveText

      public String resolveText(String text, String defaultValue)
      Description copied from interface: LocalizedTextResolver
      Localize the text using the Locale attached to the current thread.
      Specified by:
      resolveText in interface LocalizedTextResolver
      Parameters:
      text - to localize
      defaultValue - to return if resolved value is null
      Returns:
      localized text
    • resolveText

      public String resolveText(String text, String defaultValue, Locale locale)
      Description copied from interface: LocalizedTextResolver
      Localize the text using the Locale specified.
      Specified by:
      resolveText in interface LocalizedTextResolver
      Parameters:
      text - to localize
      defaultValue - to return if resolved value is null
      Returns:
      localized text
    • getMessage

      public String getMessage(String code)
      Description copied from interface: ViewElementBuilderContext
      Try to resolve the message. Return the message code no message was found.
      Specified by:
      getMessage in interface ViewElementBuilderContext
      Parameters:
      code - the code to lookup up, such as 'calculator.noRateSet'
      Returns:
      the resolved message if the lookup was successful, otherwise the code itself
      See Also:
    • getMessage

      public String getMessage(String code, Locale locale)
      Description copied from interface: ViewElementBuilderContext
      Try to resolve the message. Return default message if no message was found.
      Specified by:
      getMessage in interface ViewElementBuilderContext
      Parameters:
      code - the code to lookup up, such as 'calculator.noRateSet'
      locale - the Locale in which to do the lookup
      Returns:
      the resolved message if the lookup was successful; otherwise the code itself
      See Also:
    • getMessage

      public String getMessage(String code, String defaultMessage)
      Description copied from interface: ViewElementBuilderContext
      Try to resolve the message. Return default message if no message was found.
      Specified by:
      getMessage in interface ViewElementBuilderContext
      Parameters:
      code - the code to lookup up, such as 'calculator.noRateSet'
      defaultMessage - String to return if the lookup fails
      Returns:
      the resolved message if the lookup was successful; otherwise the default message passed as a parameter
      See Also:
    • getMessage

      public String getMessage(String code, String defaultMessage, Locale locale)
      Description copied from interface: ViewElementBuilderContext
      Try to resolve the message. Return default message if no message was found.
      Specified by:
      getMessage in interface ViewElementBuilderContext
      Parameters:
      code - the code to lookup up, such as 'calculator.noRateSet'
      defaultMessage - String to return if the lookup fails
      locale - the Locale in which to do the lookup
      Returns:
      the resolved message if the lookup was successful; otherwise the default message passed as a parameter
      See Also:
    • getMessage

      public String getMessage(String code, Object[] args)
      Description copied from interface: ViewElementBuilderContext
      Try to resolve the message. Return message code if no message was found.
      Specified by:
      getMessage in interface ViewElementBuilderContext
      Parameters:
      code - the code to lookup up, such as 'calculator.noRateSet'
      args - array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none.
      Returns:
      the resolved message if the lookup was successful; otherwise the message code
      See Also:
    • getMessage

      public String getMessage(String code, Object[] args, Locale locale)
      Description copied from interface: ViewElementBuilderContext
      Try to resolve the message. Return message code if no message was found.
      Specified by:
      getMessage in interface ViewElementBuilderContext
      Parameters:
      code - the code to lookup up, such as 'calculator.noRateSet'
      args - array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none.
      locale - the Locale in which to do the lookup
      Returns:
      the resolved message if the lookup was successful; otherwise the message code
      See Also:
    • getMessage

      public String getMessage(String code, Object[] args, String defaultMessage)
      Description copied from interface: ViewElementBuilderContext
      Try to resolve the message. Return default message if no message was found.
      Specified by:
      getMessage in interface ViewElementBuilderContext
      Parameters:
      code - the code to lookup up, such as 'calculator.noRateSet'
      args - array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none.
      defaultMessage - String to return if the lookup fails
      Returns:
      the resolved message if the lookup was successful; otherwise the default message passed as a parameter
      See Also:
    • getMessage

      public String getMessage(String code, Object[] args, String defaultMessage, Locale locale)
      Description copied from interface: ViewElementBuilderContext
      Try to resolve the message. Return default message if no message was found.
      Specified by:
      getMessage in interface ViewElementBuilderContext
      Parameters:
      code - the code to lookup up, such as 'calculator.noRateSet'
      args - array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none.
      defaultMessage - String to return if the lookup fails
      locale - the Locale in which to do the lookup
      Returns:
      the resolved message if the lookup was successful; otherwise the default message passed as a parameter
      See Also:
    • registerMissingDefaultAttributes

      public static void registerMissingDefaultAttributes(ViewElementBuilderContext builderContext)
      Registers default attributes in the builder context if they are not yet present. If they are present - either directly or in the parent - they will be skipped.

      If there is a global ViewElementBuilderContext, the default attributes will be fetched from there. Otherwise request-bound values will be looked for:

      Parameters:
      builderContext - to add the attributes to