Class ResourceLoader

java.lang.Object
de.ipb_halle.molecularfaces.util.ResourceLoader
All Implemented Interfaces:
jakarta.faces.event.ComponentSystemEventListener, jakarta.faces.event.FacesListener, Serializable, EventListener

public class ResourceLoader extends Object implements jakarta.faces.event.ComponentSystemEventListener, Serializable
This class enqueues resources and loads them either automatically or upon request.
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • RESOURCES_LIBRARY_NAME

      public static final String RESOURCES_LIBRARY_NAME
      Resource library name.
      See Also:
    • JAVASCRIPT_FACET_NAME

      public static final String JAVASCRIPT_FACET_NAME
      Name of the facet that is used when adding JavaScript resources as facet components.
      See Also:
    • STYLESHEET_FACET_NAME

      public static final String STYLESHEET_FACET_NAME
      Name of the facet that is used when adding stylesheet resources as facet components.
      See Also:
    • JAVASCRIPT

      public static final String JAVASCRIPT
      Renderer type for JavaScript resources.
      See Also:
    • STYLESHEET

      public static final String STYLESHEET
      Renderer type for stylesheet resources.
      See Also:
    • component

      private final jakarta.faces.component.UIComponent component
    • scriptResourcesToLoadInHead

      private Set<String> scriptResourcesToLoadInHead
    • scriptResourcesToLoadInBodyAtTop

      private Set<String> scriptResourcesToLoadInBodyAtTop
    • scriptsExtToLoadInHead

      private Set<String> scriptsExtToLoadInHead
    • scriptsExtToLoadInBodyAtTop

      private Set<String> scriptsExtToLoadInBodyAtTop
    • cssResourcesToLoad

      private Set<String> cssResourcesToLoad
    • cssExtToLoad

      private Set<String> cssExtToLoad
  • Constructor Details

    • ResourceLoader

      public ResourceLoader(jakarta.faces.component.UIComponent component)
      Wraps the given component and registers a PostAddToView event to it, in which this instance will attach its enqueued resources to the component tree.
      Parameters:
      component - UI component
  • Method Details

    • processEvent

      public void processEvent(jakarta.faces.event.ComponentSystemEvent event) throws jakarta.faces.event.AbortProcessingException
      Specified by:
      processEvent in interface jakarta.faces.event.ComponentSystemEventListener
      Throws:
      jakarta.faces.event.AbortProcessingException
    • addScriptResourceToHead

      public void addScriptResourceToHead(String resource)
      Enqueues loading of a JavaScript resource file that will be added via JSF's resource mechanism to the <head>.
      Parameters:
      resource - name of the file in the web project's resource library
    • getScriptResourcesToLoadInHead

      public Set<String> getScriptResourcesToLoadInHead()
      Returns:
      An immutable Set of the JavaScript resources enqueued by addScriptResourceToHead(String) and that have not been loaded via {processEvent(ComponentSystemEvent) yet.
    • addScriptResourceToBodyAtTop

      public void addScriptResourceToBodyAtTop(String resource)
      Enqueues loading of a JavaScript resource file that will be added via JSF's resource mechanism to the top of <body>.

      Note: There is no guarantee on the load order among the resources enqueued by this method.

      Parameters:
      resource - name of the file in the web project's resource library
    • getScriptResourcesToLoadInBodyAtTop

      public Set<String> getScriptResourcesToLoadInBodyAtTop()
      Returns:
      An immutable Set of the JavaScript resources enqueued by addScriptResourceToBodyAtTop(String) and that have not been loaded via {processEvent(ComponentSystemEvent) yet.
    • addScriptExtToHead

      public void addScriptExtToHead(String src)
      Enqueues loading of a JavaScript file that will be loaded in the <head> via the JavaScript class molecularfaces.ResourcesLoader. The code snippet can be requested via encodeLoadExtResources(String).
      Parameters:
      src - path of the file
    • getScriptsExtToLoadInHead

      public Set<String> getScriptsExtToLoadInHead()
      Returns:
      An immutable Set of the JavaScript resources enqueued by addScriptExtToHead(String).
    • addScriptExtToBodyAtTop

      public void addScriptExtToBodyAtTop(String src)
      Enqueues loading of a JavaScript file that will be loaded in the top of <body> via the JavaScript class molecularfaces.ResourcesLoader. The code snippet can be requested via encodeLoadExtResources(String).
      Parameters:
      src - path of the file
    • getScriptsExtToLoadInBodyAtTop

      public Set<String> getScriptsExtToLoadInBodyAtTop()
      Returns:
      An immutable Set of the JavaScript resources enqueued by addScriptExtToBodyAtTop(String).
    • addCssResource

      public void addCssResource(String resource)
      Enqueues loading of a stylesheet resource file that will be added via JSF's resource mechanism.
      Parameters:
      resource - name of the file in the web project's resource library
    • getCssResourcesToLoad

      public Set<String> getCssResourcesToLoad()
      Returns:
      An immutable Set of the stylesheet resources enqueued by addCssResource(String) and that have not been loaded via {processEvent(ComponentSystemEvent) yet.
    • addCssExt

      public void addCssExt(String href)
      Enqueues loading of a stylesheet file that will be loaded via the JavaScript class molecularfaces.ResourcesLoader. The code snippet can be requested via encodeLoadExtResources(String).
      Parameters:
      href - path of the file
    • getCssExtToLoad

      public Set<String> getCssExtToLoad()
      Returns:
      An immutable Set of the stylesheet resources enqueued by addCssExt(String).
    • addScriptResourceAsFacetComponent

      public void addScriptResourceAsFacetComponent(String resource)
      Adds a JavaScript resource component as facet to the wrapped component. The wrapped component is responsible for rendering its facets.
      Parameters:
      resource - name of the file in the web project's resource library
    • addScriptExtAsFacetComponent

      public void addScriptExtAsFacetComponent(String src)
      Adds a JavaScript file as facet resource component to the wrapped component. The wrapped component is responsible for rendering its facets.
      Parameters:
      src - path of the file
    • addCssResourceAsFacetComponent

      public void addCssResourceAsFacetComponent(String resource)
      Adds a stylesheet resource component as facet to the wrapped component. The wrapped component is responsible for rendering its facets.
      Parameters:
      resource - name of the file in the web project's resource library
    • addCssExtAsFacetComponent

      public void addCssExtAsFacetComponent(String href)
      Adds a stylesheet file as facet resource component to the wrapped component. The wrapped component is responsible for rendering its facets.
      Parameters:
      href - path of the file
    • loadScriptResources

      private void loadScriptResources(jakarta.faces.context.FacesContext context)
    • loadCssResources

      private void loadCssResources(jakarta.faces.context.FacesContext context)
    • createResourceComponent

      private jakarta.faces.component.UIComponent createResourceComponent(String resourceName, String rendererType)
    • addComponentToHead

      private void addComponentToHead(jakarta.faces.component.UIComponent componentToAdd, jakarta.faces.context.FacesContext context)
    • addComponentToBodyAtTop

      private void addComponentToBodyAtTop(jakarta.faces.component.UIComponent componentToAdd, jakarta.faces.context.FacesContext context)
    • findBodyComponent

      private jakarta.faces.component.UIComponent findBodyComponent(jakarta.faces.context.FacesContext context)
    • addComponentToResourceContainerInFacet

      private void addComponentToResourceContainerInFacet(jakarta.faces.component.UIComponent component, String facetName, Map<String,jakarta.faces.component.UIComponent> facets)
    • encodeLoadExtResources

      public StringBuilder encodeLoadExtResources(String loaderJSVar)
      Generates a JavaScript code fragment for loading resources that have been enqueued for loading via molecularfaces.ResourcesLoader.
      Parameters:
      loaderJSVar - JavaScript variable name of the molecularfaces.ResourcesLoader instance
      Returns:
      JavaScript code